don't crash when no decode_demux present
[enigma2.git] / lib / base / init.h
1 #ifndef __init_h
2 #define __init_h
3
4 #include <list>
5 #include <utility>
6 #include <lib/base/object.h>
7
8 class eAutoInit;
9
10 class eInit
11 {
12         static std::list<std::pair<int,eAutoInit*> > *cl;
13         friend class eAutoInit;
14         static int rl;
15 public:
16         eInit();
17         ~eInit();
18         static void setRunlevel(int rlev);
19         static void add(int trl, eAutoInit *c);
20         static void remove(int trl, eAutoInit *c);
21 };
22
23 class eAutoInit
24 {
25         friend class eInit;
26         virtual void initNow()=0;
27         virtual void closeNow()=0;
28 protected:
29         int rl;
30         char *description;
31 public:
32         eAutoInit(int rl, char *description): rl(rl), description(description)
33         {
34         }
35         virtual ~eAutoInit();
36         const char *getDescription() const { return description; };
37 };
38
39 template<class T1, class T2> class
40 eAutoInitP1: protected eAutoInit
41 {
42         T1 *t;
43         const T2 &arg;
44         void initNow()
45         {
46                 t=new T1(arg);
47         }
48         void closeNow()
49         {
50                 delete t;
51         }
52 public:
53         operator T1*()
54         {
55                 return t;
56         }
57         eAutoInitP1(const T2 &arg, int runl, char *description): eAutoInit(runl, description), arg(arg)
58         {
59                 eInit::add(rl, this);
60         }
61         ~eAutoInitP1()
62         {
63                 eInit::remove(rl, this);
64         }
65 };
66
67 template<class T1> class
68 eAutoInitP0: protected eAutoInit
69 {
70         T1 *t;
71         void initNow()
72         {
73                 t=new T1();
74         }
75         void closeNow()
76         {
77                 delete t;
78         }
79 public:
80         operator T1*()
81         {
82                 return t;
83         }
84         T1 *operator->()
85         {
86                 return t;
87         }
88         eAutoInitP0(int runl, char *description): eAutoInit(runl, description)
89         {
90                 eInit::add(rl, this);
91         }
92         ~eAutoInitP0()
93         {
94                 eInit::remove(rl, this);
95         }
96 };
97
98 template<class T1> class
99 eAutoInitPtr: protected eAutoInit
100 {
101         ePtr<T1> t;
102         void initNow()
103         {
104                 t = new T1();
105         }
106         void closeNow()
107         {
108                 t = 0;
109         }
110 public:
111         operator T1*()
112         {
113                 return t;
114         }
115         T1 *operator->()
116         {
117                 return t;
118         }
119         eAutoInitPtr(int runl, char *description): eAutoInit(runl, description)
120         {
121                 eInit::add(rl, this);
122         }
123         ~eAutoInitPtr()
124         {
125                 eInit::remove(rl, this);
126         }
127 };
128
129 #endif