00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef FBTK_RESOURCE_HH
00025 #define FBTK_RESOURCE_HH
00026
00027 #include "NotCopyable.hh"
00028
00029 #include <string>
00030 #include <list>
00031 #include <iostream>
00032
00033 #include <X11/Xlib.h>
00034 #include <X11/Xresource.h>
00035
00036 namespace FbTk {
00037
00038 class XrmDatabaseHelper;
00039
00041 class Resource_base:private FbTk::NotCopyable
00042 {
00043 public:
00044 virtual ~Resource_base() { };
00045
00047 virtual void setFromString(char const *strval) = 0;
00049 virtual void setDefaultValue() = 0;
00051 virtual std::string getString() = 0;
00053 inline const std::string& altName() const { return m_altname; }
00055 inline const std::string& name() const { return m_name; }
00056
00057 protected:
00058 Resource_base(const std::string &name, const std::string &altname):
00059 m_name(name), m_altname(altname)
00060 { }
00061
00062 private:
00063 std::string m_name;
00064 std::string m_altname;
00065 };
00066
00067 template <typename T>
00068 class Resource;
00069
00070 class ResourceManager
00071 {
00072 public:
00073 typedef std::list<Resource_base *> ResourceList;
00074
00075
00076
00077 ResourceManager(const char *filename, bool lock_db);
00078 virtual ~ResourceManager();
00079
00082 virtual bool load(const char *filename);
00083
00086 virtual bool save(const char *filename, const char *mergefilename=0);
00087
00088
00089
00091 template <class T>
00092 void addResource(Resource<T> &r);
00093
00095 template <class T>
00096 void removeResource(Resource<T> &r) {
00097 m_resourcelist.remove(&r);
00098 }
00099
00100 Resource_base *findResource(const std::string &resourcename);
00101 std::string resourceValue(const std::string &resourcename);
00102 void setResourceValue(const std::string &resourcename, const std::string &value);
00103
00104
00105
00106
00107
00108 ResourceManager &lock();
00109 void unlock();
00110
00111 inline int lockDepth() const { return m_db_lock; }
00112 void dump() {
00113 ResourceList::iterator it = m_resourcelist.begin();
00114 ResourceList::iterator it_end = m_resourcelist.end();
00115 for (; it != it_end; ++it) {
00116 std::cerr<<(*it)->name()<<std::endl;
00117 }
00118 }
00119 protected:
00120 static void ensureXrmIsInitialize();
00121
00122 int m_db_lock;
00123
00124 private:
00125 static bool m_init;
00126 ResourceList m_resourcelist;
00127
00128 XrmDatabaseHelper *m_database;
00129
00130 std::string m_filename;
00131 };
00132
00133
00135
00144 template <typename T>
00145 class Resource:public Resource_base
00146 {
00147 public:
00148 Resource(ResourceManager &rm, T val,
00149 const std::string &name, const std::string &altname):
00150 Resource_base(name, altname),
00151 m_value(val), m_defaultval(val),
00152 m_rm(rm)
00153 {
00154 m_rm.addResource(*this);
00155 }
00156 virtual ~Resource() {
00157 m_rm.removeResource(*this);
00158 }
00159
00160 inline void setDefaultValue() { m_value = m_defaultval; }
00162 void setFromString(const char *strval);
00163 inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;}
00166 std::string getString();
00167
00168 inline T& operator*() { return m_value; }
00169 inline const T& operator*() const { return m_value; }
00170 inline T *operator->() { return &m_value; }
00171 inline const T *operator->() const { return &m_value; }
00172 private:
00173 T m_value, m_defaultval;
00174 ResourceManager &m_rm;
00175 };
00176
00177
00178
00179 template <class T>
00180 void ResourceManager::addResource(Resource<T> &r) {
00181 m_resourcelist.push_back(&r);
00182 m_resourcelist.unique();
00183
00184
00185 lock();
00186
00187 if (m_database == 0) {
00188 unlock();
00189 return;
00190 }
00191
00192 XrmValue value;
00193 char *value_type;
00194
00195
00196 if (XrmGetResource(**m_database, r.name().c_str(),
00197 r.altName().c_str(), &value_type, &value)) {
00198 r.setFromString(value.addr);
00199 } else {
00200 std::cerr<<"Failed to read: "<<r.name()<<std::endl;
00201 std::cerr<<"Setting default value"<<std::endl;
00202 r.setDefaultValue();
00203 }
00204
00205 unlock();
00206 }
00207
00208
00209 }
00210
00211 #endif // FBTK_RESOURCE_HH