1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-01 21:41:34 +00:00

Use compile time generated indices for tuples types

This commit is contained in:
ζeh Matt 2022-09-08 00:31:29 +03:00
parent 8f7703d5c8
commit c856095562
No known key found for this signature in database
GPG key ID: 18CE582C71A225B0
3 changed files with 18 additions and 12 deletions

View file

@ -316,13 +316,6 @@ void ESMStore::setIdType(const std::string& id, ESM::RecNameInts type)
mStoreImp->mIds[id] = type; mStoreImp->mIds[id] = type;
} }
static std::size_t sTypeIndexCounter = 0;
std::size_t ESMStore::geNextTypeIndex()
{
return sTypeIndexCounter++;
}
ESM::LuaScriptsCfg ESMStore::getLuaScriptsCfg() const ESM::LuaScriptsCfg ESMStore::getLuaScriptsCfg() const
{ {
ESM::LuaScriptsCfg cfg; ESM::LuaScriptsCfg cfg;

View file

@ -126,14 +126,11 @@ namespace MWWorld
// Special entry which is hardcoded and not loaded from an ESM // Special entry which is hardcoded and not loaded from an ESM
Store<ESM::Attribute >>; Store<ESM::Attribute >>;
static std::size_t geNextTypeIndex();
template<typename T> template<typename T>
static std::size_t getTypeIndex() static constexpr std::size_t getTypeIndex()
{ {
static_assert(Misc::TupleHasType<Store<T>, StoreTuple>::value); static_assert(Misc::TupleHasType<Store<T>, StoreTuple>::value);
static std::size_t sIndex = geNextTypeIndex(); return Misc::TupleTypeIndex<Store<T>, StoreTuple>::value;
return sIndex;
} }
std::unique_ptr<ESMStoreImp> mStoreImp; std::unique_ptr<ESMStoreImp> mStoreImp;

View file

@ -1,6 +1,7 @@
#ifndef OPENMW_COMPONENTS_MISC_META_H #ifndef OPENMW_COMPONENTS_MISC_META_H
#define OPENMW_COMPONENTS_MISC_META_H #define OPENMW_COMPONENTS_MISC_META_H
#include <cstddef>
#include <tuple> #include <tuple>
#include <type_traits> #include <type_traits>
@ -14,6 +15,21 @@ namespace Misc
{ {
static constexpr bool value = (std::is_same_v<T, Args> || ...); static constexpr bool value = (std::is_same_v<T, Args> || ...);
}; };
template <class T, class Tuple>
struct TupleTypeIndex;
template <class T, class... Types>
struct TupleTypeIndex<T, std::tuple<T, Types...>>
{
static constexpr std::size_t value = 0;
};
template <class T, class U, class... Types>
struct TupleTypeIndex<T, std::tuple<U, Types...>>
{
static constexpr std::size_t value = 1 + TupleTypeIndex<T, std::tuple<Types...>>::value;
};
} }
#endif #endif