diff options
Diffstat (limited to 'src/Resource.hh')
-rw-r--r-- | src/Resource.hh | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/src/Resource.hh b/src/Resource.hh new file mode 100644 index 0000000..0e4e939 --- /dev/null +++ b/src/Resource.hh | |||
@@ -0,0 +1,132 @@ | |||
1 | // Resource.hh | ||
2 | // Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | // $Id: Resource.hh,v 1.7 2002/08/04 15:55:13 fluxgen Exp $ | ||
23 | |||
24 | #ifndef RESOURCE_HH | ||
25 | #define RESOURCE_HH | ||
26 | |||
27 | #include "NotCopyable.hh" | ||
28 | #include <string> | ||
29 | #include <list> | ||
30 | /** | ||
31 | Base class for resources | ||
32 | */ | ||
33 | class Resource_base:private NotCopyable | ||
34 | { | ||
35 | public: | ||
36 | virtual ~Resource_base() { }; | ||
37 | |||
38 | /// set from string value | ||
39 | virtual void setFromString(char const *strval) = 0; | ||
40 | /// set default value | ||
41 | virtual void setDefaultValue() = 0; | ||
42 | /// get string value | ||
43 | virtual std::string getString() = 0; | ||
44 | /// get alternative name of this resource | ||
45 | inline const std::string& altName() const { return m_altname; } | ||
46 | /// get name of this resource | ||
47 | inline const std::string& name() const { return m_name; } | ||
48 | |||
49 | protected: | ||
50 | Resource_base(const std::string &name, const std::string &altname): | ||
51 | m_name(name), m_altname(altname) | ||
52 | { } | ||
53 | |||
54 | private: | ||
55 | std::string m_name; ///< name of this resource | ||
56 | std::string m_altname; ///< alternative name | ||
57 | }; | ||
58 | |||
59 | class ResourceManager; | ||
60 | |||
61 | /** | ||
62 | Real resource class | ||
63 | */ | ||
64 | template <typename T> | ||
65 | class Resource:public Resource_base | ||
66 | { | ||
67 | public: | ||
68 | Resource(ResourceManager &rm, T val, | ||
69 | const std::string &name, const std::string &altname): | ||
70 | Resource_base(name, altname), | ||
71 | m_value(val), m_defaultval(val), | ||
72 | m_rm(rm) | ||
73 | { | ||
74 | m_rm.addResource(*this); // add this to resource handler | ||
75 | } | ||
76 | virtual ~Resource() { | ||
77 | m_rm.removeResource(*this); // remove this from resource handler | ||
78 | } | ||
79 | |||
80 | inline void setDefaultValue() { m_value = m_defaultval; } | ||
81 | void setFromString(const char *strval); | ||
82 | inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;} | ||
83 | |||
84 | std::string getString(); | ||
85 | inline T& operator*() { return m_value; } | ||
86 | inline const T& operator*() const { return m_value; } | ||
87 | inline T *operator->() { return &m_value; } | ||
88 | inline const T *operator->() const { return &m_value; } | ||
89 | private: | ||
90 | T m_value, m_defaultval; | ||
91 | ResourceManager &m_rm; | ||
92 | }; | ||
93 | |||
94 | class ResourceManager | ||
95 | { | ||
96 | public: | ||
97 | typedef std::list<Resource_base *> ResourceList; | ||
98 | |||
99 | ResourceManager() { } | ||
100 | virtual ~ResourceManager() {} | ||
101 | /** | ||
102 | load all resouces registered to this class | ||
103 | */ | ||
104 | virtual bool load(const char *filename); | ||
105 | /** | ||
106 | save all resouces registered to this class | ||
107 | */ | ||
108 | virtual bool save(const char *filename, const char *mergefilename=0); | ||
109 | /** | ||
110 | add resource to list | ||
111 | */ | ||
112 | template <class T> | ||
113 | void addResource(Resource<T> &r) { | ||
114 | m_resourcelist.push_back(&r); | ||
115 | m_resourcelist.unique(); | ||
116 | } | ||
117 | /** | ||
118 | Remove a specific resource | ||
119 | */ | ||
120 | template <class T> | ||
121 | void removeResource(Resource<T> &r) { | ||
122 | m_resourcelist.remove(&r); | ||
123 | } | ||
124 | protected: | ||
125 | static inline void ensureXrmIsInitialize(); | ||
126 | private: | ||
127 | |||
128 | static bool m_init; | ||
129 | ResourceList m_resourcelist; | ||
130 | }; | ||
131 | |||
132 | #endif //_RESOURCE_HH_ | ||