1 ////////////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2005 Peter Kümmel
4 // Permission to use, copy, modify, distribute and sell this software for any
5 // purpose is hereby granted without fee, provided that the above copyright
6 // notice appear in all copies and that both that copyright notice and this
7 // permission notice appear in supporting documentation.
8 // The author makes no representations about the
9 // suitability of this software for any purpose. It is provided "as is"
10 // without express or implied warranty.
11 ////////////////////////////////////////////////////////////////////////////////
12 #ifndef LOKI_ORDEREDSTATIC_INC_
13 #define LOKI_ORDEREDSTATIC_INC_
15 // $Id: OrderedStatic.h 751 2006-10-17 19:50:37Z syntheticpp $
21 #include "LokiExport.h"
22 #include "Singleton.h"
26 // usage: see test/OrderedStatic
32 ////////////////////////////////////////////////////////////////////////////////
33 // polymorph base class for OrderedStatic template,
34 // necessary because of the creator
35 ////////////////////////////////////////////////////////////////////////////////
36 class LOKI_EXPORT OrderedStaticCreatorFunc
39 virtual void createObject() = 0;
42 OrderedStaticCreatorFunc();
43 virtual ~OrderedStaticCreatorFunc();
46 OrderedStaticCreatorFunc(const OrderedStaticCreatorFunc &);
49 ////////////////////////////////////////////////////////////////////////////////
50 // template base clase for OrderedStatic template,
51 // common for all specializations
52 ////////////////////////////////////////////////////////////////////////////////
54 class OrderedStaticBase : public OrderedStaticCreatorFunc
69 OrderedStaticBase(unsigned int longevity) : val_(0), longevity_(longevity)
73 virtual ~OrderedStaticBase()
77 void SetLongevity(T *ptr)
80 Loki::SetLongevity(val_,longevity_);
85 OrderedStaticBase(const OrderedStaticBase &);
86 OrderedStaticBase &operator=(const OrderedStaticBase &);
88 unsigned int longevity_;
92 ////////////////////////////////////////////////////////////////////////////////
93 // OrderedStaticManagerClass implements details
94 // OrderedStaticManager is then defined as a Singleton
95 ////////////////////////////////////////////////////////////////////////////////
96 class LOKI_EXPORT OrderedStaticManagerClass
99 OrderedStaticManagerClass();
100 virtual ~OrderedStaticManagerClass();
102 typedef void (OrderedStaticCreatorFunc::*Creator)();
104 void createObjects();
105 void registerObject(unsigned int longevity,OrderedStaticCreatorFunc *,Creator);
108 OrderedStaticManagerClass(const OrderedStaticManagerClass &);
109 OrderedStaticManagerClass &operator=(const OrderedStaticManagerClass &);
113 Data(unsigned int,OrderedStaticCreatorFunc *, Creator);
114 unsigned int longevity;
115 OrderedStaticCreatorFunc *object;
119 std::vector<Data> staticObjects_;
120 unsigned int max_longevity_;
121 unsigned int min_longevity_;
124 }// namespace Private
126 ////////////////////////////////////////////////////////////////////////////////
127 // OrderedStaticManager is only a Singleton typedef
128 ////////////////////////////////////////////////////////////////////////////////
130 typedef Loki::SingletonHolder
132 Loki::Private::OrderedStaticManagerClass,
133 Loki::CreateUsingNew,
137 OrderedStaticManager;
139 ////////////////////////////////////////////////////////////////////////////////
140 // template OrderedStatic template:
143 // TList : creator parameters
144 ////////////////////////////////////////////////////////////////////////////////
146 template<unsigned int L, class T, class TList = Loki::NullType>
150 ////////////////////////////////////////////////////////////////////////////////
151 // OrderedStatic specializations
152 ////////////////////////////////////////////////////////////////////////////////
154 template<unsigned int L, class T>
155 class OrderedStatic<L, T, Loki::NullType> : public Private::OrderedStaticBase<T>
158 OrderedStatic() : Private::OrderedStaticBase<T>(L)
160 OrderedStaticManager::Instance().registerObject
161 (L,this,&Private::OrderedStaticCreatorFunc::createObject);
166 Private::OrderedStaticBase<T>::SetLongevity(new T);
170 OrderedStatic(const OrderedStatic &);
171 OrderedStatic &operator=(const OrderedStatic &);
174 template<unsigned int L, class T, typename P1>
175 class OrderedStatic<L, T, Loki::Seq<P1> > : public Private::OrderedStaticBase<T>
178 OrderedStatic(P1 p) : Private::OrderedStaticBase<T>(L), para_(p)
180 OrderedStaticManager::Instance().registerObject
181 (L,this,&Private::OrderedStaticCreatorFunc::createObject);
186 Private::OrderedStaticBase<T>::SetLongevity(new T(para_));
191 OrderedStatic(const OrderedStatic &);
192 OrderedStatic &operator=(const OrderedStatic &);
196 template<unsigned int L, class T, typename P1>
197 class OrderedStatic<L, T, P1( *)() > : public Private::OrderedStaticBase<T>
203 OrderedStatic(Func p) : Private::OrderedStaticBase<T>(L), para_(p)
205 OrderedStaticManager::Instance().registerObject
206 (L,this,&Private::OrderedStaticCreatorFunc::createObject);
211 Private::OrderedStaticBase<T>::SetLongevity(new T(para_()));
216 OrderedStatic(const OrderedStatic &);
217 OrderedStatic &operator=(const OrderedStatic &);
224 #endif // end file guardian