From 08843ea6c9efed443cecfae525cdf15a023b7e2e Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 11 Jul 2026 13:04:42 +0200 Subject: [PATCH] Use concepts to check ESM record types --- apps/esmtool/tes4.cpp | 28 ++--- apps/openmw/mwworld/esmstore.cpp | 2 +- apps/openmw/mwworld/esmstore.hpp | 8 +- apps/openmw_tests/mwworld/teststore.cpp | 36 ++++--- components/esm/typetraits.hpp | 26 +---- components/esm3/typetraits.hpp | 26 +---- components/esm4/typetraits.hpp | 136 +++++------------------- components/esmloader/load.cpp | 22 ++-- 8 files changed, 81 insertions(+), 203 deletions(-) diff --git a/apps/esmtool/tes4.cpp b/apps/esmtool/tes4.cpp index afaa4e914c..b48b1ea09f 100644 --- a/apps/esmtool/tes4.cpp +++ b/apps/esmtool/tes4.cpp @@ -146,33 +146,33 @@ namespace EsmTool return; std::cout << "\n Record: " << ESM::NAME(reader.hdr().record.typeId).toStringView(); - if constexpr (ESM::hasId) + if constexpr (ESM::HasId) std::cout << "\n Id: " << value.mId; - if constexpr (ESM4::hasFlags) + if constexpr (ESM4::HasFlags) std::cout << "\n Record flags: " << recordFlags(value.mFlags); - if constexpr (ESM4::hasParent) + if constexpr (ESM4::HasParent) std::cout << "\n Parent: " << value.mParent; - if constexpr (ESM4::hasEditorId) + if constexpr (ESM4::HasEditorId) std::cout << "\n EditorId: " << value.mEditorId; - if constexpr (ESM4::hasFullName) + if constexpr (ESM4::HasFullName) std::cout << "\n FullName: " << value.mFullName; - if constexpr (ESM4::hasCellFlags) + if constexpr (ESM4::HasCellFlags) std::cout << "\n CellFlags: " << WriteCellFlags{ value.mCellFlags }; - if constexpr (ESM4::hasX) + if constexpr (ESM4::HasX) std::cout << "\n X: " << value.mX; - if constexpr (ESM4::hasY) + if constexpr (ESM4::HasY) std::cout << "\n Y: " << value.mY; - if constexpr (ESM::hasModel) + if constexpr (ESM::HasModel) std::cout << "\n Model: " << value.mModel.getOriginal(); - if constexpr (ESM4::hasNif) + if constexpr (ESM4::HasNif) std::cout << "\n Nif:" << WriteArray("\n - ", value.mNif); - if constexpr (ESM4::hasKf) + if constexpr (ESM4::HasKf) std::cout << "\n Kf:" << WriteArray("\n - ", value.mKf); - if constexpr (ESM4::hasType) + if constexpr (ESM4::HasType) std::cout << "\n Type: " << value.mType; - if constexpr (ESM4::hasValue) + if constexpr (ESM4::HasValue) std::cout << "\n Value: " << value.mValue; - if constexpr (ESM4::hasData) + if constexpr (ESM4::HasData) std::cout << "\n Data: " << WriteData(value.mData); std::cout << '\n'; } diff --git a/apps/openmw/mwworld/esmstore.cpp b/apps/openmw/mwworld/esmstore.cpp index 2e2e4cec07..9b4350f863 100644 --- a/apps/openmw/mwworld/esmstore.cpp +++ b/apps/openmw/mwworld/esmstore.cpp @@ -268,7 +268,7 @@ namespace MWWorld auto recordType = static_cast(reader.hdr().record.typeId); ESM::RecNameInts esm4RecName = static_cast(ESM::esm4Recname(recordType)); - if constexpr (HasRecordId::value) + if constexpr (HasRecordId) { if constexpr (ESM::isESM4Rec(T::sRecordId)) { diff --git a/apps/openmw/mwworld/esmstore.hpp b/apps/openmw/mwworld/esmstore.hpp index 6e3dccfd87..4b1f2c2fc3 100644 --- a/apps/openmw/mwworld/esmstore.hpp +++ b/apps/openmw/mwworld/esmstore.hpp @@ -309,14 +309,10 @@ namespace MWWorld template <> const ESM::NPC* ESMStore::insert(const ESM::NPC& npc); - template > - struct HasRecordId : std::false_type - { - }; - template - struct HasRecordId> : std::true_type + concept HasRecordId = requires { + T::sRecordId; }; } diff --git a/apps/openmw_tests/mwworld/teststore.cpp b/apps/openmw_tests/mwworld/teststore.cpp index 35bafb49b6..20b0ebe525 100644 --- a/apps/openmw_tests/mwworld/teststore.cpp +++ b/apps/openmw_tests/mwworld/teststore.cpp @@ -346,7 +346,7 @@ TYPED_TEST_P(StoreTest, delete_test) template static unsigned int hasSameRecordId(const MWWorld::Store& store, ESM::RecNameInts recName) { - if constexpr (MWWorld::HasRecordId::value) + if constexpr (MWWorld::HasRecordId) { return T::sRecordId == recName ? 1 : 0; } @@ -359,7 +359,7 @@ static unsigned int hasSameRecordId(const MWWorld::Store& store, ESM::RecName template static void testRecNameIntCount(const MWWorld::Store& store, const MWWorld::ESMStore::StoreTuple& stores) { - if constexpr (MWWorld::HasRecordId::value) + if constexpr (MWWorld::HasRecordId) { const unsigned int recordIdCount = std::apply([](auto&&... x) { return (hasSameRecordId(x, T::sRecordId) + ...); }, stores); @@ -441,7 +441,7 @@ namespace const int index = 3; const std::string stringId = "foobar"; decltype(RecordType::mId) refId; - if constexpr (ESM::hasIndex && !std::is_same_v) + if constexpr (ESM::HasIndex && !std::is_same_v) refId = RecordType::indexToRefId(index); else if constexpr (std::is_same_v) { @@ -467,10 +467,10 @@ namespace record.mId = refId; - if constexpr (ESM::hasStringId) + if constexpr (ESM::HasStringId) record.mStringId = stringId; - if constexpr (ESM::hasIndex) + if constexpr (ESM::HasIndex) record.mIndex = index; if constexpr (std::is_same_v) @@ -497,7 +497,7 @@ namespace ASSERT_NE(texture, nullptr); return; } - else if constexpr (ESM::hasIndex) + else if constexpr (ESM::HasIndex) result = esmStore.get().search(index); else result = esmStore.get().search(refId); @@ -507,16 +507,26 @@ namespace } } - static_assert(ESM::hasStringId); + static_assert(ESM::HasStringId); - template > - struct HasSaveFunction : std::false_type + template + concept CanSave = requires(T& record, ESM::ESMWriter& writer) + { + record.save(writer, bool()); + }; + + template + struct HasSaveFunction : std::bool_constant> { }; template - struct HasSaveFunction().save(std::declval(), bool()))>> - : std::true_type + struct HasId : std::bool_constant> + { + }; + + template + struct HasModel : std::bool_constant> { }; @@ -573,9 +583,9 @@ namespace }; using RecordTypes = typename ToRecordTypes::Type; - using RecordTypesWithId = typename FilterTypes::Type; + using RecordTypesWithId = typename FilterTypes::Type; using RecordTypesWithSave = typename FilterTypes::Type; - using RecordTypesWithModel = typename FilterTypes::Type; + using RecordTypesWithModel = typename FilterTypes::Type; REGISTER_TYPED_TEST_SUITE_P(StoreSaveLoadTest, shouldNotChangeRefId); diff --git a/components/esm/typetraits.hpp b/components/esm/typetraits.hpp index 89f9e48695..a92f98cbee 100644 --- a/components/esm/typetraits.hpp +++ b/components/esm/typetraits.hpp @@ -1,35 +1,19 @@ #ifndef OPENMW_COMPONENTS_ESM_TYPETRAITS #define OPENMW_COMPONENTS_ESM_TYPETRAITS -#include - namespace ESM { - template > - struct HasId : std::false_type + template + concept HasId = requires { + T::mId; }; template - struct HasId> : std::true_type + concept HasModel = requires { + T::mModel; }; - - template - inline constexpr bool hasId = HasId::value; - - template > - struct HasModel : std::false_type - { - }; - - template - struct HasModel> : std::true_type - { - }; - - template - inline constexpr bool hasModel = HasModel::value; } #endif // OPENMW_COMPONENTS_ESM_TYPETRAITS diff --git a/components/esm3/typetraits.hpp b/components/esm3/typetraits.hpp index f5447f9508..f50b5efcc3 100644 --- a/components/esm3/typetraits.hpp +++ b/components/esm3/typetraits.hpp @@ -1,35 +1,19 @@ #ifndef OPENMW_COMPONENTS_ESM3_TYPETRAITS #define OPENMW_COMPONENTS_ESM3_TYPETRAITS -#include - namespace ESM { - template > - struct HasIndex : std::false_type + template + concept HasIndex = requires { + T::mIndex; }; template - struct HasIndex> : std::true_type + concept HasStringId = requires { + T::mStringId; }; - - template - inline constexpr bool hasIndex = HasIndex::value; - - template > - struct HasStringId : std::false_type - { - }; - - template - struct HasStringId> : std::true_type - { - }; - - template - inline constexpr bool hasStringId = HasStringId::value; } #endif // OPENMW_COMPONENTS_ESM3_TYPETRAITS diff --git a/components/esm4/typetraits.hpp b/components/esm4/typetraits.hpp index b29713dbba..6c70a15044 100644 --- a/components/esm4/typetraits.hpp +++ b/components/esm4/typetraits.hpp @@ -1,165 +1,79 @@ #ifndef OPENMW_COMPONENTS_ESM4_TYPETRAITS #define OPENMW_COMPONENTS_ESM4_TYPETRAITS -#include - namespace ESM4 { - template > - struct HasParent : std::false_type + template + concept HasParent = requires { + T::mParent; }; template - struct HasParent> : std::true_type + concept HasFlags = requires { + T::mFlags; }; template - inline constexpr bool hasParent = HasParent::value; - - template > - struct HasFlags : std::false_type + concept HasEditorId = requires { + T::mEditorId; }; template - struct HasFlags> : std::true_type + concept HasFullName = requires { + T::mFullName; }; template - inline constexpr bool hasFlags = HasFlags::value; - - template > - struct HasEditorId : std::false_type + concept HasCellFlags = requires { + T::mCellFlags; }; template - struct HasEditorId> : std::true_type + concept HasX = requires { + T::mX; }; template - inline constexpr bool hasEditorId = HasEditorId::value; - - template > - struct HasFullName : std::false_type + concept HasY = requires { + T::mY; }; template - struct HasFullName> : std::true_type + concept HasNif = requires { + T::mNif; }; template - inline constexpr bool hasFullName = HasFullName::value; - - template > - struct HasCellFlags : std::false_type + concept HasKf = requires { + T::mKf; }; template - struct HasCellFlags> : std::true_type + concept HasType = requires { + T::mType; }; template - inline constexpr bool hasCellFlags = HasCellFlags::value; - - template > - struct HasX : std::false_type + concept HasValue = requires { + T::mValue; }; template - struct HasX> : std::true_type + concept HasData = requires { + T::mData; }; - - template - inline constexpr bool hasX = HasX::value; - - template > - struct HasY : std::false_type - { - }; - - template - struct HasY> : std::true_type - { - }; - - template - inline constexpr bool hasY = HasY::value; - - template > - struct HasNif : std::false_type - { - }; - - template - struct HasNif> : std::true_type - { - }; - - template - inline constexpr bool hasNif = HasNif::value; - - template > - struct HasKf : std::false_type - { - }; - - template - struct HasKf> : std::true_type - { - }; - - template - inline constexpr bool hasKf = HasKf::value; - - template > - struct HasType : std::false_type - { - }; - - template - struct HasType> : std::true_type - { - }; - - template - inline constexpr bool hasType = HasType::value; - - template > - struct HasValue : std::false_type - { - }; - - template - struct HasValue> : std::true_type - { - }; - - template - inline constexpr bool hasValue = HasValue::value; - - template > - struct HasData : std::false_type - { - }; - - template - struct HasData> : std::true_type - { - }; - - template - inline constexpr bool hasData = HasData::value; } #endif // OPENMW_COMPONENTS_ESM4_TYPETRAITS diff --git a/components/esmloader/load.cpp b/components/esmloader/load.cpp index 2c13f9acf1..634fb08bd1 100644 --- a/components/esmloader/load.cpp +++ b/components/esmloader/load.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -30,7 +31,6 @@ #include #include #include -#include #include #include @@ -64,21 +64,11 @@ namespace EsmLoader std::map, std::size_t> mByPosition; }; - template > - struct HasId : std::false_type - { - }; - template - struct HasId> : std::true_type - { - }; + concept NotHasId = !ESM::HasId; - template - constexpr bool hasId = HasId::value; - - template - auto loadRecord(ESM::ESMReader& reader, Records& records) -> std::enable_if_t> + template + void loadRecord(ESM::ESMReader& reader, Records& records) { T record; bool deleted = false; @@ -88,8 +78,8 @@ namespace EsmLoader records.emplace_back(deleted, std::move(record)); } - template - auto loadRecord(ESM::ESMReader& reader, Records& records) -> std::enable_if_t> + template + void loadRecord(ESM::ESMReader& reader, Records& records) { T record; bool deleted = false;