comparison foosdk/sdk/pfc/other.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2
3 #include "avltree.h"
4
5 namespace pfc {
6
7 //warning: not multi-thread-safe
8 template<typename t_base>
9 class instanceTracker : public t_base {
10 private:
11 typedef instanceTracker<t_base> t_self;
12 public:
13 template<typename ... args_t> instanceTracker( args_t && ... args) : t_base(std::forward<args_t>(args) ...) {g_list += this; }
14
15 instanceTracker(const t_self & p_other) : t_base( (const t_base &)p_other) {g_list += this;}
16 ~instanceTracker() {g_list -= this;}
17
18 typedef pfc::avltree_t<t_self*> t_list;
19 static const t_list & instanceList() {return g_list;}
20 template<typename t_callback> static void forEach(t_callback && p_callback) {instanceList().enumerate(p_callback);}
21 private:
22 static t_list g_list;
23 };
24
25 template<typename t_base>
26 typename instanceTracker<t_base>::t_list instanceTracker<t_base>::g_list;
27
28
29 //warning: not multi-thread-safe
30 template<typename TClass>
31 class instanceTrackerV2 {
32 private:
33 typedef instanceTrackerV2<TClass> t_self;
34 public:
35 instanceTrackerV2(const t_self & p_other) {g_list += static_cast<TClass*>(this);}
36 instanceTrackerV2() {g_list += static_cast<TClass*>(this);}
37 ~instanceTrackerV2() {g_list -= static_cast<TClass*>(this);}
38
39 typedef pfc::avltree_t<TClass*> t_instanceList;
40 static const t_instanceList & instanceList() {return g_list;}
41 template<typename t_callback> static void forEach(t_callback && p_callback) {instanceList().enumerate(p_callback);}
42 private:
43 static t_instanceList g_list;
44 };
45
46 template<typename TClass>
47 typename instanceTrackerV2<TClass>::t_instanceList instanceTrackerV2<TClass>::g_list;
48
49
50 struct objDestructNotifyData {
51 bool m_flag;
52 objDestructNotifyData * m_next;
53
54 };
55 class objDestructNotify {
56 public:
57 objDestructNotify() : m_data() {}
58 ~objDestructNotify() {
59 set();
60 }
61
62 void set() {
63 objDestructNotifyData * w = m_data;
64 while(w) {
65 w->m_flag = true; w = w->m_next;
66 }
67 }
68 objDestructNotifyData * m_data;
69 };
70
71 class objDestructNotifyScope : private objDestructNotifyData {
72 public:
73 objDestructNotifyScope(objDestructNotify &obj) : m_obj(&obj) {
74 m_next = m_obj->m_data;
75 m_obj->m_data = this;
76 }
77 ~objDestructNotifyScope() {
78 if (!m_flag) m_obj->m_data = m_next;
79 }
80 bool get() const {return m_flag;}
81 PFC_CLASS_NOT_COPYABLE_EX(objDestructNotifyScope)
82 private:
83 objDestructNotify * m_obj;
84
85 };
86 }