MrSID Decode SDK for LiDAR Reference Manual  1.1.3.4427
Object.h
Go to the documentation of this file.
1 /* //////////////////////////////////////////////////////////////////////////
2 // //
3 // This code is Copyright (c) 2008-2010 LizardTech, Inc, 1008 Western //
4 // Avenue, Suite 200, Seattle, WA 98104. Unauthorized use or distribution //
5 // prohibited. Access to and use of this code is permitted only under //
6 // license from LizardTech, Inc. Portions of the code are protected by //
7 // US and foreign patents and other filings. All Rights Reserved. //
8 // //
10 /* PUBLIC */
11 
12 #ifndef __LIDAR_OBJECT_H__
13 #define __LIDAR_OBJECT_H__
14 
15 #include "lidar/Atomic.h"
16 
17 LT_BEGIN_LIDAR_NAMESPACE
18 
34 class Object
35 {
45 #define ABSTRACT_OBJECT(classname) \
46  DISABLE_COPY(classname); \
47  protected: \
48  classname(void); \
49  virtual ~classname(void)
50 
55 #define CONCRETE_OBJECT(classname) \
56  ABSTRACT_OBJECT(classname); \
57  public: \
58  static classname *create(void)
59 
60 #define IMPLEMENT_OBJECT_CREATE(classname) \
61  classname * classname::create(void) { return new classname; }
62 
64 public:
68  void retain(void) const;
69 
74  void release(void) const;
75 
76 protected:
78  static void *operator new(size_t size);
80  static void operator delete(void *ptr);
81 
82 private:
83  mutable AtomicInt m_refCount;
84 };
85 
90 template<typename OBJECT> static inline OBJECT *RETAIN(OBJECT *obj)
91 {
92  if(obj != NULL)
93  obj->retain();
94  return obj;
95 }
96 
101 template<typename OBJECT> static inline void RELEASE(OBJECT *&obj)
102 {
103  if(obj != NULL)
104  {
105  obj->release();
106  obj = NULL;
107  }
108 }
109 
157 template<typename TYPE>
158 class Scoped
159 {
160 public:
162  ~Scoped(void) { RELEASE(m_object); }
163 
165  Scoped(void) : m_object(TYPE::create()) {}
166 
175  Scoped(TYPE *object) : m_object(object) {}
176 
182  Scoped &operator=(TYPE *object)
183  {
184  if(object != m_object)
185  {
186  RELEASE(m_object);
187  m_object = RETAIN(object);
188  }
189  return *this;
190  }
191 
197  Scoped(const Scoped &object) : m_object(RETAIN(object.m_object)) {}
198 
204  Scoped &operator=(const Scoped &object)
205  {
206  if(m_object != object.m_object)
207  {
208  RELEASE(m_object);
209  m_object = RETAIN(object.m_object);
210  }
211  return *this;
212  }
213 
215  TYPE *operator->(void) { return m_object; }
217  TYPE &operator*(void) { return *m_object; }
218 #ifndef SWIG
219 
220  operator TYPE *&(void) { return m_object; }
221 #endif
222 
223 private:
224  TYPE *m_object;
225 };
226 
227 LT_END_LIDAR_NAMESPACE
228 #endif // __LIDAR_OBJECT_H__
~Scoped(void)
Releases the object when Scoped&lt;&gt; goes out of scope.
Definition: Object.h:162
void retain(void) const
Increment the reference count by one.
TYPE * operator->(void)
Make Scoped behave like a pointer to TYPE.
Definition: Object.h:215
Scoped(const Scoped &object)
Copy constructor.
Definition: Object.h:197
Scoped(TYPE *object)
Manage an existing object.
Definition: Object.h:175
Object is the base class for implementing reference counting.
Definition: Object.h:34
static OBJECT * RETAIN(OBJECT *obj)
Helper function for calling Object::retain().
Definition: Object.h:90
Scoped is a wrapper class around Object that gives it block scoping.
Definition: Object.h:158
static void RELEASE(OBJECT *&obj)
Helper function for calling Object::release().
Definition: Object.h:101
TYPE & operator*(void)
Make Scoped behave like a pointer to TYPE.
Definition: Object.h:217
Scoped(void)
Create an object on the heap.
Definition: Object.h:165
Scoped & operator=(TYPE *object)
Assignment operator.
Definition: Object.h:182
Scoped & operator=(const Scoped &object)
Assignment operator.
Definition: Object.h:204
void release(void) const
Decrement the reference count by one.
#define ABSTRACT_OBJECT(classname)
Macros for defining boilerplate parts of derived Object classes.
Definition: Object.h:45

LizardTech