Open Inventor Release 2024.2.0
 
Loading...
Searching...
No Matches
SoRefCounter.h
1/*=======================================================================
2 *** THE CONTENT OF THIS WORK IS PROPRIETARY TO FEI S.A.S, (FEI S.A.S.), ***
3 *** AND IS DISTRIBUTED UNDER A LICENSE AGREEMENT. ***
4 *** ***
5 *** REPRODUCTION, DISCLOSURE, OR USE, IN WHOLE OR IN PART, OTHER THAN AS ***
6 *** SPECIFIED IN THE LICENSE ARE NOT TO BE UNDERTAKEN EXCEPT WITH PRIOR ***
7 *** WRITTEN AUTHORIZATION OF FEI S.A.S. ***
8 *** ***
9 *** RESTRICTED RIGHTS LEGEND ***
10 *** USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT OF THE CONTENT OF THIS ***
11 *** WORK OR RELATED DOCUMENTATION IS SUBJECT TO RESTRICTIONS AS SET FORTH IN ***
12 *** SUBPARAGRAPH (C)(1) OF THE COMMERCIAL COMPUTER SOFTWARE RESTRICTED RIGHT ***
13 *** CLAUSE AT FAR 52.227-19 OR SUBPARAGRAPH (C)(1)(II) OF THE RIGHTS IN ***
14 *** TECHNICAL DATA AND COMPUTER SOFTWARE CLAUSE AT DFARS 52.227-7013. ***
15 *** ***
16 *** COPYRIGHT (C) 1996-2021 BY FEI S.A.S, ***
17 *** BORDEAUX, FRANCE ***
18 *** ALL RIGHTS RESERVED ***
19**=======================================================================*/
20/*=======================================================================
21** Author : VSG (MMM YYYY)
22**=======================================================================*/
23
24#ifndef SO_REF_COUNTER_H
25#define SO_REF_COUNTER_H
26
27#include <stdio.h>
28#include <Inventor/SbBase.h>
29#include <Inventor/threads/SbThreadSpinlock.h>
30#include <Inventor/errors/SoDebugError.h>
31#include <functional>
32
33class SoRefCounter;
34
35// callback function prototypes
36typedef void SoBaseRefCountCB(void *userData, const SoRefCounter *base);
37
38
71{
72public:
77 void ref() const;
78
85 void unref() const;
86
94 void unrefNoDelete() const;
95
99 inline int getRefCount() const
100 { return m_refcount; }
101
105 inline void lock() const
106 { m_refMutex.lock(); }
107
111 inline void unlock() const
112 { m_refMutex.unlock(); }
113
114
115private:
117 static void initClass();
118
120 static void exitClass();
121
122 struct OnDeleteCB{
123 SoRefCounter* context;
124 std::function<void(SoRefCounter* caller, SoRefCounter* context)> callback;
125 bool isValid;
126 OnDeleteCB(SoRefCounter* ptr, const std::function<void(SoRefCounter* caller, SoRefCounter* context)> &cb): context(ptr), callback(cb), isValid(true){}
127 };
128
137 static void setRefCountCallback(SoBaseRefCountCB *f)
138 { s_refCountCBFunc = f; }
139
152 void setRefCountCallbackData(void *userData = NULL)
153 { m_refCountCBData = userData; }
154
158 void invokeRefCountCB() const;
159
161 static inline void setTraceRefs(const bool enable)
162 { s_traceRefs = enable; }
163
165 static inline SbBool getTraceRefs()
166 { return s_traceRefs; }
167
172 void setRefCount(const int count);
173
174 // Add callback function called juste before object deletion. If a context is used, this callback is automaticaly removed when the context object is deleted.
175 void addOnDeleteCB(SoRefCounter *context, const std::function<void()> &callBackFunction);
176 void addOnDeleteCB(SoRefCounter *context, const std::function<void(SoRefCounter* caller)> &callBackFunction);
177 void addOnDeleteCB(SoRefCounter *context, const std::function<void(SoRefCounter* caller, SoRefCounter* context)> &callBackFunction);
178 void addOnDeleteCB(const std::function<void()> &callBackFunction);
179 void addOnDeleteCB(const std::function<void(SoRefCounter* caller)> &callBackFunction);
180
181
182 private:
184 SoRefCounter();
185
187 SoRefCounter(const SoRefCounter &);
188
190 SoRefCounter & operator=(const SoRefCounter &)
191 { return *this; }
192
194 virtual ~SoRefCounter();
195
196
203 virtual bool notifyDelete() const
204 { return true; }
205
210 virtual void destroy()
211 { delete this; }
212
213private:
215 static bool s_traceRefs;
216
218 static SoBaseRefCountCB* s_refCountCBFunc;
219 void* m_refCountCBData;
220
222 mutable int m_refcount;
223 mutable SbThreadSpinlock m_refMutex;
224
225 // OnDeleteCB handling
226 std::vector<OnDeleteCB> *m_onDeleteCBVec;
227 std::vector<SoRefCounter*> *m_contextVec;
228 void cleanUpContextsCB();
229 void cleanUpContextsLists();
230
231};
232
234#define SO_UNREF_RESET(object)\
235{\
236 if ( object )\
237 {\
238 object->unref();\
239 object=NULL;\
240 }\
241}
242
243// Included here to provide this service by default everywhere SoRefCounter
244// is already used, avoiding app to add a #include <Inventor/misc/SoAutoRef.h>
245#include <Inventor/misc/SoAutoRef.h>
246
247#endif // SO_REF_COUNTER_H
248
249
<a href="IconLegend.html"><img src="extTGS.gif" alt="VSG extension" border="0"></a> Portable spinloc...
void lock()
Request exclusive access for this spinlock.
void unlock()
Release exclusive access.
Base class for ref-counted objects.
void unref() const
Removes a reference from an instance.
int getRefCount() const
Returns current reference count.
void lock() const
lock this instance.
void unrefNoDelete() const
unrefNoDelete() should be called when it is desired to decrement the reference count,...
void ref() const
Adds a reference to an instance.
void unlock() const
unlock this instance.
int SbBool
Boolean type.
Definition SbBase.h:87