use decltype

This commit is contained in:
jadebenn 2024-11-22 16:33:04 -08:00
parent b799f8967c
commit ac4fd02a6c

View File

@ -82,8 +82,7 @@ using AMFStringValue = AMFValue<std::string>;
using AMFDoubleValue = AMFValue<double>; using AMFDoubleValue = AMFValue<double>;
// Template deduction guide to ensure string literals deduce // Template deduction guide to ensure string literals deduce
template <size_t N> AMFValue(const char*) -> AMFValue<std::string>; // AMFStringValue
AMFValue(const char (&)[N]) -> AMFValue<std::string>; // AMFStringValue
/** /**
* The AMFArrayValue object holds 2 types of lists: * The AMFArrayValue object holds 2 types of lists:
@ -126,17 +125,20 @@ public:
* @return The inserted element if the type matched, * @return The inserted element if the type matched,
* or nullptr if a key existed and was not the same type * or nullptr if a key existed and was not the same type
*/ */
template <typename ValueType> template <typename T>
[[maybe_unused]] std::pair<AMFValue<ValueType>*, bool> Insert(const std::string_view key, const ValueType value) { [[maybe_unused]] auto Insert(const std::string_view key, const T value) -> std::pair<decltype(AMFValue(value))*, bool> {
// This ensures the deduced type matches the AMFValue constructor
using AMFValueType = decltype(AMFValue(value));
const auto element = m_Associative.find(key); const auto element = m_Associative.find(key);
AMFValue<ValueType>* val = nullptr; AMFValueType* val = nullptr;
bool found = true; bool found = true;
if (element == m_Associative.cend()) { if (element == m_Associative.cend()) {
auto newVal = std::make_unique<AMFValue<ValueType>>(value); auto newVal = std::make_unique<AMFValueType>(value);
val = newVal.get(); val = newVal.get();
m_Associative.emplace(key, std::move(newVal)); m_Associative.emplace(key, std::move(newVal));
} else { } else {
val = dynamic_cast<AMFValue<ValueType>*>(element->second.get()); val = dynamic_cast<AMFValueType*>(element->second.get());
found = false; found = false;
} }
return std::make_pair(val, found); return std::make_pair(val, found);
@ -179,15 +181,18 @@ public:
* @return The inserted element, or nullptr if the type did not match * @return The inserted element, or nullptr if the type did not match
* what was at the index. * what was at the index.
*/ */
template <typename ValueType> template <typename T>
[[maybe_unused]] std::pair<AMFValue<ValueType>*, bool> Insert(const size_t index, const ValueType value) { [[maybe_unused]] auto Insert(const size_t index, const T value) -> std::pair<decltype(AMFValue(value))*, bool> {
// This ensures the deduced type matches the AMFValue constructor
using AMFValueType = decltype(AMFValue(value));
bool inserted = false; bool inserted = false;
if (index >= m_Dense.size()) { if (index >= m_Dense.size()) {
m_Dense.resize(index + 1); m_Dense.resize(index + 1);
m_Dense.at(index) = std::make_unique<AMFValue<ValueType>>(value); m_Dense.at(index) = std::make_unique<AMFValueType>(value);
inserted = true; inserted = true;
} }
return std::make_pair(dynamic_cast<AMFValue<ValueType>*>(m_Dense.at(index).get()), inserted); return std::make_pair(dynamic_cast<AMFValueType*>(m_Dense.at(index).get()), inserted);
} }
/** /**
@ -234,8 +239,8 @@ public:
* *
* @return The inserted pointer, or nullptr should the key already be in use. * @return The inserted pointer, or nullptr should the key already be in use.
*/ */
template <typename ValueType> template <typename T>
[[maybe_unused]] inline AMFValue<ValueType>* Push(const ValueType value) { [[maybe_unused]] inline auto Push(const T value) -> decltype(AMFValue(value))* {
return Insert(m_Dense.size(), value).first; return Insert(m_Dense.size(), value).first;
} }