00001 #ifndef Factory_H
00002 #define Factory_H
00003
00004 #include "Sti/Base/Named.h"
00005 #include <cassert>
00006
00016 class BFactory : public Named
00017 {
00018 protected:
00019
00020 BFactory(const string& name);
00021
00022 virtual ~BFactory()
00023 {;}
00024
00025 public:
00027 virtual void clear()=0;
00028
00030 virtual void reset()=0;
00031
00033 virtual void free(void *obj)=0;
00034
00036 static void Free(void *obj);
00037
00038 void setFastDelete() {fFastDel=1;}
00039 void setMaxIncrementCount(int maxCount) {fMaxCount=maxCount;}
00040 int getMaxIncrementCount() const {return fMaxCount; }
00041 int getCurrentSize() const {return fCurCount; }
00042 int getCurrentCount() const {return fCurCount; }
00043 protected:
00044 int fMaxCount;
00045 int fCurCount;
00046 int fUseCount;
00047 int fFastDel;
00048 int fInstCount;
00049 int fFreeCount;
00050 static double fgTotal;
00051 };
00052
00053 template <class Abstract>
00054 class Factory : public BFactory
00055 {
00056 public:
00057
00058 Factory(const string& name): BFactory(name)
00059 {;}
00060
00061 virtual ~Factory()
00062 {;}
00063
00065 virtual void free(Abstract *obj)=0;
00066 virtual void free(void *obj)=0;
00067
00069 virtual Abstract *getInstance()=0;
00070
00071 };
00072
00073 inline void BFactory::Free(void *obj)
00074 {
00075 long *v = ((long*)obj) - 1;
00076 if (!*v) v--;
00077 assert((*v)&1L);
00078 BFactory *f = (BFactory*)((*v)-1);
00079 f->free(obj);
00080 }
00081
00082 #endif