00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef SO_REF_COUNTER_H
00025 #define SO_REF_COUNTER_H
00026
00027 #include <stdio.h>
00028 #include <Inventor/SbBase.h>
00029 #include <Inventor/threads/SbThreadSpinlock.h>
00030 #include <Inventor/errors/SoDebugError.h>
00031
00032 class SoRefCounter;
00033
00034
00035 typedef void SoBaseRefCountCB(void *userData, const SoRefCounter *base);
00036
00037
00071 class SoRefCounter
00072 {
00073 public:
00078 void ref() const;
00079
00086 void unref() const;
00087
00095 void unrefNoDelete() const;
00096
00100 inline int getRefCount() const
00101 { return m_refcount; }
00102
00106 inline void lock() const
00107 { m_refMutex.lock(); }
00108
00112 inline void unlock() const
00113 { m_refMutex.unlock(); }
00114
00115
00116 private:
00118 static void initClass();
00119
00121 static void exitClass();
00122
00131 static void setRefCountCallback(SoBaseRefCountCB *f)
00132 { s_refCountCBFunc = f; }
00133
00146 void setRefCountCallbackData(void *userData = NULL)
00147 { m_refCountCBData = userData; }
00148
00152 void invokeRefCountCB() const;
00153
00155 static inline void setTraceRefs(const bool enable)
00156 { s_traceRefs = enable; }
00157
00159 static inline SbBool getTraceRefs()
00160 { return s_traceRefs; }
00161
00166 void setRefCount(const int count);
00167
00168 private:
00170 SoRefCounter();
00171
00173 SoRefCounter(const SoRefCounter &);
00174
00176 SoRefCounter & operator=(const SoRefCounter &)
00177 { return *this; }
00178
00180 virtual ~SoRefCounter();
00181
00182
00189 virtual bool notifyDelete() const
00190 { return true; }
00191
00196 virtual void destroy()
00197 { delete this; }
00198
00199 private:
00201 static bool s_traceRefs;
00202
00204 static SoBaseRefCountCB* s_refCountCBFunc;
00205 void* m_refCountCBData;
00206
00208 mutable int m_refcount;
00209 mutable SbThreadSpinlock m_refMutex;
00210
00211 };
00212
00214 #define SO_UNREF_RESET(object)\
00215 {\
00216 if ( object )\
00217 {\
00218 object->unref();\
00219 object=NULL;\
00220 }\
00221 }
00222
00223
00224
00225 #include <Inventor/misc/SoAutoRef.h>
00226
00227 #endif // SO_REF_COUNTER_H
00228
00229
00230