NAVTOR SDK  v.3.0
Software Development Kit for fast and efficient development of ECDIS, ECS and generic marine applications
sdk_ref_ptr.h
Go to the documentation of this file.
1 #ifndef SDK_SDK_REF_PTR_H_
11 #define SDK_SDK_REF_PTR_H_
12 
13 #include <base/inc/base_types.h>
14 
15 SDK_BEGIN_NAMESPACE
16 
18 
22 template<class T>
23 class SDKRefPtr {
24  public:
25  SDKRefPtr() throw() : m_instance(NULL) {}
26 
27  SDKRefPtr(const SDKRefPtr& lp) throw() : m_instance(NULL) {
28  if (lp) {
29  m_instance = lp.m_instance;
30  if (m_instance)
31  m_instance->AddRef();
32  }
33  }
34 
35  SDKRefPtr(T* lp) throw() {
36  m_instance = lp;
37  if (m_instance)
38  m_instance->AddRef();
39  }
40 
41  ~SDKRefPtr() throw() {
42  if (m_instance)
43  m_instance->Release();
44  }
45 
47  operator T*() const throw() { return m_instance; }
48  T& operator*() const throw() { return *m_instance; }
49  T** operator&() throw() { return &m_instance; }
50  T* operator->() const throw() { return m_instance; }
51  bool operator!() const throw() { return (NULL == m_instance); }
52  bool operator!=(T* p) const throw() { return !operator==(p); }
53  bool operator==(T* p) const throw() { return m_instance == p; }
54 
55  SDKRefPtr& operator=(const SDKRefPtr& lp) throw() {
56  AssignNewPtr(lp.m_instance);
57  return *this;
58  }
59 
61  SDKAtomicRefCount Release() throw()
62  {
63  T* tmp = m_instance;
64  if (tmp)
65  {
66  m_instance = NULL;
67  return tmp->Release();
68  }
69 
70  return 0;
71  }
72 
74  T* Get() const throw() { return m_instance; }
75 
77  void Attach(T* p) throw() {
78  T* tmp = m_instance;
79  m_instance = p;
80  if (tmp)
81  tmp->Release();
82  }
83 
85  T* Detach() throw() {
86  T* p = m_instance;
87  m_instance = NULL;
88  return p;
89  }
90 
91  template< class I >
92  SDKRefPtr<I> GetInterface() const throw() {
93  SDKRefPtr<I> out;
94  if (m_instance) {
95  m_instance->GetInterface(I::IID(), reinterpret_cast<void**>(&out));
96  }
97  return out;
98  }
99 
100  private:
101 
102  inline void AssignNewPtr(T* p) {
103  if (p != m_instance) {
104  if (p)
105  p->AddRef();
106  T* tmp = m_instance;
107  m_instance = p;
108  if (tmp)
109  tmp->Release();
110  }
111  }
112 
113  T* m_instance;
114 };
115 
117 template<class T> class RefPtr : public SDKRefPtr<T> { public: };
118 
122 template <class T>
123 class STLAdapt {
124 public:
125  STLAdapt() {
126  }
127 
128  STLAdapt(const T& src) {
129  m_T = src;
130  }
131 
132  STLAdapt(const STLAdapt& src_a) {
133  m_T = src_a.m_T;
134  }
135 
136  STLAdapt& operator=(const T& src) {
137  m_T = src;
138  return *this;
139  }
140 
141  bool operator<(const T& src) const {
142  return m_T < src;
143  }
144 
145  bool operator==(const T& src) const {
146  return m_T == src;
147  }
148 
149  operator T&() {
150  return m_T;
151  }
152 
153  operator const T&() const {
154  return m_T;
155  }
156 
157  T m_T;
158 };
159 
160 SDK_END_NAMESPACE
161 
162 #endif // SDK_SDK_REF_PTR_H_
SDKRefPtr::Detach
T * Detach()
Detaches the instance from the wrapper.
Definition: sdk_ref_ptr.h:85
base_types.h
SDK base types.
SDKRefPtr::Get
T * Get() const
Return wrapped pointer.
Definition: sdk_ref_ptr.h:74
RefPtr
Synonym for SDKRefPtr.
Definition: sdk_ref_ptr.h:117
STLAdapt
Class to adapt SDKStringHandler and SDKRefPtr for use with STL containers. The syntax to use it is st...
Definition: sdk_ref_ptr.h:123
SDKRefPtr
A smart pointer class for managing SDK components interface pointers.
Definition: sdk_ref_ptr.h:23
SDKRefPtr::Release
SDKAtomicRefCount Release()
Releases the reference to the object.
Definition: sdk_ref_ptr.h:61
SDKRefPtr::Attach
void Attach(T *p)
Attaches existing object to the wrapper.
Definition: sdk_ref_ptr.h:77