LibGimbal  1.0.0
Ultimate C17/C++20 Cross-Language Runtime Framework and Meta Type System
gimbal_instance.hpp
Go to the documentation of this file.
1 #ifndef GIMBAL_INSTANCE_HPP
2 #define GIMBAL_INSTANCE_HPP
3 
4 #include "gimbal_instance.h"
5 #include "gimbal_type.hpp"
6 
7 namespace gimbal {
8 
9 class ClassRef;
10 
11 template<typename CRTP>
12 class ClassBase {
13 protected:
14  GblClass* class_(void) {
15  return static_cast<CRTP*>(this)->instancePtr_();
16  }
17 
18  const GblClass* class_(void) const {
19  return static_cast<const CRTP*>(this)->instancePtr_();
20  }
21 public:
22  Type getType(void) const {
23  return GblType_fromClass(class_());
24  }
25 
26  RefCount getRefCount(void) const {
27  return GblClass_refCount(class_());
28  }
29 };
30 
31 
32 class ClassRef: public ClassBase<ClassRef> {
33 private:
34  GblClass* pClass_ = nullptr;
35 public:
36  const GblClass* instancePtr_(void) const {
37  return pClass_;
38  }
39 
41  return pClass_;
42  }
43 
44  ClassRef(GblClass* pClass):
45  pClass_(GblClass_refFromClass(pClass)) {}
46 
47  ~ClassRef(void) {
48  GblClass_unref(pClass_);
49  }
50 };
51 
52 
53 
54 template<typename CRTP>
55 class InstanceBase {
56 protected:
58  return static_cast<CRTP*>(this)->instancePtr_();
59  }
60 
61  const GblInstance* instance_(void) const {
62  return static_cast<const CRTP*>(this)->instancePtr_();
63  }
64 
65 public:
66 
67  Type getType(void) const {
68  return GBL_INSTANCE_TYPE(instance_());
69  }
70 
71  ClassRef getClass(void) const {
72  return GblInstance_classOf(instance_());
73  }
74 };
75 
76 }
77 
78 #endif // GIMBAL_INSTANCE_HPP
gimbal::Type
Definition: gimbal_type.hpp:38
gimbal::ClassBase
Definition: gimbal_instance.hpp:12
gimbal_instance.h
gimbal::InstanceBase::instance_
const GblInstance * instance_(void) const
Definition: gimbal_instance.hpp:61
gimbal::ClassBase::getType
Type getType(void) const
Definition: gimbal_instance.hpp:22
gimbal::ClassRef::~ClassRef
~ClassRef(void)
Definition: gimbal_instance.hpp:47
gimbal::InstanceBase::getClass
ClassRef getClass(void) const
Definition: gimbal_instance.hpp:71
gimbal_type.hpp
gimbal::ClassBase::class_
GblClass * class_(void)
Definition: gimbal_instance.hpp:14
gimbal::ClassRef
Definition: gimbal_instance.hpp:32
gimbal::InstanceBase
Definition: gimbal_instance.hpp:55
gimbal::ClassBase::class_
const GblClass * class_(void) const
Definition: gimbal_instance.hpp:18
gimbal::ClassRef::instancePtr_
GblClass * instancePtr_(void)
Definition: gimbal_instance.hpp:40
GblClass
Definition: gimbal_class.h:32
gimbal::InstanceBase::getType
Type getType(void) const
Definition: gimbal_instance.hpp:67
gimbal::ClassRef::ClassRef
ClassRef(GblClass *pClass)
Definition: gimbal_instance.hpp:44
gimbal
Definition: gimbal_byte_array.hpp:9
GblInstance
Definition: gimbal_instance.h:78
GBL_INSTANCE_TYPE
#define GBL_INSTANCE_TYPE(instance)
Definition: gimbal_instance.h:11
gimbal::ClassRef::instancePtr_
const GblClass * instancePtr_(void) const
Definition: gimbal_instance.hpp:36
gimbal::InstanceBase::instance_
GblInstance * instance_(void)
Definition: gimbal_instance.hpp:57
gimbal::ClassBase::getRefCount
RefCount getRefCount(void) const
Definition: gimbal_instance.hpp:26
GblType_fromClass
GBL_EXPORT GblType GblType_fromClass(const GblClass *pClass) GBL_NOEXCEPT
gimbal::RefCount
::GblRefCount RefCount
Definition: gimbal_typedefs.hpp:28
GblClass_unref
GBL_EXPORT GblRefCount GblClass_unref(SELF) GBL_NOEXCEPT
GblClass_refFromClass
GBL_INLINE GblClass * GblClass_refFromClass(SELF) GBL_NOEXCEPT