CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Class.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
12#include "MetaContainer.hpp"
13#include "MetaItem.hpp"
14#include "Type.hpp"
15
17
26
27namespace CeresEngine {
28
30
34 None = 0,
35
37 IncludeFromBase = (1u << 0u),
38
40 Default = None,
41 };
42
45
46 class Class : public MetaContainer {
47 public:
48 using CastFunctionType = const void* (*)(const void*);
49 template<typename T> using MemberIterator = Generator<const T>;
50 template<typename T> using MemberIteratorFunc = FunctionView<bool(const T&) const>;
51
52 public:
55
56 private:
58
59 public:
60 explicit Class(const StringView name) : name(name) {}
61 ~Class() override = default;
62
63 public:
65 [[nodiscard]] static const Class* find(TypeID typeID);
66
68 [[nodiscard]] static const Class* find(StringView name);
69
70 public: // Base classes
73
74 [[nodiscard]] bool inheritedFrom(const Class* base) const;
75
76 public: // Constructors
81
84
85 template<typename... Args> [[nodiscard]] const ClassConstructor* getConstructor() const;
86
89
93
94 public: // Properties
99
104
107
109
110 public: // Methods
115
120
123
124 template<typename... Args> [[nodiscard]] const ClassMethod* getMethod(StringView name) const;
125
126 public:
127 [[nodiscard]] const void* cast(const Class* base, const void* instance) const;
128 [[nodiscard]] void* cast(const Class* base, void* instance) const;
129
130 private:
133
134 protected:
136 };
137
139 public:
140 virtual ~ReflectableClassTypeTrait() = default;
141 [[nodiscard]] virtual ClassInfo getClassInfo(void* target) const = 0;
142 [[nodiscard]] virtual ClassInfo getClassInfo(const void* target) const = 0;
143 };
144
148 template<typename T> const Class* getClass() { return getType<T>().getClass(); }
149
150 class ClassMember : public MetaItem {
151 protected:
152 using MetaItem::MetaItem;
153 };
154
156 public:
159
160 public:
162
163 public:
165 [[nodiscard]] MetaCategory getCategory() const override { return kCategory; }
166
169
170 virtual Box invoke(const MetaValues& arguments) const = 0;
171
172 template<typename... Args> [[nodiscard]] auto invoke(Args&&... args) const { return invoke({std::ref(args)...}); }
173 };
174
176 class ClassProperty : public ClassMember {
177 public:
180
181 public:
184
185 public:
188
191
193 [[nodiscard]] virtual bool isStatic() const noexcept = 0;
194
197
200
201 public: // Getter
204
209
212 [[nodiscard]] auto get(T& target) const {
213 return get(std::ref(target));
214 }
215
218 [[nodiscard]] Box get() const;
219
220 public: // Setter
223
227 virtual void set(const Box& target, const Box& value) const = 0;
228
230 virtual void set(const Box& target, Box&& value) const = 0;
231
234 void set(T& target, const V& value) const {
235 set(std::ref(target), std::ref(value));
236 }
237
239 template<typename T, typename V> requires(!std::is_same_v<std::decay_t<T>, Box> && !std::is_same_v<std::decay_t<V>, Box>)
240 void set(T& target, V& value) const {
241 set(std::ref(target), std::ref(value));
242 }
243
245 template<typename T, typename V> requires(!std::is_same_v<std::decay_t<T>, Box> && !std::is_same_v<std::decay_t<V>, Box>)
246 void set(T& target, V&& value) const {
247 set(std::ref(target), std::forward<V>(value));
248 }
249
252 void set(const Box& value) const;
253
255 void set(Box&& value) const;
256
258 template<typename V> requires(!std::is_same_v<std::decay_t<V>, Box>)
259 void set(const V& value) const {
260 set(nullptr, std::ref(value));
261 }
262
264 template<typename V> requires(!std::is_same_v<std::decay_t<V>, Box>)
265 void set(V& value) const {
266 set(nullptr, std::ref(value));
267 }
268
270 template<typename V> requires(!std::is_same_v<std::decay_t<V>, Box>)
271 void set(V&& value) const {
272 set(nullptr, std::forward<V>(value));
273 }
274 };
275
305
306 template<typename... Args> auto ClassMethod::invoke(Args&&... args) const { return invoke({std::ref(args)...}); }
307
308} // namespace CeresEngine
A value type that can hold any alongside it's type information.
Definition Box.hpp:40
Definition Class.hpp:155
static constexpr MetaCategory kCategory
Definition Class.hpp:164
auto invoke(Args &&... args) const
Definition Class.hpp:172
virtual Box invoke(const MetaValues &arguments) const =0
ReflectionAttributeContainer attributes
Definition Class.hpp:158
MetaCategory getCategory() const override
Gets the category of the item.
Definition Class.hpp:165
Span< const Type > getParameterTypes() const
ClassConstructor(Class &owner, StringView name)
String name
Definition Class.hpp:157
virtual Span< const TypeID > getParameterTypeIDs() const =0
Definition Class.hpp:46
void forEachProperty(const MemberIteratorFunc< ClassProperty > &func) const
Enumerates over all properties in the class.
Definition Class.hpp:98
Generator< const Class > forEachBase() const
const ClassConstructor * getCopyConstructor() const
bool inheritedFrom(const Class *base) const
static const Class * find(StringView name)
Finds a Class by it's fully qualified name.
const ClassProperty * getProperty(StringView name) const
const ClassMethod * getMethodInternal(StringView name) const
MemberIterator< ClassProperty > forEachProperty(MetaMemberIterationFlags flags=MetaMemberIterationFlag::Default) const
Enumerates over all properties in the class.
const ClassConstructor * getConstructor(StringView oldSignature) const
void * cast(const Class *base, void *instance) const
static const Class * find(TypeID typeID)
Finds a Class by it's type ID.
ReflectionAttributeContainer attributes
Definition Class.hpp:54
void addBaseClass(TypeID typeID, CastFunctionType caster)
Generator< const Class > forEachDirectBase() const
const ClassConstructor * getDefaultConstructor() const
const ClassMethod * getMethod(StringView name) const
void forEachConstructor(const MemberIteratorFunc< ClassConstructor > &func) const
Enumerates over all constructors in the class.
const ClassConstructor * getConstructor() const
void forEachMethod(const MetaMemberIterationFlags &flags, const MemberIteratorFunc< ClassMethod > &func) const
Enumerates over all methods in the class.
void forEachMethod(const MemberIteratorFunc< ClassMethod > &func) const
Enumerates over all methods in the class.
Definition Class.hpp:114
FunctionView< bool(const T &) const > MemberIteratorFunc
Definition Class.hpp:50
HashMap< TypeID, CastFunctionType > mBaseClasses
Definition Class.hpp:57
const StringView name
Definition Class.hpp:53
const ClassConstructor * getMoveConstructor() const
const ClassProperty * getPropertyInternal(StringView name) const
const void *(*)(const void *) CastFunctionType
Definition Class.hpp:48
MemberIterator< ClassConstructor > forEachConstructor() const
Enumerates over all constructors in the class.
const void * cast(const Class *base, const void *instance) const
Class(const StringView name)
Definition Class.hpp:60
void forEachProperty(const MetaMemberIterationFlags &flags, const MemberIteratorFunc< ClassProperty > &func) const
Enumerates over all properties in the class.
~Class() override=default
MemberIterator< ClassMethod > forEachMethod(MetaMemberIterationFlags flags=MetaMemberIterationFlag::Default) const
Enumerates over all methods in the class.
Definition Class.hpp:150
Definition Class.hpp:276
ClassMethod(Class &owner, StringView name)
virtual Box invoke(const MetaValues &arguments) const =0
virtual TypeID getReturnTypeID() const noexcept=0
Type getReturnType() const
StringView name
Definition Class.hpp:278
Vector< Type > mParameterTypes
Definition Class.hpp:282
virtual bool isStatic() const noexcept=0
Returns true if the method is static.
ReflectionAttributeContainer attributes
Definition Class.hpp:279
MetaCategory getCategory() const override
Gets the category of the item.
Definition Class.hpp:292
Span< const Type > getParameterTypes() const
virtual Span< const TypeID > getParameterTypeIDs() const =0
static constexpr MetaCategory kCategory
Gets the category of the item.
Definition Class.hpp:289
Represents a reflected property from metadata defined by the class.
Definition Class.hpp:176
ReflectionAttributeContainer attributes
Definition Class.hpp:179
MetaCategory getCategory() const final
Gets the category of the item.
Definition Class.hpp:190
virtual TypeID getSetterParameterTypeID() const noexcept=0
void set(const Box &value) const
Sets the property value.
void set(const V &value) const
Definition Class.hpp:259
virtual TypeID getGetterReturnTypeID() const noexcept=0
virtual bool isReadOnly() const noexcept=0
Determines if the property is read-only and cannot be set.
ClassProperty(Class &owner, StringView name)
virtual void set(const Box &target, const Box &value) const =0
Sets the property value.
Type getSetterParameterType() const
TypeID getTypeID() const noexcept
void set(V &value) const
Definition Class.hpp:265
void set(T &target, V &value) const
Definition Class.hpp:240
void set(Box &&value) const
~ClassProperty() noexcept override
Box get() const
Gets the value of a static property.
static constexpr MetaCategory kCategory
Gets the category of the item.
Definition Class.hpp:187
void set(T &target, V &&value) const
Definition Class.hpp:246
StringView name
Definition Class.hpp:178
Type getGetterReturnType() const
void set(V &&value) const
Definition Class.hpp:271
virtual bool isStatic() const noexcept=0
Determines if the property is static or not.
A generator represents a coroutine type that produces a sequence of values of type T,...
Definition Generator.hpp:50
A base class for a container of MetaItems.
Definition MetaContainer.hpp:35
A generic item that can be added as a member of a namespace.
Definition MetaItem.hpp:90
virtual ClassInfo getClassInfo(const void *target) const =0
virtual ClassInfo getClassInfo(void *target) const =0
virtual ~ReflectableClassTypeTrait()=default
Represents a reflected C++ type. Can be used to get metadata from a C++ type.
Definition Type.hpp:32
Definition Application.hpp:19
SmallVector< Box, 4 > MetaValues
Definition Class.hpp:29
MetaCategory
The category of a MetaItem.
Definition MetaItem.hpp:23
@ Method
Indicates that the item is a method.
@ Property
Indicates that the item is a property.
@ Constructor
Indicates that the item is a constructor.
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
const Class * getClass()
Gets a pointer to the Class object that contains reflection metadata for the given class type T.
Definition Class.hpp:148
std::unordered_map< Key, T, Hash, KeyEqual, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > HashMap
HashMap is an associative container that contains key-value pairs with unique keys.
Definition Map.hpp:33
FunctionBase< false, true, fu2::capacity_default, true, false, Signatures... > FunctionView
A non owning copyable function wrapper for arbitrary callable types.
Definition Function.hpp:64
MetaMemberIterationFlag
A set of flags that control the iteration of class members.
Definition Class.hpp:32
@ IncludeFromBase
If set, will include members from base classes.
@ Default
The default value for member iteration properties.
sfl::small_vector< T, N, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > SmallVector
SmallVector is a sequence container similar to Vector.
Definition SmallVector.hpp:31
tcb ::span< T, Extent > Span
Span describes an object that can refer to a contiguous sequence of objects with the first element of...
Definition Span.hpp:708
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668
A structure that contains type information for a class.
Definition IReflectable.hpp:15
Wrapper around an enum that allows simple use of bitwise logic operations.
Definition Flags.hpp:19