diff --git a/Code/Framework/AzCore/AzCore/DOM/DomValue.cpp b/Code/Framework/AzCore/AzCore/DOM/DomValue.cpp index 3dafc02c6e..3836568f37 100644 --- a/Code/Framework/AzCore/AzCore/DOM/DomValue.cpp +++ b/Code/Framework/AzCore/AzCore/DOM/DomValue.cpp @@ -26,6 +26,51 @@ namespace AZ::Dom return refCountedPointer; } + namespace Internal + { + template + constexpr size_t GetTypeIndexInternal(size_t index = 0) + { + static_assert(false, "Type not found in ValueType"); + return index; + } + + template + constexpr size_t GetTypeIndexInternal(size_t index = 0) + { + if constexpr (AZStd::is_same_v) + { + return index; + } + else + { + return GetTypeIndexInternal(index + 1); + } + } + + template + struct ExtractTypeArgs + { + }; + + template class TypeToExtract, typename... Args> + struct ExtractTypeArgs> + { + template + static constexpr size_t GetTypeIndex() + { + return GetTypeIndexInternal(); + } + }; + } // namespace Internal + + // Helper function, looks up the index of a type within Value::m_value's storage + template + constexpr size_t GetTypeIndex() + { + return Internal::ExtractTypeArgs::GetTypeIndex(); + } + Node::Node(AZ::Name name) : m_name(name) { @@ -227,27 +272,27 @@ namespace AZ::Dom { switch (m_value.index()) { - case 0: // AZStd::monostate + case GetTypeIndex(): return Type::Null; - case 1: // int64_t + case GetTypeIndex(): return Type::Int64; - case 2: // uint64_t + case GetTypeIndex(): return Type::Uint64; - case 3: // double + case GetTypeIndex(): return Type::Double; - case 4: // bool + case GetTypeIndex(): return Type::Bool; - case 5: // AZStd::string_view - case 6: // AZStd::shared_ptr - case 7: // ShortStringType + case GetTypeIndex(): + case GetTypeIndex(): + case GetTypeIndex(): return Type::String; - case 8: // ObjectPtr + case GetTypeIndex(): return Type::Object; - case 9: // ArrayPtr + case GetTypeIndex(): return Type::Array; - case 10: // NodePtr + case GetTypeIndex(): return Type::Node; - case 11: // AZStd::any* + case GetTypeIndex>(): return Type::Opaque; } AZ_Assert(false, "AZ::Dom::Value::GetType: m_value has an unexpected type"); @@ -813,11 +858,11 @@ namespace AZ::Dom { switch (m_value.index()) { - case 1: // int64_t + case GetTypeIndex(): return AZStd::get(m_value); - case 2: // uint64_t + case GetTypeIndex(): return aznumeric_cast(AZStd::get(m_value)); - case 3: // double + case GetTypeIndex(): return aznumeric_cast(AZStd::get(m_value)); } AZ_Assert(false, "AZ::Dom::Value: Called GetInt on a non-numeric type"); @@ -843,11 +888,11 @@ namespace AZ::Dom { switch (m_value.index()) { - case 1: // int64_t + case GetTypeIndex(): return aznumeric_cast(AZStd::get(m_value)); - case 2: // uint64_t + case GetTypeIndex(): return AZStd::get(m_value); - case 3: // double + case GetTypeIndex(): return aznumeric_cast(AZStd::get(m_value)); } AZ_Assert(false, "AZ::Dom::Value: Called GetInt on a non-numeric type"); @@ -888,11 +933,11 @@ namespace AZ::Dom { switch (m_value.index()) { - case 1: // int64_t + case GetTypeIndex(): return aznumeric_cast(AZStd::get(m_value)); - case 2: // uint64_t + case GetTypeIndex(): return aznumeric_cast(AZStd::get(m_value)); - case 3: // double + case GetTypeIndex(): return AZStd::get(m_value); } AZ_Assert(false, "AZ::Dom::Value: Called GetInt on a non-numeric type"); @@ -923,14 +968,14 @@ namespace AZ::Dom { switch (m_value.index()) { - case 5: // AZStd::string_view + case GetTypeIndex(): return AZStd::get(m_value); - case 6: // AZStd::shared_ptr> + case GetTypeIndex(): { auto& buffer = *AZStd::get(m_value); return { buffer.data(), buffer.size() }; } - case 7: // ShortStringType + case GetTypeIndex(): { const ShortStringType& shortString = AZStd::get(m_value); return { shortString.data(), shortString.size() }; @@ -965,8 +1010,7 @@ namespace AZ::Dom } else { - SharedStringType sharedString = - AZStd::allocate_shared(StdValueAllocator(), value.begin(), value.end()); + SharedStringType sharedString = AZStd::allocate_shared(StdValueAllocator(), value.begin(), value.end()); m_value = AZStd::move(sharedString); } } diff --git a/Code/Framework/AzCore/AzCore/DOM/DomValue.h b/Code/Framework/AzCore/AzCore/DOM/DomValue.h index bc1b790009..86e98030e2 100644 --- a/Code/Framework/AzCore/AzCore/DOM/DomValue.h +++ b/Code/Framework/AzCore/AzCore/DOM/DomValue.h @@ -159,6 +159,33 @@ namespace AZ::Dom using SharedStringContainer = AZStd::vector; using SharedStringType = AZStd::shared_ptr; + //! The internal storage type for Value. + //! These types do not correspond one-to-one with the Value's external Type as there may be multiple storage classes + //! for the same type in some instances, such as string storage. + using ValueType = AZStd::variant< + // Null + AZStd::monostate, + // Int64 + int64_t, + // Uint64 + uint64_t, + // Double + double, + // Bool + bool, + // StringType + AZStd::string_view, + SharedStringType, + ShortStringType, + // Object + ObjectPtr, + // Array + ArrayPtr, + // Node + NodePtr, + // Opaque + AZStd::shared_ptr>; + // Constructors... Value(); Value(const Value&); @@ -347,33 +374,6 @@ namespace AZ::Dom explicit Value(const AZStd::any& opaqueValue); - //! The internal storage type for Value. - //! These types do not correspond one-to-one with the Value's external Type as there may be multiple storage classes - //! for the same type in some instances, such as string storage. - using ValueType = AZStd::variant< - // Null - AZStd::monostate, - // Int64 - int64_t, - // Uint64 - uint64_t, - // Double - double, - // Bool - bool, - // StringType - AZStd::string_view, - SharedStringType, - ShortStringType, - // Object - ObjectPtr, - // Array - ArrayPtr, - // Node - NodePtr, - // Opaque - AZStd::shared_ptr>; - static_assert( sizeof(ValueType) == sizeof(ShortStringType) + sizeof(size_t), "ValueType should have no members larger than ShortStringType");