aboutsummaryrefslogtreecommitdiff
path: root/lib/base/init.h
blob: 4b40bea026b4614744e0ae213605efd436d246a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef __init_h
#define __init_h

#include <list>
#include <utility>
#include <lib/base/object.h>

class eAutoInit;

class eInit
{
	static std::list<std::pair<int,eAutoInit*> > *cl;
	friend class eAutoInit;
	static int rl;
public:
	eInit();
	~eInit();
	static void setRunlevel(int rlev);
	static void add(int trl, eAutoInit *c);
	static void remove(int trl, eAutoInit *c);
};

class eAutoInit
{
	friend class eInit;
	virtual void initNow()=0;
	virtual void closeNow()=0;
protected:
	int rl;
	const char *description;
public:
	eAutoInit(int rl, const char *description): rl(rl), description(description)
	{
	}
	virtual ~eAutoInit();
	const char *getDescription() const { return description; };
};

template<class T1, class T2> class
eAutoInitP1: protected eAutoInit
{
	T1 *t;
	const T2 &arg;
	void initNow()
	{
		t=new T1(arg);
	}
	void closeNow()
	{
		delete t;
	}
public:
	operator T1*()
	{
		return t;
	}
	eAutoInitP1(const T2 &arg, int runl, const char *description): eAutoInit(runl, description), arg(arg)
	{
		eInit::add(rl, this);
	}
	~eAutoInitP1()
	{
		eInit::remove(rl, this);
	}
};

template<class T1> class
eAutoInitP0: protected eAutoInit
{
	T1 *t;
	void initNow()
	{
		t=new T1();
	}
	void closeNow()
	{
		delete t;
	}
public:
	operator T1*()
	{
		return t;
	}
	T1 *operator->()
	{
		return t;
	}
	eAutoInitP0(int runl, const char *description): eAutoInit(runl, description)
	{
		eInit::add(rl, this);
	}
	~eAutoInitP0()
	{
		eInit::remove(rl, this);
	}
};

template<class T1> class
eAutoInitPtr: protected eAutoInit
{
	ePtr<T1> t;
	void initNow()
	{
		t = new T1();
	}
	void closeNow()
	{
		t = 0;
	}
public:
	operator T1*()
	{
		return t;
	}
	T1 *operator->()
	{
		return t;
	}
	eAutoInitPtr(int runl, const char *description): eAutoInit(runl, description)
	{
		eInit::add(rl, this);
	}
	~eAutoInitPtr()
	{
		eInit::remove(rl, this);
	}
};

#endif