Skip to content

Commit

Permalink
use decltype
Browse files Browse the repository at this point in the history
  • Loading branch information
jadebenn committed Nov 23, 2024
1 parent b799f89 commit ac4fd02
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions dCommon/Amf3.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ using AMFStringValue = AMFValue<std::string>;
using AMFDoubleValue = AMFValue<double>;

// Template deduction guide to ensure string literals deduce
template <size_t N>
AMFValue(const char (&)[N]) -> AMFValue<std::string>; // AMFStringValue
AMFValue(const char*) -> AMFValue<std::string>; // AMFStringValue

/**
* The AMFArrayValue object holds 2 types of lists:
Expand Down Expand Up @@ -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 <typename ValueType>
[[maybe_unused]] std::pair<AMFValue<ValueType>*, bool> Insert(const std::string_view key, const ValueType value) {
template <typename T>
[[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);
AMFValue<ValueType>* val = nullptr;
AMFValueType* val = nullptr;
bool found = true;
if (element == m_Associative.cend()) {
auto newVal = std::make_unique<AMFValue<ValueType>>(value);
auto newVal = std::make_unique<AMFValueType>(value);
val = newVal.get();
m_Associative.emplace(key, std::move(newVal));
} else {
val = dynamic_cast<AMFValue<ValueType>*>(element->second.get());
val = dynamic_cast<AMFValueType*>(element->second.get());
found = false;
}
return std::make_pair(val, found);
Expand Down Expand Up @@ -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 <typename ValueType>
[[maybe_unused]] std::pair<AMFValue<ValueType>*, bool> Insert(const size_t index, const ValueType value) {
template <typename T>
[[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;
if (index >= m_Dense.size()) {
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;
}
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);
}

/**
Expand Down Expand Up @@ -234,8 +239,8 @@ class AMFArrayValue : public AMFBaseValue {
*
* @return The inserted pointer, or nullptr should the key already be in use.
*/
template <typename ValueType>
[[maybe_unused]] inline AMFValue<ValueType>* Push(const ValueType value) {
template <typename T>
[[maybe_unused]] inline auto Push(const T value) -> decltype(AMFValue(value))* {
return Insert(m_Dense.size(), value).first;
}

Expand Down

0 comments on commit ac4fd02

Please sign in to comment.