diff options
Diffstat (limited to 'src/FbTk/Resource.hh')
-rw-r--r-- | src/FbTk/Resource.hh | 65 |
1 files changed, 58 insertions, 7 deletions
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh index 4471225..25152bc 100644 --- a/src/FbTk/Resource.hh +++ b/src/FbTk/Resource.hh | |||
@@ -19,7 +19,7 @@ | |||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. | 20 | // DEALINGS IN THE SOFTWARE. |
21 | 21 | ||
22 | // $Id: Resource.hh,v 1.1 2003/05/18 22:06:59 fluxgen Exp $ | 22 | // $Id: Resource.hh,v 1.2 2003/07/18 15:40:55 rathnor Exp $ |
23 | 23 | ||
24 | #ifndef FBTK_RESOURCE_HH | 24 | #ifndef FBTK_RESOURCE_HH |
25 | #define FBTK_RESOURCE_HH | 25 | #define FBTK_RESOURCE_HH |
@@ -28,9 +28,12 @@ | |||
28 | 28 | ||
29 | #include <string> | 29 | #include <string> |
30 | #include <list> | 30 | #include <list> |
31 | #include <X11/Xresource.h> | ||
31 | 32 | ||
32 | namespace FbTk { | 33 | namespace FbTk { |
33 | 34 | ||
35 | class XrmDatabaseHelper; | ||
36 | |||
34 | /// Base class for resources, this is only used in ResourceManager | 37 | /// Base class for resources, this is only used in ResourceManager |
35 | class Resource_base:private FbTk::NotCopyable | 38 | class Resource_base:private FbTk::NotCopyable |
36 | { | 39 | { |
@@ -66,8 +69,10 @@ class ResourceManager | |||
66 | public: | 69 | public: |
67 | typedef std::list<Resource_base *> ResourceList; | 70 | typedef std::list<Resource_base *> ResourceList; |
68 | 71 | ||
69 | ResourceManager() { } | 72 | // lock specifies if the database should be opened with one level locked |
70 | virtual ~ResourceManager() {} | 73 | // (useful for constructing inside initial set of constructors) |
74 | ResourceManager(const char *filename, bool lock_db); | ||
75 | virtual ~ResourceManager(); | ||
71 | 76 | ||
72 | /// Load all resources registered to this class | 77 | /// Load all resources registered to this class |
73 | /// @return true on success | 78 | /// @return true on success |
@@ -77,12 +82,11 @@ public: | |||
77 | /// @return true on success | 82 | /// @return true on success |
78 | virtual bool save(const char *filename, const char *mergefilename=0); | 83 | virtual bool save(const char *filename, const char *mergefilename=0); |
79 | 84 | ||
85 | |||
86 | |||
80 | /// Add resource to list, only used in Resource<T> | 87 | /// Add resource to list, only used in Resource<T> |
81 | template <class T> | 88 | template <class T> |
82 | void addResource(Resource<T> &r) { | 89 | void addResource(Resource<T> &r); |
83 | m_resourcelist.push_back(&r); | ||
84 | m_resourcelist.unique(); | ||
85 | } | ||
86 | 90 | ||
87 | /// Remove a specific resource, only used in Resource<T> | 91 | /// Remove a specific resource, only used in Resource<T> |
88 | template <class T> | 92 | template <class T> |
@@ -90,12 +94,27 @@ public: | |||
90 | m_resourcelist.remove(&r); | 94 | m_resourcelist.remove(&r); |
91 | } | 95 | } |
92 | 96 | ||
97 | // this marks the database as "in use" and will avoid reloading | ||
98 | // resources unless it is zero. | ||
99 | // It returns this resource manager. Useful for passing to | ||
100 | // constructors like Object(m_rm.lock()) | ||
101 | ResourceManager &lock(); | ||
102 | void unlock(); | ||
103 | // for debugging | ||
104 | inline int lockDepth() const { return m_db_lock; } | ||
105 | |||
93 | protected: | 106 | protected: |
94 | static void ensureXrmIsInitialize(); | 107 | static void ensureXrmIsInitialize(); |
95 | 108 | ||
109 | int m_db_lock; | ||
110 | |||
96 | private: | 111 | private: |
97 | static bool m_init; | 112 | static bool m_init; |
98 | ResourceList m_resourcelist; | 113 | ResourceList m_resourcelist; |
114 | |||
115 | XrmDatabaseHelper *m_database; | ||
116 | |||
117 | std::string m_filename; | ||
99 | }; | 118 | }; |
100 | 119 | ||
101 | 120 | ||
@@ -142,6 +161,38 @@ private: | |||
142 | ResourceManager &m_rm; | 161 | ResourceManager &m_rm; |
143 | }; | 162 | }; |
144 | 163 | ||
164 | |||
165 | // add the resource and load its value | ||
166 | template <class T> | ||
167 | void ResourceManager::addResource(Resource<T> &r) { | ||
168 | m_resourcelist.push_back(&r); | ||
169 | m_resourcelist.unique(); | ||
170 | |||
171 | // lock ensures that the database is loaded. | ||
172 | lock(); | ||
173 | |||
174 | if (m_database == 0) { | ||
175 | unlock(); | ||
176 | return; | ||
177 | } | ||
178 | |||
179 | XrmValue value; | ||
180 | char *value_type; | ||
181 | |||
182 | // now, load the value for this resource | ||
183 | if (XrmGetResource(**m_database, r.name().c_str(), | ||
184 | r.altName().c_str(), &value_type, &value)) { | ||
185 | r.setFromString(value.addr); | ||
186 | } else { | ||
187 | cerr<<"Failed to read: "<<r.name()<<endl; | ||
188 | cerr<<"Setting default value"<<endl; | ||
189 | r.setDefaultValue(); | ||
190 | } | ||
191 | |||
192 | unlock(); | ||
193 | } | ||
194 | |||
195 | |||
145 | }; // end namespace FbTk | 196 | }; // end namespace FbTk |
146 | 197 | ||
147 | #endif // FBTK_RESOURCE_HH | 198 | #endif // FBTK_RESOURCE_HH |