draconisplusplus/include/rfl/Variant.hpp

91 lines
2.5 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_VARIANT_HPP_
#define RFL_VARIANT_HPP_
#include <variant>
namespace rfl {
2024-06-08 14:10:59 -04:00
template <class... AlternativeTypes>
struct Variant {
/// The type of the underlying variant.
using VariantType = std::variant<AlternativeTypes...>;
Variant(const VariantType& _variant) : variant_(_variant) {}
Variant(VariantType&& _variant) noexcept : variant_(std::move(_variant)) {}
Variant(const Variant<AlternativeTypes...>& _variant) = default;
Variant(Variant<AlternativeTypes...>&& _variant) noexcept = default;
2024-06-08 15:53:06 -04:00
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
2024-06-08 14:10:59 -04:00
Variant(const T& _t) : variant_(_t) {}
2024-06-08 15:53:06 -04:00
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
2024-06-08 14:10:59 -04:00
Variant(T&& _t) noexcept : variant_(std::forward<T>(_t)) {}
~Variant() = default;
/// Assigns the underlying object.
Variant<AlternativeTypes...>& operator=(const VariantType& _variant) {
variant_ = _variant;
return *this;
}
/// Assigns the underlying object.
Variant<AlternativeTypes...>& operator=(VariantType&& _variant) {
variant_ = std::move(_variant);
return *this;
}
/// Assigns the underlying object.
2024-06-08 15:53:06 -04:00
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
2024-06-08 14:10:59 -04:00
Variant<AlternativeTypes...>& operator=(T&& _variant) {
variant_ = std::forward<T>(_variant);
return *this;
}
/// Assigns the underlying object.
2024-06-08 15:53:06 -04:00
template <
class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, bool>::
type = true>
2024-06-08 14:10:59 -04:00
Variant<AlternativeTypes...>& operator=(const T& _variant) {
variant_ = _variant;
return *this;
}
/// Assigns the underlying object.
Variant<AlternativeTypes...>& operator=(
2024-06-08 15:53:06 -04:00
const Variant<AlternativeTypes...>& _other
) = default;
2024-06-08 14:10:59 -04:00
/// Assigns the underlying object.
Variant<AlternativeTypes...>& operator=(
2024-06-08 15:53:06 -04:00
Variant<AlternativeTypes...>&& _other
) = default;
2024-06-08 14:10:59 -04:00
/// Returns the underlying variant.
VariantType& variant() { return variant_; }
/// Returns the underlying variant.
const VariantType& variant() const { return variant_; }
/// The underlying variant - a Variant is a thin wrapper
/// around a variant that is mainly used for parsing.
VariantType variant_;
};
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif