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
00069 class SoRefCounter
00070 {
00071 public:
00076 void ref() const;
00077
00084 void unref() const;
00085
00093 void unrefNoDelete() const;
00094
00098 inline int getRefCount() const
00099 { return m_refcount; }
00100
00104 inline void lock() const
00105 { m_refMutex.lock(); }
00106
00110 inline void unlock() const
00111 { m_refMutex.unlock(); }
00112
00113
00114 private:
00116 static void initClass();
00117
00119 static void exitClass();
00120
00129 static void setRefCountCallback(SoBaseRefCountCB *f)
00130 { s_refCountCBFunc = f; }
00131
00144 void setRefCountCallbackData(void *userData = NULL)
00145 { m_refCountCBData = userData; }
00146
00150 void invokeRefCountCB() const;
00151
00153 static inline void setTraceRefs(const bool enable)
00154 { s_traceRefs = enable; }
00155
00157 static inline SbBool getTraceRefs()
00158 { return s_traceRefs; }
00159
00164 void setRefCount(const int count);
00165
00166 private:
00168 SoRefCounter();
00169
00171 SoRefCounter(const SoRefCounter &);
00172
00174 SoRefCounter & operator=(const SoRefCounter &)
00175 { return *this; }
00176
00178 virtual ~SoRefCounter();
00179
00180
00187 virtual bool notifyDelete() const
00188 { return true; }
00189
00194 virtual void destroy()
00195 { delete this; }
00196
00197 private:
00199 static bool s_traceRefs;
00200
00202 static SoBaseRefCountCB* s_refCountCBFunc;
00203 void* m_refCountCBData;
00204
00206 mutable int m_refcount;
00207 mutable SbThreadSpinlock m_refMutex;
00208
00209 };
00210
00212 #define SO_UNREF_RESET(object)\
00213 {\
00214 if ( object )\
00215 {\
00216 object->unref();\
00217 object=NULL;\
00218 }\
00219 }
00220
00221
00222
00223 #include <Inventor/misc/SoAutoRef.h>
00224
00225 #endif // SO_REF_COUNTER_H
00226
00227
00228