The Battle for Wesnoth  1.19.7+dev
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
vconfig Class Reference

A variable-expanding proxy for the config class. More...

#include <variable.hpp>

Classes

struct  all_children_iterator
 
struct  attribute_iterator
 
struct  recursion_error
 

Public Types

typedef std::vector< vconfigchild_list
 

Public Member Functions

 vconfig (const config &cfg)
 Constructor from a config. More...
 
 vconfig (config &&cfg)
 
 vconfig (const config &cfg, const variable_set &variables)
 
 vconfig (const config &cfg, bool manage_memory, const variable_set *variables=nullptr)
 Constructor from a config, with an option to manage memory. More...
 
 ~vconfig ()
 Default destructor, but defined here for possibly faster compiles (templates sometimes can be rough on the compiler). More...
 
 operator bool () const
 A vconfig evaluates to true iff it can be dereferenced. More...
 
bool null () const
 
const vconfigmake_safe () const
 instruct the vconfig to make a private copy of its underlying data. More...
 
const configget_config () const
 
config get_parsed_config () const
 
child_list get_children (const std::string &key) const
 
std::size_t count_children (const std::string &key) const
 
vconfig child (const std::string &key) const
 Returns a child of *this whose key is key. More...
 
bool has_child (const std::string &key) const
 Returns whether or not *this has a child whose key is key. More...
 
const config::attribute_value operator[] (const std::string &key) const
 Note: vconfig::operator[] returns const, and this should not be changed because vconfig is often used as a drop-in replacement for config, and this const will properly warn you if you try to assign vcfg["key"]=val;. More...
 
config::attribute_value expand (const std::string &) const
 
bool has_attribute (const std::string &key) const
 < Synonym for operator[] More...
 
bool empty () const
 
boost::iterator_range< attribute_iteratorattribute_range ()
 
all_children_iterator ordered_begin () const
 In-order iteration over all children. More...
 
all_children_iterator ordered_end () const
 
boost::iterator_range< all_children_iteratorall_ordered () const
 

Static Public Member Functions

static vconfig empty_vconfig ()
 
static vconfig unconstructed_vconfig ()
 This is just a wrapper for the default constructor; it exists for historical reasons and to make it clear that default construction cannot be dereferenced (in contrast to an empty vconfig). More...
 

Private Member Functions

 vconfig ()
 
 vconfig (const config &cfg, const std::shared_ptr< const config > &cache)
 
 vconfig (const config &cfg, const std::shared_ptr< const config > &cache, const variable_set &variables)
 
bool memory_managed () const
 Returns true if *this has made a copy of its config. More...
 

Private Attributes

std::shared_ptr< const configcache_
 Keeps a copy of our config alive when we manage our own memory. More...
 
const configcfg_
 Used to access our config (original or copy, as appropriate). More...
 
const variable_setvariables_
 

Static Private Attributes

static const config default_empty_config = config()
 

Detailed Description

A variable-expanding proxy for the config class.

This class roughly behaves as a constant config object, but automatically expands variables.

When dealing with a vconfig, keep in mind its lifetime. By default, vconfigs do not maintain a copy their data; if you need a vconfig to stick around, either construct it with manage_memory=true or call make_safe(). This will cause the vconfig to make a copy of the underlying config object.

Definition at line 44 of file variable.hpp.

Member Typedef Documentation

◆ child_list

typedef std::vector<vconfig> vconfig::child_list

Definition at line 78 of file variable.hpp.

Constructor & Destructor Documentation

◆ vconfig() [1/7]

vconfig::vconfig ( )
private

◆ vconfig() [2/7]

vconfig::vconfig ( const config cfg,
const std::shared_ptr< const config > &  cache 
)
private

Definition at line 94 of file variable.cpp.

◆ vconfig() [3/7]

vconfig::vconfig ( const config cfg,
const std::shared_ptr< const config > &  cache,
const variable_set variables 
)
private

Definition at line 125 of file variable.cpp.

◆ vconfig() [4/7]

vconfig::vconfig ( const config cfg)
explicit

Constructor from a config.

Equivalent to vconfig(cfg, false). Do not use if the vconfig will persist after cfg is destroyed!

Definition at line 117 of file variable.cpp.

◆ vconfig() [5/7]

vconfig::vconfig ( config &&  cfg)
explicit

Definition at line 121 of file variable.cpp.

◆ vconfig() [6/7]

vconfig::vconfig ( const config cfg,
const variable_set variables 
)

Definition at line 129 of file variable.cpp.

◆ vconfig() [7/7]

vconfig::vconfig ( const config cfg,
bool  manage_memory,
const variable_set vars = nullptr 
)

Constructor from a config, with an option to manage memory.

Parameters
[in]cfgThe "WML source" of the vconfig being constructed.
[in]manage_memoryIf true, a copy of cfg will be made, allowing the vconfig to safely persist after cfg is destroyed. If false, no copy is made, so cfg must be guaranteed to persist as long as the vconfig will. If in doubt, set to true; it is less efficient, but safe.
[in]varsSee also make_safe().

Definition at line 110 of file variable.cpp.

◆ ~vconfig()

vconfig::~vconfig ( )

Default destructor, but defined here for possibly faster compiles (templates sometimes can be rough on the compiler).

Definition at line 137 of file variable.cpp.

Referenced by lua_common::impl_vconfig_collect().

Member Function Documentation

◆ all_ordered()

boost::iterator_range<all_children_iterator> vconfig::all_ordered ( ) const
inline

◆ attribute_range()

boost::iterator_range<attribute_iterator> vconfig::attribute_range ( )
inline

Definition at line 131 of file variable.hpp.

References config::attribute_range(), cfg_, and variables_.

◆ child()

vconfig vconfig::child ( const std::string &  key) const

◆ count_children()

std::size_t vconfig::count_children ( const std::string &  key) const

Definition at line 256 of file variable.cpp.

References config::all_children_view(), cfg_, n, and variables_.

◆ empty()

bool vconfig::empty ( ) const
inline

◆ empty_vconfig()

vconfig vconfig::empty_vconfig ( )
static

◆ expand()

config::attribute_value vconfig::expand ( const std::string &  key) const

◆ get_children()

vconfig::child_list vconfig::get_children ( const std::string &  key) const

◆ get_config()

const config& vconfig::get_config ( ) const
inline

◆ get_parsed_config()

config vconfig::get_parsed_config ( ) const

◆ has_attribute()

bool vconfig::has_attribute ( const std::string &  key) const
inline

◆ has_child()

bool vconfig::has_child ( const std::string &  key) const

Returns whether or not *this has a child whose key is key.

Definition at line 315 of file variable.cpp.

References cfg_, config::child_range(), config::has_child(), and variables_.

Referenced by terrain_filter::get_locs_impl(), terrain_filter::match_impl(), terrain_filter::match_internal(), and side_filter::match_internal().

◆ make_safe()

const vconfig & vconfig::make_safe ( ) const

instruct the vconfig to make a private copy of its underlying data.

Ensures that *this manages its own memory, making it safe for *this to outlive the config it was ultimately constructed from.

It is perfectly safe to call this for a vconfig that already manages its memory. This does not work on a null() vconfig.

Definition at line 163 of file variable.cpp.

References cache_, cfg_, and memory_managed().

Referenced by ai::ai_default_rca::aspect_attacks::aspect_attacks(), ai::candidate_action::candidate_action(), terrain_filter::match_internal(), side_filter::match_internal(), and game_events::wml_menu_item::update().

◆ memory_managed()

bool vconfig::memory_managed ( ) const
inlineprivate

Returns true if *this has made a copy of its config.

Definition at line 196 of file variable.hpp.

References cache_.

Referenced by make_safe().

◆ null()

bool vconfig::null ( ) const
inline

◆ operator bool()

vconfig::operator bool ( ) const
inlineexplicit

A vconfig evaluates to true iff it can be dereferenced.

Definition at line 70 of file variable.hpp.

◆ operator[]()

const config::attribute_value vconfig::operator[] ( const std::string &  key) const
inline

Note: vconfig::operator[] returns const, and this should not be changed because vconfig is often used as a drop-in replacement for config, and this const will properly warn you if you try to assign vcfg["key"]=val;.

Note: The following construction is unsafe: const std::string& temp = vcfg["foo"]; This binds temp to a member of a temporary t_string. The lifetime of the temporary is not extended by this reference binding and the temporary's lifetime ends which causes UB. Instead use: const std::string temp = vcfg["foo"];

Definition at line 96 of file variable.hpp.

References expand().

◆ ordered_begin()

vconfig::all_children_iterator vconfig::ordered_begin ( ) const

◆ ordered_end()

vconfig::all_children_iterator vconfig::ordered_end ( ) const

◆ unconstructed_vconfig()

vconfig vconfig::unconstructed_vconfig ( )
static

This is just a wrapper for the default constructor; it exists for historical reasons and to make it clear that default construction cannot be dereferenced (in contrast to an empty vconfig).

Definition at line 152 of file variable.cpp.

References vconfig().

Referenced by child(), game_lua_kernel::intf_find_cost_map(), game_lua_kernel::intf_gamestate_inspector(), lua_check_impl::lua_to_or_default(), luaW_checkvconfig(), luaW_tofaivariant(), and luaW_tolocation().

Member Data Documentation

◆ cache_

std::shared_ptr<const config> vconfig::cache_
mutableprivate

Keeps a copy of our config alive when we manage our own memory.

If this is not null, then cfg_ points to *cache_ or a child of *cache_.

Definition at line 202 of file variable.hpp.

Referenced by child(), vconfig::all_children_iterator::get_child(), get_children(), make_safe(), memory_managed(), ordered_begin(), and ordered_end().

◆ cfg_

const config* vconfig::cfg_
mutableprivate

Used to access our config (original or copy, as appropriate).

Definition at line 204 of file variable.hpp.

Referenced by attribute_range(), child(), count_children(), empty(), get_children(), get_config(), get_parsed_config(), has_attribute(), has_child(), make_safe(), null(), ordered_begin(), and ordered_end().

◆ default_empty_config

const config vconfig::default_empty_config = config()
staticprivate

Definition at line 206 of file variable.hpp.

Referenced by null().

◆ variables_

const variable_set* vconfig::variables_
private

The documentation for this class was generated from the following files: