The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
filter.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2014 - 2025
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
39class filter_context;
40class unit;
41
42
43
44namespace unit_filter_impl
45{
46 struct filter_error : public game::error
47 {
48 explicit filter_error(const std::string& message = "filter error")
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...
74 };
75
77 {
78 virtual bool matches(const unit_filter_args&) const = 0;
79 virtual ~unit_filter_base() {}
80 };
81
83 {
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{
104public:
105 explicit unit_filter(const vconfig& cfg);
106
107 unit_filter(const unit_filter&) = default;
109
110 unit_filter(unit_filter&&) noexcept = default;
111 unit_filter& operator=(unit_filter&&) noexcept = default;
112
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
173 return cfg_.get_config();
174 }
175
176 bool empty() const {
177 return cfg_.get_config().empty();
178 }
179
180private:
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:157
bool empty() const
Definition config.cpp:823
const filter_context * fc_
Definition filter.hpp:183
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
std::vector< const unit * > all_matches_with_unit_at(const unit &u, const map_location &loc) const
Definition filter.hpp:166
unit_filter & set_use_flat_tod(bool value)
Definition filter.hpp:113
bool operator()(const unit &u) const
Definition filter.hpp:144
std::vector< const unit * > all_matches_at(const map_location &loc) const
Definition filter.hpp:158
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
std::vector< const unit * > all_matches_on_map(const map_location *loc=nullptr, const unit *other_unit=nullptr) const
Definition filter.cpp:68
bool matches(const unit &u, const map_location &loc, const unit &u2) const
Definition filter.hpp:134
bool operator()(const unit &u, const unit &u2) const
Definition filter.hpp:152
std::vector< const unit * > all_matches_with_unit(const unit &u) const
Definition filter.hpp:162
unit_const_ptr first_match_on_map() const
Definition filter.cpp:85
unit_filter_impl::unit_filter_compound impl_
Definition filter.hpp:185
bool empty() const
Definition filter.hpp:176
unit_filter(unit_filter &&) noexcept=default
vconfig cfg_
Definition filter.hpp:182
bool operator()(const unit &u, const map_location &loc) const
Definition filter.hpp:140
unit_filter & operator=(const unit_filter &)=default
This class represents a single unit of a specific type.
Definition unit.hpp:39
A variable-expanding proxy for the config class.
Definition variable.hpp:45
const config & get_config() const
Definition variable.hpp:75
const config * cfg
std::shared_ptr< const unit > unit_const_ptr
Definition ptr.hpp:27
std::unique_ptr< MIX_Audio, decltype(&MIX_DestroyAudio)> value
Definition sound.cpp:139
Base class for all the errors encountered by the engine.
std::string message
Encapsulates the map of the game.
Definition location.hpp:46
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 * fc
Definition filter.hpp:59
const filter_context & context() const
Definition filter.hpp:62
virtual bool matches(const unit_filter_args &) const =0
void fill(const vconfig &cfg)
Definition filter.cpp:309
void create_child(const vconfig &c, F func)
Definition filter.cpp:291
void create_attribute(const config::attribute_value &c, C conv, F func)
Definition filter.cpp:297
std::vector< std::pair< conditional_type::type, unit_filter_compound > > cond_children_
Definition filter.hpp:97
virtual bool matches(const unit_filter_args &u) const override
Definition filter.cpp:245
std::vector< std::shared_ptr< unit_filter_base > > children_
Definition filter.hpp:96
#define c