IDS peak AFL v2.1.0
Loading...
Searching...
No Matches
peak_common_any.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
10
11#include <type_traits>
12#include <memory>
13#include <string>
14
15namespace peak
16{
17namespace common
18{
19
30class Any
31{
32 struct Placeholder
33 {
34 virtual ~Placeholder() = default;
35
36 PEAK_COMMON_NO_DISCARD virtual const std::type_info& Type() const = 0;
37 PEAK_COMMON_NO_DISCARD virtual std::unique_ptr<Placeholder> Clone() const = 0;
38 };
39
40 template <typename T>
41 struct Holder : public Placeholder
42 {
43 T value;
44
45 explicit Holder(const T& val)
46 : value(val)
47 {}
48
49 explicit Holder(T&& val)
50 : value(std::move(val))
51 {}
52
53 PEAK_COMMON_NO_DISCARD const std::type_info& Type() const override
54 {
55 return typeid(T);
56 }
57
58 PEAK_COMMON_NO_DISCARD std::unique_ptr<Placeholder> Clone() const override
59 {
60 return std::make_unique<Holder<T>>(value);
61 }
62 };
63
64 std::unique_ptr<Placeholder> m_content;
65
66public:
72 Any() = default;
73
82 template <typename T>
83 explicit Any(const T& value)
84 : m_content(std::make_unique<Holder<typename std::decay_t<T>>>(value))
85 {}
86
95 template <typename T>
96 explicit Any(T&& value
98 ,
99 typename std::enable_if<!std::is_same<Any, typename std::decay<T>::type>::value>::type* = nullptr
101 )
102 : m_content(std::make_unique<Holder<typename std::decay_t<T>>>(std::forward<T>(value)))
103 {}
104
110 Any(const Any& other)
111 : m_content(other.m_content ? other.m_content->Clone() : nullptr)
112 {}
113
119 Any(Any&& other) noexcept = default;
120
126 Any& operator=(const Any& other)
127 {
128 if (this != &other)
129 {
130 m_content = other.m_content ? other.m_content->Clone() : nullptr;
131 }
132 return *this;
133 }
134
140 Any& operator=(Any&& other) noexcept = default;
141
147 void Reset()
148 {
149 m_content.reset();
150 }
151
159 PEAK_COMMON_NO_DISCARD const std::type_info& GetType() const
160 {
161 return m_content ? m_content->Type() : typeid(void);
162 }
163
173 template <typename T>
175 {
176 using namespace std::string_literals;
177
178 if (typeid(T) != GetType())
179 {
181 "Casting from "s + GetType().name() + " to the given type " + typeid(T).name() + " is not supported!");
182 }
183 return static_cast<Holder<T>*>(m_content.get())->value;
184 }
185
195 template <typename T>
196 PEAK_COMMON_NO_DISCARD const T& AnyCast() const
197 {
198 using namespace std::string_literals;
199
200 if (typeid(T) != GetType())
201 {
203 "Casting from "s + GetType().name() + " to the given type " + typeid(T).name() + " is not supported!");
204 }
205 return static_cast<const Holder<T>*>(m_content.get())->value;
206 }
207
215 PEAK_COMMON_NO_DISCARD bool HasValue() const
216 {
217 return m_content != nullptr;
218 }
219};
220
221} // namespace common
222} // namespace peak
Any & operator=(const Any &other)
Copy assignment operator.
Definition peak_common_any.hpp:126
Any()=default
Default constructor. Constructs an empty Any object.
const std::type_info & GetType() const
Gets the type of the stored value.
Definition peak_common_any.hpp:159
void Reset()
Clears the stored value.
Definition peak_common_any.hpp:147
Any(T &&value)
Constructs an Any from a rvalue reference.
Definition peak_common_any.hpp:96
const T & AnyCast() const
Const version of AnyCast.
Definition peak_common_any.hpp:196
Any(Any &&other) noexcept=default
Move constructor.
Any(const Any &other)
Copy constructor.
Definition peak_common_any.hpp:110
Any(const T &value)
Constructs an Any from a const reference.
Definition peak_common_any.hpp:83
Any & operator=(Any &&other) noexcept=default
Move assignment operator.
T & AnyCast()
Casts the stored value to the specified type.
Definition peak_common_any.hpp:174
bool HasValue() const
Checks if the Any contains a value.
Definition peak_common_any.hpp:215
Thrown when a cast to an incompatible type is attempted.
Definition peak_common_exceptions.hpp:88
Root namespace for common types and interfaces shared across multiple libraries.
Definition peak_common_math.hpp:17