The Battle for Wesnoth  1.19.0-dev
config_filters.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
17 #include "config.hpp"
18 
19 /**
20  * Utility functions for implementing [filter], [filter_ability], [filter_weapon], etc.
21  *
22  * For example, a filter of `x=1` puts a requirement on the value of `x` but accepts any value of `y`.
23  *
24  * Both `int` and `double` assume a default value of zero, so a filter that accepts zero will match an
25  * unset value as well as a present value.
26  */
28 {
29 /**
30  * Checks whether the filter matches the value of @a cfg[@a attribute]. If @a cfg doesn't have that
31  * attribute, assume that an unset value is equivalent to @a def.
32  *
33  * Always returns true if the filter puts no restriction on the value of @a cfg[@a attribute].
34  */
35 bool bool_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, bool def);
36 
37 /**
38  * Checks whether the filter matches the value of @a cfg[@a attribute]. If @a cfg doesn't have that
39  * attribute, assume that an unset value is equivalent to @a def if exist, else value false is returned.
40  *
41  * Always returns true if the filter puts no restriction on the value of @a cfg[@a attribute].
42  */
43 bool double_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, std::optional<double> def = std::nullopt);
44 bool int_matches_if_present(const config& filter, const config& cfg, const std::string& attribute, std::optional<int> def = std::nullopt);
45 
46 /**
47  * Restricts filters to only looking for values that are zero or more.
48  *
49  * A filter such as "-1-10" or "-10--1,1-10" is considered invalid and never matches anything, not
50  * even a positive value that would be accepted by a stricter subset of the filter.
51  */
52 bool unsigned_matches_if_present(const config& filter, const config& cfg, const std::string& attribute);
53 
54 /**
55  * Supports filters using "add" and "sub" attributes, for example a filter `add=1` matching a cfg containing either
56  * `add=1` or `sub=-1`; this assumes that code elsewhere has already checked that cfg contains at most one of those
57  * keys.
58  *
59  * This only checks for the presence of @a attribute and @a opposite in the filter, so the caller should call this function a second
60  * time, with @a attribute and @a opposite reversed and if none of these attribute is here value false is returned.
61  *
62  * The function is named "negative" in case we later want to add a "reciprocal" for the "multiply"/"divide" pair.
63  */
65  const config& filter, const config& cfg, const std::string& attribute, const std::string& opposite, std::optional<int> def = std::nullopt);
66 
68  const config& filter, const config& cfg, const std::string& attribute, const std::string& def);
69 
70 bool bool_or_empty(const config& filter, const config& cfg, const std::string& attribute);
71 
72 } // namespace utils::config_filters
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
Utility functions for implementing [filter], [filter_ability], [filter_weapon], etc.
bool unsigned_matches_if_present(const config &filter, const config &cfg, const std::string &attribute)
Restricts filters to only looking for values that are zero or more.
bool int_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, std::optional< int > def=std::nullopt)
bool string_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, const std::string &def)
bool double_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, std::optional< double > def=std::nullopt)
Checks whether the filter matches the value of cfg[attribute].
bool bool_or_empty(const config &filter, const config &cfg, const std::string &attribute)
bool int_matches_if_present_or_negative(const config &filter, const config &cfg, const std::string &attribute, const std::string &opposite, std::optional< int > def=std::nullopt)
Supports filters using "add" and "sub" attributes, for example a filter add=1 matching a cfg containi...
bool bool_matches_if_present(const config &filter, const config &cfg, const std::string &attribute, bool def)
Checks whether the filter matches the value of cfg[attribute].