aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Resource.cc21
-rw-r--r--src/Resource.hh49
2 files changed, 42 insertions, 28 deletions
diff --git a/src/Resource.cc b/src/Resource.cc
index 8bd4e26..cb871f1 100644
--- a/src/Resource.cc
+++ b/src/Resource.cc
@@ -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.cc,v 1.2 2002/02/04 06:47:34 fluxgen Exp $ 22// $Id: Resource.cc,v 1.3 2002/07/20 09:51:26 fluxgen Exp $
23 23
24#include "Resource.hh" 24#include "Resource.hh"
25#include "XrmDatabaseHelper.hh" 25#include "XrmDatabaseHelper.hh"
@@ -55,11 +55,11 @@ bool ResourceManager::load(const char *filename) {
55 for (; i != i_end; ++i) { 55 for (; i != i_end; ++i) {
56 56
57 Resource_base *resource = *i; 57 Resource_base *resource = *i;
58 if (XrmGetResource(*database, resource->getName().c_str(), 58 if (XrmGetResource(*database, resource->name().c_str(),
59 resource->getAltName().c_str(), &value_type, &value)) 59 resource->altName().c_str(), &value_type, &value))
60 resource->setFromString(value.addr); 60 resource->setFromString(value.addr);
61 else { 61 else {
62 cerr<<"Faild to read: "<<resource->getName()<<endl; 62 cerr<<"Failed to read: "<<resource->name()<<endl;
63 cerr<<"Setting default value"<<endl; 63 cerr<<"Setting default value"<<endl;
64 resource->setDefaultValue(); 64 resource->setDefaultValue();
65 } 65 }
@@ -87,7 +87,7 @@ bool ResourceManager::save(const char *filename, const char *mergefilename) {
87 //write all resources to database 87 //write all resources to database
88 for (; i != i_end; ++i) { 88 for (; i != i_end; ++i) {
89 Resource_base *resource = *i; 89 Resource_base *resource = *i;
90 rc_string = resource->getName() + string(": ") + resource->getString(); 90 rc_string = resource->name() + string(": ") + resource->getString();
91 XrmPutLineResource(&*database, rc_string.c_str()); 91 XrmPutLineResource(&*database, rc_string.c_str());
92 } 92 }
93 93
@@ -97,13 +97,14 @@ bool ResourceManager::save(const char *filename, const char *mergefilename) {
97 //check if we want to merge a database 97 //check if we want to merge a database
98 if (mergefilename) { 98 if (mergefilename) {
99 XrmDatabaseHelper olddatabase(mergefilename); 99 XrmDatabaseHelper olddatabase(mergefilename);
100 if (olddatabase == 0) 100 if (olddatabase == 0) // did we load the file?
101 return false; 101 return false;
102 102
103 XrmMergeDatabases(*database, &*olddatabase); 103 XrmMergeDatabases(*database, &*olddatabase); // merge databases
104 XrmPutFileDatabase(*olddatabase, filename); //save database to file 104 XrmPutFileDatabase(*olddatabase, filename); // save database to file
105 *database=0; //don't try to destroy the database 105
106 } else //save database to file 106 *database = 0; // don't try to destroy the database
107 } else // save database to file
107 XrmPutFileDatabase(*database, filename); 108 XrmPutFileDatabase(*database, filename);
108 109
109 return true; 110 return true;
diff --git a/src/Resource.hh b/src/Resource.hh
index 10a77f7..8055701 100644
--- a/src/Resource.hh
+++ b/src/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.5 2002/05/17 10:59:58 fluxgen Exp $ 22// $Id: Resource.hh,v 1.6 2002/07/20 09:51:03 fluxgen Exp $
23 23
24#ifndef RESOURCE_HH 24#ifndef RESOURCE_HH
25#define RESOURCE_HH 25#define RESOURCE_HH
@@ -31,23 +31,29 @@
31class Resource_base:private NotCopyable 31class Resource_base:private NotCopyable
32{ 32{
33public: 33public:
34 34 virtual ~Resource_base() { };
35 virtual void setFromString(char const *strval)=0;
36 virtual void setDefaultValue()=0;
37
38 virtual std::string getString()=0;
39 inline const std::string& getAltName() const { return m_altname; }
40 inline const std::string& getName() const { return m_name; }
41 35
36 /// set from string value
37 virtual void setFromString(char const *strval) = 0;
38 /// set default value
39 virtual void setDefaultValue() = 0;
40 /// get string value
41 virtual std::string getString() = 0;
42 /// get alternative name of this resource
43 inline const std::string& altName() const { return m_altname; }
44 /// get name of this resource
45 inline const std::string& name() const { return m_name; }
46
42protected: 47protected:
43 Resource_base(const std::string &name, const std::string &altname): 48 Resource_base(const std::string &name, const std::string &altname):
44 m_name(name), m_altname(altname) 49 m_name(name), m_altname(altname)
45 { 50 {
46 51
47 } 52 }
48 virtual ~Resource_base(){ }; 53
49private: 54private:
50 std::string m_name, m_altname; 55 std::string m_name; // name of this resource
56 std::string m_altname; // alternative name
51}; 57};
52 58
53class ResourceManager; 59class ResourceManager;
@@ -62,10 +68,10 @@ public:
62 m_value(val), m_defaultval(val), 68 m_value(val), m_defaultval(val),
63 m_rm(rm) 69 m_rm(rm)
64 { 70 {
65 m_rm.addResource(*this); 71 m_rm.addResource(*this); // add this to resource handler
66 } 72 }
67 ~Resource() { 73 virtual ~Resource() {
68 m_rm.removeResource(*this); 74 m_rm.removeResource(*this); // remove this from resource handler
69 } 75 }
70 76
71 inline void setDefaultValue() { m_value = m_defaultval; } 77 inline void setDefaultValue() { m_value = m_defaultval; }
@@ -87,10 +93,16 @@ class ResourceManager
87public: 93public:
88 typedef std::list<Resource_base *> ResourceList; 94 typedef std::list<Resource_base *> ResourceList;
89 95
90 ResourceManager(){ } 96 ResourceManager() { }
91 97 virtual ~ResourceManager() {}
92 bool load(const char *filename); 98 /**
93 bool save(const char *filename, const char *mergefilename=0); 99 load all resouces registered to this class
100 */
101 virtual bool load(const char *filename);
102 /**
103 save all resouces registered to this class
104 */
105 virtual bool save(const char *filename, const char *mergefilename=0);
94 template <class T> 106 template <class T>
95 void addResource(Resource<T> &r) { 107 void addResource(Resource<T> &r) {
96 m_resourcelist.push_back(&r); 108 m_resourcelist.push_back(&r);
@@ -100,8 +112,9 @@ public:
100 void removeResource(Resource<T> &r) { 112 void removeResource(Resource<T> &r) {
101 m_resourcelist.remove(&r); 113 m_resourcelist.remove(&r);
102 } 114 }
103private: 115protected:
104 static inline void ensureXrmIsInitialize(); 116 static inline void ensureXrmIsInitialize();
117private:
105 static bool m_init; 118 static bool m_init;
106 ResourceList m_resourcelist; 119 ResourceList m_resourcelist;
107 120