summaryrefslogtreecommitdiff
path: root/plugins/Boltun/src/config.h
blob: 6ebd395693c7b43f221ebd1dbccfb2487f4f14a4 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//***********************************************************
//	Copyright © 2003-2008 Alexander S. Kiselev, Valentin Pavlyuchenko
//
//	This file is part of Boltun.
//
//    Boltun is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    Boltun is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//	  along with Boltun. If not, see <http://www.gnu.org/licenses/>.
//
//***********************************************************

#ifndef _CONFIG_H
#define _CONFIG_H

class BoltunConfig;

template <class T, class BaseClass>
class Property
{
public:
	typedef const T(__thiscall BaseClass::*Getter)();
	typedef const T(__thiscall BaseClass::*Setter)(const T);

private:
	const Getter getter;
	const Setter setter;
	BaseClass* owner;
	bool cacheValid;
	T cached;

	Property& operator=(const Property&);

public:
	Property(Getter g, Setter s)
		:getter(g), setter(s), cacheValid(false)
	{
	}

	void SetOwner(BaseClass* o)
	{
		owner = o;
	}

	inline operator const T()
	{
		if (cacheValid)
			return cached;
		cached = (owner->*getter)();
		cacheValid = true;
		return cached;
	}

	inline const T operator= (const T& value)
	{
		cacheValid = true;
		return (owner->*setter)(cached = value);
	}
};

template <class T, class BaseClass>
class PtrProperty
{
public:
	typedef const T* (__thiscall BaseClass::*Getter)();
	typedef const T* (__thiscall BaseClass::*Setter)(const T*);

private:
	const Getter getter;
	const Setter setter;
	BaseClass* owner;
	bool cacheValid;
	const T* cached;

	PtrProperty& operator=(const PtrProperty&);

public:
	PtrProperty(Getter g, Setter s)
		:getter(g), setter(s), cacheValid(false), cached(NULL)
	{
	}

	~PtrProperty()
	{
		delete cached;
	}

	void SetOwner(BaseClass* o)
	{
		owner = o;
	}

	inline operator const T*()
	{
		if (cacheValid)
			return cached;
		cached = (owner->*getter)();
		cacheValid = true;
		return cached;
	}

	inline const T* operator= (const T* value)
	{
		cacheValid = true;
		delete cached;
		cached = (owner->*setter)(value);
		return cached;
	}
};

class _BoltunConfigInit;

#define BUILDDEFETTERS(x, typ) \
	const typ Get##x(); \
	const typ Set##x(const typ value);

class BoltunConfig
{
	BUILDDEFETTERS(TalkWithEverybody, bool);
	BUILDDEFETTERS(TalkWithNotInList, bool);
	BUILDDEFETTERS(TalkWarnContacts, bool);
	BUILDDEFETTERS(TalkEveryoneWhileAway, bool);
	BUILDDEFETTERS(MarkAsRead, bool);
	BUILDDEFETTERS(AnswerPauseTime, int);
	BUILDDEFETTERS(AnswerThinkTime, int);
	BUILDDEFETTERS(PauseDepends, bool);
	BUILDDEFETTERS(PauseRandom, bool);
	BUILDDEFETTERS(WarnText, wchar_t*);
	BUILDDEFETTERS(MindFileName, wchar_t*);
	BUILDDEFETTERS(EngineStaySilent, bool);
	BUILDDEFETTERS(EngineMakeLowerCase, bool);
	BUILDDEFETTERS(EngineUnderstandAlways, bool);
	BoltunConfig();
	~BoltunConfig();
	friend class _BoltunConfigInit;
public:
	Property<bool, BoltunConfig> TalkWithEverybody;
	Property<bool, BoltunConfig> TalkWithNotInList;
	Property<bool, BoltunConfig> TalkWarnContacts;
	Property<bool, BoltunConfig> TalkEveryoneWhileAway;
	Property<bool, BoltunConfig> MarkAsRead;
	Property<int, BoltunConfig> AnswerPauseTime;
	Property<int, BoltunConfig> AnswerThinkTime;
	Property<bool, BoltunConfig> PauseDepends;
	Property<bool, BoltunConfig> PauseRandom;
	PtrProperty<wchar_t, BoltunConfig> WarnText;
	PtrProperty<wchar_t, BoltunConfig> MindFileName;
	Property<bool, BoltunConfig> EngineStaySilent;
	Property<bool, BoltunConfig> EngineMakeLowerCase;
	Property<bool, BoltunConfig> EngineUnderstandAlways;
};

extern BoltunConfig &Config;

#endif /* _CONFIG_H */