MeVisLab Toolbox Reference
macSmartPtr.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2008, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef MAC_SMART_PTR_H
14 #define MAC_SMART_PTR_H
15 
17 
18 #if defined(__APPLE__)
19 
21 #include <stdlib.h>
22 
23 namespace macos {
24 
26  template <class T>
27  class SmartPtr
28  {
29  public:
30 
31  SmartPtr() : _object(NULL) {}
32 
33  SmartPtr(const SmartPtr<T> &p) : _object(NULL) {
34  setObject(p.object());
35  }
36 
37  SmartPtr(T *o) {
38  _object = o;
39  if (o) o->retain();
40  }
41 
43  if (_object) _object->release();
44  }
45 
47  setObject(p.object());
48  return *this;
49  }
50 
52  setObject(o);
53  return *this;
54  }
55 
56  bool operator==(const SmartPtr<T> &p) const {
57  return object() == p.object();
58  }
59 
60  bool operator!= (const SmartPtr<T>& p) const {
61  return !(*this == p);
62  }
63 
64  bool operator==(T *p) const {
65  return object() == p;
66  }
67 
68  bool operator!= (T *p) const {
69  return object() != p;
70  }
71 
72  bool isNull() const { return !object(); }
73 
74  T* operator->() const { return object(); }
75 
76  T& operator*() const { return *(object()); }
77 
78  operator T*() const { return object(); }
79 
80  protected:
81 
82  T *object() const {
83  return _object;
84  }
85 
86  void setObject(T *o) {
87  if (o != _object) {
88  if (_object) _object->release();
89  _object = o;
90  if (_object) _object->retain();
91  }
92  }
93 
94  private:
95 
96  T *_object;
97  };
98 
99 }
100 
101 #endif // __APPLE__
102 #endif // __macSmartPtr_H
@ T
Definition: SoKeyGrabber.h:71
Smart pointer to reference counted object (Helper class)
Definition: macSmartPtr.h:28
SmartPtr(const SmartPtr< T > &p)
Definition: macSmartPtr.h:33
void setObject(T *o)
Definition: macSmartPtr.h:86
SmartPtr< T > & operator=(T *o)
Definition: macSmartPtr.h:51
T * operator->() const
Definition: macSmartPtr.h:74
SmartPtr< T > & operator=(const SmartPtr< T > &p)
Definition: macSmartPtr.h:46
bool operator==(const SmartPtr< T > &p) const
Definition: macSmartPtr.h:56
T * object() const
Definition: macSmartPtr.h:82
T & operator*() const
Definition: macSmartPtr.h:76
bool isNull() const
Definition: macSmartPtr.h:72
bool operator!=(const SmartPtr< T > &p) const
Definition: macSmartPtr.h:60
bool operator==(T *p) const
Definition: macSmartPtr.h:64
AppleScript support.