OpenTTD Source 20260917-master-gec444f7315
SaveLoad Struct Reference

SaveLoad type struct. More...

#include <saveload.h>

Public Types

template<typename R = void>
using AddressFunction = const R *(*)(const void *base, size_t extra)
 Function that returns the address of a variable.

Static Public Member Functions

static constexpr bool IsIntegralFileType (VarFileType file_type)
 Check whether the given file type is stored as a simple number.
template<typename T, VarFileType file_type>
static constexpr VarMemType DetermineMemType ()
 Determine the VarMemType type given the variable's decltype and occasionally the file type to distinguish options.
template<typename T>
static constexpr bool IsValidRefType (SLRefType ref_type)
 Checks whether the memory type and given reference type match.
template<typename T>
static constexpr AddressFunction ToAddressFunction (AddressFunction< T > address_func)
 To be able to get the type information into the SaveLoad constructing functions, we need some infrastructure.
template<VarFileType file_type, typename T>
static SaveLoad Variable (std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
 Storage of a variable in some savegame versions.
template<SLRefType type, typename T>
static SaveLoad Reference (std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
 Storage of a reference to a pool element in some savegame versions.
template<typename T>
static SaveLoad String (std::string name, AddressFunction< T > address_func, StringValidationSettings string_validation_settings={}, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
 Storage of a string in some savegame versions.
template<typename T>
requires std::is_base_of_v<SaveLoadHandler, T>
static SaveLoad Struct (std::string name, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
 Storage of a structs, optionally in some savegame versions.
template<VarFileType file_type, uint16_t length, typename T>
static SaveLoad Array (std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
 Storage of an array in some savegame versions.
template<VarFileType file_type, typename T>
static SaveLoad Vector (std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
 Storage of a vector in some savegame versions.
template<SLRefType type, typename T>
static SaveLoad ReferenceList (std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
 Storage of a list of references to pool elements in some savegame versions.
template<typename T>
requires std::is_base_of_v<SaveLoadHandler, T>
static SaveLoad StructList (std::string name, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
 Storage of a list of structs, optionally in some savegame versions.
template<typename T>
static SaveLoad SaveByte (std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
 Storage of a byte variable in some savegame versions that is not automatically read by the SaveLoad code.
template<SLRefType type, typename T>
static SaveLoad ReferenceVector (std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion)
 Storage of a vector of references to pool elements in some savegame versions.

Data Fields

std::string name
 Name of this field (optional, used for tables).
SaveLoadType cmd
 The action to take with the saved/loaded type, All types need different action.
VarType conv {}
 Type of the variable to be saved; this field combines both FileVarType and MemVarType.
uint16_t length {}
 (Conditional) length of the variable (eg. arrays) (max array size is 65536 elements).
SaveLoadVersion version_from
 Save/load the variable starting from this savegame version.
SaveLoadVersion version_to
 Save/load the variable before this savegame version.
AddressFunction address_func = nullptr
 Callback function the get the actual variable address in memory.
size_t extra_data = 0
 Extra data for the callback proc.
std::shared_ptr< SaveLoadHandler > handler {}
 Custom handler for Save/Load procs.

Detailed Description

SaveLoad type struct.

Do NOT use this directly but use the SLE_ macros defined just below!

Definition at line 764 of file saveload.h.

Member Typedef Documentation

◆ AddressFunction

template<typename R = void>
using SaveLoad::AddressFunction = const R *(*)(const void *base, size_t extra)

Function that returns the address of a variable.

Template Parameters
RThe return type of the function.
Parameters
baseIn case the variable comes from an object, this is the pointer to the begin of that object. Will be non-nullptr for objects, can be both non-nullptr and nullptr for global variables.
extraAn extra offset to apply. Mostly 0, except for a few LinkGraph settings variables.
Returns
The address of the variable.

Definition at line 774 of file saveload.h.

Member Function Documentation

◆ Array()

template<VarFileType file_type, uint16_t length, typename T>
SaveLoad SaveLoad::Array ( std::string name,
AddressFunction< T > address_func,
SaveLoadVersion from = SaveLoadVersion::MinVersion,
SaveLoadVersion to = SaveLoadVersion::MaxVersion,
size_t extra = 0 )
inlinestatic

Storage of an array in some savegame versions.

Template Parameters
file_typeStorage type of the array's elements in the savegame.
lengthThe length of the array.
TThe type of the field in memory. This is automatically deduced and used for validation.
Parameters
nameField name for table chunks.
address_funcFunction to get the address of the array.
fromFirst savegame version that has the field. Defaults to the first version.
toLast savegame version that has the field. Defaults to the last version.
extraArbitrary extra data to pass to the address_func.
Returns
The constructed SaveLoad object.

Definition at line 1026 of file saveload.h.

References address_func, Array, DetermineMemType(), length, MaxVersion, MinVersion, name, SlVarSize(), T, and ToAddressFunction().

◆ DetermineMemType()

template<typename T, VarFileType file_type>
constexpr VarMemType SaveLoad::DetermineMemType ( )
inlinestaticconstexpr

Determine the VarMemType type given the variable's decltype and occasionally the file type to distinguish options.

If feasible a sanity check is do to check the file type against the variable's decltype, for example to prevent a bool to be saved as string.

Template Parameters
TThe variable's decltype.
file_typeThe way this variable will be/is stored in the save file.
Returns
The VarMemType.
Note
file_type is a template parameter to be able to perform all type checks at compile time.

Definition at line 818 of file saveload.h.

References Bool, DetermineMemType(), I16, I32, I64, I8, IsIntegralFileType(), Label, Name, Str, String, StringID, T, U16, U32, U64, and U8.

Referenced by Array(), DetermineMemType(), SaveByte(), Variable(), and Vector().

◆ IsIntegralFileType()

constexpr bool SaveLoad::IsIntegralFileType ( VarFileType file_type)
inlinestaticconstexpr

Check whether the given file type is stored as a simple number.

Parameters
file_typeThe file type to check.
Returns
true iff signed or unsigned 8, 16, 32 or 64 bit integer.

Definition at line 791 of file saveload.h.

References I16, I32, I64, I8, U16, U32, U64, and U8.

Referenced by DetermineMemType().

◆ IsValidRefType()

template<typename T>
constexpr bool SaveLoad::IsValidRefType ( SLRefType ref_type)
inlinestaticconstexpr

Checks whether the memory type and given reference type match.

Template Parameters
TThe type of the reference in memory.
Parameters
ref_typeThe specified reference type.
Returns
true iff the memory and reference type match.

Definition at line 879 of file saveload.h.

References CargoPacket, EngineRenew, LinkGraph, LinkGraphJob, OldVehicle, OrderList, RoadStop, Station, Storage, Town, and Vehicle.

Referenced by ReferenceList(), and ReferenceVector().

◆ Reference()

template<SLRefType type, typename T>
SaveLoad SaveLoad::Reference ( std::string name,
AddressFunction< T > address_func,
SaveLoadVersion from = SaveLoadVersion::MinVersion,
SaveLoadVersion to = SaveLoadVersion::MaxVersion )
inlinestatic

Storage of a reference to a pool element in some savegame versions.

Template Parameters
typeThe type of reference.
TThe type of the reference in memory. This is automatically deduced and used for validation.
Parameters
nameField name for table chunks.
address_funcFunction to get the address of the reference.
fromFirst savegame version that has the field. Defaults to the first version.
toLast savegame version that has the field. Defaults to the last version.
Returns
The constructed SaveLoad object.

Definition at line 951 of file saveload.h.

References address_func, MaxVersion, MinVersion, name, Reference, and ToAddressFunction().

Referenced by OrderBackup::GetOrderBackupDescription.

◆ ReferenceList()

template<SLRefType type, typename T>
SaveLoad SaveLoad::ReferenceList ( std::string name,
AddressFunction< T > address_func,
SaveLoadVersion from = SaveLoadVersion::MinVersion,
SaveLoadVersion to = SaveLoadVersion::MaxVersion )
inlinestatic

Storage of a list of references to pool elements in some savegame versions.

Template Parameters
typeThe type of reference.
TThe type of the field in memory. This is automatically deduced and used for validation.
Parameters
nameField name for table chunks.
address_funcFunction to get the address of the list.
fromFirst savegame version that has the field. Defaults to the first version.
toLast savegame version that has the field. Defaults to the last version.
Returns
The constructed SaveLoad object.

Definition at line 1078 of file saveload.h.

References address_func, IsValidRefType(), MaxVersion, MinVersion, name, ReferenceList, T, and ToAddressFunction().

Referenced by LinkGraphSchedule::GetLinkGraphScheduleDesc.

◆ ReferenceVector()

template<SLRefType type, typename T>
SaveLoad SaveLoad::ReferenceVector ( std::string name,
AddressFunction< T > address_func,
SaveLoadVersion from = SaveLoadVersion::MinVersion,
SaveLoadVersion to = SaveLoadVersion::MaxVersion )
inlinestatic

Storage of a vector of references to pool elements in some savegame versions.

Template Parameters
typeThe type of reference.
TThe type of the field in memory. This is automatically deduced and used for validation.
Parameters
nameField name for table chunks.
address_funcFunction to get the address of the vector.
fromFirst savegame version that has the field. Defaults to the first version.
toLast savegame version that has the field. Defaults to the last version.
Returns
The constructed SaveLoad object.

Definition at line 1149 of file saveload.h.

References address_func, IsValidRefType(), MaxVersion, MinVersion, name, ReferenceVector, T, and ToAddressFunction().

◆ SaveByte()

template<typename T>
SaveLoad SaveLoad::SaveByte ( std::string name,
AddressFunction< T > address_func,
SaveLoadVersion from = SaveLoadVersion::MinVersion,
SaveLoadVersion to = SaveLoadVersion::MaxVersion )
inlinestatic

Storage of a byte variable in some savegame versions that is not automatically read by the SaveLoad code.

Template Parameters
TThe type of the variable in memory. This is automatically deduced and used for validation.
Parameters
nameField name for table chunks.
address_funcFunction to get the address of the variable.
fromFirst savegame version that has the field. Defaults to the first version.
toLast savegame version that has the field. Defaults to the last version.
Returns
The constructed SaveLoad object.

Definition at line 1125 of file saveload.h.

References address_func, DetermineMemType(), MaxVersion, MinVersion, name, SaveByte, ToAddressFunction(), and U8.

◆ String()

template<typename T>
SaveLoad SaveLoad::String ( std::string name,
AddressFunction< T > address_func,
StringValidationSettings string_validation_settings = {},
SaveLoadVersion from = SaveLoadVersion::MinVersion,
SaveLoadVersion to = SaveLoadVersion::MaxVersion,
size_t extra = 0 )
inlinestatic

Storage of a string in some savegame versions.

Template Parameters
TThe type of the field in memory. This is automatically deduced and used for validation.
Parameters
nameField name for table chunks.
address_funcFunction to get the address of the string.
string_validation_settingsSettings to the string validation that is performed.
fromFirst savegame version that has the field. Defaults to the first version.
toLast savegame version that has the field. Defaults to the last version.
extraArbitrary extra data to pass to the address_func.
Returns
The constructed SaveLoad object.

Definition at line 977 of file saveload.h.

References address_func, and name.

Referenced by OrderBackup::GetOrderBackupDescription.

◆ Struct()

template<typename T>
requires std::is_base_of_v<SaveLoadHandler, T>
SaveLoad SaveLoad::Struct ( std::string name,
SaveLoadVersion from = SaveLoadVersion::MinVersion,
SaveLoadVersion to = SaveLoadVersion::MaxVersion )
inlinestatic

Storage of a structs, optionally in some savegame versions.

Template Parameters
TSaveLoadHandler for the structs.
Parameters
nameThe name of the field.
fromFirst savegame version that has the field. Defaults to the first version.
toLast savegame version that has the field. Defaults to the last version.
Returns
The constructed SaveLoad object.

Definition at line 1001 of file saveload.h.

References MaxVersion, MinVersion, name, and Struct.

Referenced by LinkGraph::GetLinkGraphJobDesc.

◆ StructList()

template<typename T>
requires std::is_base_of_v<SaveLoadHandler, T>
SaveLoad SaveLoad::StructList ( std::string name,
SaveLoadVersion from = SaveLoadVersion::MinVersion,
SaveLoadVersion to = SaveLoadVersion::MaxVersion )
inlinestatic

Storage of a list of structs, optionally in some savegame versions.

Template Parameters
TSaveLoadHandler for the list of structs.
Parameters
nameThe name of the field.
fromFirst savegame version that has the field. Defaults to the first version.
toLast savegame version that has the field. Defaults to the last version.
Returns
The constructed SaveLoad object.

Definition at line 1103 of file saveload.h.

References MaxVersion, MinVersion, name, and StructList.

Referenced by LinkGraph::GetLinkGraphDesc, OrderBackup::GetOrderBackupDescription, and OrderList::GetOrderListDescription.

◆ ToAddressFunction()

template<typename T>
constexpr AddressFunction SaveLoad::ToAddressFunction ( AddressFunction< T > address_func)
inlinestaticconstexpr

To be able to get the type information into the SaveLoad constructing functions, we need some infrastructure.

The easiest way to achieve this is not casting away the type information in the lambda that gets our address, however to store this we need to erase this type. This function practically erases this type.

Template Parameters
TReturn type of the given address function.
Parameters
address_funcThe function to convert.
Returns
The AddressFunction.

Definition at line 906 of file saveload.h.

References address_func, and ToAddressFunction().

Referenced by Array(), Reference(), ReferenceList(), ReferenceVector(), SaveByte(), ToAddressFunction(), Variable(), and Vector().

◆ Variable()

template<VarFileType file_type, typename T>
SaveLoad SaveLoad::Variable ( std::string name,
AddressFunction< T > address_func,
SaveLoadVersion from = SaveLoadVersion::MinVersion,
SaveLoadVersion to = SaveLoadVersion::MaxVersion,
size_t extra = 0 )
inlinestatic

Storage of a variable in some savegame versions.

Template Parameters
file_typeStorage type of the variable in the savegame.
TThe type of the variable in memory. This is automatically deduced and used for validation.
Parameters
nameField name for table chunks.
address_funcFunction to get the address of the variable.
fromFirst savegame version that has the field. Defaults to the first version.
toLast savegame version that has the field. Defaults to the last version.
extraArbitrary extra data to pass to the address_func.
Returns
The constructed SaveLoad object.

Definition at line 926 of file saveload.h.

References address_func, DetermineMemType(), MaxVersion, MinVersion, name, ToAddressFunction(), and Variable.

Referenced by CargoPacket::GetCargoPacketDesc, LinkGraph::GetLinkGraphDesc, LinkGraph::GetLinkGraphJobDesc, OrderBackup::GetOrderBackupDescription, Order::GetOrderDescription, and OrderList::GetOrderListDescription.

◆ Vector()

template<VarFileType file_type, typename T>
SaveLoad SaveLoad::Vector ( std::string name,
AddressFunction< T > address_func,
SaveLoadVersion from = SaveLoadVersion::MinVersion,
SaveLoadVersion to = SaveLoadVersion::MaxVersion )
inlinestatic

Storage of a vector in some savegame versions.

Template Parameters
file_typeStorage type of the vector's elements in the savegame.
TThe type of the field in memory. This is automatically deduced and used for validation.
Parameters
nameField name for table chunks.
address_funcFunction to get the address of the vector.
fromFirst savegame version that has the field. Defaults to the first version.
toLast savegame version that has the field. Defaults to the last version.
Returns
The constructed SaveLoad object.

Definition at line 1053 of file saveload.h.

References address_func, DetermineMemType(), MaxVersion, MinVersion, name, T, ToAddressFunction(), and Vector.

Field Documentation

◆ address_func

AddressFunction SaveLoad::address_func = nullptr

Callback function the get the actual variable address in memory.

Definition at line 782 of file saveload.h.

Referenced by Array(), GetVariableAddress(), Reference(), ReferenceList(), ReferenceVector(), SaveByte(), String(), ToAddressFunction(), Variable(), and Vector().

◆ cmd

SaveLoadType SaveLoad::cmd

The action to take with the saved/loaded type, All types need different action.

Definition at line 777 of file saveload.h.

Referenced by GetSavegameFileType(), GetSettingsDesc(), SlCompatTableHeader(), and SlTableHeader().

◆ conv

VarType SaveLoad::conv {}

Type of the variable to be saved; this field combines both FileVarType and MemVarType.

Definition at line 778 of file saveload.h.

Referenced by GetSavegameFileType(), and GetVariableAddress().

◆ extra_data

size_t SaveLoad::extra_data = 0

Extra data for the callback proc.

Definition at line 783 of file saveload.h.

Referenced by GetVariableAddress().

◆ handler

std::shared_ptr<SaveLoadHandler> SaveLoad::handler {}

Custom handler for Save/Load procs.

Definition at line 784 of file saveload.h.

Referenced by SlCompatTableHeader(), and SlTableHeader().

◆ length

uint16_t SaveLoad::length {}

(Conditional) length of the variable (eg. arrays) (max array size is 65536 elements).

Definition at line 779 of file saveload.h.

Referenced by Array(), and GetSettingsDesc().

◆ name

std::string SaveLoad::name

Name of this field (optional, used for tables).

Definition at line 776 of file saveload.h.

Referenced by Array(), SettingDesc::GetName(), Reference(), ReferenceList(), ReferenceVector(), SaveByte(), SlCompatTableHeader(), SlTableHeader(), String(), Struct(), StructList(), Variable(), and Vector().

◆ version_from

SaveLoadVersion SaveLoad::version_from

◆ version_to

SaveLoadVersion SaveLoad::version_to

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