The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
filter.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * This namespace contains the function that checks if a unit matches
17  * a filter. It helps by simplifying the unit object (which before now
18  * holds the "match" function).
19  *
20  * TODO:
21  * Make a class that abstracts a unit filter, assembles the constituent
22  * side filters and terrain filters and conditional filters, and caches
23  * these to speed up repeated application of the filter.
24  */
25 
26 #ifndef INCLUDED_UNIT_FILTER_HPP_
27 #define INCLUDED_UNIT_FILTER_HPP_
28 
29 #include "units/ptr.hpp"
30 
31 #include <boost/shared_ptr.hpp>
32 #include <vector>
33 
34 class filter_context;
35 class unit;
36 class config;
37 class vconfig;
38 struct map_location;
39 
41 public:
42  virtual bool matches(const unit & u, const map_location & loc, const unit * u2 = nullptr) const = 0;
43  virtual std::vector<const unit*> all_matches_on_map(unsigned max_matches) const = 0;
44  virtual unit_const_ptr first_match_on_map() const = 0;
45  virtual config to_config() const = 0;
46  virtual bool empty() const {return false;}
48 };
49 
50 class unit_filter {
51 public:
52  unit_filter(const vconfig & cfg, const filter_context * fc, bool use_flat_tod = false); //!< Constructs a unit filter from a config and a context. This function should give the most efficient implementation available.
53 
54  // Copy and Swap Idiom for the interface -- does not copy the underlying impl
55  unit_filter(const unit_filter & o ) : impl_(o.impl_) {}
56  void swap(unit_filter & o) {
57  impl_.swap(o.impl_);
58  }
60  swap(o);
61  return *this;
62  }
63 
64  /// Determine if *this matches @a filter at a specified location.
65  /// Use this for units on a recall list, or to test for a match if
66  /// a unit is hypothetically moved.
67  bool matches(const unit & u, const map_location & loc) const {
68  return impl_->matches(u,loc);
69  }
70  /// Determine if *this matches @a filter at its current location.
71  /// (Only use for units currently on the map; otherwise use the overload
72  /// that takes a location, possibly with a null location.)
73  bool matches(const unit & u) const;
74 
75  bool matches(const unit & u, const map_location & loc, const unit & u2) const;
76  bool matches(const unit & u, const unit & u2) const;
77 
78  bool operator()(const unit & u, const map_location & loc) const {
79  return matches(u,loc);
80  }
81 
82  bool operator()(const unit & u) const {
83  return matches(u);
84  }
85 
86  bool operator()(const unit & u, const map_location & loc, const unit & u2) const {
87  return matches(u,loc,u2);
88  }
89 
90  bool operator()(const unit & u, const unit & u2) const {
91  return matches(u,u2);
92  }
93 
94  std::vector<const unit *> all_matches_on_map() const {
95  return impl_->all_matches_on_map(max_matches_);
96  }
97 
99  return impl_->first_match_on_map();
100  }
101 
102  config to_config() const;
103 
104  bool empty() const {
105  return impl_->empty();
106  }
107 private:
109  unsigned max_matches_;
110 };
111 
112 #endif
virtual std::vector< const unit * > all_matches_on_map(unsigned max_matches) const =0
bool empty() const
Definition: filter.hpp:104
virtual ~unit_filter_abstract_impl()
Definition: filter.hpp:47
std::vector< const unit * > all_matches_on_map() const
Definition: filter.hpp:94
Definition: unit.hpp:95
boost::shared_ptr< unit_filter_abstract_impl > impl_
Definition: filter.hpp:108
void swap(unit_filter &o)
Definition: filter.hpp:56
bool operator()(const unit &u, const map_location &loc, const unit &u2) const
Definition: filter.hpp:86
bool operator()(const unit &u) const
Definition: filter.hpp:82
unit_const_ptr first_match_on_map() const
Definition: filter.hpp:98
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
Definition: filter.hpp:67
virtual bool matches(const unit &u, const map_location &loc, const unit *u2=nullptr) const =0
virtual unit_const_ptr first_match_on_map() const =0
unsigned max_matches_
Definition: filter.hpp:109
unit_filter & operator=(unit_filter o)
Definition: filter.hpp:59
Encapsulates the map of the game.
Definition: location.hpp:38
virtual bool empty() const
Definition: filter.hpp:46
config to_config() const
Definition: filter.cpp:52
unit_filter(const vconfig &cfg, const filter_context *fc, bool use_flat_tod=false)
Constructs a unit filter from a config and a context. This function should give the most efficient im...
Definition: filter.cpp:210
bool operator()(const unit &u, const unit &u2) const
Definition: filter.hpp:90
A variable-expanding proxy for the config class.
Definition: variable.hpp:36
virtual config to_config() const =0
unit_filter(const unit_filter &o)
Definition: filter.hpp:55
GLuint GLdouble GLdouble u2
Definition: glew.h:2972
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
bool operator()(const unit &u, const map_location &loc) const
Definition: filter.hpp:78