Skip to content

Commit

Permalink
add limit=n parameter to standard unit filters
Browse files Browse the repository at this point in the history
and specially to wesnoth.get_units
  • Loading branch information
gfgtdf committed Mar 14, 2016
1 parent 413d8cf commit a91cb9a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/config.hpp
Expand Up @@ -406,6 +406,9 @@ class config
const_child_itors child_range(const std::string& key) const;
unsigned child_count(const std::string &key) const;
unsigned all_children_count() const;
/** Note: this function also counts the 'blank' attributes, so it might return more than one might expect */
unsigned attribute_count() const
{ return values.size(); }

/**
* Determine whether a config has a child or not.
Expand Down
23 changes: 19 additions & 4 deletions src/unit_filter.cpp
Expand Up @@ -77,10 +77,14 @@ class null_unit_filter_impl : public unit_filter_abstract_impl {
virtual bool matches(const unit & /*u*/, const map_location & /*loc*/, const unit *) const {
return true;
}
virtual std::vector<const unit *> all_matches_on_map() const {
virtual std::vector<const unit *> all_matches_on_map(unsigned max_matches) const {
std::vector<const unit *> ret;
BOOST_FOREACH(const unit & u, fc_.get_disp_context().units()) {
--max_matches;
ret.push_back(&u);
if(max_matches == 0) {
return ret;
}
}
return ret;
}
Expand Down Expand Up @@ -153,7 +157,7 @@ class basic_unit_filter_impl : public unit_filter_abstract_impl {
}

virtual bool matches(const unit & u, const map_location & loc, const unit * u2) const;
virtual std::vector<const unit *> all_matches_on_map() const;
virtual std::vector<const unit *> all_matches_on_map(unsigned max_matches) const;
virtual unit_const_ptr first_match_on_map() const;

virtual ~basic_unit_filter_impl() {}
Expand All @@ -174,7 +178,10 @@ class basic_unit_filter_impl : public unit_filter_abstract_impl {

static boost::shared_ptr<unit_filter_abstract_impl> construct(const vconfig & vcfg, const filter_context & fc, bool flat_tod)
{
if (vcfg.null()) {
if (vcfg.empty()) {

This comment has been minimized.

Copy link
@CelticMinstrel

CelticMinstrel Mar 14, 2016

Member

Is this true also if vcfg.null()?

This comment has been minimized.

Copy link
@AI0867

AI0867 Mar 14, 2016

Member

bool empty() const { return (null() || cfg_->empty()); }

return boost::make_shared<null_unit_filter_impl> (fc);
}
if (vcfg.get_config().attribute_count() == 1 && vcfg.get_config().all_children_count() == 0 && vcfg.has_attribute("limit")) {
return boost::make_shared<null_unit_filter_impl> (fc);
}
return boost::make_shared<basic_unit_filter_impl>(vcfg, fc, flat_tod);
Expand All @@ -185,7 +192,11 @@ static boost::shared_ptr<unit_filter_abstract_impl> construct(const vconfig & vc
* unit_filter::unit_filter acts as a factory, selecting the appropriate implementation class
*/
unit_filter::unit_filter(const vconfig & vcfg, const filter_context * fc, bool flat_tod)
: max_matches_(static_cast<unsigned>(-1))
{
if(vcfg) {
max_matches_ = vcfg["limit"].to_unsigned(max_matches_);
}
if (!fc) {
assert(false && "attempt to instantiate a unit filter with a null filter context!");
}
Expand Down Expand Up @@ -539,10 +550,14 @@ bool basic_unit_filter_impl::internal_matches_filter(const unit & u, const map_l
return true;
}

std::vector<const unit *> basic_unit_filter_impl::all_matches_on_map() const {
std::vector<const unit *> basic_unit_filter_impl::all_matches_on_map(unsigned max_matches) const {
std::vector<const unit *> ret;
BOOST_FOREACH(const unit & u, fc_.get_disp_context().units()) {
if (matches(u, u.get_location(), NULL)) {
if(max_matches == 0) {
return ret;
}
--max_matches;
ret.push_back(&u);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/unit_filter.hpp
Expand Up @@ -39,7 +39,7 @@ struct map_location;
class unit_filter_abstract_impl {
public:
virtual bool matches(const unit & u, const map_location & loc, const unit * u2 = NULL) const = 0;
virtual std::vector<const unit*> all_matches_on_map() const = 0;
virtual std::vector<const unit*> all_matches_on_map(unsigned max_matches) const = 0;
virtual unit_const_ptr first_match_on_map() const = 0;
virtual ~unit_filter_abstract_impl() {}
};
Expand Down Expand Up @@ -89,14 +89,15 @@ class unit_filter {
}

std::vector<const unit *> all_matches_on_map() const {
return impl_->all_matches_on_map();
return impl_->all_matches_on_map(max_matches_);
}

unit_const_ptr first_match_on_map() const {
return impl_->first_match_on_map();
}
private:
boost::shared_ptr<unit_filter_abstract_impl> impl_;
unsigned max_matches_;
};

#endif

0 comments on commit a91cb9a

Please sign in to comment.