LibGimbal  1.0.0
Ultimate C17/C++20 Cross-Language Runtime Framework and Meta Type System
gimbal_meta.hpp
Go to the documentation of this file.
1 #ifndef GIMBAL_META_HPP
2 #define GIMBAL_META_HPP
3 
4 #include "gimbal_meta.h"
5 #include "../types/gimbal_typedefs.hpp"
6 //#include "../types/gimbal_variant.hpp"
7 
8 namespace gimbal {
9 
10 template<typename T=void>
11 struct MetaTypeVTable: public GblMetaTypeVTable {
13  GblMetaTypeVTable({}) {}
14 };
15 
16 #if 0
17 #define CHECK_ADD_OVERRIDE(cFunc, cppFunc) \
18  do { \
19  if constexpr(requires { this->pFn##cFunc = &Derived::cppFunc; }) { \
20  this->pFn##cFunc = &Derived::cppFunc; \
21  } \
22  } while(0)
23 
24 template<typename Derived>
25 struct MetaTypeVTableStatic: public MetaTypeVTable, public Derived {
26  using ThisType = void;
27  using DerivedType = Derived;
28  MetaTypeVTableVTableStatic(void) {
29  CHECK_ADD_OVERRIDE(Construct, construct);
30  CHECK_ADD_OVERRIDE(Destruct, destruct);
31  CHECK_ADD_OVERRIDE(Convert, convert);
32  CHECK_ADD_OVERRIDE(Compare, compare);
33  CHECK_ADD_OVERRIDE(Serialize, serialize);
34  CHECK_ADD_OVERRIDE(Deserialize, deserialize);
35  }
36  //static constexpr const MetaTypeVTable<T> defaultVTable = MetaTypeVTable<T>();
37 };
38 #endif
39 
40 
41 
42 template<typename CRTP>
44 public:
45  gimbal::Size getSize(void) const { return getDispatcher_().vTableSizeOf; }
46  gimbal::Size getMethodCount(void) const { return getSize() / sizeof([](){}); }
47 
48  template<typename V,
49  typename F,
50  typename = std::enable_if_t<std::is_function_v<F>>>
51  F vMethod(F V::*pMethod) const {
52  const V* pVTable = getVTable<V>();
53  return pVTable->*pMethod;
54  }
55 
56  template<typename V,
57  typename F,
58  typename = std::enable_if_t<std::is_function_v<F>>>
59  bool hasMethod(F V::*pMethod) const {
60  return vMethod(pMethod) != nullptr;
61  }
62 
63  template<typename V,
64  typename F,
65  typename = std::enable_if_t<std::is_function_v<F>>>
66  decltype(auto) vCall(F V::*pMethod, auto&&... args) const {
67  auto pFunc = vMethod(pMethod);
68  GBL_ASSERT(pFunc);
69  return pFunc(std::forward<decltype(args)...>);
70  }
71 
72  template<typename V=const void*>
73  const V* getVTable(void) const {
74  if(sizeof(V) > getDispatcher_()->vTableSizeOf) return nullptr;
75  return getDispatcher_()->pVTable();
76  }
77 
78  operator const GblDispatcher*() const { return getDispatcher_();}
79 
80 private:
81  const GblDispatcher* getDispatcher_(void) const { return static_cast<const CRTP>(this)->getDispatcher(); }
82 
83 };
84 
85 class DispatcherView: public DispatcherBase<DispatcherView> {
86 public:
87  DispatcherView(const GblDispatcher* pSource):
88  pDispatcher_(pSource) {}
89 
90  const GblDispatcher* getDispatcher_(void) const { return pDispatcher_; }
91 private:
92  const GblDispatcher* pDispatcher_ = nullptr;
93 };
94 
95 class Dispatcher: public GblDispatcher,
96  public DispatcherBase<Dispatcher> {
97 public:
98 
99 
100  operator GblDispatcher*() { return static_cast<GblDispatcher*>(this); }
101  const GblDispatcher* getDispatcher_(void) const { return static_cast<const GblDispatcher*>(this); }
102 };
103 
104 
105 
106 
107 typedef struct GblMetaInstance {
110  GblDispatcher dispatcher;
112 
113 
114 template<typename CRTP>
116 public:
117  gimbal::Size getSizeOf(void) const { return getMetaInstance_()->sizeOf; }
118  gimbal::Size getAlignOf(void) const { return getMetaInstance_()->alignOf; }
119  DispatcherView getDispatcher(void) const { return &getMetaInstance_()->dispatcher; }
120 
121  operator const GblMetaInstance*() const {
122  return getMetaInstance_();
123  }
124 
125 private:
126  const GblMetaInstance* getMetaInstance_(void) const {
127  return static_cast<CRTP>(this)->getMetaInstance_();
128  }
129 };
130 
131 class MetaInstanceView: public MetaInstanceBase<MetaInstanceView> {
132 public:
134  pInstance_(pSource) {}
135 
136  const GblMetaInstance* getMetaInstance_(void) const { return pInstance_; }
137 private:
138  const GblMetaInstance* pInstance_ = nullptr;
139 
140 };
141 
143  public GblMetaInstance,
144  public MetaInstanceBase<MetaInstance>
145 {
146 public:
147  const GblMetaInstance* getMetaInstance_(void) const { return static_cast<const GblMetaInstance*>(this); }
148  operator GblMetaInstance*() {
149  return static_cast<GblMetaInstance*>(this);
150  }
151 
152  void setSizeOf(gimbal::Size size) { this->sizeOf = size; }
153  void setAlignOf(gimbal::Size align) { this->alignOf = align; }
154 
155 };
156 
157 
158 
159 template<typename T=void>
160 class MetaType: public GblMetaType {
161 public:
162  //MetaType(void) = default;
163 
164  template<typename V=MetaTypeVTable<T>>
165  requires std::is_base_of_v<GblMetaTypeVTable, V>
166  MetaType(const V* pVTable, const char* pTypeName="Unknown"):
167  GblMetaType({
168  pTypeName,
169  GBL_VARIANT_TYPE_NIL,
170  GBL_META_TYPE_ID_INVALID,
171  GBL_META_TYPE_FLAG_NONE,
172  NULL,
173  {
174  sizeof(T),
175  alignof(T),
176  {
177  sizeof(V),
178  pVTable
179  }
180  }
181  }) {}
182 
183  const char* getTypeName(void) const { return this->pTypeName; }
184  MetaTypeId getMetaTypeId(void) const { return this->typeId; }
185  auto getFlags(void) const { return this->flags; }
186  Size getSize(void) const { return this->metaInstance.sizeOf; }
187  Size getAlignment(void) const { return this->metaInstance.alignOf; }
188  Size getVTableSize(void) const { return this->metaInstance.dispatcher.vTableSizeOf; }
189  //Size getVTableEntries(void) const;
190  //MetaObject* getMetaObject(void) const;
191 
192  T* create(Context* pCtx, const Variant& variant) const {
193  T* pData = nullptr;
194  Exception::checkThrow(gblMetaTypeCreate(this, *pCtx, &variant, reinterpret_cast<void**>(&pData)));
195  return pData;
196  }
197  void construct(Context* pCtx, T* pData, const Variant& variant) const {
198  Exception::checkThrow(gblMetaTypeConstruct(this, *pCtx, reinterpret_cast<void*>(pData), &variant));
199  }
200  void destroy(Context* pCtx, T* pData) const {
201  Exception::checkThrow(gblMetaTypeDestroy(this, *pCtx, reinterpret_cast<void*>(pData)));
202  }
203  void destruct(Context* pCtx, T* pData) const {
204  Exception::checkThrow(gblMetaTypeDestruct(this, *pCtx, reinterpret_cast<void*>(pData)));
205  }
206  void convert(Context* pCtx, const T* pData, Variant* pTo) const {
207  Exception::checkThrow(gblMetaTypeConvert(this, *pCtx, reinterpret_cast<const void*>(pData), pTo));
208  }
209  gimbal::Int compare(Context* pCtx, const T* pData, const Variant& rhs) const {
210  GblInt result = 1;
211  Exception::checkThrow(gblMetaTypeCompare(this, *pCtx, reinterpret_cast<const void*>(pData), &rhs, &result));
212  return result;
213  }
214  bool compare(Context* pCtx, const T* pData, const Variant& rhs, Variant::OpCmpType cmpType) const {
215  gimbal::Int result = compare(pCtx, pData, rhs);
216  return GBL_COMPARISON_VERIFY_OP_CMP_TYPE(result, cmpType);
217  }
218  bool serialize(Context* pCtx, const T* pData, void* pBuffer, Size* pSize) const {
219  Exception::checkThrow(gblMetaTypeSerialize(this, *pCtx, reinterpret_cast<const void*>(pData), pBuffer, pSize));
220  return true;
221  }
222  bool deserialize(Context* pCtx, T* pData, const void* pBuffer, Size* pSize) const {
223  Exception::checkThrow(gblMetaTypeDeserialize(this, *pCtx, reinterpret_cast<void*>(pData), pBuffer, pSize));
224  return true;
225  }
226 };
227 
228 
229 }
230 
231 
232 #endif // GIMBAL_META_HPP
gimbal::MetaInstance::setSizeOf
void setSizeOf(gimbal::Size size)
Definition: gimbal_meta.hpp:152
gimbal_meta.h
gimbal::GblMetaInstance::sizeOf
GblSize sizeOf
Definition: gimbal_meta.hpp:108
gimbal::MetaType::MetaType
requires std::is_base_of_v< GblMetaTypeVTable, V > MetaType(const V *pVTable, const char *pTypeName="Unknown")
Definition: gimbal_meta.hpp:166
gimbal::MetaInstanceView::MetaInstanceView
MetaInstanceView(const GblMetaInstance *pSource)
Definition: gimbal_meta.hpp:133
gimbal::MetaType::getTypeName
const char * getTypeName(void) const
Definition: gimbal_meta.hpp:183
GblSize
GBL_SIZE GblSize
Definition: gimbal_typedefs.h:18
gimbal::MetaType
Definition: gimbal_meta.hpp:160
gimbal::MetaType::compare
gimbal::Int compare(Context *pCtx, const T *pData, const Variant &rhs) const
Definition: gimbal_meta.hpp:209
gimbal::MetaType::getVTableSize
Size getVTableSize(void) const
Definition: gimbal_meta.hpp:188
gimbal::MetaType::compare
bool compare(Context *pCtx, const T *pData, const Variant &rhs, Variant::OpCmpType cmpType) const
Definition: gimbal_meta.hpp:214
gimbal::GblMetaInstance::dispatcher
GblDispatcher dispatcher
Definition: gimbal_meta.hpp:110
GBL_ASSERT
#define GBL_ASSERT(...)
Definition: gimbal_macro_utils.h:136
gimbal::MetaInstance::setAlignOf
void setAlignOf(gimbal::Size align)
Definition: gimbal_meta.hpp:153
gimbal::MetaType::destroy
void destroy(Context *pCtx, T *pData) const
Definition: gimbal_meta.hpp:200
gimbal::MetaType::getAlignment
Size getAlignment(void) const
Definition: gimbal_meta.hpp:187
gimbal::MetaInstance
Definition: gimbal_meta.hpp:142
gimbal::MetaTypeVTable::MetaTypeVTable
MetaTypeVTable(void)
Definition: gimbal_meta.hpp:12
GblMetaType
Definition: gimbal_meta.h:73
gimbal::MetaType::serialize
bool serialize(Context *pCtx, const T *pData, void *pBuffer, Size *pSize) const
Definition: gimbal_meta.hpp:218
gimbal::DispatcherBase::vMethod
F vMethod(F V::*pMethod) const
Definition: gimbal_meta.hpp:51
GblMetaType::pVTable
GblVTableEntry * pVTable
Definition: gimbal_meta.h:74
gimbal::MetaType::convert
void convert(Context *pCtx, const T *pData, Variant *pTo) const
Definition: gimbal_meta.hpp:206
gimbal::GblMetaInstance
struct gimbal::GblMetaInstance GblMetaInstance
gimbal::DispatcherBase::getMethodCount
gimbal::Size getMethodCount(void) const
Definition: gimbal_meta.hpp:46
gimbal::GblMetaInstance::alignOf
GblSize alignOf
Definition: gimbal_meta.hpp:109
gimbal::Size
::GblSize Size
Definition: gimbal_typedefs.hpp:19
gimbal::DispatcherBase::getSize
gimbal::Size getSize(void) const
Definition: gimbal_meta.hpp:45
gimbal::MetaType::destruct
void destruct(Context *pCtx, T *pData) const
Definition: gimbal_meta.hpp:203
gimbal::DispatcherBase::hasMethod
bool hasMethod(F V::*pMethod) const
Definition: gimbal_meta.hpp:59
gimbal::DispatcherBase::getVTable
const V * getVTable(void) const
Definition: gimbal_meta.hpp:73
gimbal::Dispatcher
Definition: gimbal_meta.hpp:95
gimbal::MetaInstanceBase::getAlignOf
gimbal::Size getAlignOf(void) const
Definition: gimbal_meta.hpp:118
gimbal::Int
::GblInt Int
Definition: gimbal_typedefs.hpp:21
gimbal::MetaInstanceBase::getSizeOf
gimbal::Size getSizeOf(void) const
Definition: gimbal_meta.hpp:117
gimbal
Definition: gimbal_byte_array.hpp:9
gimbal::Exception::checkThrow
static const CallRecord & checkThrow(const CallRecord &record)
Definition: gimbal_exception.hpp:32
gimbal::Context
Definition: gimbal_context.hpp:38
gimbal::MetaType::construct
void construct(Context *pCtx, T *pData, const Variant &variant) const
Definition: gimbal_meta.hpp:197
gimbal::DispatcherBase
Definition: gimbal_meta.hpp:43
gimbal::DispatcherView::getDispatcher_
const GblDispatcher * getDispatcher_(void) const
Definition: gimbal_meta.hpp:90
gimbal::DispatcherBase::vCall
decltype(auto) vCall(F V::*pMethod, auto &&... args) const
Definition: gimbal_meta.hpp:66
gimbal::MetaInstanceView
Definition: gimbal_meta.hpp:131
gimbal::MetaType::deserialize
bool deserialize(Context *pCtx, T *pData, const void *pBuffer, Size *pSize) const
Definition: gimbal_meta.hpp:222
gimbal::MetaInstanceView::getMetaInstance_
const GblMetaInstance * getMetaInstance_(void) const
Definition: gimbal_meta.hpp:136
gimbal::MetaType::getFlags
auto getFlags(void) const
Definition: gimbal_meta.hpp:185
gimbal::GblMetaInstance
Definition: gimbal_meta.hpp:107
gimbal::MetaInstanceBase::getDispatcher
DispatcherView getDispatcher(void) const
Definition: gimbal_meta.hpp:119
gimbal::MetaInstanceBase
Definition: gimbal_meta.hpp:115
gimbal::MetaType::create
T * create(Context *pCtx, const Variant &variant) const
Definition: gimbal_meta.hpp:192
gimbal::MetaType::getMetaTypeId
MetaTypeId getMetaTypeId(void) const
Definition: gimbal_meta.hpp:184
gimbal::DispatcherView
Definition: gimbal_meta.hpp:85
gimbal::MetaInstance::getMetaInstance_
const GblMetaInstance * getMetaInstance_(void) const
Definition: gimbal_meta.hpp:147
gimbal::DispatcherView::DispatcherView
DispatcherView(const GblDispatcher *pSource)
Definition: gimbal_meta.hpp:87
GblInt
GBL_INT GblInt
Definition: gimbal_typedefs.h:20
GblMetaType::pTypeName
const char * pTypeName
Definition: gimbal_meta.h:75
gimbal::Dispatcher::getDispatcher_
const GblDispatcher * getDispatcher_(void) const
Definition: gimbal_meta.hpp:101
gimbal::MetaTypeVTable
Definition: gimbal_meta.hpp:11
gimbal::MetaType::getSize
Size getSize(void) const
Definition: gimbal_meta.hpp:186