Skip to content

Commit

Permalink
feat: variant support
Browse files Browse the repository at this point in the history
  • Loading branch information
killcerr committed Jan 5, 2025
1 parent 4bf6545 commit eaa1c6d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/Utils/Convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cstddef>
#include <type_traits>
#include <unordered_map>
#include <variant>
#include <vector>


Expand Down Expand Up @@ -77,7 +78,14 @@ struct ToScriptType<T, std::enable_if_t<std::is_enum_v<T>>> {
using Type = Number;
};

// variant
template <typename... Ts>
struct ToScriptType<std::variant<Ts...>> {
using Type = Value;
};

template <typename T, std::size_t I = 0>
Local<Value> VariantConvert(const T& value);
template <typename T>
Local<Value> DoScriptTypeConvert(const T& value) {
using ScriptType = typename ToScriptType<T>::Type;
Expand All @@ -104,6 +112,19 @@ Local<Value> DoScriptTypeConvert(const T& value) {
obj.set(fmt::to_string(key), DoScriptTypeConvert(val));
}
return obj;
} else if constexpr (std::is_same_v<ScriptType, Value>) {
VariantConvert(value);
}
}
template <typename T, std::size_t I>
Local<Value> VariantConvert(const T& value) {
if (auto res = std::get_if<I>(&value)) {
return DoScriptTypeConvert(*res);
} else {
if constexpr (I < std::variant_size_v<T>) return VariantConvert<T, I + 1>(value);
else {
throw std::runtime_error("Invalid variant");
}
}
}
template <typename T>
Expand Down

0 comments on commit eaa1c6d

Please sign in to comment.