FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_property.hpp
1/*
2 * Copyright 2024 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_PROPERTY_HPP_INCLUDED
18#define _FGE_C_PROPERTY_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/extra/extra_string.hpp"
22#include <optional>
23#include <string>
24#include <typeinfo>
25#include <vector>
26
27namespace fge
28{
29
30using PintType = int64_t;
31using PuintType = uint64_t;
32using PfloatType = float;
33using PdoubleType = double;
34
35class Property;
36using ParrayType = std::vector<fge::Property>;
37
38class PropertyClassWrapper;
39template<class T>
40class PropertyClassWrapperType;
41
53class FGE_API Property
54{
55 template<class T>
56 struct remove_cvref
57 {
58 typedef std::remove_cv_t<std::remove_reference_t<T>> type;
59 };
60 template<class T>
61 using remove_cvref_t = typename remove_cvref<T>::type;
62
63public:
64 enum class Types : uint8_t
65 {
66 PTYPE_NULL,
67
68 PTYPE_INTEGERS,
69 PTYPE_FLOAT,
70 PTYPE_DOUBLE,
71
72 PTYPE_STRING,
73
74 PTYPE_POINTER,
75 PTYPE_CLASS
76 };
77 union Data
78 {
79 fge::PuintType _u;
80 fge::PintType _i;
81
82 fge::PfloatType _f;
83 fge::PdoubleType _d;
84
85 void* _ptr;
86
87 constexpr void setClassWrapper(fge::PropertyClassWrapper* val) { this->_ptr = val; }
88 [[nodiscard]] constexpr fge::PropertyClassWrapper* getClassWrapper() const
89 {
90 return static_cast<fge::PropertyClassWrapper*>(this->_ptr);
91 }
92 template<class T>
93 [[nodiscard]] constexpr fge::PropertyClassWrapperType<T>* getClassWrapper() const
94 {
95 return static_cast<fge::PropertyClassWrapperType<T>*>(this->_ptr);
96 }
97 [[nodiscard]] constexpr fge::PropertyClassWrapperType<fge::ParrayType>* getArray() const
98 {
99 return static_cast<fge::PropertyClassWrapperType<fge::ParrayType>*>(this->_ptr);
100 }
101 [[nodiscard]] constexpr std::string* getString() const { return static_cast<std::string*>(this->_ptr); }
102 };
103
104 Property() = default;
105
106 //Copy/Move constructor
107 Property(fge::Property const& val);
108 Property(fge::Property&& val) noexcept;
109
110 //Copy/Move some type constructor
111 template<class T, typename = std::enable_if_t<!std::is_same_v<remove_cvref_t<T>, fge::Property>>>
112 Property(T&& val);
113
114 //Special string copy constructor
115 Property(char const* val);
116
117 ~Property();
118
119 void clear();
120
121 [[nodiscard]] bool operator==(fge::Property const& val) const;
122
123 //Copy/Move operator
124 fge::Property& operator=(fge::Property const& val);
125 fge::Property& operator=(fge::Property&& val) noexcept;
126
127 //Copy/Move some type operator
128 template<class T, typename = std::enable_if_t<!std::is_same_v<remove_cvref_t<T>, fge::Property>>>
129 fge::Property& operator=(T&& val);
130
131 //Special string copy operator
132 fge::Property& operator=(char const* val);
133
134 template<class T>
135 T& setType();
136 void setType(Types type);
137 template<class T>
138 [[nodiscard]] bool isType() const;
139 [[nodiscard]] bool isType(Types type) const;
140
141 [[nodiscard]] std::type_info const& getClassType() const;
142 [[nodiscard]] Types getType() const;
143 [[nodiscard]] bool isSigned() const;
144
145 [[nodiscard]] std::string toString() const;
146
147 bool set(fge::Property const& val);
148 bool set(fge::Property&& val);
149
150 template<class T, typename = std::enable_if_t<!std::is_same_v<remove_cvref_t<T>, fge::Property>>>
151 bool set(T&& val);
152
153 bool set(char const* val);
154
155 template<class T>
156 bool get(T& val) const;
157 template<class T>
158 std::optional<T> get() const;
159
160 template<class T>
161 T* getPtr();
162 template<class T>
163 T const* getPtr() const;
164
165 //Value array control
166 fge::ParrayType& setArrayType();
167
168 bool resize(std::size_t n);
169 bool reserve(std::size_t n);
170
171 bool pushData(fge::Property const& value);
172 bool pushData(fge::Property&& value);
173
174 template<class T>
175 bool pushType();
176
177 bool setData(std::size_t index, fge::Property const& value);
178 bool setData(std::size_t index, fge::Property&& value);
179
180 [[nodiscard]] fge::Property* getData(std::size_t index);
181 [[nodiscard]] fge::Property const* getData(std::size_t index) const;
182
183 template<class T>
184 bool getData(std::size_t index, T& val) const;
185 template<class T>
186 T* getDataPtr(std::size_t index);
187 template<class T>
188 T const* getDataPtr(std::size_t index) const;
189
190 [[nodiscard]] std::size_t getDataSize() const;
191
192 fge::Property* operator[](std::size_t index);
193 fge::Property const* operator[](std::size_t index) const;
194
195 [[nodiscard]] bool isModified() const;
196 void setModifiedFlag(bool flag);
197
198private:
199 Types g_type{Types::PTYPE_NULL};
200 Data g_data{};
201 bool g_isSigned{};
202 bool g_isModified{false};
203};
204
206{
207public:
208 PropertyClassWrapper() = default;
209 virtual ~PropertyClassWrapper() = default;
210
211 [[nodiscard]] virtual std::type_info const& getType() const = 0;
212
213 [[nodiscard]] virtual std::string toString() const = 0;
214
215 [[nodiscard]] virtual fge::PropertyClassWrapper* copy() const = 0;
216
217 virtual bool tryToCopy(fge::PropertyClassWrapper const* val) = 0;
218
219 [[nodiscard]] virtual bool compare(fge::PropertyClassWrapper const* val) = 0;
220};
221
222namespace comparisonCheck
223{
224struct No
225{};
226template<typename T, typename Arg>
227No operator==(T const&, Arg const&);
228
229template<typename T, typename Arg = T>
231{
232 enum
233 {
234 value = !std::is_same<decltype(std::declval<T>() == std::declval<Arg>()), No>::value
235 };
236};
237} // namespace comparisonCheck
238
239template<class T>
241{
242 static_assert(std::negation_v<std::is_base_of<fge::PropertyClassWrapper, T>>,
243 "fge::PropertyClassWrapperType<T>, T must not be based on fge::PropertyClassWrapper class type !");
244 static_assert(std::negation_v<std::is_base_of<fge::Property, T>>,
245 "fge::PropertyClassWrapperType<T>, T must not be based on fge::Property class type !");
246 static_assert(std::negation_v<std::is_pointer<T>>, "fge::PropertyClassWrapperType<T>, T must not be a pointer !");
247 static_assert(std::negation_v<std::is_reference<T>>,
248 "fge::PropertyClassWrapperType<T>, T must not be a reference !");
249
250public:
251 PropertyClassWrapperType() = default;
252 template<typename = std::enable_if_t<std::is_copy_constructible_v<T>>>
253 explicit PropertyClassWrapperType(T const& val);
254 template<typename = std::enable_if_t<std::is_move_constructible_v<T>>>
255 explicit PropertyClassWrapperType(T&& val) noexcept;
256 ~PropertyClassWrapperType() override = default;
257
258 [[nodiscard]] std::type_info const& getType() const override;
259
260 [[nodiscard]] std::string toString() const override;
261
262 [[nodiscard]] fge::PropertyClassWrapper* copy() const override;
263
264 bool tryToCopy(fge::PropertyClassWrapper const* val) override;
265
266 [[nodiscard]] bool compare(fge::PropertyClassWrapper const* val) override;
267
268 T _data;
269};
270
271} // namespace fge
272
273#include "FastEngine/C_property.inl"
274
275#endif // _FGE_C_PROPERTY_HPP_INCLUDED
Definition C_property.hpp:241
Definition C_property.hpp:206
A class that can store any type of data.
Definition C_property.hpp:54
Definition C_property.hpp:231
Definition C_property.hpp:225
Definition C_property.hpp:78