-
Notifications
You must be signed in to change notification settings - Fork 2
/
object.h
96 lines (83 loc) · 2.22 KB
/
object.h
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
#pragma once
//Declaration
namespace fetch
{
// Configurable
// ============
//
// This class implements a behavior for communicating configuration
// changes.
//
// Child objects get a pointer to a Config object.
//
// By default, the pointer addresses a default-constructed private instance.
// However, it can be set to an outside instance via construction or
// through the set_config() method.
//
// The set_config() method can be overloaded to intercept changes to
// the <config> pointer and, for instance, communicate changes to other
// objects.
//
// Aside
// -----
// The behavior is not specific to Configuration problems...it's more
// really there just to provide a hook for a particular special property.
// In fetch, it's used for configuration...hence the name.
//
// Usage
// -----
//
// 1. Derive your class.
//
// class A: public Configurable<TConfig>
// ...
//
// 2. (optional) over-ride set_config().
//
// void A::set_config(Config *cfg)
// { printf("changing config\n");
// config = cfg;
// }
//template<typename T>
//class ConfigurableBase
//{
// public: virtual void set_config(T *cfg) = 0;
//};
template<typename T>
class Configurable//:public virtual ConfigurableBase<T>
{
public:
typedef T Config;
Config *_config;
Configurable();
Configurable(T *cfg);
virtual void set_config(Config *cfg); // returns 1 if updated, 0 otherwise
private:
Config _default_config;
};
// Implementation
template<typename T>
Configurable<T>::Configurable()
: _config(0)
{set_config(&_default_config);}
template<typename T>
Configurable<T>::Configurable( T *cfg )
: _config(0)
{set_config(cfg);}
//************************************
// Method: set_config
// FullName: fetch::Configurable<T>::set_config
// Access: virtual public
// Returns: void
// Qualifier:
// Parameter: Config * cfg
//
// Child classes might want to override this to communicate
// config changes to other objects or parent classes.
//
//************************************
template<typename T>
void Configurable<T>::set_config( Config *cfg )
{ _config = cfg;
}
}