Plasma Engine  2.0
Loading...
Searching...
No Matches
EnumerableClass.h
1#pragma once
2
4
26template <typename Derived, typename Base = plNoBase>
27class plEnumerable : public Base
28{
29public:
31 {
32 if (Derived::s_pFirstInstance == nullptr)
33 Derived::s_pFirstInstance = this;
34 else
35 Derived::s_pLastInstance->m_pNextInstance = this;
36
37 Derived::s_pLastInstance = this;
38 m_pNextInstance = nullptr;
39 ++Derived::s_uiInstances;
40 }
41
42 virtual ~plEnumerable()
43 {
44 --Derived::s_uiInstances;
45 plEnumerable* pPrev = nullptr;
46 plEnumerable* pCur = Derived::s_pFirstInstance;
47
48 while (pCur)
49 {
50 if (pCur == this)
51 {
52 if (pPrev == nullptr)
53 Derived::s_pFirstInstance = m_pNextInstance;
54 else
55 pPrev->m_pNextInstance = m_pNextInstance;
56
57 if (Derived::s_pLastInstance == this)
58 Derived::s_pLastInstance = pPrev;
59
60 break;
61 }
62
63 pPrev = pCur;
64 pCur = pCur->m_pNextInstance;
65 }
66 }
67
68protected:
69 plEnumerable* m_pNextInstance;
70};
71
75#define PL_DECLARE_ENUMERABLE_CLASS(self) PL_DECLARE_ENUMERABLE_CLASS_WITH_BASE(self, plNoBase)
76
80#define PL_DECLARE_ENUMERABLE_CLASS_WITH_BASE(self, base) \
81private: \
82 using plEnumerableBase = base; \
83 friend class plEnumerable<self, base>; \
84 static plEnumerable<self, base>* s_pFirstInstance; \
85 static plEnumerable<self, base>* s_pLastInstance; \
86 static plUInt32 s_uiInstances; \
87 \
88public: \
89 static self* GetFirstInstance() \
90 { \
91 return (self*)s_pFirstInstance; \
92 } \
93 self* GetNextInstance() \
94 { \
95 return (self*)m_pNextInstance; \
96 } \
97 const self* GetNextInstance() const \
98 { \
99 return (const self*)m_pNextInstance; \
100 } \
101 \
102private:
103
107#define PL_ENUMERABLE_CLASS_IMPLEMENTATION(self) \
108 plEnumerable<self, self::plEnumerableBase>* self::s_pFirstInstance = nullptr; \
109 plEnumerable<self, self::plEnumerableBase>* self::s_pLastInstance = nullptr; \
110 plUInt32 self::s_uiInstances = 0
Base class to add the ability to another class to enumerate all active instance of it,...
Definition EnumerableClass.h:28