config::all_children_range(), attribute_range() now return iterator_range

This also fixed compilation with boost veryion < 1.56 by using
iterator_range::pop_front() instead of iterator_range::drop_front()
which was added in boost 1.56

This also makes config::all_children_iterator a random access iterator.
This commit is contained in:
gfgtdf 2016-08-14 13:54:07 +02:00
parent 05340bb81f
commit 16f5980a3c
13 changed files with 49 additions and 33 deletions

View file

@ -1532,11 +1532,11 @@ bool operator==(const config& a, const config& b)
return false;
config::all_children_itors x = a.all_children_range(), y = b.all_children_range();
for (; x.first != x.second && y.first != y.second; ++x.first, ++y.first) {
if (x.first->key != y.first->key || x.first->cfg != y.first->cfg) {
for (; !x.empty() && !y.empty(); x.pop_front(), y.pop_front()) {
if (x.front().key != y.front().key || x.front().cfg != y.front().cfg) {
return false;
}
}
return x.first == x.second && y.first == y.second;
return x.empty() && y.empty();
}

View file

@ -412,7 +412,7 @@ public:
Itor i_;
};
typedef std::pair<const_attribute_iterator,const_attribute_iterator> const_attr_itors;
typedef boost::iterator_range<const_attribute_iterator> const_attr_itors;
child_itors child_range(const std::string& key);
const_child_itors child_range(const std::string& key) const;
@ -637,15 +637,18 @@ public:
};
typedef any_child value_type;
typedef std::forward_iterator_tag iterator_category;
typedef std::random_access_iterator_tag iterator_category;
typedef int difference_type;
typedef const arrow_helper pointer;
typedef const any_child reference;
typedef std::vector<child_pos>::const_iterator Itor;
typedef all_children_iterator this_type;
explicit all_children_iterator(const Itor &i): i_(i) {}
all_children_iterator &operator++() { ++i_; return *this; }
all_children_iterator operator++(int) { return all_children_iterator(i_++); }
this_type &operator--() { --i_; return *this; }
this_type operator--(int) { return this_type(i_--); }
reference operator*() const;
pointer operator->() const { return *this; }
@ -653,13 +656,27 @@ public:
bool operator==(const all_children_iterator &i) const { return i_ == i.i_; }
bool operator!=(const all_children_iterator &i) const { return i_ != i.i_; }
friend bool operator<(const this_type& a, const this_type& b) { return a.i_ < b.i_; }
friend bool operator<=(const this_type& a, const this_type& b) { return a.i_ <= b.i_; }
friend bool operator>=(const this_type& a, const this_type& b) { return a.i_ >= b.i_; }
friend bool operator>(const this_type& a, const this_type& b) { return a.i_ > b.i_; }
this_type& operator+=(difference_type n) { i_ += n; return *this; }
this_type& operator-=(difference_type n) { i_ -= n; return *this; }
reference operator[](difference_type n) const { return any_child(&i_[n].pos->first, i_[n].pos->second[i_->index]); }
friend difference_type operator-(const this_type& a, const this_type& b) { return a.i_ - b.i_; }
friend this_type operator-(const this_type& a, difference_type n) { return this_type(a.i_ - n); }
friend this_type operator+(const this_type& a, difference_type n) { return this_type(a.i_ + n); }
friend this_type operator+(difference_type n, const this_type& a) { return this_type(a.i_ + n); }
private:
Itor i_;
friend class config;
};
typedef std::pair<all_children_iterator, all_children_iterator> all_children_itors;
typedef boost::iterator_range<all_children_iterator> all_children_itors;
/** In-order iteration over all children. */
all_children_itors all_children_range() const;

View file

@ -3400,7 +3400,7 @@ 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.begin() != elements.end(); elements.drop_front())
elements.begin() != elements.end(); elements.pop_front(1))
{
SDL_Rect area = sdl::create_rect(x, y, rect.w + rect.x - x, rect.h + rect.y - y);
if (area.h <= 0) break;

View file

@ -155,7 +155,7 @@ register_builder_widget(const std::string& id,
tbuilder_widget_ptr create_builder_widget(const config& cfg)
{
config::all_children_itors children = cfg.all_children_range();
size_t nb_children = std::distance(children.first, children.second);
size_t nb_children = std::distance(children.begin(), children.end());
VALIDATE(nb_children == 1, "Grid cell does not have exactly 1 child.");
for(const auto & item : builder_widget_lookup())

View file

@ -126,7 +126,7 @@ turn_info::PROCESS_DATA_RESULT turn_info::process_network_data(const config& cfg
{
// the simple wesnothserver implementation in wesnoth was removed years ago.
assert(cfg.all_children_count() == 1);
assert(cfg.attribute_range().first == cfg.attribute_range().second);
assert(cfg.attribute_range().empty());
if(!resources::recorder->at_end())
{
ERR_NW << "processing network data while still having data on the replay." << std::endl;

View file

@ -48,7 +48,7 @@ void playturn_network_adapter::read_from_network()
back.remove_attribute("side_drop");
back.remove_attribute("controller");
}
else if(back.attribute_range().first != back.attribute_range().second )
else if(!back.attribute_range().empty() )
{
ERR_NW << "found unexpected attribute:" <<back.debug() << std::endl;
this->data_.pop_back();

View file

@ -698,7 +698,7 @@ REPLAY_RETURN do_replay_handle(bool one_move)
const config::all_children_itors ch_itors = cfg->all_children_range();
//if there is an empty command tag or a start tag
if (ch_itors.first == ch_itors.second || cfg->has_child("start"))
if (ch_itors.empty() || cfg->has_child("start"))
{
//this shouldn't happen anymore because replaycontroller now moves over the [start] with get_next_action
//also we removed the the "add empty replay entry at scenario reload" behavior.
@ -821,7 +821,7 @@ REPLAY_RETURN do_replay_handle(bool one_move)
// but we are called from
// the only other option for "dependent" command is checksum wich is already checked.
assert(cfg->all_children_count() == 1);
std::string child_name = cfg->all_children_range().first->key;
std::string child_name = cfg->all_children_range().front().key;
DBG_REPLAY << "got an dependent action name = " << child_name <<"\n";
resources::recorder->revert_action();
return REPLAY_FOUND_DEPENDENT;

View file

@ -1667,7 +1667,7 @@ 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.front()["id"]);
its.drop_front();
its.pop_front(1);
for(const auto& cfg : its) {
eras_list = eras_list + "," + cfg["id"];
}
@ -3623,9 +3623,8 @@ static int intf_remove_modifications(lua_State *L)
unit& u = luaW_checkunit(L, 1);
config filter = luaW_checkconfig(L, 2);
std::string tag = luaL_optstring(L, 3, "object");
config::const_attr_itors ai = filter.attribute_range();
config::all_children_itors ci = filter.all_children_range();
if(std::distance(ai.first, ai.second) == 1 && std::distance(ci.first, ci.second) == 0 && ai.first->first == "duration") {
//TODO
if(filter.attribute_count() == 1 && filter.all_children_count() == 0 && filter.attribute_range().front().first == "duration") {
u.expire_modifications(filter["duration"]);
} else {
for(config& obj : u.get_modifications().child_range(tag)) {

View file

@ -271,10 +271,11 @@ static int impl_vconfig_pairs_iter(lua_State *L)
vconfig vcfg = luaW_checkvconfig(L, 1);
void* p = luaL_checkudata(L, lua_upvalueindex(1), vconfigpairsKey);
config::const_attr_itors& range = *static_cast<config::const_attr_itors*>(p);
if (range.first == range.second) {
if (range.empty()) {
return 0;
}
config::attribute value = *range.first++;
config::attribute value = range.front();
range.pop_front();
lua_pushlstring(L, value.first.c_str(), value.first.length());
luaW_pushscalar(L, vcfg[value.first]);
return 2;

View file

@ -119,7 +119,7 @@ static config &find_ref(const std::string &id, config &cfg, bool remove = false)
static config empty_config;
config::all_children_itors itors = cfg.all_children_range();
for (config::all_children_iterator i = itors.first; i != itors.second; ++i)
for (config::all_children_iterator i = itors.begin(); i != itors.end(); ++i)
{
config &icfg = const_cast<config &>(i->cfg);
if (i->cfg["id"] == id) {

View file

@ -210,29 +210,29 @@ static void prepare_single_animation(const config &anim_cfg, animation_branches
animation_cursor &ac = anim_cursors.back();
// Reached end of sub-tag config block
if (ac.itors.first == ac.itors.second) {
if (ac.itors.empty()) {
if (!ac.parent) break;
// Merge all the current branches into the parent.
ac.parent->branches.splice(ac.parent->branches.end(), ac.branches);
anim_cursors.pop_back();
continue;
}
if (ac.itors.first->key != "if") {
if (ac.itors.front().key != "if") {
// Append current config object to all the branches in scope.
for (animation_branch &ab : ac.branches) {
ab.children.push_back(ac.itors.first);
ab.children.push_back(ac.itors.begin());
}
++ac.itors.first;
ac.itors.pop_front();
continue;
}
int count = 0;
do {
/* Copies the current branches to each cursor created for the
conditional clauses. Merge attributes of the clause into them. */
anim_cursors.push_back(animation_cursor(ac.itors.first->cfg, &ac));
++ac.itors.first;
anim_cursors.push_back(animation_cursor(ac.itors.front().cfg, &ac));
ac.itors.pop_front();
++count;
} while (ac.itors.first != ac.itors.second && ac.itors.first->key == "else");
} while (!ac.itors.empty() && ac.itors.front().key == "else");
if (count > 1) {
// When else statements present, clear all branches before 'if'
ac.branches.clear();

View file

@ -461,12 +461,11 @@ bool basic_unit_filter_impl::internal_matches_filter(const unit & u, const map_l
config fwml = wmlcfg.get_parsed_config();
/* Check if the filter only cares about variables.
If so, no need to serialize the whole unit. */
config::const_attr_itors ai = fwml.attribute_range();
config::all_children_itors ci = fwml.all_children_range();
if (std::distance(ai.first, ai.second) == 0 &&
std::distance(ci.first, ci.second) == 1 &&
ci.first->key == "variables") {
if (!u.variables().matches(ci.first->cfg))
if (fwml.all_children_count() == 1 &&
fwml.attribute_count() == 1 &&
ci.front().key == "variables") {
if (!u.variables().matches(ci.front().cfg))
return false;
} else {
if (unit_cfg.empty())

View file

@ -126,8 +126,8 @@ namespace {
void warn_unknown_attribute(const config::const_attr_itors& cfg)
{
config::const_attribute_iterator cur = cfg.first;
config::const_attribute_iterator end = cfg.second;
config::const_attribute_iterator cur = cfg.begin();
config::const_attribute_iterator end = cfg.end();
const std::string* cur_known = std::begin(internalized_attrs);
const std::string* end_known = std::end(internalized_attrs);
while(cur_known != end_known) {