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.hh214
1 files changed, 214 insertions, 0 deletions
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh
new file mode 100644
index 0000000..e2939d7
--- /dev/null
+++ b/src/FbTk/Resource.hh
@@ -0,0 +1,214 @@
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.7 2003/12/19 18:25:39 fluxgen Exp $
23
24#ifndef FBTK_RESOURCE_HH
25#define FBTK_RESOURCE_HH
26
27#include "NotCopyable.hh"
28#include "XrmDatabaseHelper.hh"
29#include <string>
30#include <list>
31#include <iostream>
32
33#include <X11/Xlib.h>
34#include <X11/Xresource.h>
35
36namespace FbTk {
37
38class XrmDatabaseHelper;
39
40/// Base class for resources, this is only used in ResourceManager
41class Resource_base:private FbTk::NotCopyable
42{
43public:
44 virtual ~Resource_base() { };
45
46 /// set from string value
47 virtual void setFromString(char const *strval) = 0;
48 /// set default value
49 virtual void setDefaultValue() = 0;
50 /// get string value
51 virtual std::string getString() = 0;
52 /// get alternative name of this resource
53 inline const std::string& altName() const { return m_altname; }
54 /// get name of this resource
55 inline const std::string& name() const { return m_name; }
56
57protected:
58 Resource_base(const std::string &name, const std::string &altname):
59 m_name(name), m_altname(altname)
60 { }
61
62private:
63 std::string m_name; ///< name of this resource
64 std::string m_altname; ///< alternative name
65};
66
67template <typename T>
68class Resource;
69
70class ResourceManager
71{
72public:
73 typedef std::list<Resource_base *> ResourceList;
74
75 // lock specifies if the database should be opened with one level locked
76 // (useful for constructing inside initial set of constructors)
77 ResourceManager(const char *filename, bool lock_db);
78 virtual ~ResourceManager();
79
80 /// Load all resources registered to this class
81 /// @return true on success
82 virtual bool load(const char *filename);
83
84 /// Save all resouces registered to this class
85 /// @return true on success
86 virtual bool save(const char *filename, const char *mergefilename=0);
87
88
89
90 /// Add resource to list, only used in Resource<T>
91 template <class T>
92 void addResource(Resource<T> &r);
93
94 /// Remove a specific resource, only used in Resource<T>
95 template <class T>
96 void removeResource(Resource<T> &r) {
97 m_resourcelist.remove(&r);
98 }
99
100 Resource_base *findResource(const std::string &resourcename);
101 std::string resourceValue(const std::string &resourcename);
102 void setResourceValue(const std::string &resourcename, const std::string &value);
103
104 // this marks the database as "in use" and will avoid reloading
105 // resources unless it is zero.
106 // It returns this resource manager. Useful for passing to
107 // constructors like Object(m_rm.lock())
108 ResourceManager &lock();
109 void unlock();
110 // for debugging
111 inline int lockDepth() const { return m_db_lock; }
112 void dump(bool show_value = false) {
113 ResourceList::iterator it = m_resourcelist.begin();
114 ResourceList::iterator it_end = m_resourcelist.end();
115 for (; it != it_end; ++it) {
116 if (show_value)
117 std::cout<<(*it)->name()<<": "<<(*it)->getString()<<std::endl;
118 else
119 std::cout<<(*it)->name()<<std::endl;
120 }
121 }
122protected:
123 static void ensureXrmIsInitialize();
124
125 int m_db_lock;
126
127private:
128 static bool m_init;
129 ResourceList m_resourcelist;
130
131 XrmDatabaseHelper *m_database;
132
133 std::string m_filename;
134};
135
136
137/// Real resource class
138/**
139 * usage: Resource<int> someresource(resourcemanager, 10, "someresourcename", "somealternativename"); \n
140 * and then implement setFromString and getString \n
141 * example: \n
142 * template <> \n
143 * void Resource<int>::setFromString(const char *str) { \n
144 * *(*this) = atoi(str); \n
145 * }
146 */
147template <typename T>
148class Resource:public Resource_base
149{
150public:
151 Resource(ResourceManager &rm, T val,
152 const std::string &name, const std::string &altname):
153 Resource_base(name, altname),
154 m_value(val), m_defaultval(val),
155 m_rm(rm)
156 {
157 m_rm.addResource(*this); // add this to resource handler
158 }
159 virtual ~Resource() {
160 m_rm.removeResource(*this); // remove this from resource handler
161 }
162
163 inline void setDefaultValue() { m_value = m_defaultval; }
164 /// sets resource from string, specialized, must be implemented
165 void setFromString(const char *strval);
166 inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;}
167 /// specialized, must be implemented
168 /// @return string value of resource
169 std::string getString();
170
171 inline T& operator*() { return m_value; }
172 inline const T& operator*() const { return m_value; }
173 inline T *operator->() { return &m_value; }
174 inline const T *operator->() const { return &m_value; }
175private:
176 T m_value, m_defaultval;
177 ResourceManager &m_rm;
178};
179
180
181// add the resource and load its value
182template <class T>
183void ResourceManager::addResource(Resource<T> &r) {
184 m_resourcelist.push_back(&r);
185 m_resourcelist.unique();
186
187 // lock ensures that the database is loaded.
188 lock();
189
190 if (m_database == 0) {
191 unlock();
192 return;
193 }
194
195 XrmValue value;
196 char *value_type;
197
198 // now, load the value for this resource
199 if (XrmGetResource(**m_database, r.name().c_str(),
200 r.altName().c_str(), &value_type, &value)) {
201 r.setFromString(value.addr);
202 } else {
203 std::cerr<<"Failed to read: "<<r.name()<<std::endl;
204 std::cerr<<"Setting default value"<<std::endl;
205 r.setDefaultValue();
206 }
207
208 unlock();
209}
210
211
212} // end namespace FbTk
213
214#endif // FBTK_RESOURCE_HH