news download themes documentation links










Resource.hh

00001 // Resource.hh
00002 // Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net)
00003 //
00004 // Permission is hereby granted, free of charge, to any person obtaining a
00005 // copy of this software and associated documentation files (the "Software"),
00006 // to deal in the Software without restriction, including without limitation
00007 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 // and/or sell copies of the Software, and to permit persons to whom the
00009 // Software is furnished to do so, subject to the following conditions:
00010 //
00011 // The above copyright notice and this permission notice shall be included in
00012 // all copies or substantial portions of the Software.
00013 //
00014 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00017 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00019 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00020 // DEALINGS IN THE SOFTWARE.
00021 
00022 // $Id: Resource.hh,v 1.7 2003/12/19 18:25:39 fluxgen Exp $
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     // lock specifies if the database should be opened with one level locked
00076     // (useful for constructing inside initial set of constructors)
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     // this marks the database as "in use" and will avoid reloading 
00105     // resources unless it is zero.
00106     // It returns this resource manager. Useful for passing to 
00107     // constructors like Object(m_rm.lock())
00108     ResourceManager &lock();
00109     void unlock();
00110     // for debugging
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); // add this to resource handler
00155     }
00156     virtual ~Resource() {
00157         m_rm.removeResource(*this); // remove this from resource handler
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 // add the resource and load its value
00179 template <class T>
00180 void ResourceManager::addResource(Resource<T> &r) {
00181     m_resourcelist.push_back(&r);
00182     m_resourcelist.unique();
00183 
00184     // lock ensures that the database is loaded.
00185     lock();
00186 
00187     if (m_database == 0) {
00188         unlock();
00189         return;
00190     }
00191 
00192     XrmValue value;
00193     char *value_type;
00194 
00195     // now, load the value for this resource
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 } // end namespace FbTk
00210 
00211 #endif // FBTK_RESOURCE_HH

Fluxbox CVS-Jan-2003




      



Got comments about the page? Send them to webmaster.
If you have general Fluxbox related questions ask them on our irc channel or mailing lists.

Show Source








Designed by aLEczapKA