MrSID Decode SDK for LiDAR Reference Manual  1.1.5.5244
Object.h
Go to the documentation of this file.
1 //
2 // This code is property of Celartem, Inc dba Extensis.
3 // Copyright (c) 1995-2023. All rights reserved.
4 // Use permitted only under license from Extensis.
5 //
6 /* PUBLIC */
7 
8 #ifndef __LIDAR_OBJECT_H__
9 #define __LIDAR_OBJECT_H__
10 
11 #include "lidar/Atomic.h"
12 
13 LT_BEGIN_LIDAR_NAMESPACE
14 
30 class Object
31 {
41 #define ABSTRACT_OBJECT(classname) \
42  DISABLE_COPY(classname); \
43  protected: \
44  classname(void); \
45  virtual ~classname(void)
46 
51 #define CONCRETE_OBJECT(classname) \
52  ABSTRACT_OBJECT(classname); \
53  public: \
54  static classname *create(void)
55 
56 #define IMPLEMENT_OBJECT_CREATE(classname) \
57  classname * classname::create(void) { return new classname; }
58 
60 public:
64  int retain(void) const;
65 
70  int release(void) const;
71 
72 private:
73  mutable AtomicInt m_refCount;
74 };
75 
80 template<typename OBJECT> static inline OBJECT *RETAIN(OBJECT *obj)
81 {
82  if(obj != nullptr)
83  obj->retain();
84  return obj;
85 }
86 
91 template<typename OBJECT> static inline void RELEASE(OBJECT *&obj)
92 {
93  if(obj != nullptr)
94  {
95  obj->release();
96  obj = nullptr;
97  }
98 }
99 
147 template<typename TYPE>
148 class Scoped
149 {
150 public:
152  ~Scoped(void) { RELEASE(m_object); }
153 
155  Scoped(void) : m_object(TYPE::create()) {}
156 
165  Scoped(TYPE *object) : m_object(object) {}
166 
172  Scoped &operator=(TYPE *object)
173  {
174  if(object != m_object)
175  {
176  RELEASE(m_object);
177  m_object = RETAIN(object);
178  }
179  return *this;
180  }
181  Scoped &operator=(const TYPE *object)
182  {
183  if(object != m_object)
184  {
185  RELEASE(m_object);
186  m_object = RETAIN(const_cast<TYPE *>(object));
187  }
188  return *this;
189  }
190 
196  Scoped(const Scoped &object) : m_object(RETAIN(object.m_object)) {}
197 
203  Scoped &operator=(const Scoped &object)
204  {
205  if(m_object != object.m_object)
206  {
207  RELEASE(m_object);
208  m_object = RETAIN(object.m_object);
209  }
210  return *this;
211  }
212 
214  TYPE *operator->(void) { return m_object; }
215  const TYPE *operator->(void) const { return m_object; }
217  TYPE &operator*(void) { return *m_object; }
218  const TYPE &operator*(void) const { return *m_object; }
219 #ifndef SWIG
221  operator TYPE *&(void) { return m_object; }
222  operator const TYPE *&(void) const { return const_cast<const TYPE *&>(m_object); }
223 #endif
224 
225 private:
226  TYPE *m_object;
227 };
228 
229 template<typename OBJECT> static inline OBJECT *RETAIN(Scoped<OBJECT> &obj)
230 {
231  if(obj != nullptr)
232  obj->retain();
233  return obj;
234 }
235 
236 LT_END_LIDAR_NAMESPACE
237 #endif // __LIDAR_OBJECT_H__
#define ABSTRACT_OBJECT(classname)
Macros for defining boilerplate parts of derived Object classes.
Definition: Object.h:41
static void RELEASE(OBJECT *&obj)
Helper function for calling Object::release().
Definition: Object.h:91
static OBJECT * RETAIN(OBJECT *obj)
Helper function for calling Object::retain().
Definition: Object.h:80
Object is the base class for implementing reference counting.
Definition: Object.h:31
int retain(void) const
Increment the reference count by one.
int release(void) const
Decrement the reference count by one.
Scoped is a wrapper class around Object that gives it block scoping.
Definition: Object.h:149
const TYPE * operator->(void) const
Definition: Object.h:215
TYPE & operator*(void)
Make Scoped behave like a pointer to TYPE.
Definition: Object.h:217
Scoped & operator=(const TYPE *object)
Definition: Object.h:181
Scoped(TYPE *object)
Manage an existing object.
Definition: Object.h:165
const TYPE & operator*(void) const
Definition: Object.h:218
Scoped & operator=(const Scoped &object)
Assignment operator.
Definition: Object.h:203
~Scoped(void)
Releases the object when Scoped<> goes out of scope.
Definition: Object.h:152
Scoped(const Scoped &object)
Copy constructor.
Definition: Object.h:196
Scoped(void)
Create an object on the heap.
Definition: Object.h:155
TYPE * operator->(void)
Make Scoped behave like a pointer to TYPE.
Definition: Object.h:214
Scoped & operator=(TYPE *object)
Assignment operator.
Definition: Object.h:172

Extensis