author | tchaikov |
Fri, 13 Nov 2009 22:15:06 +0800 | |
changeset 460 | 954a21b2c6cc |
parent 406 | 97269b2f9ea4 |
child 522 | ab21b1111321 |
permissions | -rw-r--r-- |
383 | 1 |
#ifndef _IMI_SESSION_H |
2 |
#define _IMI_SESSION_H |
|
3 |
||
4 |
#include <list> |
|
5 |
#include <string> |
|
6 |
#include <vector> |
|
7 |
||
8 |
#include "utils.h" |
|
9 |
||
10 |
class COptionEventBus; |
|
11 |
||
12 |
struct COptionEvent |
|
13 |
{ |
|
14 |
enum Type { |
|
15 |
TYPE_SHARED = (1 << 0), |
|
16 |
TYPE_GLOBAL = (1 << 1) |
|
17 |
}; |
|
406 | 18 |
template<typename ValueType> |
19 |
COptionEvent(const std::string& k, const ValueType& v, |
|
20 |
int t = TYPE_SHARED|TYPE_GLOBAL) |
|
21 |
: type(t), name(k), value(v) |
|
22 |
{} |
|
383 | 23 |
bool is_shared() const { return type & TYPE_SHARED; } |
24 |
bool is_global() const { return type & TYPE_GLOBAL; } |
|
401 | 25 |
int get_int() const; |
383 | 26 |
bool get_bool() const; |
27 |
std::string get_string() const; |
|
28 |
std::vector<std::string> get_string_list() const; |
|
29 |
||
30 |
/* TODO */ |
|
31 |
std::vector<bool> get_bool_list() const; |
|
32 |
std::vector<int> get_int_list() const; |
|
33 |
||
401 | 34 |
int type; |
383 | 35 |
std::string name; |
36 |
||
37 |
struct variant_ |
|
38 |
{ |
|
401 | 39 |
variant_(int); |
383 | 40 |
variant_(const std::string&); |
41 |
variant_(bool); |
|
42 |
variant_(const std::vector<std::string>&); |
|
43 |
struct val_ |
|
44 |
{ |
|
401 | 45 |
int d_int; |
383 | 46 |
std::string d_string; |
47 |
bool d_bool; |
|
48 |
std::vector<std::string> d_strings; |
|
49 |
} data; |
|
50 |
enum { |
|
51 |
VAL_UNSIGNED, |
|
52 |
VAL_UNSIGNED_LIST, |
|
53 |
VAL_STRING, |
|
54 |
VAL_STRING_LIST, |
|
55 |
VAL_BOOL, |
|
56 |
VAL_BOOL_LIST |
|
57 |
} type; |
|
58 |
} value; |
|
59 |
}; |
|
60 |
||
61 |
class IConfigurable |
|
62 |
{ |
|
63 |
public: |
|
64 |
IConfigurable(); |
|
65 |
~IConfigurable(); |
|
66 |
/** |
|
67 |
* onConfigChanged will be called whenever an option is changed |
|
68 |
* @param event presents the changed option |
|
69 |
* @return true if the event is consumed, and not intented to be |
|
70 |
* sent to another event listener, false otherwise. |
|
71 |
*/ |
|
460
954a21b2c6cc
* fix issue#46 - "unable to input after a fresh install": use open() instead of fopen()
tchaikov
parents:
406
diff
changeset
|
72 |
virtual bool onConfigChanged(const COptionEvent&) { return false; } |
383 | 73 |
void addRef(); |
74 |
void release(); |
|
75 |
||
76 |
private: |
|
77 |
int m_refcnt; |
|
78 |
}; |
|
79 |
||
80 |
class COptionEventBus |
|
81 |
{ |
|
82 |
public: |
|
83 |
/** |
|
84 |
* listener will receive a message whenever an option is changed |
|
85 |
* @param listener who is interested in a change of options |
|
86 |
*/ |
|
87 |
void registerAsListener(IConfigurable* listener); |
|
88 |
||
89 |
/** |
|
90 |
* remove listener from the subscriber list |
|
91 |
* @param listener who is no more interested in a change of options |
|
92 |
*/ |
|
93 |
void unregisterAsListener(IConfigurable* listener); |
|
94 |
||
95 |
/** |
|
96 |
* publish an event to all listeners of this event bus |
|
97 |
* @param event the event which presents an option change |
|
98 |
*/ |
|
99 |
void publishEvent(const COptionEvent& event); |
|
100 |
template<class> friend class SingletonHolder; |
|
101 |
||
102 |
private: |
|
103 |
COptionEventBus() {} |
|
104 |
||
105 |
typedef std::list<IConfigurable*> Subscribers; |
|
106 |
Subscribers m_listeners; |
|
107 |
}; |
|
108 |
||
109 |
typedef SingletonHolder<COptionEventBus> AOptionEventBus; |
|
110 |
||
111 |
#endif // _IMI_SESSION_H |