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

Stores a set of data based on terrain, in some cases with raw pointers to other instances of terrain_info (the fallback_). More...

Inheritance diagram for movetype::terrain_info:

Classes

class  data
 
struct  parameters
 The parameters used when calculating a terrain-based value. More...
 

Public Member Functions

 terrain_info (const parameters &params, const terrain_info *fallback)
 Constructor. More...
 
 terrain_info (const config &cfg, const parameters &params, const terrain_info *fallback)
 Constructor. More...
 
 ~terrain_info () override
 Destructor. More...
 
 terrain_info (const terrain_info &that)=delete
 
 terrain_info (terrain_info &&that)=delete
 
 terrain_info (terrain_info &&that, const terrain_info *fallback)
 
 terrain_info (const terrain_info &that, const terrain_info *fallback)
 Copy constructor for callers that handle the fallback and cascade. More...
 
terrain_infooperator= (const terrain_info &that)=delete
 
terrain_infooperator= (terrain_info &&that)=delete
 
void copy_data (const movetype::terrain_info &that)
 This is only expected to be called either when 1) both this and that have no siblings, as happens when terrain_defense is copied, or 2) all of the siblings are being copied, as happens when movetype is copied. More...
 
void swap_data (movetype::terrain_info &that)
 Swap function for the terrain_info class. More...
 
bool empty () const
 Returns whether or not our data is empty. More...
 
void merge (const config &new_values, bool overwrite, const std::vector< movetype::terrain_info * > &dependants)
 Merges the given config over the existing values. More...
 
int value (const t_translation::terrain_code &terrain) const override
 Returns the value associated with the given terrain. More...
 
void write (config &cfg, const std::string &child_name="", bool merged=true) const override
 Writes our data to a config. More...
 
std::unique_ptr< terrain_costsmake_standalone () const override
 Does a sufficiently deep copy so that the returned object's lifespan is independent of other objects' lifespan. More...
 
- Public Member Functions inherited from movetype::terrain_costs
virtual ~terrain_costs ()=default
 
int cost (const t_translation::terrain_code &terrain, bool slowed=false) const
 Returns the cost associated with the given terrain. More...
 

Private Member Functions

void make_data_shareable () const
 Move data to an immutable copy in shared_data_, no-op if the data is already in shared_data_. More...
 
void make_data_writable () const
 Copy the immutable data back to unique_data_, no-op if the data is already in unique_data_. More...
 
const dataget_data () const
 Returns either *unique_data_ or *shared_data_, choosing the one that currently holds the data. More...
 

Private Attributes

std::unique_ptr< dataunique_data_
 
std::shared_ptr< const datashared_data_
 
const terrain_info *const fallback_
 

Detailed Description

Stores a set of data based on terrain, in some cases with raw pointers to other instances of terrain_info (the fallback_).

The data can either be a single instance (in which case it's writable and stored in unique_data_) or may have already been shared (via make_data_shareable()), in which case it's stored in shared_data_. There will always be exactly one of those two that's non-null, get_data() returns it from where-ever it is.

Definition at line 102 of file movetype.hpp.

Constructor & Destructor Documentation

◆ terrain_info() [1/6]

movetype::terrain_info::terrain_info ( const parameters params,
const terrain_info fallback 
)
explicit

Constructor.

Parameters
[in]paramsThe parameters to use when calculating values. This is stored as a reference, so it must be long-lived (typically a static variable).
[in]fallbackUsed as a backup in case we are asked for data we do not have (think vision costs falling back to movement costs).

Definition at line 401 of file movetype.cpp.

◆ terrain_info() [2/6]

movetype::terrain_info::terrain_info ( const config cfg,
const parameters params,
const terrain_info fallback 
)

Constructor.

Parameters
[in]cfgAn initial data set.
[in]paramsThe parameters to use when calculating values. This is stored as a reference, so it must be long-lived (typically a static variable).
[in]fallbackUsed as a backup in case we are asked for data we do not have (think vision costs falling back to movement costs).

Definition at line 416 of file movetype.cpp.

◆ ~terrain_info()

movetype::terrain_info::~terrain_info ( )
overridedefault

Destructor.

While this is simply the default destructor, it needs to be defined in this file so that it knows about ~data(), which is called from the smart pointers' destructor.

◆ terrain_info() [3/6]

movetype::terrain_info::terrain_info ( const terrain_info that)
delete

◆ terrain_info() [4/6]

movetype::terrain_info::terrain_info ( terrain_info &&  that)
delete

◆ terrain_info() [5/6]

movetype::terrain_info::terrain_info ( terrain_info &&  that,
const terrain_info fallback 
)
explicit

Definition at line 449 of file movetype.cpp.

References fallback_, and swap_data().

◆ terrain_info() [6/6]

movetype::terrain_info::terrain_info ( const terrain_info that,
const terrain_info fallback 
)

Copy constructor for callers that handle the fallback and cascade.

This is intended for terrain_defense or movetype's copy constructors, where a similar set of terrain_infos will be created, complete with the same relationships between parts of the set.

Parameters
[in]thatThe terrain_info to copy.
[in]fallbackUsed as a backup in case we are asked for data we do not have (think vision costs falling back to movement costs).

Definition at line 441 of file movetype.cpp.

References copy_data(), and fallback_.

Member Function Documentation

◆ copy_data()

void movetype::terrain_info::copy_data ( const movetype::terrain_info that)

This is only expected to be called either when 1) both this and that have no siblings, as happens when terrain_defense is copied, or 2) all of the siblings are being copied, as happens when movetype is copied.

Definition at line 471 of file movetype.cpp.

References make_data_shareable(), and shared_data_.

Referenced by terrain_info().

◆ empty()

bool movetype::terrain_info::empty ( ) const

Returns whether or not our data is empty.

Definition at line 538 of file movetype.cpp.

Referenced by movetype::has_jamming_data(), and movetype::has_vision_data().

◆ get_data()

const movetype::terrain_info::data & movetype::terrain_info::get_data ( ) const
private

Returns either *unique_data_ or *shared_data_, choosing the one that currently holds the data.

Definition at line 628 of file movetype.cpp.

◆ make_data_shareable()

void movetype::terrain_info::make_data_shareable ( ) const
private

Move data to an immutable copy in shared_data_, no-op if the data is already in shared_data_.

This is recursive on the fallback chain, because if the data shouldn't be writable then the data shouldn't be writable via the fallback either.

Definition at line 670 of file movetype.cpp.

References fallback_, and t.

Referenced by copy_data().

◆ make_data_writable()

void movetype::terrain_info::make_data_writable ( ) const
private

Copy the immutable data back to unique_data_, no-op if the data is already in unique_data_.

Ensures our data is not shared, and therefore that changes only affect this instance of terrain_info (and any instances using it as a fallback).

This does not need to affect the fallback - it's no problem if a writable instance has a fallback to a shareable instance, although a shareable instance must not fallback to a writable instance.

Definition at line 649 of file movetype.cpp.

References data, and t.

◆ make_standalone()

std::unique_ptr< movetype::terrain_costs > movetype::terrain_info::make_standalone ( ) const
overridevirtual

Does a sufficiently deep copy so that the returned object's lifespan is independent of other objects' lifespan.

Never returns nullptr.

This implements terrain_costs's virtual method for getting an instance that doesn't depend on the lifespan of a terrain_defense or movetype object. This will do a deep copy of the data (with fallback_ already merged) if needed.

Implements movetype::terrain_costs.

Definition at line 608 of file movetype.cpp.

References fallback_, t, and movetype::write().

◆ merge()

void movetype::terrain_info::merge ( const config new_values,
bool  overwrite,
const std::vector< movetype::terrain_info * > &  dependants 
)

Merges the given config over the existing values.

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.
[in]dependantsOther instances that use this as a fallback.

Definition at line 551 of file movetype.cpp.

Referenced by movetype::merge().

◆ operator=() [1/2]

terrain_info& movetype::terrain_info::operator= ( const terrain_info that)
delete

◆ operator=() [2/2]

terrain_info& movetype::terrain_info::operator= ( terrain_info &&  that)
delete

◆ swap_data()

void movetype::terrain_info::swap_data ( movetype::terrain_info that)

Swap function for the terrain_info class.

This is only expected to be called either when 1) both this and that have no siblings, as happens when swapping two terrain_defenses, or 2) all of the siblings are being swapped, as happens when two movetypes are swapped.

Definition at line 485 of file movetype.cpp.

References shared_data_, swap(), and unique_data_.

Referenced by terrain_info().

◆ value()

int movetype::terrain_info::value ( const t_translation::terrain_code terrain) const
overridevirtual

◆ write()

void movetype::terrain_info::write ( config cfg,
const std::string &  child_name = "",
bool  merged = true 
) const
overridevirtual

Writes our data to a config.

Parameters
[out]cfgThe config that will receive the data.
[in]child_nameIf not empty, create and write to a child config with this tag.
[in]mergedIf true, our data will be merged with our fallback's, and it is possible an empty child will be created. If false, data will not be merged, and an empty child will not be created.

Implements movetype::terrain_costs.

Definition at line 589 of file movetype.cpp.

References fallback_.

Referenced by movetype::write(), movetype::terrain_defense::write(), and movetype::terrain_info::data::write().

Member Data Documentation

◆ fallback_

const terrain_info* const movetype::terrain_info::fallback_
private

Definition at line 165 of file movetype.hpp.

Referenced by terrain_info().

◆ shared_data_

std::shared_ptr<const data> movetype::terrain_info::shared_data_
private

Definition at line 164 of file movetype.hpp.

Referenced by copy_data(), and swap_data().

◆ unique_data_

std::unique_ptr<data> movetype::terrain_info::unique_data_
private

Definition at line 163 of file movetype.hpp.

Referenced by swap_data().


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