MrSID Decode SDK for LiDAR Reference Manual  1.1.3.4427
Object.h
Go to the documentation of this file.
00001 /* //////////////////////////////////////////////////////////////////////////
00002 //                                                                         //
00003 // This code is Copyright (c) 2008-2010 LizardTech, Inc, 1008 Western      //
00004 // Avenue, Suite 200, Seattle, WA 98104.  Unauthorized use or distribution //
00005 // prohibited.  Access to and use of this code is permitted only under     //
00006 // license from LizardTech, Inc.  Portions of the code are protected by    //
00007 // US and foreign patents and other filings. All Rights Reserved.          //
00008 //                                                                         //
00010 /* PUBLIC */
00011 
00012 #ifndef __LIDAR_OBJECT_H__
00013 #define __LIDAR_OBJECT_H__
00014 
00015 #include "lidar/Atomic.h"
00016 
00017 LT_BEGIN_LIDAR_NAMESPACE
00018 
00034 class Object
00035 {
00045 #define ABSTRACT_OBJECT(classname) \
00046    DISABLE_COPY(classname); \
00047    protected: \
00048       classname(void); \
00049       virtual ~classname(void)
00050    
00055 #define CONCRETE_OBJECT(classname) \
00056    ABSTRACT_OBJECT(classname); \
00057    public: \
00058       static classname *create(void)
00059 
00060 #define IMPLEMENT_OBJECT_CREATE(classname) \
00061    classname * classname::create(void) { return new classname; }
00062    
00063    ABSTRACT_OBJECT(Object);
00064 public:
00068    void retain(void) const;
00069 
00074    void release(void) const;
00075 
00076 protected:
00078    static void *operator new(size_t size);
00080    static void operator delete(void *ptr);
00081 
00082 private:
00083    mutable AtomicInt m_refCount;
00084 };
00085 
00090 template<typename OBJECT> static inline OBJECT *RETAIN(OBJECT *obj)
00091 {
00092    if(obj != NULL)
00093       obj->retain();
00094    return obj;
00095 }
00096 
00101 template<typename OBJECT> static inline void RELEASE(OBJECT *&obj)
00102 {
00103    if(obj != NULL)
00104    {
00105       obj->release();
00106       obj = NULL;
00107    }
00108 }
00109 
00157 template<typename TYPE>
00158 class Scoped
00159 {
00160 public:
00162    ~Scoped(void) { RELEASE(m_object); }
00163 
00165    Scoped(void) : m_object(TYPE::create()) {}
00166 
00175    Scoped(TYPE *object) : m_object(object) {}
00176 
00182    Scoped &operator=(TYPE *object)
00183    {
00184       if(object != m_object)
00185       {
00186          RELEASE(m_object);
00187          m_object = RETAIN(object);
00188       }
00189       return *this;
00190    }
00191 
00197    Scoped(const Scoped &object) : m_object(RETAIN(object.m_object)) {}
00198 
00204    Scoped &operator=(const Scoped &object)
00205    {
00206       if(m_object != object.m_object)
00207       {
00208          RELEASE(m_object);
00209          m_object = RETAIN(object.m_object);
00210       }
00211       return *this;
00212    }
00213 
00215    TYPE *operator->(void) { return m_object; }
00217    TYPE &operator*(void) { return *m_object; }
00218 #ifndef SWIG
00219 
00220    operator TYPE *&(void) { return m_object; }
00221 #endif
00222 
00223 private:
00224    TYPE *m_object;
00225 };
00226 
00227 LT_END_LIDAR_NAMESPACE
00228 #endif // __LIDAR_OBJECT_H__