Stores a set of data based on terrain, in some cases with raw pointers to other instances of terrain_info (the fallback_). More...
Classes | |
class | data |
struct | parameters |
The parameters used when calculating a terrain-based value. More... | |
Public Member Functions | |
terrain_info (const parameters ¶ms, const terrain_info *fallback) | |
Constructor. More... | |
terrain_info (const config &cfg, const parameters ¶ms, 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_info & | operator= (const terrain_info &that)=delete |
terrain_info & | operator= (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_costs > | make_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 data & | get_data () const |
Returns either *unique_data_ or *shared_data_, choosing the one that currently holds the data. More... | |
Private Attributes | |
std::unique_ptr< data > | unique_data_ |
std::shared_ptr< const data > | shared_data_ |
const terrain_info *const | fallback_ |
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.
|
explicit |
Constructor.
[in] | params | The parameters to use when calculating values. This is stored as a reference, so it must be long-lived (typically a static variable). |
[in] | fallback | Used 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.
movetype::terrain_info::terrain_info | ( | const config & | cfg, |
const parameters & | params, | ||
const terrain_info * | fallback | ||
) |
Constructor.
[in] | cfg | An initial data set. |
[in] | params | The parameters to use when calculating values. This is stored as a reference, so it must be long-lived (typically a static variable). |
[in] | fallback | Used 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.
|
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.
|
delete |
|
delete |
|
explicit |
Definition at line 449 of file movetype.cpp.
References fallback_, and swap_data().
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.
[in] | that | The terrain_info to copy. |
[in] | fallback | Used 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_.
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().
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().
|
private |
Returns either *unique_data_ or *shared_data_, choosing the one that currently holds the data.
Definition at line 628 of file movetype.cpp.
|
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.
Referenced by copy_data().
|
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.
|
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().
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.
[in] | new_values | The new values. |
[in] | overwrite | If true, the new values overwrite the old. If false, the new values are added to the old. |
[in] | dependants | Other instances that use this as a fallback. |
Definition at line 551 of file movetype.cpp.
Referenced by movetype::merge().
|
delete |
|
delete |
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().
|
overridevirtual |
Returns the value associated with the given terrain.
Implements movetype::terrain_costs.
Definition at line 577 of file movetype.cpp.
References fallback_.
Referenced by movetype::terrain_info::data::calc_value(), movetype::terrain_defense::capped(), movetype::terrain_info::data::config_has_changes(), movetype::terrain_defense::defense(), and movetype::terrain_info::data::merge().
|
overridevirtual |
Writes our data to a config.
[out] | cfg | The config that will receive the data. |
[in] | child_name | If not empty, create and write to a child config with this tag. |
[in] | merged | If 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().
|
private |
Definition at line 165 of file movetype.hpp.
Referenced by terrain_info().
|
private |
Definition at line 164 of file movetype.hpp.
Referenced by copy_data(), and swap_data().
|
private |
Definition at line 163 of file movetype.hpp.
Referenced by swap_data().