diff options
Diffstat (limited to 'src/Resource.hh')
-rw-r--r-- | src/Resource.hh | 142 |
1 files changed, 0 insertions, 142 deletions
diff --git a/src/Resource.hh b/src/Resource.hh deleted file mode 100644 index d657b5b..0000000 --- a/src/Resource.hh +++ /dev/null | |||
@@ -1,142 +0,0 @@ | |||
1 | // Resource.hh | ||
2 | // Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net) | ||
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.12 2003/05/10 13:44:24 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, this is only used in ResourceManager | ||
32 | class Resource_base:private FbTk::NotCopyable | ||
33 | { | ||
34 | public: | ||
35 | virtual ~Resource_base() { }; | ||
36 | |||
37 | /// set from string value | ||
38 | virtual void setFromString(char const *strval) = 0; | ||
39 | /// set default value | ||
40 | virtual void setDefaultValue() = 0; | ||
41 | /// get string value | ||
42 | virtual std::string getString() = 0; | ||
43 | /// get alternative name of this resource | ||
44 | inline const std::string& altName() const { return m_altname; } | ||
45 | /// get name of this resource | ||
46 | inline const std::string& name() const { return m_name; } | ||
47 | |||
48 | protected: | ||
49 | Resource_base(const std::string &name, const std::string &altname): | ||
50 | m_name(name), m_altname(altname) | ||
51 | { } | ||
52 | |||
53 | private: | ||
54 | std::string m_name; ///< name of this resource | ||
55 | std::string m_altname; ///< alternative name | ||
56 | }; | ||
57 | |||
58 | template <typename T> | ||
59 | class Resource; | ||
60 | |||
61 | class ResourceManager | ||
62 | { | ||
63 | public: | ||
64 | typedef std::list<Resource_base *> ResourceList; | ||
65 | |||
66 | ResourceManager() { } | ||
67 | virtual ~ResourceManager() {} | ||
68 | |||
69 | /// Load all resources registered to this class | ||
70 | /// @return true on success | ||
71 | virtual bool load(const char *filename); | ||
72 | |||
73 | /// Save all resouces registered to this class | ||
74 | /// @return true on success | ||
75 | virtual bool save(const char *filename, const char *mergefilename=0); | ||
76 | |||
77 | /// Add resource to list, only used in Resource<T> | ||
78 | template <class T> | ||
79 | void addResource(Resource<T> &r) { | ||
80 | m_resourcelist.push_back(&r); | ||
81 | m_resourcelist.unique(); | ||
82 | } | ||
83 | |||
84 | /// Remove a specific resource, only used in Resource<T> | ||
85 | template <class T> | ||
86 | void removeResource(Resource<T> &r) { | ||
87 | m_resourcelist.remove(&r); | ||
88 | } | ||
89 | |||
90 | protected: | ||
91 | static void ensureXrmIsInitialize(); | ||
92 | |||
93 | private: | ||
94 | static bool m_init; | ||
95 | ResourceList m_resourcelist; | ||
96 | }; | ||
97 | |||
98 | |||
99 | /// Real resource class | ||
100 | /** | ||
101 | * usage: Resource<int> someresource(resourcemanager, 10, "someresourcename", "somealternativename"); \n | ||
102 | * and then implement setFromString and getString \n | ||
103 | * example: \n | ||
104 | * template <> \n | ||
105 | * void Resource<int>::setFromString(const char *str) { \n | ||
106 | * *(*this) = atoi(str); \n | ||
107 | * } | ||
108 | */ | ||
109 | template <typename T> | ||
110 | class Resource:public Resource_base | ||
111 | { | ||
112 | public: | ||
113 | Resource(ResourceManager &rm, T val, | ||
114 | const std::string &name, const std::string &altname): | ||
115 | Resource_base(name, altname), | ||
116 | m_value(val), m_defaultval(val), | ||
117 | m_rm(rm) | ||
118 | { | ||
119 | m_rm.addResource(*this); // add this to resource handler | ||
120 | } | ||
121 | virtual ~Resource() { | ||
122 | m_rm.removeResource(*this); // remove this from resource handler | ||
123 | } | ||
124 | |||
125 | inline void setDefaultValue() { m_value = m_defaultval; } | ||
126 | /// sets resource from string, specialized, must be implemented | ||
127 | void setFromString(const char *strval); | ||
128 | inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;} | ||
129 | /// specialized, must be implemented | ||
130 | /// @return string value of resource | ||
131 | std::string getString(); | ||
132 | |||
133 | inline T& operator*() { return m_value; } | ||
134 | inline const T& operator*() const { return m_value; } | ||
135 | inline T *operator->() { return &m_value; } | ||
136 | inline const T *operator->() const { return &m_value; } | ||
137 | private: | ||
138 | T m_value, m_defaultval; | ||
139 | ResourceManager &m_rm; | ||
140 | }; | ||
141 | |||
142 | #endif // RESOURCE_HH | ||