1
0
Fork 0

Codechange: support strongly typed pool element IDs in squirrel/saveload

pull/13511/head
Rubidium 2025-01-28 22:52:59 +01:00 committed by rubidium42
parent 30127dfe90
commit 07dd5bf01d
3 changed files with 13 additions and 2 deletions

View File

@ -1318,6 +1318,7 @@ int64_t ReadValue(const void *ptr, VarType conv);
void WriteValue(void *ptr, VarType conv, int64_t val);
void SlSetArrayIndex(uint index);
static void SlSetArrayIndex(const ConvertibleThroughBase auto &index) { SlSetArrayIndex(index.base()); }
int SlIterateArray();
void SlSetStructListLength(size_t length);

View File

@ -43,13 +43,19 @@ private:
int modifications; ///< Number of modification that has been done. To prevent changing data while valuating.
protected:
/* Temporary helper functions to get the raw index from either strongly and non-strongly typed pool items. */
template <typename T>
static auto GetRawIndex(const T &index) { return index; }
template <ConvertibleThroughBase T>
static auto GetRawIndex(const T &index) { return index.base(); }
template <typename T, class ItemValid, class ItemFilter>
static void FillList(ScriptList *list, ItemValid item_valid, ItemFilter item_filter)
{
for (const T *item : T::Iterate()) {
if (!item_valid(item)) continue;
if (!item_filter(item)) continue;
list->AddItem(item->index);
list->AddItem(GetRawIndex(item->index));
}
}
@ -99,7 +105,7 @@ protected:
/* Push the root table as instance object, this is what squirrel does for meta-functions. */
sq_pushroottable(vm);
/* Push all arguments for the valuator function. */
sq_pushinteger(vm, item->index);
sq_pushinteger(vm, GetRawIndex(item->index));
for (int i = 0; i < nparam - 1; i++) {
sq_push(vm, i + 3);
}

View File

@ -11,6 +11,7 @@
#define SQUIRREL_HPP
#include <squirrel.h>
#include "../core/convertible_through_base.hpp"
/** The type of script we're working with, i.e. for who is it? */
enum class ScriptType : uint8_t {
@ -110,6 +111,8 @@ public:
*/
void AddConst(const char *var_name, uint value) { this->AddConst(var_name, (int)value); }
void AddConst(const char *var_name, const ConvertibleThroughBase auto &value) { this->AddConst(var_name, static_cast<int>(value.base())); }
/**
* Adds a const to the stack. Depending on the current state this means
* either a const to a class or to the global space.
@ -152,6 +155,7 @@ public:
void InsertResult(bool result);
void InsertResult(int result);
void InsertResult(uint result) { this->InsertResult((int)result); }
void InsertResult(ConvertibleThroughBase auto result) { this->InsertResult(static_cast<int>(result.base())); }
/**
* Call a method of an instance, in various flavors.