The Battle for Wesnoth  1.19.7+dev
filter.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2024
3  by Chris Beck <render787@gmail.com>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 /**
17  * This namespace contains the function that checks if a unit matches
18  * a filter. It helps by simplifying the unit object (which before now
19  * holds the "match" function).
20  *
21  * TODO:
22  * Make a class that abstracts a unit filter, assembles the constituent
23  * side filters and terrain filters and conditional filters, and caches
24  * these to speed up repeated application of the filter.
25  */
26 
27 #pragma once
28 
30 #include "units/ptr.hpp"
31 
32 #include "filter_context.hpp"
33 #include "units/map.hpp"
34 #include "variable.hpp"
35 
36 #include <memory>
37 #include <vector>
38 
39 class filter_context;
40 class unit;
41 
42 
43 
44 namespace unit_filter_impl
45 {
46  struct filter_error : public game::error
47  {
48  explicit filter_error(const std::string& message = "filter error")
49  : game::error(message)
50  {
51  }
52  };
53 
55  {
56  const unit& u;
58  const unit* u2;
61 
62  const filter_context& context() const
63  {
64  if(fc) {
65  return *fc;
66  }
67  throw filter_error();
68  }
69  // This constructor is here to shut down warnings that the default constructor couldn't be generated.
70  // It's technically unnecessary since lacking of a default constructor doesn't prevent aggregate-initialization, but...
73  {}
74  };
75 
77  {
78  virtual bool matches(const unit_filter_args&) const = 0;
79  virtual ~unit_filter_base() {}
80  };
81 
83  {
84  unit_filter_compound(const vconfig& cfg);
85 
86  template<typename C, typename F>
87  void create_attribute(const config::attribute_value c, C conv, F func);
88  template<typename F>
89  void create_child(const vconfig& c, F func);
90 
91  void fill(const vconfig& cfg);
92 
93  virtual bool matches(const unit_filter_args& u) const override;
94  bool filter_impl(const unit_filter_args& u) const;
95 
96  std::vector<std::shared_ptr<unit_filter_base>> children_;
97  std::vector<std::pair<conditional_type::type, unit_filter_compound>> cond_children_;
98  };
99 
100 }
101 
103 {
104 public:
105  explicit unit_filter(const vconfig& cfg);
106 
107  unit_filter(const unit_filter&) = default;
108  unit_filter& operator=(const unit_filter&) = default;
109 
110  unit_filter(unit_filter&&) = default;
112 
114  use_flat_tod_ = value;
115  return *this;
116  }
117 
118  /**
119  * Determine if *this matches @a filter at a specified location.
120  * Use this for units on a recall list, or to test for a match if
121  * a unit is hypothetically moved.
122  */
123  bool matches(const unit & u, const map_location & loc) const {
125  }
126 
127  /**
128  * Determine if *this matches @a filter at its current location.
129  * (Only use for units currently on the map; otherwise use the overload
130  * that takes a location, possibly with a null location.)
131  */
132  bool matches(const unit & u) const;
133 
134  bool matches(const unit & u, const map_location & loc, const unit & u2) const {
136  }
137 
138  bool matches(const unit & u, const unit & u2) const;
139 
140  bool operator()(const unit & u, const map_location & loc) const {
141  return matches(u, loc);
142  }
143 
144  bool operator()(const unit & u) const {
145  return matches(u);
146  }
147 
148  bool operator()(const unit & u, const map_location & loc, const unit & u2) const {
149  return matches(u, loc, u2);
150  }
151 
152  bool operator()(const unit & u, const unit & u2) const {
153  return matches(u, u2);
154  }
155 
156  std::vector<const unit *> all_matches_on_map(const map_location* loc = nullptr, const unit* other_unit = nullptr) const;
157 
158  std::vector<const unit*> all_matches_at(const map_location& loc) const {
159  return all_matches_on_map(&loc);
160  }
161 
162  std::vector<const unit*> all_matches_with_unit(const unit& u) const {
163  return all_matches_on_map(nullptr, &u);
164  }
165 
166  std::vector<const unit*> all_matches_with_unit_at(const unit& u, const map_location& loc) const {
167  return all_matches_on_map(&loc, &u);
168  }
169 
171 
172  config to_config() const {
173  return cfg_.get_config();
174  }
175 
176  bool empty() const {
177  return cfg_.get_config().empty();
178  }
179 
180 private:
181 
187 };
map_location loc
Definition: move.cpp:172
Variant for storing WML attributes.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
bool empty() const
Definition: config.cpp:849
unit_filter(const vconfig &cfg)
Definition: filter.cpp:50
const filter_context * fc_
Definition: filter.hpp:183
std::vector< const unit * > all_matches_with_unit_at(const unit &u, const map_location &loc) const
Definition: filter.hpp:166
int max_matches_
Definition: filter.hpp:186
unit_filter(const unit_filter &)=default
config to_config() const
Definition: filter.hpp:172
bool use_flat_tod_
Definition: filter.hpp:184
bool operator()(const unit &u) const
Definition: filter.hpp:144
std::vector< const unit * > all_matches_with_unit(const unit &u) const
Definition: filter.hpp:162
bool operator()(const unit &u, const map_location &loc, const unit &u2) const
Definition: filter.hpp:148
bool matches(const unit &u, const map_location &loc) const
Determine if *this matches filter at a specified location.
Definition: filter.hpp:123
unit_filter & operator=(const unit_filter &)=default
std::vector< const unit * > all_matches_at(const map_location &loc) const
Definition: filter.hpp:158
std::vector< const unit * > all_matches_on_map(const map_location *loc=nullptr, const unit *other_unit=nullptr) const
Definition: filter.cpp:67
bool matches(const unit &u, const map_location &loc, const unit &u2) const
Definition: filter.hpp:134
unit_filter & set_use_flat_tod(bool value)
Definition: filter.hpp:113
unit_filter & operator=(unit_filter &&)=default
bool operator()(const unit &u, const unit &u2) const
Definition: filter.hpp:152
unit_const_ptr first_match_on_map() const
Definition: filter.cpp:84
unit_filter_impl::unit_filter_compound impl_
Definition: filter.hpp:185
bool empty() const
Definition: filter.hpp:176
unit_filter(unit_filter &&)=default
vconfig cfg_
Definition: filter.hpp:182
bool operator()(const unit &u, const map_location &loc) const
Definition: filter.hpp:140
This class represents a single unit of a specific type.
Definition: unit.hpp:133
A variable-expanding proxy for the config class.
Definition: variable.hpp:45
const config & get_config() const
Definition: variable.hpp:75
std::shared_ptr< const unit > unit_const_ptr
Definition: ptr.hpp:27
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
std::string message
Definition: exceptions.hpp:30
Encapsulates the map of the game.
Definition: location.hpp:45
filter_error(const std::string &message="filter error")
Definition: filter.hpp:48
unit_filter_args(const unit &u, map_location loc, const unit *u2, const filter_context *fc, bool use_flat_tod)
Definition: filter.hpp:71
const filter_context & context() const
Definition: filter.hpp:62
const filter_context * fc
Definition: filter.hpp:59
virtual bool matches(const unit_filter_args &) const =0
bool filter_impl(const unit_filter_args &u) const
Definition: filter.cpp:267
void fill(const vconfig &cfg)
Definition: filter.cpp:316
void create_child(const vconfig &c, F func)
Definition: filter.cpp:278
void create_attribute(const config::attribute_value c, C conv, F func)
Definition: filter.cpp:284
std::vector< std::pair< conditional_type::type, unit_filter_compound > > cond_children_
Definition: filter.hpp:97
unit_filter_compound(const vconfig &cfg)
Definition: filter.cpp:225
virtual bool matches(const unit_filter_args &u) const override
Definition: filter.cpp:232
std::vector< std::shared_ptr< unit_filter_base > > children_
Definition: filter.hpp:96
mock_char c