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