import of enigma2
[vuplus_dvbapp] / lib / base / init.h
1 #ifndef __init_h
2 #define __init_h
3
4 #include <list>
5 #include <utility>
6
7 class eAutoInit;
8
9 class eInit
10 {
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);
20 };
21
22 class eAutoInit
23 {
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)
32         {
33         }
34         virtual ~eAutoInit();
35         const char *getDescription() const { return description; };
36 };
37
38 template<class T1, class T2> class
39 eAutoInitP1: protected eAutoInit
40 {
41         T1 *t;
42         const T2 &arg;
43         void initNow()
44         {
45                 t=new T1(arg);
46         }
47         void closeNow()
48         {
49                 delete t;
50         }
51 public:
52         operator T1*()
53         {
54                 return t;
55         }
56         eAutoInitP1(const T2 &arg, int runl, char *description): eAutoInit(runl, description), arg(arg)
57         {
58                 eInit::add(rl, this);
59         }
60         ~eAutoInitP1()
61         {
62                 eInit::remove(rl, this);
63         }
64 };
65
66 template<class T1> class
67 eAutoInitP0: protected eAutoInit
68 {
69         T1 *t;
70         void initNow()
71         {
72                 t=new T1();
73         }
74         void closeNow()
75         {
76                 delete t;
77         }
78 public:
79         operator T1*()
80         {
81                 return t;
82         }
83         T1 *operator->()
84         {
85                 return t;
86         }
87         eAutoInitP0(int runl, char *description): eAutoInit(runl, description)
88         {
89                 eInit::add(rl, this);
90         }
91         ~eAutoInitP0()
92         {
93                 eInit::remove(rl, this);
94         }
95 };
96
97 #endif