summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-06-14 20:03:35 +0200
committerPatric Stout <github@truebrain.nl>2021-06-15 19:36:15 +0200
commitb9ab9e4d051eea7d6aedfb60930b039b778568af (patch)
tree9daf0ac327a839997e3da58ea6583fb8aff2c26c
parent97b94bdc9ab4ceeb589c5022d5c238442faf0454 (diff)
downloadopenttd-b9ab9e4d051eea7d6aedfb60930b039b778568af.tar.xz
Codechange: add the ability to save/load a std::vector
std::vector<bool> is not possible, as .. that is a nice special case in C++. This new type will be used in next commit.
-rw-r--r--src/saveload/saveload.cpp46
-rw-r--r--src/saveload/saveload.h36
2 files changed, 74 insertions, 8 deletions
diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp
index 04002fd3f..9b177dd0d 100644
--- a/src/saveload/saveload.cpp
+++ b/src/saveload/saveload.cpp
@@ -44,6 +44,7 @@
#include "../error.h"
#include <atomic>
#include <deque>
+#include <vector>
#include <string>
#ifdef __EMSCRIPTEN__
# include <emscripten.h>
@@ -1452,6 +1453,48 @@ static void SlDeque(void *deque, VarType conv)
}
}
+/**
+ * Return the size in bytes of a std::vector.
+ * @param vector The std::vector to find the size of
+ * @param conv VarType type of variable that is used for calculating the size
+ */
+static inline size_t SlCalcVectorLen(const void *vector, VarType conv)
+{
+ switch (GetVarMemType(conv)) {
+ case SLE_VAR_BL: NOT_REACHED(); // Not supported
+ case SLE_VAR_I8: return SlStorageHelper<std::vector, int8>::SlCalcLen(vector, conv);
+ case SLE_VAR_U8: return SlStorageHelper<std::vector, uint8>::SlCalcLen(vector, conv);
+ case SLE_VAR_I16: return SlStorageHelper<std::vector, int16>::SlCalcLen(vector, conv);
+ case SLE_VAR_U16: return SlStorageHelper<std::vector, uint16>::SlCalcLen(vector, conv);
+ case SLE_VAR_I32: return SlStorageHelper<std::vector, int32>::SlCalcLen(vector, conv);
+ case SLE_VAR_U32: return SlStorageHelper<std::vector, uint32>::SlCalcLen(vector, conv);
+ case SLE_VAR_I64: return SlStorageHelper<std::vector, int64>::SlCalcLen(vector, conv);
+ case SLE_VAR_U64: return SlStorageHelper<std::vector, uint64>::SlCalcLen(vector, conv);
+ default: NOT_REACHED();
+ }
+}
+
+/**
+ * Save/load a std::vector.
+ * @param vector The std::vector being manipulated
+ * @param conv VarType type of variable that is used for calculating the size
+ */
+static void SlVector(void *vector, VarType conv)
+{
+ switch (GetVarMemType(conv)) {
+ case SLE_VAR_BL: NOT_REACHED(); // Not supported
+ case SLE_VAR_I8: SlStorageHelper<std::vector, int8>::SlSaveLoad(vector, conv); break;
+ case SLE_VAR_U8: SlStorageHelper<std::vector, uint8>::SlSaveLoad(vector, conv); break;
+ case SLE_VAR_I16: SlStorageHelper<std::vector, int16>::SlSaveLoad(vector, conv); break;
+ case SLE_VAR_U16: SlStorageHelper<std::vector, uint16>::SlSaveLoad(vector, conv); break;
+ case SLE_VAR_I32: SlStorageHelper<std::vector, int32>::SlSaveLoad(vector, conv); break;
+ case SLE_VAR_U32: SlStorageHelper<std::vector, uint32>::SlSaveLoad(vector, conv); break;
+ case SLE_VAR_I64: SlStorageHelper<std::vector, int64>::SlSaveLoad(vector, conv); break;
+ case SLE_VAR_U64: SlStorageHelper<std::vector, uint64>::SlSaveLoad(vector, conv); break;
+ default: NOT_REACHED();
+ }
+}
+
/** Are we going to save this object or not? */
static inline bool SlIsObjectValidInSavegame(const SaveLoad &sld)
{
@@ -1488,6 +1531,7 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld)
case SL_STR: return SlCalcStringLen(GetVariableAddress(object, sld), sld.length, sld.conv);
case SL_REFLIST: return SlCalcRefListLen(GetVariableAddress(object, sld), sld.conv);
case SL_DEQUE: return SlCalcDequeLen(GetVariableAddress(object, sld), sld.conv);
+ case SL_VECTOR: return SlCalcVectorLen(GetVariableAddress(object, sld), sld.conv);
case SL_STDSTR: return SlCalcStdStringLen(GetVariableAddress(object, sld));
case SL_SAVEBYTE: return 1; // a byte is logically of size 1
case SL_NULL: return SlCalcConvFileLen(sld.conv) * sld.length;
@@ -1583,6 +1627,7 @@ static bool SlObjectMember(void *object, const SaveLoad &sld)
case SL_STR:
case SL_REFLIST:
case SL_DEQUE:
+ case SL_VECTOR:
case SL_STDSTR: {
void *ptr = GetVariableAddress(object, sld);
@@ -1593,6 +1638,7 @@ static bool SlObjectMember(void *object, const SaveLoad &sld)
case SL_STR: SlString(ptr, sld.length, sld.conv); break;
case SL_REFLIST: SlRefList(ptr, conv); break;
case SL_DEQUE: SlDeque(ptr, conv); break;
+ case SL_VECTOR: SlVector(ptr, conv); break;
case SL_STDSTR: SlStdString(ptr, sld.conv); break;
default: NOT_REACHED();
}
diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h
index 7ed76ff2d..559876b97 100644
--- a/src/saveload/saveload.h
+++ b/src/saveload/saveload.h
@@ -565,15 +565,19 @@ typedef uint32 VarType;
enum SaveLoadType : byte {
SL_VAR = 0, ///< Save/load a variable.
SL_REF = 1, ///< Save/load a reference.
- SL_ARR = 2, ///< Save/load a fixed-size array of #SL_VAR elements.
+ SL_STRUCT = 2, ///< Save/load a struct.
+
SL_STR = 3, ///< Save/load a string.
- SL_REFLIST = 4, ///< Save/load a list of #SL_REF elements.
- SL_DEQUE = 5, ///< Save/load a deque of #SL_VAR elements.
- SL_STDSTR = 6, ///< Save/load a \c std::string.
- SL_STRUCT = 7, ///< Save/load a struct.
- SL_STRUCTLIST = 8, ///< Save/load a list of structs.
- SL_SAVEBYTE = 9, ///< Save (but not load) a byte.
- SL_NULL = 10, ///< Save null-bytes and load to nowhere.
+ SL_STDSTR = 4, ///< Save/load a \c std::string.
+
+ SL_ARR = 5, ///< Save/load a fixed-size array of #SL_VAR elements.
+ SL_DEQUE = 6, ///< Save/load a deque of #SL_VAR elements.
+ SL_VECTOR = 7, ///< Save/load a vector of #SL_VAR elements.
+ SL_REFLIST = 8, ///< Save/load a list of #SL_REF elements.
+ SL_STRUCTLIST = 9, ///< Save/load a list of structs.
+
+ SL_SAVEBYTE = 10, ///< Save (but not load) a byte.
+ SL_NULL = 11, ///< Save null-bytes and load to nowhere.
};
typedef void *SaveLoadAddrProc(void *base, size_t extra);
@@ -829,6 +833,15 @@ struct SaveLoad {
#define SLEG_CONDREFLIST(variable, type, from, to) SLEG_GENERAL(SL_REFLIST, variable, type, 0, from, to, 0)
/**
+ * Storage of a global vector of #SL_VAR elements in some savegame versions.
+ * @param variable Name of the global variable.
+ * @param type Storage of the data in memory and in the savegame.
+ * @param from First savegame version that has the list.
+ * @param to Last savegame version that has the list.
+ */
+#define SLEG_CONDVECTOR(variable, type, from, to) SLEG_GENERAL(SL_VECTOR, variable, type, 0, from, to, 0)
+
+/**
* Storage of a list of structs in some savegame versions.
* @param handler SaveLoadHandler for the list of structs.
* @param from First savegame version that has the list.
@@ -885,6 +898,13 @@ struct SaveLoad {
#define SLEG_REFLIST(variable, type) SLEG_CONDREFLIST(variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
/**
+ * Storage of a global vector of #SL_VAR elements in every savegame version.
+ * @param variable Name of the global variable.
+ * @param type Storage of the data in memory and in the savegame.
+ */
+#define SLEG_VECTOR(variable, type) SLEG_CONDVECTOR(variable, type, SL_MIN_VERSION, SL_MAX_VERSION)
+
+/**
* Storage of a list of structs in every savegame version.
* @param handler SaveLoadHandler for the list of structs.
*/