From ac4fd02a6ccdaac584927991ec122fdad46686c1 Mon Sep 17 00:00:00 2001 From: jadebenn Date: Fri, 22 Nov 2024 16:33:04 -0800 Subject: [PATCH] use decltype --- dCommon/Amf3.h | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/dCommon/Amf3.h b/dCommon/Amf3.h index b4e25058..ad41600f 100644 --- a/dCommon/Amf3.h +++ b/dCommon/Amf3.h @@ -82,8 +82,7 @@ using AMFStringValue = AMFValue; using AMFDoubleValue = AMFValue; // Template deduction guide to ensure string literals deduce -template -AMFValue(const char (&)[N]) -> AMFValue; // AMFStringValue +AMFValue(const char*) -> AMFValue; // AMFStringValue /** * The AMFArrayValue object holds 2 types of lists: @@ -126,17 +125,20 @@ class AMFArrayValue : public AMFBaseValue { * @return The inserted element if the type matched, * or nullptr if a key existed and was not the same type */ - template - [[maybe_unused]] std::pair*, bool> Insert(const std::string_view key, const ValueType value) { + template + [[maybe_unused]] auto Insert(const std::string_view key, const T value) -> std::pair { + // This ensures the deduced type matches the AMFValue constructor + using AMFValueType = decltype(AMFValue(value)); + const auto element = m_Associative.find(key); - AMFValue* val = nullptr; + AMFValueType* val = nullptr; bool found = true; if (element == m_Associative.cend()) { - auto newVal = std::make_unique>(value); + auto newVal = std::make_unique(value); val = newVal.get(); m_Associative.emplace(key, std::move(newVal)); } else { - val = dynamic_cast*>(element->second.get()); + val = dynamic_cast(element->second.get()); found = false; } return std::make_pair(val, found); @@ -179,15 +181,18 @@ class AMFArrayValue : public AMFBaseValue { * @return The inserted element, or nullptr if the type did not match * what was at the index. */ - template - [[maybe_unused]] std::pair*, bool> Insert(const size_t index, const ValueType value) { + template + [[maybe_unused]] auto Insert(const size_t index, const T value) -> std::pair { + // This ensures the deduced type matches the AMFValue constructor + using AMFValueType = decltype(AMFValue(value)); + bool inserted = false; if (index >= m_Dense.size()) { m_Dense.resize(index + 1); - m_Dense.at(index) = std::make_unique>(value); + m_Dense.at(index) = std::make_unique(value); inserted = true; } - return std::make_pair(dynamic_cast*>(m_Dense.at(index).get()), inserted); + return std::make_pair(dynamic_cast(m_Dense.at(index).get()), inserted); } /** @@ -234,8 +239,8 @@ class AMFArrayValue : public AMFBaseValue { * * @return The inserted pointer, or nullptr should the key already be in use. */ - template - [[maybe_unused]] inline AMFValue* Push(const ValueType value) { + template + [[maybe_unused]] inline auto Push(const T value) -> decltype(AMFValue(value))* { return Insert(m_Dense.size(), value).first; }