Drizzled Public API Documentation

CSObject.h

00001 /* Copyright (C) 2008 PrimeBase Technologies GmbH, Germany
00002  *
00003  * PrimeBase Media Stream for MySQL
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00018  *
00019  * Original author: Paul McCullagh (H&G2JCtL)
00020  * Continued development: Barry Leslie
00021  *
00022  * 2007-05-20
00023  *
00024  *
00025  */
00026 
00027 #pragma once
00028 #ifndef __CSOBJECT_H__
00029 #define __CSOBJECT_H__
00030 
00031 #include "CSDefs.h"
00032 #include "CSMemory.h"
00033 #include "CSMutex.h"
00034 
00035 // The RETAIN() macro is useful when passing object references to functions
00036 // that you want to retain. For example istead of:
00037 // x->retain();
00038 // foo(x);
00039 // You can do:
00040 // foo(RETAIN(x));
00041 //
00042 // After 20 years of programming I have finally found a use for the 'C' comma
00043 // operator out side of a 'for' loop. :)
00044 #define RETAIN(x) ((x)->retain(),(x))
00045 
00046 // A back reference is a reference stored by an object pointing to it's owner.
00047 // Since the owner will free all of it's object when it is released the  object
00048 // itself cannot hold a reference to its owner.
00049 // Use this macro to make it clear that this is what you intended. 
00050 #define BACK_REFERENCE(x) (x)
00051 
00052 class CSObject {
00053 public:
00054   CSObject() { }
00055   virtual ~CSObject() { finalize(); }
00056 
00057 #ifdef DEBUG
00058   virtual void retain(const char *func, const char *file, uint32_t line);
00059   virtual void release(const char *func, const char *file, uint32_t line);
00060 #else
00061   virtual void retain();
00062   virtual void release();
00063 #endif
00064 
00065   /*
00066    * All objects are sortable, hashable and linkable,
00067    * as long as these methods are implemented:
00068    */
00069   virtual void finalize() { }
00070   virtual CSObject *getKey();
00071   virtual int compareKey(CSObject *);
00072   virtual uint32_t hashKey();
00073   virtual CSObject *getHashLink();
00074   virtual void setHashLink(CSObject *);
00075   virtual CSObject *getNextLink();
00076   virtual CSObject *getPrevLink();
00077   virtual void setNextLink(CSObject *);
00078   virtual void setPrevLink(CSObject *);
00079 
00080 #ifdef DEBUG
00081   static void *operator new(size_t size, const char *func, const char *file, uint32_t line) {
00082     return cs_mm_malloc(func, file, line, size);
00083   }
00084 
00085   static void operator delete(void *ptr, size_t size) {
00086     UNUSED(size);
00087     cs_mm_free(ptr);
00088   }
00089 
00090 #endif
00091 };
00092 
00093 class CSStaticObject : public CSObject {
00094 #ifdef DEBUG
00095   virtual void retain(const char *func, const char *file, uint32_t line);
00096   virtual void release(const char *func, const char *file, uint32_t line);
00097 
00098   int   iTrackMe;
00099 #else
00100   virtual void retain();
00101   virtual void release();
00102 #endif
00103 };
00104 
00105 class CSRefObject : public CSObject {
00106 public:
00107   CSRefObject();
00108   virtual ~CSRefObject();
00109 
00110 #ifdef DEBUG
00111   virtual void startTracking();
00112   virtual void retain(const char *func, const char *file, uint32_t line);
00113   virtual void release(const char *func, const char *file, uint32_t line);
00114 
00115   int   iTrackMe;
00116 #else
00117   virtual void retain();
00118   virtual void release();
00119 #endif
00120 
00121   uint32_t getRefCount() { return iRefCount;}
00122 
00123 private:
00124   uint32_t  iRefCount;
00125 };
00126 
00127 class CSSharedRefObject : public CSObject, public CSSync {
00128 public:
00129   CSSharedRefObject();
00130   virtual ~CSSharedRefObject();
00131 
00132 #ifdef DEBUG
00133   virtual void startTracking();
00134   virtual void retain(const char *func, const char *file, uint32_t line);
00135   virtual void release(const char *func, const char *file, uint32_t line);
00136 
00137   int   iTrackMe;
00138 #else
00139   virtual void retain();
00140   virtual void release();
00141 #endif
00142 
00143   uint32_t getRefCount() { return iRefCount;}
00144 
00145 private:
00146   uint32_t  iRefCount;
00147 };
00148 
00149 #ifdef DEBUG
00150 #define new                    new(__FUNC__, __FILE__, __LINE__)
00151 
00152 #define retain()  retain(__FUNC__, __FILE__, __LINE__)
00153 #define release() release(__FUNC__, __FILE__, __LINE__)
00154 #endif
00155 
00156 class CSPooled {
00157 public:
00158   virtual ~CSPooled() {}
00159   virtual void returnToPool() = 0;
00160 };
00161 
00162 #endif