3#include <StormByte/exception.hxx>
24 (!std::is_pointer_v<T>) &&
25 (std::is_same_v<
T, std::shared_ptr<typename T::element_type>> ||
26 std::is_same_v<
T, std::unique_ptr<typename T::element_type>> ||
27 !std::is_class_v<T> ||
51 template <
typename T,
typename = std::enable_if_t<(std::is_same_v<T, Types> || ...)>>
64 _processCopy(
other.m_values);
74 _processCopy(
other.m_values);
98 return m_values ==
other.m_values;
107 return !(*
this ==
other);
116 template <
typename T>
T&
Get() {
117 constexpr bool isDirectMatch = (std::is_same_v<T, Types> || ...);
119 ([]<
typename U>()
constexpr {
120 if constexpr (std::is_same_v<U, std::shared_ptr<T>> || std::is_same_v<U, std::unique_ptr<T>>) {
128 "Requested type is not in the variant or is not compatible with smart pointers");
131 if (std::holds_alternative<T>(m_values)) {
132 return std::get<T>(m_values);
134 throw Exception(
"Variant does not hold the requested type");
138 return std::visit([](
auto& value) ->
T& {
139 using ValueType = std::decay_t<
decltype(value)>;
140 if constexpr (std::is_same_v<ValueType, std::shared_ptr<T>> || std::is_same_v<ValueType, std::unique_ptr<T>>) {
146 throw Exception(
"Invalid type for dereferencing");
159 template <
typename T>
const T&
Get()
const {
168 template <
typename T>
169 struct is_unique_ptr : std::false_type {};
175 template <
typename T>
176 struct is_unique_ptr<std::
unique_ptr<T>> : std::true_type {};
182 template <
typename T>
183 struct is_shared_ptr : std::false_type {};
189 template <
typename T>
190 struct is_shared_ptr<std::
shared_ptr<T>> : std::true_type {};
196 void _processCopy(
const std::variant<Types...>&
variant) {
197 std::visit([
this](
const auto& value) {
198 using ValueType = std::decay_t<
decltype(value)>;
199 if constexpr (is_unique_ptr<ValueType>::value) {
201 using Foo =
typename ValueType::element_type;
202 if constexpr (!std::is_abstract_v<Foo>) {
204 m_values = std::make_unique<Foo>(*value);
206 m_values = std::unique_ptr<Foo>();
209 static_assert(!std::is_abstract_v<Foo>,
"Abstract classes cannot be copied directly");
211 }
else if constexpr (is_shared_ptr<ValueType>::value) {
221 std::variant<
Types...> m_values;
Base class for exceptions in the StormByte library.
Definition exception.hxx:23
A class that can hold a value of any of the specified types.
Definition variadic_value.hxx:39
const T & Get() const
Retrieves the stored value of the specified type (const version).
Definition variadic_value.hxx:159
VariadicValue & operator=(const VariadicValue &other)
Copy assignment operator with deep copy for std::unique_ptr.
Definition variadic_value.hxx:72
VariadicValue(VariadicValue &&) noexcept=default
Move constructor (default behavior).
VariadicValue()=default
Default constructor.
T & Get()
Retrieves the stored value of the specified type.
Definition variadic_value.hxx:116
constexpr bool operator!=(const VariadicValue &other) const
Inequality operator.
Definition variadic_value.hxx:106
VariadicValue(T value)
Constructor with a value of one of the specified types.
Definition variadic_value.hxx:52
~VariadicValue() noexcept=default
Destructor.
Concept to check if a type is a valid variadic type.
Definition variadic_value.hxx:23
Main namespace for the StormByte library.
std::conditional_t< is_reference< T >::value, std::expected< std::reference_wrapper< std::remove_reference_t< T > >, std::shared_ptr< E > >, std::expected< T, std::shared_ptr< E > > > Expected
Expected type with support for reference types.
Definition expected.hxx:32