iterator_range instead of pair for config::child_itors

with this wwe can use .size(), .empty(), .front(), operator bool, etc.
on config::child_itors objects.
This commit is contained in:
gfgtdf 2016-08-13 16:59:34 +02:00
parent 7146614906
commit 4d84d42c47
20 changed files with 55 additions and 71 deletions

View file

@ -150,7 +150,7 @@ void set_about(const config &cfg)
for (const config &campaign : cfg.child_range("campaign"))
{
config::const_child_itors abouts = campaign.child_range("about");
if (abouts.first == abouts.second) continue;
if (abouts.empty()) continue;
config temp;
std::ostringstream text;

View file

@ -43,6 +43,7 @@
#include <boost/exception/exception.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/variant.hpp>
#include <boost/range/iterator_range.hpp>
#include "exceptions.hpp"
#include "tstring.hpp"
@ -206,8 +207,8 @@ public:
Itor i_;
};
typedef std::pair<child_iterator,child_iterator> child_itors;
typedef std::pair<const_child_iterator,const_child_iterator> const_child_itors;
typedef boost::iterator_range<child_iterator> child_itors;
typedef boost::iterator_range<const_child_iterator> const_child_itors;
/**
* Variant for storing WML attributes.

View file

@ -3400,12 +3400,12 @@ void display::refresh_report(std::string const &report_name, const config * new_
SDL_Rect ellipsis_area = rect;
for (config::const_child_itors elements = report.child_range("element");
elements.first != elements.second; ++elements.first)
elements.begin() != elements.end(); elements.drop_front())
{
SDL_Rect area = sdl::create_rect(x, y, rect.w + rect.x - x, rect.h + rect.y - y);
if (area.h <= 0) break;
std::string t = (*elements.first)["text"];
std::string t = elements.front()["text"];
if (!t.empty())
{
if (used_ellipsis) goto skip_element;
@ -3431,9 +3431,9 @@ void display::refresh_report(std::string const &report_name, const config * new_
// check if next element is text with almost no space to show it
const int minimal_text = 12; // width in pixels
config::const_child_iterator ee = elements.first;
config::const_child_iterator ee = elements.begin();
if (!eol && rect.w - (x - rect.x + s->w) < minimal_text &&
++ee != elements.second && !(*ee)["text"].empty())
++ee != elements.end() && !(*ee)["text"].empty())
{
// make this element longer to trigger rendering of ellipsis
// (to indicate that next elements have not enough space)
@ -3463,7 +3463,7 @@ void display::refresh_report(std::string const &report_name, const config * new_
x += area.w;
}
}
else if (!(t = (*elements.first)["image"].str()).empty())
else if (!(t = elements.front()["image"].str()).empty())
{
if (used_ellipsis) goto skip_element;
@ -3503,17 +3503,17 @@ void display::refresh_report(std::string const &report_name, const config * new_
}
skip_element:
t = (*elements.first)["tooltip"].t_str().base_str();
t = elements.front()["tooltip"].t_str().base_str();
if (!t.empty()) {
if (!used_ellipsis) {
tooltips::add_tooltip(area, t, (*elements.first)["help"].t_str().base_str());
tooltips::add_tooltip(area, t, elements.front()["help"].t_str().base_str());
} else {
// Collect all tooltips for the ellipsis.
// TODO: need a better separator
// TODO: assign an action
ellipsis_tooltip << t;
config::const_child_iterator ee = elements.first;
if (++ee != elements.second)
config::const_child_iterator ee = elements.begin();
if (++ee != elements.end())
ellipsis_tooltip << "\n _________\n\n";
}
}

View file

@ -104,7 +104,7 @@ void game_data::set_variable(const std::string& key, const t_string& value)
config& game_data::add_variable_cfg(const std::string& key, const config& value)
{
std::vector<config> temp = {value};
return *get_variable_access_write(key).append_array(temp).first;
return get_variable_access_write(key).append_array(temp).front();
}
void game_data::clear_variable_cfg(const std::string& varname)

View file

@ -984,7 +984,7 @@ bool lobby::lobby_sorter::less(int column, const gui::menu::item& row1, const gu
return false;
}
config::const_child_iterator gi = list.child_range("game").first, gs = gi;
config::const_child_iterator gi = list.child_range("game").begin(), gs = gi;
std::advance(gi, row1.id);
const config &game1 = *gi;
gi = gs;

View file

@ -302,11 +302,7 @@ void wait::join_game(bool observe)
++nb_sides;
}
if (!side_choice) {
size_t count = 0;
for(config::child_itors its = get_scenario().child_range("side"); its.second != its.first; ++its.first) {
++count;
}
DBG_MP << "could not find a side, all " << count << " sides were unsuitable\n";
DBG_MP << "could not find a side, all " << get_scenario().child_count("side") << " sides were unsuitable\n";
set_result(QUIT);
return;
}
@ -323,7 +319,7 @@ void wait::join_game(bool observe)
if (!era)
throw config::error(_("No era information found."));
config::const_child_itors possible_sides = era.child_range("multiplayer_side");
if (possible_sides.first == possible_sides.second) {
if (possible_sides.empty()) {
set_result(QUIT);
throw config::error(_("No multiplayer sides found"));
}

View file

@ -145,7 +145,7 @@ tcontrol_definition::tcontrol_definition(const config& cfg)
* extra header dependencies.
*/
config::const_child_itors itors = cfg.child_range("resolution");
VALIDATE(itors.first != itors.second, _("No resolution defined."));
VALIDATE(!itors.empty(), _("No resolution defined."));
}
} // namespace gui2

View file

@ -268,7 +268,7 @@ const std::string& twindow_builder::read(const config& cfg)
DBG_GUI_P << "Window builder: reading data for window " << id_ << ".\n";
config::const_child_itors cfgs = cfg.child_range("resolution");
VALIDATE(cfgs.first != cfgs.second, _("No resolution defined."));
VALIDATE(!cfgs.empty(), _("No resolution defined."));
for(const auto & i : cfgs)
{
resolutions.push_back(tresolution(i));

View file

@ -136,9 +136,7 @@ bool lobby_info::process_gamelist_diff(const config& data)
}
DBG_LB << "postdiff " << dump_games_config(gamelist_.child("gamelist"));
DBG_LB << dump_games_map(games_by_id_);
config::child_itors range = gamelist_.child("gamelist").child_range("game");
for(config::child_iterator i = range.first; i != range.second; ++i) {
config& c = *i;
for(config& c : gamelist_.child("gamelist").child_range("game")) {
DBG_LB << "data process: " << c["id"] << " ("
<< c[config::diff_track_attribute] << ")\n";
int game_id = c["id"];

View file

@ -76,7 +76,7 @@ void tunit_advance::pre_show(twindow& window)
// This checks if we've finished iterating over the last unit type advancements
// and are into the modification-based advancements.
if(i >= last_real_advancement_) {
auto iter = sample.get_modifications().child_range("advancement").second;
auto iter = sample.get_modifications().child_range("advancement").end();
iter--;
if(iter->has_attribute("image")) {

View file

@ -639,8 +639,7 @@ std::vector<topic> generate_trait_topics(const bool sort_generated)
{
const unit_type &type = i.second;
if (description_type(type) == FULL_DESCRIPTION) {
config::const_child_itors traits = type.possible_traits();
if (traits.first != traits.second) {
if (config::const_child_itors traits = type.possible_traits()) {
for (const config & trait : traits) {
const std::string trait_id = trait["id"];
trait_list.insert(std::make_pair(trait_id, trait));

View file

@ -391,8 +391,7 @@ std::string unit_topic_generator::operator()() const {
// Print the possible traits of the unit, cross-reference them
// to their respective topics.
config::const_child_itors traits = type_.possible_traits();
if (traits.first != traits.second) {
if (config::const_child_itors traits = type_.possible_traits()) {
std::vector<trait_data> must_have_traits;
std::vector<trait_data> random_traits;
@ -556,8 +555,8 @@ std::string unit_topic_generator::operator()() const {
// Generate the movement type of the unit, with resistance, defense, movement, jamming and vision data updated according to any 'musthave' traits which always apply
movetype movement_type = type_.movement_type();
traits = type_.possible_traits();
if (traits.first != traits.second && type_.num_traits() > 0) {
config::const_child_itors traits = type_.possible_traits();
if (!traits.empty() && type_.num_traits() > 0) {
for (const config & t : traits) {
if (t["availability"].str() == "musthave") {
for (const config & effect : t.child_range("effect")) {

View file

@ -1666,10 +1666,10 @@ int game_lua_kernel::impl_game_config_get(lua_State *L)
//This code for SigurdFD, not the cleanest implementation but seems to work just fine.
config::const_child_itors its = game_config_manager::get()->game_config().child_range("era");
std::string eras_list((*(its.first))["id"]);
++its.first;
for(; its.first != its.second; ++its.first) {
eras_list = eras_list + "," + (*(its.first))["id"];
std::string eras_list(its.front()["id"]);
its.drop_front();
for(const auto& cfg : its) {
eras_list = eras_list + "," + cfg["id"];
}
return_string_attrib("eras", eras_list);
}
@ -3475,7 +3475,7 @@ int game_lua_kernel::intf_modify_side(lua_State *L)
ai::manager::add_ai_for_side_from_file(side_num,switch_ai,true);
}
// Override AI parameters
if (ai.first != ai.second) {
if (!ai.empty()) {
ai::manager::modify_active_ai_config_old_for_side(side_num,ai);
}
// Change team color

View file

@ -40,9 +40,9 @@ void show_story(CVideo& video, const std::string &scenario_name,
events::event_context story_context;
int segment_count = 0;
config::const_child_iterator itor = story.first;
config::const_child_iterator itor = story.begin();
storyscreen::START_POSITION startpos = storyscreen::START_BEGINNING;
while (itor != story.second)
while (itor != story.end())
{
storyscreen::controller ctl(video, vconfig(*itor, true),
scenario_name, segment_count);
@ -50,14 +50,14 @@ void show_story(CVideo& video, const std::string &scenario_name,
switch(result) {
case storyscreen::NEXT:
if(itor != story.second) {
if(itor != story.end()) {
++itor;
++segment_count;
startpos = storyscreen::START_BEGINNING;
}
break;
case storyscreen::BACK:
if(itor != story.first) {
if(itor != story.begin()) {
--itor;
--segment_count;
startpos = storyscreen::START_END;

View file

@ -418,8 +418,7 @@ bool unit::ability_affects_self(const std::string& ability,const config& cfg,con
bool unit::has_ability_type(const std::string& ability) const
{
config::const_child_itors itors = this->abilities_.child_range(ability);
return itors.first != itors.second;
return !abilities_.child_range(ability).empty();
}

View file

@ -770,10 +770,10 @@ void unit_animation::add_anims( std::vector<unit_animation> & animations, const
anim["apply_to"] = "attack";
if (anim["layer"].empty()) anim["layer"] = move_layer;
config::const_child_itors missile_fs = anim.child_range("missile_frame");
if (anim["offset"].empty() && missile_fs.first == missile_fs.second) {
if (anim["offset"].empty() && missile_fs.empty()) {
anim["offset"] ="0~0.6,0.6~0";
}
if (missile_fs.first != missile_fs.second) {
if (!missile_fs.empty()) {
if (anim["missile_offset"].empty()) anim["missile_offset"] = "0~0.8";
if (anim["missile_layer"].empty()) anim["missile_layer"] = missile_layer;
config tmp;
@ -883,7 +883,7 @@ unit_animation::particule::particule(
{
config::const_child_itors range = cfg.child_range(frame_string+"frame");
starting_frame_time_=INT_MAX;
if(cfg[frame_string+"start_time"].empty() &&range.first != range.second) {
if(!range.empty() && cfg[frame_string+"start_time"].empty()) {
for (const config &frame : range) {
starting_frame_time_ = std::min(starting_frame_time_, frame["begin"].to_int());
}

View file

@ -723,12 +723,10 @@ const std::string& unit_type::flag_rgb() const {
bool unit_type::has_random_traits() const
{
if (num_traits() == 0) return false;
config::const_child_itors t = possible_traits();
while(t.first != t.second) {
const config::attribute_value& availability = (*t.first)["availability"];
for(const auto& cfg : possible_traits()) {
const config::attribute_value& availability = cfg["availability"];
if(availability.blank()) return true;
if(strcmp(availability.str().c_str(), "musthave") != 0) return true;
++t.first;
}
return false;
}

View file

@ -519,12 +519,11 @@ unit::unit(const config &cfg, bool use_traits, const vconfig* vcfg)
}
//don't use the unit_type's attacks if this config has its own defined
config::const_child_itors cfg_range = cfg.child_range("attack");
if(cfg_range.first != cfg_range.second) {
if(config::const_child_itors cfg_range = cfg.child_range("attack")) {
attacks_.clear();
do {
attacks_.push_back(attack_type(*cfg_range.first));
} while(++cfg_range.first != cfg_range.second);
for (const config& cfg : cfg_range) {
attacks_.push_back(attack_type(cfg));
}
}
//If cfg specifies [advancement]s, replace this [advancement]s with them.
@ -536,11 +535,9 @@ unit::unit(const config &cfg, bool use_traits, const vconfig* vcfg)
//don't use the unit_type's abilities if this config has its own defined
//Why do we allow multiple [abilities] tags?
cfg_range = cfg.child_range("abilities");
if(cfg_range.first != cfg_range.second) {
this->abilities_.clear();
for(const config& abilities : cfg_range)
{
if (config::const_child_itors cfg_range = cfg.child_range("abilities")) {
abilities_.clear();
for (const config& abilities : cfg_range) {
this->abilities_.append(abilities);
}
}
@ -863,7 +860,7 @@ void unit::generate_traits(bool musthaveonly)
// Now randomly fill out to the number of traits required or until
// there aren't any more traits.
int nb_traits = std::distance(current_traits.first, current_traits.second);
int nb_traits = current_traits.size();
int max_traits = u_type.num_traits();
for (; nb_traits < max_traits && !candidate_traits.empty(); ++nb_traits)
{

View file

@ -47,7 +47,7 @@ namespace
assert(resources::gamedata);
config::const_child_itors range = resources::gamedata->get_variable_access_read(varname).as_array();
if(range.first == range.second)
if(range.empty())
{
return as_nonempty_range_default.child_range("_");
}
@ -224,7 +224,7 @@ size_t vconfig::count_children(const std::string& key) const
try
{
config::const_child_itors range = as_nonempty_range(insert_cfg["variable"]);
n += range.second - range.first;
n += range.size();
}
catch(const invalid_variablename_exception&)
{
@ -254,7 +254,7 @@ vconfig vconfig::child(const std::string& key) const
try
{
config::const_child_itors range = as_nonempty_range(insert_cfg["variable"]);
return vconfig(*range.first, true);
return vconfig(range.front(), true);
}
catch(const invalid_variablename_exception&)
{
@ -329,7 +329,7 @@ vconfig::all_children_iterator& vconfig::all_children_iterator::operator++()
config::const_child_itors range = vinfo.as_array();
if (++inner_index_ < std::distance(range.first, range.second))
if (++inner_index_ < int(range.size()))
{
return *this;
}
@ -380,8 +380,8 @@ vconfig vconfig::all_children_iterator::get_child() const
{
config::const_child_itors range = as_nonempty_range(vconfig(i_->cfg)["variable"]);
std::advance(range.first, inner_index_);
return vconfig(*range.first, true);
range.advance_begin(inner_index_);
return vconfig(range.front(), true);
}
catch(const invalid_variablename_exception&)
{

View file

@ -34,10 +34,7 @@ namespace
auto get_child_range(TConfig& cfg, const std::string& key, int start, int count) -> decltype(cfg.child_range(key))
{
auto res = cfg.child_range(key);
std::advance(res.first, start);
res.second = res.first;
std::advance(res.second, count);
return res;
return { res.begin() + start, res.begin() + start + count};
}
void resolve_negative_value(int size, int& val)