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.hh211
1 files changed, 211 insertions, 0 deletions
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh
new file mode 100644
index 0000000..6fddbac
--- /dev/null
+++ b/src/FbTk/Resource.hh
@@ -0,0 +1,211 @@
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
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() {
113 ResourceList::iterator it = m_resourcelist.begin();
114 ResourceList::iterator it_end = m_resourcelist.end();
115 for (; it != it_end; ++it) {
116 std::cerr<<(*it)->name()<<std::endl;
117 }
118 }
119protected:
120 static void ensureXrmIsInitialize();
121
122 int m_db_lock;
123
124private:
125 static bool m_init;
126 ResourceList m_resourcelist;
127
128 XrmDatabaseHelper *m_database;
129
130 std::string m_filename;
131};
132
133
134/// Real resource class
135/**
136 * usage: Resource<int> someresource(resourcemanager, 10, "someresourcename", "somealternativename"); \n
137 * and then implement setFromString and getString \n
138 * example: \n
139 * template <> \n
140 * void Resource<int>::setFromString(const char *str) { \n
141 * *(*this) = atoi(str); \n
142 * }
143 */
144template <typename T>
145class Resource:public Resource_base
146{
147public:
148 Resource(ResourceManager &rm, T val,
149 const std::string &name, const std::string &altname):
150 Resource_base(name, altname),
151 m_value(val), m_defaultval(val),
152 m_rm(rm)
153 {
154 m_rm.addResource(*this); // add this to resource handler
155 }
156 virtual ~Resource() {
157 m_rm.removeResource(*this); // remove this from resource handler
158 }
159
160 inline void setDefaultValue() { m_value = m_defaultval; }
161 /// sets resource from string, specialized, must be implemented
162 void setFromString(const char *strval);
163 inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;}
164 /// specialized, must be implemented
165 /// @return string value of resource
166 std::string getString();
167
168 inline T& operator*() { return m_value; }
169 inline const T& operator*() const { return m_value; }
170 inline T *operator->() { return &m_value; }
171 inline const T *operator->() const { return &m_value; }
172private:
173 T m_value, m_defaultval;
174 ResourceManager &m_rm;
175};
176
177
178// add the resource and load its value
179template <class T>
180void ResourceManager::addResource(Resource<T> &r) {
181 m_resourcelist.push_back(&r);
182 m_resourcelist.unique();
183
184 // lock ensures that the database is loaded.
185 lock();
186
187 if (m_database == 0) {
188 unlock();
189 return;
190 }
191
192 XrmValue value;
193 char *value_type;
194
195 // now, load the value for this resource
196 if (XrmGetResource(**m_database, r.name().c_str(),
197 r.altName().c_str(), &value_type, &value)) {
198 r.setFromString(value.addr);
199 } else {
200 std::cerr<<"Failed to read: "<<r.name()<<std::endl;
201 std::cerr<<"Setting default value"<<std::endl;
202 r.setDefaultValue();
203 }
204
205 unlock();
206}
207
208
209} // end namespace FbTk
210
211#endif // FBTK_RESOURCE_HH