The Battle for Wesnoth  1.19.0-dev
Functions
utils::config_filters Namespace Reference

Utility functions for implementing [filter], [filter_ability], [filter_weapon], etc. More...

Functions

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]. More...
 
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]. More...
 
bool int_matches_if_present (const config &filter, const config &cfg, const std::string &attribute, std::optional< int > def=std::nullopt)
 
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. More...
 
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 containing either add=1 or sub=-1; this assumes that code elsewhere has already checked that cfg contains at most one of those keys. More...
 
bool string_matches_if_present (const config &filter, const config &cfg, const std::string &attribute, const std::string &def)
 
bool bool_or_empty (const config &filter, const config &cfg, const std::string &attribute)
 

Detailed Description

Utility functions for implementing [filter], [filter_ability], [filter_weapon], etc.

For example, a filter of x=1 puts a requirement on the value of x but accepts any value of y.

Both int and double assume a default value of zero, so a filter that accepts zero will match an unset value as well as a present value.

Function Documentation

◆ bool_matches_if_present()

bool utils::config_filters::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].

If cfg doesn't have that attribute, assume that an unset value is equivalent to def.

Always returns true if the filter puts no restriction on the value of cfg[attribute].

Definition at line 23 of file config_filters.cpp.

References config::has_attribute().

Referenced by matches_ability_filter().

◆ bool_or_empty()

bool utils::config_filters::bool_or_empty ( const config filter,
const config cfg,
const std::string &  attribute 
)

Definition at line 102 of file config_filters.cpp.

References config::has_attribute(), and utils::split_set().

Referenced by matches_ability_filter().

◆ double_matches_if_present()

bool utils::config_filters::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].

If cfg doesn't have that attribute, assume that an unset value is equivalent to def if exist, else value false is returned.

Always returns true if the filter puts no restriction on the value of cfg[attribute].

Definition at line 89 of file config_filters.cpp.

References config::has_attribute(), and utils::parse_ranges_real().

Referenced by matches_ability_filter().

◆ int_matches_if_present()

bool utils::config_filters::int_matches_if_present ( const config filter,
const config cfg,
const std::string &  attribute,
std::optional< int >  def = std::nullopt 
)

◆ int_matches_if_present_or_negative()

bool utils::config_filters::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 containing either add=1 or sub=-1; this assumes that code elsewhere has already checked that cfg contains at most one of those keys.

This only checks for the presence of attribute and opposite in the filter, so the caller should call this function a second time, with attribute and opposite reversed and if none of these attribute is here value false is returned.

The function is named "negative" in case we later want to add a "reciprocal" for the "multiply"/"divide" pair.

Definition at line 69 of file config_filters.cpp.

References config::has_attribute(), int_matches_if_present(), and utils::parse_ranges_int().

Referenced by BOOST_AUTO_TEST_CASE(), and matches_ability_filter().

◆ string_matches_if_present()

bool utils::config_filters::string_matches_if_present ( const config filter,
const config cfg,
const std::string &  attribute,
const std::string &  def 
)

Definition at line 32 of file config_filters.cpp.

References config::has_attribute(), and utils::split().

Referenced by matches_ability_filter().

◆ unsigned_matches_if_present()

bool utils::config_filters::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.

A filter such as "-1-10" or "-10--1,1-10" is considered invalid and never matches anything, not even a positive value that would be accepted by a stricter subset of the filter.

Definition at line 44 of file config_filters.cpp.

References config::has_attribute(), and utils::parse_ranges_unsigned().

Referenced by BOOST_AUTO_TEST_CASE().