CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
MetaContainer.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 "MetaItem.hpp"
11#include "Type.hpp"
12
16
17#include <functional>
18
19namespace CeresEngine {
20
21 constexpr auto ConstructorSignature = "constructor";
22 constexpr auto DefaultConstructorSignature = "defaultConstructor";
23 constexpr auto CopyConstructorSignature = "copyConstructor";
24 constexpr auto MoveConstructorSignature = "moveConstructor";
25
26 class MetaContainer;
27 class Class;
28 class ClassConstructor;
29 class ClassMethod;
30 class ClassProperty;
31 class Enum;
32 class EnumValue;
33
36 friend class ClassProperty;
37 friend class ClassConstructor;
38 friend class ClassMethod;
39 friend class EnumValue;
40
41 private:
42 // Need custom deleter since MetaItem destructor is protected
43 struct Deleter {
44 void operator()(const MetaItem* value) const;
45 };
46
49
50 protected:
51 explicit MetaContainer() = default;
52 virtual ~MetaContainer() = default;
53
54 public:
55 template<typename T, typename Func> void forEach(Func&& func) const;
56 template<typename Func> void forEach(MetaCategory category, Func&& func) const;
57
58 template<typename T> [[nodiscard]] Generator<const T> forEach() const;
60
61 protected:
62 [[nodiscard]] bool addItem(MetaItem* value);
63 [[nodiscard]] std::size_t count(MetaCategory category) const;
64 };
65
66 extern template Generator<const ClassProperty> MetaContainer::forEach<ClassProperty>() const;
67 extern template Generator<const ClassConstructor> MetaContainer::forEach<ClassConstructor>() const;
68 extern template Generator<const ClassMethod> MetaContainer::forEach<ClassMethod>() const;
69 extern template Generator<const EnumValue> MetaContainer::forEach<EnumValue>() const;
70
71 template<typename T, typename Func> void MetaContainer::forEach(Func&& func) const {
72 forEach(T::kCategory, [&func](const MetaItem& item) { return func(static_cast<const T&>(item)); });
73 }
74
75 template<typename Func> void MetaContainer::forEach(const MetaCategory category, Func&& func) const {
76 using ReturnType = decltype(func(std::declval<const MetaItem&>()));
77 for(const ItemPtr& item : mItems) {
78 CE_ASSERT(item != nullptr);
79
80 if(item->getCategory() != category)
81 continue;
82
83 if constexpr(std::is_same_v<ReturnType, void>) {
84 func(*item);
85 } else {
86 if(!func(*item)) {
87 break;
88 }
89 }
90 }
91 }
92
93 template<typename T> [[nodiscard]] Generator<const T> MetaContainer::forEach() const {
94 for(const ItemPtr& item : mItems) {
95 CE_ASSERT(item != nullptr);
96
97 if(item->getCategory() != T::kCategory) {
98 continue;
99 }
100
101 co_yield static_cast<const T&>(*item);
102 }
103 }
104
105} // namespace CeresEngine
#define CE_ASSERT(...)
Definition Macros.hpp:323
Definition Class.hpp:155
Definition Class.hpp:46
Definition Class.hpp:276
Represents a reflected property from metadata defined by the class.
Definition Class.hpp:176
Represents a reflected enum from C++.
Definition Enum.hpp:26
Represents an enum value.
Definition Enum.hpp:75
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
UPtr< MetaItem, Deleter > ItemPtr
Definition MetaContainer.hpp:47
Vector< ItemPtr > mItems
Definition MetaContainer.hpp:48
bool addItem(MetaItem *value)
Generator< const T > forEach() const
Definition MetaContainer.hpp:93
Generator< const MetaItem > forEach(MetaCategory category) const
std::size_t count(MetaCategory category) const
virtual ~MetaContainer()=default
A generic item that can be added as a member of a namespace.
Definition MetaItem.hpp:90
Definition Application.hpp:19
std::unique_ptr< T, Deleter > UPtr
UPtr is a smart pointer that owns and manages another object through a pointer and disposes of that o...
Definition SmartPtr.hpp:28
constexpr auto MoveConstructorSignature
Definition MetaContainer.hpp:24
MetaCategory
The category of a MetaItem.
Definition MetaItem.hpp:23
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
constexpr auto DefaultConstructorSignature
Definition MetaContainer.hpp:22
constexpr auto CopyConstructorSignature
Definition MetaContainer.hpp:23
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
constexpr auto ConstructorSignature
Definition MetaContainer.hpp:21
Definition MetaContainer.hpp:43
void operator()(const MetaItem *value) const