aboutsummaryrefslogtreecommitdiff
path: root/lib/base/init.h
blob: 465bac40a5ae5db77e7c4e1c287b3c87fb07416a (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
#ifndef __init_h
#define __init_h

#include <list>
#include <utility>

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;
	char *description;
public:
	eAutoInit(int rl, 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, 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, char *description): eAutoInit(runl, description)
	{
		eInit::add(rl, this);
	}
	~eAutoInitP0()
	{
		eInit::remove(rl, this);
	}
};

#endif