Open Inventor Reference
SoRef.h
Go to the documentation of this file.
1 //----------------------------------------------------------------------------------
2 // **InsertLicense** code
3 //----------------------------------------------------------------------------------
4 #ifndef SO_REF_H
5 #define SO_REF_H
6 
9 template <class T>
10 class SoRef
11 {
12 public:
13  SoRef():_object(0) {}
14 
15  SoRef(const SoRef<T> &p):_object(0) {
16  setObject(p._object);
17  }
18 
19  SoRef(T* o) {
20  _object = o;
21  if (o) {
22  o->ref();
23  }
24  }
25 
26  ~SoRef() { if (_object) _object->unref(); }
27 
29  setObject(p._object);
30  return *this;
31  }
32 
33  template<class T2>
35  setObject(p._object);
36  return *this;
37  }
38 
40  setObject(o);
41  return *this;
42  }
43 
44  bool operator==( const SoRef<T> &p ) const {
45  return _object == p._object;
46  }
47 
48  bool operator!= ( const SoRef<T>& p ) const {
49  return !( *this == p );
50  }
51 
52  bool operator==( T* p ) const {
53  return _object == p;
54  }
55 
56  bool operator!= ( T* p ) const {
57  return _object != p;
58  }
59 
60  T* operator->() const { return _object; }
61 
62  T& operator*() const { return *( _object ); }
63 
66  operator T*() const { return _object; }
67 
69  T* ptr() const { return _object; }
70 
72  T* get() const { return _object; }
73 
78  T* p = _object;
79  if (_object) {
80  _object->unrefNoDelete();
81  _object = NULL;
82  }
83  return p;
84  }
85 
86 protected:
87  void setObject(T* o) {
88  if (o != _object) {
89  T* oldObject = _object;
90  _object = o;
91  if (_object) _object->ref();
92  if (oldObject) oldObject->unref();
93  }
94  }
95 
96 private:
97  T* _object;
98 };
99 
100 #endif
101 
SoRef proves an intrusive smart pointer for any SoBase derived class and uses the ref()/unref() metho...
Definition: SoRef.h:11
T & operator*() const
Definition: SoRef.h:62
T * operator->() const
Definition: SoRef.h:60
SoRef< T > & operator=(const SoRef< T2 > &p)
Definition: SoRef.h:34
~SoRef()
Definition: SoRef.h:26
bool operator==(T *p) const
Definition: SoRef.h:52
SoRef< T > & operator=(const SoRef< T > &p)
Definition: SoRef.h:28
SoRef< T > & operator=(T *o)
Definition: SoRef.h:39
T * releaseNoDelete()
Get access to the underlying pointer without deleting the object.
Definition: SoRef.h:77
SoRef(const SoRef< T > &p)
Definition: SoRef.h:15
void setObject(T *o)
Definition: SoRef.h:87
T * ptr() const
Get access to the underlying pointer (VSG compatibility).
Definition: SoRef.h:69
SoRef()
Definition: SoRef.h:13
bool operator!=(const SoRef< T > &p) const
Definition: SoRef.h:48
SoRef(T *o)
Definition: SoRef.h:19
bool operator==(const SoRef< T > &p) const
Definition: SoRef.h:44
T * get() const
Get access to the underlying pointer.
Definition: SoRef.h:72