Saveload: De-duplicate save load struct handler factory lambda

This commit is contained in:
Jonathan G Rennison 2024-08-03 11:43:05 +01:00
parent 3e1b0f76f4
commit aef51c08cf

View File

@ -190,6 +190,15 @@ inline constexpr NamedSaveLoad NSLT(const char *name, SaveLoad save_load)
return { name, save_load, NSLF_TABLE_ONLY };
}
template<typename T, auto... ARGS>
inline constexpr SaveLoadStructHandlerFactory MakeSaveLoadStructHandlerFactory()
{
SaveLoadStructHandlerFactory factory = []() -> std::unique_ptr<class SaveLoadStructHandler> {
return std::make_unique<T>(ARGS...);
};
return factory;
}
inline constexpr NamedSaveLoad NSLT_STRUCT(const char *name, SaveLoadStructHandlerFactory factory, SaveLoadVersion from = SL_MIN_VERSION, SaveLoadVersion to = SL_MAX_VERSION, SlXvFeatureTest extver = {})
{
return { name, SaveLoad { true, SL_STRUCT, SLE_FILE_STRUCT, 0, from, to, SLTAG_DEFAULT, { .struct_handler_factory = factory }, extver }, NSLF_TABLE_ONLY };
@ -198,10 +207,7 @@ inline constexpr NamedSaveLoad NSLT_STRUCT(const char *name, SaveLoadStructHandl
template <typename T>
inline constexpr NamedSaveLoad NSLT_STRUCT(const char *name, SaveLoadVersion from = SL_MIN_VERSION, SaveLoadVersion to = SL_MAX_VERSION, SlXvFeatureTest extver = {})
{
SaveLoadStructHandlerFactory factory = []() -> std::unique_ptr<class SaveLoadStructHandler> {
return std::make_unique<T>();
};
return NSLT_STRUCT(name, factory, from, to, extver);
return NSLT_STRUCT(name, MakeSaveLoadStructHandlerFactory<T>(), from, to, extver);
}
inline constexpr NamedSaveLoad NSLT_STRUCTLIST(const char *name, SaveLoadStructHandlerFactory factory, SaveLoadVersion from = SL_MIN_VERSION, SaveLoadVersion to = SL_MAX_VERSION, SlXvFeatureTest extver = {})
@ -212,10 +218,7 @@ inline constexpr NamedSaveLoad NSLT_STRUCTLIST(const char *name, SaveLoadStructH
template <typename T>
inline constexpr NamedSaveLoad NSLT_STRUCTLIST(const char *name, SaveLoadVersion from = SL_MIN_VERSION, SaveLoadVersion to = SL_MAX_VERSION, SlXvFeatureTest extver = {})
{
SaveLoadStructHandlerFactory factory = []() -> std::unique_ptr<class SaveLoadStructHandler> {
return std::make_unique<T>();
};
return NSLT_STRUCTLIST(name, factory, from, to, extver);
return NSLT_STRUCTLIST(name, MakeSaveLoadStructHandlerFactory<T>(), from, to, extver);
}
inline constexpr NamedSaveLoad NSLTAG(uint16_t label_tag, NamedSaveLoad nsl)