MyGUI 3.5.3
MyGUI_Any.h
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#ifndef MYGUI_ANY_H_
8#define MYGUI_ANY_H_
9
10#include "MyGUI_Prerequest.h"
11#include "MyGUI_Diagnostic.h"
12#include <type_traits>
13#include <typeinfo>
14#include <utility>
15
16namespace MyGUI
17{
18
47
48 class MYGUI_EXPORT Any
49 {
50 public:
51 static const Any Null;
52 Any();
53 Any(const Any& other);
54 Any(Any&& other) noexcept;
55 ~Any();
56
57 template<typename ValueType>
58 Any(const ValueType& value) :
59 mData(::new std::decay_t<const ValueType>(value)),
60 mType(getTypeOps<std::decay_t<const ValueType>>())
61 {
62 }
63
64 template<typename ValueType>
65 Any& operator=(const ValueType& rhs)
66 {
67 return *this = Any(rhs);
68 }
69
70 Any& operator=(const Any& rhs);
71 Any& operator=(Any&& rhs) noexcept;
72
73 bool empty() const;
74
75 const std::type_info& getType() const;
76
77 template<typename ValueType>
78 ValueType* castType(bool _throw = true)
79 {
80 if constexpr (std::is_object_v<ValueType>)
81 {
82 if (mData && mType->type == typeid(ValueType))
83 return static_cast<ValueType*>(mData);
84 }
85 if (_throw)
86 detail::throwBadCast(getType().name(), typeid(ValueType).name(), __FILE__, __LINE__);
87 return nullptr;
88 }
89
90 template<typename ValueType>
91 const ValueType* castType(bool _throw = true) const
92 {
93 if constexpr (std::is_object_v<ValueType>)
94 {
95 if (mData && mType->type == typeid(ValueType))
96 return static_cast<const ValueType*>(mData);
97 }
98 if (_throw)
99 detail::throwBadCast(getType().name(), typeid(ValueType).name(), __FILE__, __LINE__);
100 return nullptr;
101 }
102
103 bool compare(const Any& other) const;
104
105 private:
106 template<typename T, typename = void>
107 struct HasOperatorEqual : std::false_type
108 {
109 };
110
111 template<typename T>
112 struct HasOperatorEqual<T, std::void_t<decltype(std::declval<const T&>() == std::declval<const T&>())>> :
113 std::is_same<bool, decltype(std::declval<const T&>() == std::declval<const T&>())>
114 {
115 };
116
117 template<typename T1, typename T2>
118 struct HasOperatorEqual<std::pair<T1, T2>> : std::conjunction<HasOperatorEqual<T1>, HasOperatorEqual<T2>>
119 {
120 };
121
122 struct TypeOps
123 {
124 const std::type_info& type;
125 void* (*copy)(const void*);
126 void (*destroy)(void*);
127 bool (*compare)(const void*, const void*, const std::type_info&);
128 };
129
130 template<typename T>
131 static const TypeOps* getTypeOps() noexcept
132 {
133 static constexpr TypeOps operations{
134 typeid(T),
135 [](const void* source) -> void* { return ::new T(*static_cast<const T*>(source)); },
136 [](void* source) { ::delete static_cast<T*>(source); },
137 [](const void* left, const void* right, const std::type_info& rightType) -> bool
138 {
139 if constexpr (HasOperatorEqual<T>::value)
140 return typeid(T) == rightType && *static_cast<const T*>(left) == *static_cast<const T*>(right);
141 else
142 MYGUI_EXCEPT("Type '" << typeid(T).name() << "' is not comparable");
143 }};
144 return &operations;
145 }
146
147 private:
148 // The allocation contains only the value; its operations are shared by type.
149 // Moving an Any transfers ownership without relocating its stored value.
150 void* mData = nullptr;
151 const TypeOps* mType = nullptr;
152 };
153
154} // namespace MyGUI
155
156#endif // MYGUI_ANY_H_
#define MYGUI_EXCEPT(dest)
#define MYGUI_EXPORT