]> git.cworth.org Git - vogl/blob - src/extlib/loki/include/loki/OrderedStatic.h
Initial vogl checkin
[vogl] / src / extlib / loki / include / loki / OrderedStatic.h
1 ////////////////////////////////////////////////////////////////////////////////
2 // The Loki Library
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_
14
15 // $Id: OrderedStatic.h 751 2006-10-17 19:50:37Z syntheticpp $
16
17
18 #include <vector>
19 #include <iostream>
20
21 #include "LokiExport.h"
22 #include "Singleton.h"
23 #include "Typelist.h"
24 #include "Sequence.h"
25
26 // usage: see test/OrderedStatic
27
28 namespace Loki
29 {
30 namespace Private
31 {
32 ////////////////////////////////////////////////////////////////////////////////
33 // polymorph base class for OrderedStatic template,
34 // necessary because of the creator
35 ////////////////////////////////////////////////////////////////////////////////
36 class LOKI_EXPORT OrderedStaticCreatorFunc
37 {
38 public:
39         virtual void createObject() = 0;
40
41 protected:
42         OrderedStaticCreatorFunc();
43         virtual ~OrderedStaticCreatorFunc();
44
45 private:
46         OrderedStaticCreatorFunc(const OrderedStaticCreatorFunc &);
47 };
48
49 ////////////////////////////////////////////////////////////////////////////////
50 // template base clase for OrderedStatic template,
51 // common for all specializations
52 ////////////////////////////////////////////////////////////////////////////////
53 template<class T>
54 class OrderedStaticBase : public OrderedStaticCreatorFunc
55 {
56 public:
57         T &operator*()
58         {
59                 return *val_;
60         }
61
62         T *operator->()
63         {
64                 return val_;
65         }
66
67 protected:
68
69         OrderedStaticBase(unsigned int longevity) :  val_(0), longevity_(longevity)
70         {
71         }
72
73         virtual ~OrderedStaticBase()
74         {
75         }
76
77         void SetLongevity(T *ptr)
78         {
79                 val_=ptr;
80                 Loki::SetLongevity(val_,longevity_);
81         }
82
83 private:
84         OrderedStaticBase();
85         OrderedStaticBase(const OrderedStaticBase &);
86         OrderedStaticBase &operator=(const OrderedStaticBase &);
87         T *val_;
88         unsigned int longevity_;
89
90 };
91
92 ////////////////////////////////////////////////////////////////////////////////
93 // OrderedStaticManagerClass implements details
94 // OrderedStaticManager is then defined as a Singleton
95 ////////////////////////////////////////////////////////////////////////////////
96 class LOKI_EXPORT OrderedStaticManagerClass
97 {
98 public:
99         OrderedStaticManagerClass();
100         virtual ~OrderedStaticManagerClass();
101
102         typedef void (OrderedStaticCreatorFunc::*Creator)();
103
104         void createObjects();
105         void registerObject(unsigned int longevity,OrderedStaticCreatorFunc *,Creator);
106
107 private:
108         OrderedStaticManagerClass(const OrderedStaticManagerClass &);
109         OrderedStaticManagerClass &operator=(const OrderedStaticManagerClass &);
110
111         struct Data
112         {
113                 Data(unsigned int,OrderedStaticCreatorFunc *, Creator);
114                 unsigned int longevity;
115                 OrderedStaticCreatorFunc *object;
116                 Creator creator;
117         };
118
119         std::vector<Data> staticObjects_;
120         unsigned int max_longevity_;
121         unsigned int min_longevity_;
122 };
123
124 }// namespace Private
125
126 ////////////////////////////////////////////////////////////////////////////////
127 // OrderedStaticManager is only a Singleton typedef
128 ////////////////////////////////////////////////////////////////////////////////
129
130 typedef Loki::SingletonHolder
131 <
132 Loki::Private::OrderedStaticManagerClass,
133      Loki::CreateUsingNew,
134      Loki::NoDestroy,
135      Loki::SingleThreaded
136      >
137      OrderedStaticManager;
138
139 ////////////////////////////////////////////////////////////////////////////////
140 // template OrderedStatic template:
141 // L        : longevity
142 // T        : object type
143 // TList    : creator parameters
144 ////////////////////////////////////////////////////////////////////////////////
145
146 template<unsigned int L, class T, class TList = Loki::NullType>
147 class OrderedStatic;
148
149
150 ////////////////////////////////////////////////////////////////////////////////
151 // OrderedStatic specializations
152 ////////////////////////////////////////////////////////////////////////////////
153
154 template<unsigned int L, class T>
155 class OrderedStatic<L, T, Loki::NullType> : public Private::OrderedStaticBase<T>
156 {
157 public:
158         OrderedStatic() : Private::OrderedStaticBase<T>(L)
159         {
160                 OrderedStaticManager::Instance().registerObject
161                 (L,this,&Private::OrderedStaticCreatorFunc::createObject);
162         }
163
164         void createObject()
165         {
166                 Private::OrderedStaticBase<T>::SetLongevity(new T);
167         }
168
169 private:
170         OrderedStatic(const OrderedStatic &);
171         OrderedStatic &operator=(const OrderedStatic &);
172 };
173
174 template<unsigned int L, class T, typename P1>
175 class OrderedStatic<L, T, Loki::Seq<P1> > : public Private::OrderedStaticBase<T>
176 {
177 public:
178         OrderedStatic(P1 p) : Private::OrderedStaticBase<T>(L), para_(p)
179         {
180                 OrderedStaticManager::Instance().registerObject
181                 (L,this,&Private::OrderedStaticCreatorFunc::createObject);
182         }
183
184         void createObject()
185         {
186                 Private::OrderedStaticBase<T>::SetLongevity(new T(para_));
187         }
188
189 private:
190         OrderedStatic();
191         OrderedStatic(const OrderedStatic &);
192         OrderedStatic &operator=(const OrderedStatic &);
193         P1 para_;
194 };
195
196 template<unsigned int L, class T, typename P1>
197 class OrderedStatic<L, T,  P1( *)() > : public Private::OrderedStaticBase<T>
198 {
199 public:
200
201         typedef P1(*Func)();
202
203         OrderedStatic(Func p) : Private::OrderedStaticBase<T>(L), para_(p)
204         {
205                 OrderedStaticManager::Instance().registerObject
206                 (L,this,&Private::OrderedStaticCreatorFunc::createObject);
207         }
208
209         void createObject()
210         {
211                 Private::OrderedStaticBase<T>::SetLongevity(new T(para_()));
212         }
213
214 private:
215         OrderedStatic();
216         OrderedStatic(const OrderedStatic &);
217         OrderedStatic &operator=(const OrderedStatic &);
218         Func para_;
219 };
220
221 }// namespace Loki
222
223
224 #endif // end file guardian
225