diff options
Diffstat (limited to 'src/Resource.hh')
-rw-r--r-- | src/Resource.hh | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/Resource.hh b/src/Resource.hh new file mode 100644 index 0000000..42156a8 --- /dev/null +++ b/src/Resource.hh | |||
@@ -0,0 +1,108 @@ | |||
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.3 2002/02/17 18:57:47 fluxgen Exp $ | ||
23 | |||
24 | #ifndef RESOURCE_HH | ||
25 | #define RESOURCE_HH | ||
26 | |||
27 | #include "NotCopyable.hh" | ||
28 | #include <string> | ||
29 | #include <list> | ||
30 | |||
31 | class Resource_base:private NotCopyable | ||
32 | { | ||
33 | public: | ||
34 | |||
35 | virtual void setFromString(char const *strval)=0; | ||
36 | virtual void setDefaultValue()=0; | ||
37 | |||
38 | virtual std::string getString()=0; | ||
39 | inline std::string& getAltName() { return m_altname; } | ||
40 | inline std::string& getName() { return m_name; } | ||
41 | |||
42 | protected: | ||
43 | Resource_base(const std::string &name, const std::string &altname): | ||
44 | m_name(name), m_altname(altname) | ||
45 | { | ||
46 | |||
47 | } | ||
48 | virtual ~Resource_base(){ }; | ||
49 | private: | ||
50 | std::string m_name, m_altname; | ||
51 | }; | ||
52 | |||
53 | class ResourceManager; | ||
54 | |||
55 | template <typename T> | ||
56 | class Resource:public Resource_base | ||
57 | { | ||
58 | public: | ||
59 | Resource(ResourceManager &rm, T val, | ||
60 | const std::string &name, const std::string &altname): | ||
61 | Resource_base(name, altname), | ||
62 | m_value(val), m_defaultval(val), | ||
63 | m_rm(rm) | ||
64 | { | ||
65 | m_rm.addResource(*this); | ||
66 | } | ||
67 | ~Resource() { | ||
68 | m_rm.removeResource(*this); | ||
69 | } | ||
70 | |||
71 | inline void setDefaultValue() { m_value = m_defaultval; } | ||
72 | void setFromString(const char *strval); | ||
73 | inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;} | ||
74 | |||
75 | std::string getString(); | ||
76 | inline T& operator*(void) { return m_value; } | ||
77 | inline T *operator->(void) { return &m_value; } | ||
78 | private: | ||
79 | T m_value, m_defaultval; | ||
80 | ResourceManager &m_rm; | ||
81 | }; | ||
82 | |||
83 | class ResourceManager | ||
84 | { | ||
85 | public: | ||
86 | typedef std::list<Resource_base *> ResourceList; | ||
87 | |||
88 | ResourceManager(){ } | ||
89 | |||
90 | bool load(const char *filename); | ||
91 | bool save(const char *filename, const char *mergefilename=0); | ||
92 | template <class T> | ||
93 | void addResource(Resource<T> &r) { | ||
94 | m_resourcelist.push_back(&r); | ||
95 | m_resourcelist.unique(); | ||
96 | } | ||
97 | template <class T> | ||
98 | void removeResource(Resource<T> &r) { | ||
99 | m_resourcelist.remove(&r); | ||
100 | } | ||
101 | private: | ||
102 | static inline void ensureXrmIsInitialize(); | ||
103 | static bool m_init; | ||
104 | ResourceList m_resourcelist; | ||
105 | |||
106 | }; | ||
107 | |||
108 | #endif //_RESOURCE_HH_ | ||