All Classes Functions Variables Typedefs Friends Pages
Object.h
1 //------------------------------------------------------------------------------
2 //
3 // Copyright (c) 2013 Glympse Inc. All rights reserved.
4 //
5 //------------------------------------------------------------------------------
6 
7 #ifndef OBJECT_H__GLYMPSE__
8 #define OBJECT_H__GLYMPSE__
9 
10 namespace Glympse
11 {
12 
13 // Provides explicit scope for Common class used in friend declaration below. Needed for
14 // blackberry compiler.
15 template< class T > class Common;
16 
30 template< class T > class O
31 {
35  private: T* _object;
36 
41  template< class U > friend class O;
42  template< class V > friend class Common;
43 
51  public: O()
52  : _object(NULL)
53  {
54  }
55  public: O(T* address)
56  : _object(address)
57  {
58  }
59  public: O(const O<T>& rO)
60  : _object(rO._object)
61  {
62  retain();
63  }
64  public: template< class T2 > O(const O<T2>& rO)
65  : _object(dynamic_cast<T*>(rO._object))
66  {
67  retain();
68  }
69 
74  public: O<T>& operator=(T* address)
75  {
76  release();
77  _object = address;
78  return *this;
79  }
80  public: O<T>& operator=(const O<T>& rO)
81  {
82  if ( this != &rO )
83  {
84  release();
85  _object = rO._object;
86  retain();
87  }
88  return *this;
89  }
90  public: template< class T2 > O<T>& operator=(const O<T2>& rO)
91  {
92  release();
93  _object = dynamic_cast<T*>(rO._object);
94  retain();
95  return *this;
96  }
97 
105  public: virtual ~O()
106  {
107  release();
108  }
109 
115  public: template<class T2> bool operator==(const O<T2> &rO)
116  {
117  return _object == rO._object;
118  }
119  public: template<class T2> bool operator==(const O<T2> &rO) const
120  {
121  return _object == rO._object;
122  }
123  public: template<class T2> bool operator!=(const O<T2> &rO)
124  {
125  return _object != rO._object;
126  }
127  public: template<class T2> bool operator!=(const O<T2> &rO) const
128  {
129  return _object != rO._object;
130  }
131  public: template<class T2> bool operator<(const O<T2> &rO)
132  {
133  return _object < rO._object;
134  }
135  public: template<class T2> bool operator<(const O<T2> &rO) const
136  {
137  return _object < rO._object;
138  }
139 
146  public: bool operator==(T* address)
147  {
148  return (_object == address);
149  }
150  public: bool operator==(T* address) const
151  {
152  return (_object == address);
153  }
154  public: bool operator!=(T* address)
155  {
156  return (_object != address);
157  }
158  public: bool operator!=(T* address) const
159  {
160  return (_object != address);
161  }
162 
168  T* operator->()
169  {
170  return _object;
171  }
172  T* operator->() const
173  {
174  return _object;
175  }
176 
184  private: inline void retain()
185  {
186  if ( _object )
187  {
188  _object->retain();
189  }
190  }
191 
195  private: inline void release()
196  {
197  if ( _object )
198  {
199  _object->release();
200  _object = NULL;
201  }
202  }
203 };
204 
209 template< class T > inline bool operator==(void* lhs, const O<T>& rhs)
210 {
211  return ( lhs == rhs.operator->() );
212 }
213 
218 template< class T > inline bool operator!=(void* lhs, const O<T>& rhs)
219 {
220  return ( lhs != rhs.operator->() );
221 }
222 
226 class Object
227 {
249  public: template<typename T> static inline O<T> fromThis(T* t)
250  {
251  const O<T>& o(t);
252  o->retain();
253  return o;
254  }
255 };
256 
257 }
258 
259 #endif // !OBJECT_H__GLYMPSE__