The Battle for Wesnoth  1.19.7+dev
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
movetype::terrain_info::data Class Reference

Public Member Functions

 data (const parameters &params)
 Constructor. More...
 
 data (const config &cfg, const parameters &params)
 Constructor. More...
 
 data (const data &that)
 
void clear_cache () const
 Clears the cached data (presumably our fallback has changed). More...
 
bool config_has_changes (const config &new_values, bool overwrite) const
 Tests if merging new_values would result in changes. More...
 
bool empty () const
 Tests for no data in this object. More...
 
void merge (const config &new_values, bool overwrite)
 Merges the given config over the existing costs. More...
 
const parametersparams () const
 Read-only access to our parameters. More...
 
int value (const t_translation::terrain_code &terrain, const terrain_info *fallback) const
 Returns the value associated with the given terrain. More...
 
void write (config &out_cfg, const std::string &child_name) const
 If there is data, writes it to the config. More...
 
void write (config &out_cfg, const std::string &child_name, const terrain_info *fallback) const
 If there is (merged) data, writes it to the config. More...
 

Private Types

typedef std::map< t_translation::terrain_code, int > cache_t
 

Private Member Functions

int calc_value (const t_translation::terrain_code &terrain, const terrain_info *fallback, unsigned recurse_count) const
 Calculates the value associated with the given terrain. More...
 
int value (const t_translation::terrain_code &terrain, const terrain_info *fallback, unsigned recurse_count) const
 Returns the value associated with the given terrain (possibly cached). More...
 

Private Attributes

config cfg_
 Config describing the terrain values. More...
 
cache_t cache_
 Cache of values based on the config. More...
 
const parametersparams_
 Various parameters used when calculating values. More...
 

Detailed Description

Definition at line 89 of file movetype.cpp.

Member Typedef Documentation

◆ cache_t

Definition at line 142 of file movetype.cpp.

Constructor & Destructor Documentation

◆ data() [1/3]

movetype::terrain_info::data::data ( const parameters params)
inlineexplicit

Constructor.

params must be long-lived (typically a static variable).

Definition at line 96 of file movetype.cpp.

◆ data() [2/3]

movetype::terrain_info::data::data ( const config cfg,
const parameters params 
)
inline

Constructor.

params must be long-lived (typically a static variable).

Definition at line 103 of file movetype.cpp.

◆ data() [3/3]

movetype::terrain_info::data::data ( const data that)
inline

Definition at line 109 of file movetype.cpp.

Member Function Documentation

◆ calc_value()

int movetype::terrain_info::data::calc_value ( const t_translation::terrain_code terrain,
const terrain_info fallback,
unsigned  recurse_count 
) const
private

Calculates the value associated with the given terrain.

This is separate from value() to separate the calculating of the value from the caching of it.

Parameters
[in]terrainThe terrain whose value is requested.
[in]fallbackConsulted if we are missing data.
[in]recurse_countDetects (probable) infinite recursion.

Definition at line 273 of file movetype.cpp.

References ERR_CF, game_config_manager::get(), i, terrain_type::is_indivisible(), t_translation::MINUS, t_translation::PLUS, game_config_manager::terrain_types(), movetype::terrain_info::value(), t_translation::write_terrain_code(), and WRN_CF.

◆ clear_cache()

void movetype::terrain_info::data::clear_cache ( ) const

Clears the cached data (presumably our fallback has changed).

Definition at line 156 of file movetype.cpp.

References cache_.

◆ config_has_changes()

bool movetype::terrain_info::data::config_has_changes ( const config new_values,
bool  overwrite 
) const

Tests if merging new_values would result in changes.

This allows the shared data to actually work, as otherwise each unit created via WML (including unstored units) would "overwrite" its movement data with a usually identical copy and thus break the sharing.

Definition at line 168 of file movetype.cpp.

References _(), config::attribute_range(), and movetype::terrain_info::value().

◆ empty()

bool movetype::terrain_info::data::empty ( ) const
inline

Tests for no data in this object.

Definition at line 118 of file movetype.cpp.

References cfg_, and config::empty().

◆ merge()

void movetype::terrain_info::data::merge ( const config new_values,
bool  overwrite 
)

Merges the given config over the existing costs.

After calling this function, the caller must call clear_cache on any terrain_info that uses this one as a fallback.

Parameters
[in]new_valuesThe new values.
[in]overwriteIf true, the new values overwrite the old. If false, the new values are added to the old.

Definition at line 197 of file movetype.cpp.

References config::attribute_range(), config_attribute_value::to_int(), and movetype::terrain_info::value().

◆ params()

const parameters& movetype::terrain_info::data::params ( ) const
inline

Read-only access to our parameters.

Definition at line 122 of file movetype.cpp.

References params_.

◆ value() [1/2]

int movetype::terrain_info::data::value ( const t_translation::terrain_code terrain,
const terrain_info fallback 
) const
inline

Returns the value associated with the given terrain.

Definition at line 124 of file movetype.cpp.

◆ value() [2/2]

int movetype::terrain_info::data::value ( const t_translation::terrain_code terrain,
const terrain_info fallback,
unsigned  recurse_count 
) const
private

Returns the value associated with the given terrain (possibly cached).

Parameters
[in]terrainThe terrain whose value is requested.
[in]fallbackConsulted if we are missing data.
[in]recurse_countDetects (probable) infinite recursion.

Definition at line 376 of file movetype.cpp.

◆ write() [1/2]

void movetype::terrain_info::data::write ( config out_cfg,
const std::string &  child_name 
) const

If there is data, writes it to the config.

If there is data, writes it to a config.

Parameters
[out]out_cfgThe config that will receive the data.
[in]child_nameIf not empty, create and write to a child config with this tag. This child will not be created if there is no data to write.

Definition at line 233 of file movetype.cpp.

References config::add_child(), and config::merge_with().

◆ write() [2/2]

void movetype::terrain_info::data::write ( config out_cfg,
const std::string &  child_name,
const terrain_info fallback 
) const

If there is (merged) data, writes it to the config.

Writes merged data to a config.

Parameters
[out]out_cfgThe config that will receive the data.
[in]child_nameIf not empty, create and write to a child config with this tag. This will be created even if there is no data to write.
[in]fallbackIf not nullptr, its data will be merged with ours for the write.

Definition at line 253 of file movetype.cpp.

References config::add_child(), config::empty(), config::merge_with(), and movetype::terrain_info::write().

Member Data Documentation

◆ cache_

cache_t movetype::terrain_info::data::cache_
mutableprivate

Cache of values based on the config.

Definition at line 147 of file movetype.cpp.

Referenced by clear_cache().

◆ cfg_

config movetype::terrain_info::data::cfg_
private

Config describing the terrain values.

Definition at line 145 of file movetype.cpp.

Referenced by empty().

◆ params_

const parameters& movetype::terrain_info::data::params_
private

Various parameters used when calculating values.

Definition at line 149 of file movetype.cpp.

Referenced by params().


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