The Battle for Wesnoth  1.15.1+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:
Inheritance graph

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 91 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 394 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 409 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.

Referenced by terrain_info().

◆ 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 442 of file movetype.cpp.

References fallback_, swap_data(), and ~terrain_info().

◆ 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 434 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 464 of file movetype.cpp.

References make_data_shareable(), shared_data_, and unique_data_.

Referenced by movetype::terrain_defense::operator=(), and terrain_info().

◆ empty()

bool movetype::terrain_info::empty ( ) const

Returns whether or not our data is empty.

Definition at line 530 of file movetype.cpp.

References movetype::terrain_info::data::empty(), and get_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 620 of file movetype.cpp.

References shared_data_, and unique_data_.

Referenced by empty(), make_standalone(), merge(), value(), and write().

◆ 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 662 of file movetype.cpp.

References fallback_, make_data_shareable(), t, and unique_data_.

Referenced by copy_data(), and make_data_shareable().

◆ 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 641 of file movetype.cpp.

References shared_data_, t, and unique_data_.

Referenced by merge().

◆ 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 600 of file movetype.cpp.

References movetype::terrain_info::data::empty(), fallback_, get_data(), make_standalone(), movetype::terrain_info::data::params(), t, and write().

Referenced by make_standalone().

◆ 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 543 of file movetype.cpp.

References get_data(), make_data_writable(), and unique_data_.

Referenced by movetype::terrain_info::data::empty(), movetype::terrain_defense::merge(), and 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 478 of file movetype.cpp.

References shared_data_, swap(), and unique_data_.

Referenced by movetype::terrain_defense::operator=(), swap(), and terrain_info().

◆ value()

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

Returns the value associated with the given terrain.

Implements movetype::terrain_costs.

Definition at line 569 of file movetype.cpp.

References fallback_, get_data(), and movetype::terrain_info::data::value().

Referenced by movetype::terrain_info::data::calc_value(), movetype::terrain_info::data::merge(), and movetype::terrain_info::data::value().

◆ 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 581 of file movetype.cpp.

References fallback_, get_data(), and movetype::terrain_info::data::write().

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

Member Data Documentation

◆ fallback_

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

Definition at line 148 of file movetype.hpp.

Referenced by make_data_shareable(), make_standalone(), terrain_info(), value(), and write().

◆ shared_data_

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

Definition at line 147 of file movetype.hpp.

Referenced by copy_data(), get_data(), make_data_writable(), and swap_data().

◆ unique_data_

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

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