diff options
author | rathnor <rathnor> | 2003-07-18 15:40:55 (GMT) |
---|---|---|
committer | rathnor <rathnor> | 2003-07-18 15:40:55 (GMT) |
commit | 7e1733c4352befe577150bad63d362dd7c1a94b9 (patch) | |
tree | 1f339ae234f85aaa98529e9c511117dcb620a9f1 /src/FbTk | |
parent | 06d723aefe7fb794f50b1e7a0ad8ad4473ae8ac6 (diff) | |
download | fluxbox_lack-7e1733c4352befe577150bad63d362dd7c1a94b9.zip fluxbox_lack-7e1733c4352befe577150bad63d362dd7c1a94b9.tar.bz2 |
reorganise operation of ResourceManager
plus fix initialisation of layer for slit and toolbar
Diffstat (limited to 'src/FbTk')
-rw-r--r-- | src/FbTk/Resource.cc | 105 | ||||
-rw-r--r-- | src/FbTk/Resource.hh | 65 | ||||
-rw-r--r-- | src/FbTk/XrmDatabaseHelper.hh | 5 |
3 files changed, 144 insertions, 31 deletions
diff --git a/src/FbTk/Resource.cc b/src/FbTk/Resource.cc index a222984..c21e55d 100644 --- a/src/FbTk/Resource.cc +++ b/src/FbTk/Resource.cc | |||
@@ -19,10 +19,10 @@ | |||
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.cc,v 1.1 2003/05/18 22:06:59 fluxgen Exp $ | 22 | // $Id: Resource.cc,v 1.2 2003/07/18 15:40:55 rathnor Exp $ |
23 | 23 | ||
24 | #include "Resource.hh" | ||
25 | #include "XrmDatabaseHelper.hh" | 24 | #include "XrmDatabaseHelper.hh" |
25 | #include "Resource.hh" | ||
26 | 26 | ||
27 | #include <iostream> | 27 | #include <iostream> |
28 | #include <cassert> | 28 | #include <cassert> |
@@ -31,22 +31,43 @@ using namespace std; | |||
31 | 31 | ||
32 | namespace FbTk { | 32 | namespace FbTk { |
33 | 33 | ||
34 | ResourceManager::ResourceManager(const char *filename, bool lock_db) : | ||
35 | m_db_lock(0), | ||
36 | m_database(0), | ||
37 | m_filename(filename) | ||
38 | { | ||
39 | ensureXrmIsInitialize(); | ||
40 | |||
41 | if (lock_db) | ||
42 | lock(); | ||
43 | } | ||
44 | |||
45 | ResourceManager::~ResourceManager() { | ||
46 | if (m_database) | ||
47 | delete m_database; | ||
48 | } | ||
49 | |||
34 | bool ResourceManager::m_init = false; | 50 | bool ResourceManager::m_init = false; |
35 | 51 | ||
36 | /** | 52 | /** |
37 | loads a resourcefile | 53 | reloads all resources from resourcefile |
38 | @return true on success else false | 54 | @return true on success else false |
39 | */ | 55 | */ |
40 | bool ResourceManager::load(const char *filename) { | 56 | bool ResourceManager::load(const char *filename) { |
41 | assert(filename); | 57 | m_filename = filename; |
42 | 58 | ||
43 | ensureXrmIsInitialize(); | 59 | // force reload (lock will ensure it exists) |
44 | 60 | if (m_database) { | |
45 | XrmDatabaseHelper database; | 61 | delete m_database; |
46 | database = XrmGetFileDatabase(filename); | 62 | m_database = 0; |
47 | if (database==0) | 63 | } |
64 | |||
65 | lock(); | ||
66 | if (!m_database) { | ||
67 | unlock(); | ||
48 | return false; | 68 | return false; |
49 | 69 | } | |
70 | |||
50 | XrmValue value; | 71 | XrmValue value; |
51 | char *value_type; | 72 | char *value_type; |
52 | 73 | ||
@@ -56,7 +77,7 @@ bool ResourceManager::load(const char *filename) { | |||
56 | for (; i != i_end; ++i) { | 77 | for (; i != i_end; ++i) { |
57 | 78 | ||
58 | Resource_base *resource = *i; | 79 | Resource_base *resource = *i; |
59 | if (XrmGetResource(*database, resource->name().c_str(), | 80 | if (XrmGetResource(**m_database, resource->name().c_str(), |
60 | resource->altName().c_str(), &value_type, &value)) | 81 | resource->altName().c_str(), &value_type, &value)) |
61 | resource->setFromString(value.addr); | 82 | resource->setFromString(value.addr); |
62 | else { | 83 | else { |
@@ -66,6 +87,8 @@ bool ResourceManager::load(const char *filename) { | |||
66 | } | 87 | } |
67 | } | 88 | } |
68 | 89 | ||
90 | unlock(); | ||
91 | |||
69 | return true; | 92 | return true; |
70 | } | 93 | } |
71 | 94 | ||
@@ -75,9 +98,8 @@ bool ResourceManager::load(const char *filename) { | |||
75 | */ | 98 | */ |
76 | bool ResourceManager::save(const char *filename, const char *mergefilename) { | 99 | bool ResourceManager::save(const char *filename, const char *mergefilename) { |
77 | assert(filename); | 100 | assert(filename); |
78 | 101 | ||
79 | ensureXrmIsInitialize(); | 102 | // empty database |
80 | |||
81 | XrmDatabaseHelper database; | 103 | XrmDatabaseHelper database; |
82 | 104 | ||
83 | string rc_string; | 105 | string rc_string; |
@@ -92,20 +114,32 @@ bool ResourceManager::save(const char *filename, const char *mergefilename) { | |||
92 | 114 | ||
93 | if (database==0) | 115 | if (database==0) |
94 | return false; | 116 | return false; |
95 | 117 | ||
96 | //check if we want to merge a database | 118 | //check if we want to merge a database |
97 | if (mergefilename) { | 119 | if (mergefilename) { |
98 | XrmDatabaseHelper olddatabase(mergefilename); | 120 | // force reload of file |
99 | if (olddatabase == 0) // did we load the file? | 121 | m_filename = mergefilename; |
122 | if (m_database) | ||
123 | delete m_database; | ||
124 | m_database = 0; | ||
125 | |||
126 | lock(); | ||
127 | |||
128 | if (!m_database) { | ||
129 | unlock(); | ||
100 | return false; | 130 | return false; |
101 | 131 | } | |
102 | XrmMergeDatabases(*database, &*olddatabase); // merge databases | 132 | |
103 | XrmPutFileDatabase(*olddatabase, filename); // save database to file | 133 | XrmMergeDatabases(*database, &**m_database); // merge databases |
104 | 134 | XrmPutFileDatabase(**m_database, filename); // save database to file | |
105 | *database = 0; // don't try to destroy the database | 135 | |
136 | // don't try to destroy the database (XrmMergeDatabases destroys it) | ||
137 | *database = 0; | ||
138 | unlock(); | ||
106 | } else // save database to file | 139 | } else // save database to file |
107 | XrmPutFileDatabase(*database, filename); | 140 | XrmPutFileDatabase(*database, filename); |
108 | 141 | ||
142 | m_filename = filename; | ||
109 | return true; | 143 | return true; |
110 | } | 144 | } |
111 | 145 | ||
@@ -115,5 +149,30 @@ void ResourceManager::ensureXrmIsInitialize() { | |||
115 | m_init = true; | 149 | m_init = true; |
116 | } | 150 | } |
117 | } | 151 | } |
118 | 152 | ||
153 | ResourceManager &ResourceManager::lock() { | ||
154 | ++m_db_lock; | ||
155 | // if the lock was zero, then load the database | ||
156 | if ((m_db_lock == 1 || !m_database) && | ||
157 | m_filename != "") { | ||
158 | m_database = new XrmDatabaseHelper(m_filename.c_str()); | ||
159 | |||
160 | // check that the database loaded ok | ||
161 | if (m_database && *m_database == 0) { | ||
162 | // didn't work | ||
163 | delete m_database; | ||
164 | m_database = 0; | ||
165 | } | ||
166 | } | ||
167 | |||
168 | return *this; | ||
169 | } | ||
170 | |||
171 | void ResourceManager::unlock() { | ||
172 | if (--m_db_lock == 0 && m_database) { | ||
173 | delete m_database; | ||
174 | m_database = 0; | ||
175 | } | ||
176 | } | ||
177 | |||
119 | }; // end namespace FbTk | 178 | }; // end namespace FbTk |
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 |
diff --git a/src/FbTk/XrmDatabaseHelper.hh b/src/FbTk/XrmDatabaseHelper.hh index 1b2618d..3388c7b 100644 --- a/src/FbTk/XrmDatabaseHelper.hh +++ b/src/FbTk/XrmDatabaseHelper.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: XrmDatabaseHelper.hh,v 1.1 2003/05/18 22:06:59 fluxgen Exp $ | 22 | // $Id: XrmDatabaseHelper.hh,v 1.2 2003/07/18 15:40:55 rathnor Exp $ |
23 | 23 | ||
24 | // This is a helper for XrmDatabase | 24 | // This is a helper for XrmDatabase |
25 | // when database goes out of scope | 25 | // when database goes out of scope |
@@ -34,6 +34,8 @@ | |||
34 | /** | 34 | /** |
35 | Helper class for XrmDatabase. | 35 | Helper class for XrmDatabase. |
36 | */ | 36 | */ |
37 | namespace FbTk { | ||
38 | |||
37 | class XrmDatabaseHelper | 39 | class XrmDatabaseHelper |
38 | { | 40 | { |
39 | public: | 41 | public: |
@@ -75,4 +77,5 @@ private: | |||
75 | XrmDatabase m_database; | 77 | XrmDatabase m_database; |
76 | }; | 78 | }; |
77 | 79 | ||
80 | }; // namespace FbTk | ||
78 | #endif //_XRMDATABASEHELPER_HH_ | 81 | #endif //_XRMDATABASEHELPER_HH_ |