trunk 20080912
[gitenigma.git] / include / lib / system / init.h
blob465bac40a5ae5db77e7c4e1c287b3c87fb07416a
1 #ifndef __init_h
2 #define __init_h
4 #include <list>
5 #include <utility>
7 class eAutoInit;
9 class eInit
11 static std::list<std::pair<int,eAutoInit*> > *cl;
12 friend class eAutoInit;
13 static int rl;
14 public:
15 eInit();
16 ~eInit();
17 static void setRunlevel(int rlev);
18 static void add(int trl, eAutoInit *c);
19 static void remove(int trl, eAutoInit *c);
22 class eAutoInit
24 friend class eInit;
25 virtual void initNow()=0;
26 virtual void closeNow()=0;
27 protected:
28 int rl;
29 char *description;
30 public:
31 eAutoInit(int rl, char *description): rl(rl), description(description)
34 virtual ~eAutoInit();
35 const char *getDescription() const { return description; };
38 template<class T1, class T2> class
39 eAutoInitP1: protected eAutoInit
41 T1 *t;
42 const T2 &arg;
43 void initNow()
45 t=new T1(arg);
47 void closeNow()
49 delete t;
51 public:
52 operator T1*()
54 return t;
56 eAutoInitP1(const T2 &arg, int runl, char *description): eAutoInit(runl, description), arg(arg)
58 eInit::add(rl, this);
60 ~eAutoInitP1()
62 eInit::remove(rl, this);
66 template<class T1> class
67 eAutoInitP0: protected eAutoInit
69 T1 *t;
70 void initNow()
72 t=new T1();
74 void closeNow()
76 delete t;
78 public:
79 operator T1*()
81 return t;
83 T1 *operator->()
85 return t;
87 eAutoInitP0(int runl, char *description): eAutoInit(runl, description)
89 eInit::add(rl, this);
91 ~eAutoInitP0()
93 eInit::remove(rl, this);
97 #endif