OrderedStatic.h

00001 
00002 // The Loki Library
00003 // Copyright (c) 2005 Peter Kümmel
00004 // Permission to use, copy, modify, distribute and sell this software for any 
00005 //     purpose is hereby granted without fee, provided that the above copyright 
00006 //     notice appear in all copies and that both that copyright notice and this 
00007 //     permission notice appear in supporting documentation.
00008 // The author makes no representations about the 
00009 //     suitability of this software for any purpose. It is provided "as is" 
00010 //     without express or implied warranty.
00012 #ifndef LOKI_ORDEREDSTATIC_INC_
00013 #define LOKI_ORDEREDSTATIC_INC_
00014 
00015 // $Id: OrderedStatic.h 751 2006-10-17 19:50:37Z syntheticpp $
00016 
00017 
00018 #include <vector>
00019 #include <iostream>
00020 
00021 #include "LokiExport.h"
00022 #include "Singleton.h"
00023 #include "Typelist.h"
00024 #include "Sequence.h"
00025 
00026 // usage: see test/OrderedStatic
00027 
00028 namespace Loki
00029 {
00030     namespace Private
00031     {
00033         // polymorph base class for OrderedStatic template,
00034         // necessary because of the creator
00036         class LOKI_EXPORT OrderedStaticCreatorFunc
00037         {
00038         public:
00039             virtual void createObject() = 0;
00040         
00041         protected:
00042             OrderedStaticCreatorFunc();
00043             virtual ~OrderedStaticCreatorFunc();
00044         
00045         private:
00046             OrderedStaticCreatorFunc(const OrderedStaticCreatorFunc&);
00047         };
00048 
00050         // template base clase for OrderedStatic template, 
00051         // common for all specializations
00053         template<class T>
00054         class OrderedStaticBase : public OrderedStaticCreatorFunc
00055         {
00056         public:
00057             T& operator*()
00058             {
00059                 return *val_;
00060             }
00061 
00062             T* operator->()
00063             {
00064                 return val_;
00065             }
00066 
00067         protected:
00068 
00069             OrderedStaticBase(unsigned int longevity) :  val_(0), longevity_(longevity)
00070             {
00071             }
00072             
00073             virtual ~OrderedStaticBase()
00074             {
00075             }
00076             
00077             void SetLongevity(T* ptr)
00078             {
00079                 val_=ptr;
00080                 Loki::SetLongevity(val_,longevity_);
00081             }
00082 
00083         private:
00084             OrderedStaticBase();
00085             OrderedStaticBase(const OrderedStaticBase&);
00086             OrderedStaticBase& operator=(const OrderedStaticBase&);
00087             T* val_;
00088             unsigned int longevity_;
00089             
00090         };
00091 
00093         // OrderedStaticManagerClass implements details 
00094         // OrderedStaticManager is then defined as a Singleton
00096         class LOKI_EXPORT OrderedStaticManagerClass
00097         {
00098         public:
00099             OrderedStaticManagerClass();
00100             virtual ~OrderedStaticManagerClass();
00101 
00102             typedef void (OrderedStaticCreatorFunc::*Creator)();
00103 
00104             void createObjects();
00105             void registerObject(unsigned int longevity,OrderedStaticCreatorFunc*,Creator);
00106 
00107         private:
00108             OrderedStaticManagerClass(const OrderedStaticManagerClass&);
00109             OrderedStaticManagerClass& operator=(const OrderedStaticManagerClass&);
00110             
00111             struct Data
00112             {
00113                 Data(unsigned int,OrderedStaticCreatorFunc*, Creator);
00114                 unsigned int longevity;
00115                 OrderedStaticCreatorFunc* object;
00116                 Creator creator;
00117             };
00118 
00119             std::vector<Data> staticObjects_;
00120             unsigned int max_longevity_;
00121             unsigned int min_longevity_;
00122         };
00123 
00124     }// namespace Private
00125 
00127     // OrderedStaticManager is only a Singleton typedef
00129 
00130     typedef Loki::SingletonHolder
00131     <
00132         Loki::Private::OrderedStaticManagerClass, 
00133         Loki::CreateUsingNew,
00134         Loki::NoDestroy,
00135         Loki::SingleThreaded
00136     >
00137     OrderedStaticManager;
00138 
00140     // template OrderedStatic template: 
00141     // L        : longevity
00142     // T        : object type
00143     // TList    : creator parameters
00145 
00146     template<unsigned int L, class T, class TList = Loki::NullType>
00147     class OrderedStatic;
00148 
00149 
00151     // OrderedStatic specializations
00153 
00154     template<unsigned int L, class T>
00155     class OrderedStatic<L, T, Loki::NullType> : public Private::OrderedStaticBase<T>
00156     {
00157     public:    
00158         OrderedStatic() : Private::OrderedStaticBase<T>(L)
00159         {
00160             OrderedStaticManager::Instance().registerObject
00161                                 (L,this,&Private::OrderedStaticCreatorFunc::createObject);
00162         }
00163 
00164         void createObject()
00165         {
00166             Private::OrderedStaticBase<T>::SetLongevity(new T);
00167         }
00168 
00169     private:
00170         OrderedStatic(const OrderedStatic&);
00171         OrderedStatic& operator=(const OrderedStatic&);
00172     };
00173 
00174     template<unsigned int L, class T, typename P1>
00175     class OrderedStatic<L, T, Loki::Seq<P1> > : public Private::OrderedStaticBase<T>
00176     {
00177     public:
00178         OrderedStatic(P1 p) : Private::OrderedStaticBase<T>(L), para_(p)
00179         {
00180             OrderedStaticManager::Instance().registerObject
00181                                 (L,this,&Private::OrderedStaticCreatorFunc::createObject);
00182         }
00183         
00184         void createObject()
00185         {
00186             Private::OrderedStaticBase<T>::SetLongevity(new T(para_));
00187         }
00188 
00189     private:
00190         OrderedStatic();
00191         OrderedStatic(const OrderedStatic&);
00192         OrderedStatic& operator=(const OrderedStatic&);
00193         P1 para_;
00194     };
00195 
00196     template<unsigned int L, class T, typename P1>
00197     class OrderedStatic<L, T,  P1(*)() > : public Private::OrderedStaticBase<T>
00198     {
00199     public:
00200 
00201         typedef P1(*Func)();
00202 
00203         OrderedStatic(Func p) : Private::OrderedStaticBase<T>(L), para_(p)
00204         {
00205             OrderedStaticManager::Instance().registerObject
00206                                 (L,this,&Private::OrderedStaticCreatorFunc::createObject);
00207         }
00208 
00209         void createObject()
00210         {
00211             Private::OrderedStaticBase<T>::SetLongevity(new T(para_()));
00212         }
00213 
00214     private:
00215         OrderedStatic();
00216         OrderedStatic(const OrderedStatic&);
00217         OrderedStatic& operator=(const OrderedStatic&);
00218         Func para_;
00219     };
00220 
00221 }// namespace Loki
00222 
00223 
00224 #endif // end file guardian
00225 

Generated on Sun Feb 25 16:52:26 2007 for Loki by  doxygen 1.5.1-p1