aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/Resource.hh
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/Resource.hh')
-rw-r--r--src/FbTk/Resource.hh56
1 files changed, 28 insertions, 28 deletions
diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh
index 20ce3ba..63137e7 100644
--- a/src/FbTk/Resource.hh
+++ b/src/FbTk/Resource.hh
@@ -55,8 +55,8 @@ private:
55class Resource_base:private FbTk::NotCopyable 55class Resource_base:private FbTk::NotCopyable
56{ 56{
57public: 57public:
58 virtual ~Resource_base() { }; 58 virtual ~Resource_base() { };
59 59
60 /// set from string value 60 /// set from string value
61 virtual void setFromString(char const *strval) = 0; 61 virtual void setFromString(char const *strval) = 0;
62 /// set default value 62 /// set default value
@@ -64,18 +64,18 @@ public:
64 /// get string value 64 /// get string value
65 virtual std::string getString() const = 0; 65 virtual std::string getString() const = 0;
66 /// get alternative name of this resource 66 /// get alternative name of this resource
67 inline const std::string& altName() const { return m_altname; } 67 const std::string& altName() const { return m_altname; }
68 /// get name of this resource 68 /// get name of this resource
69 inline const std::string& name() const { return m_name; } 69 const std::string& name() const { return m_name; }
70 70
71protected: 71protected:
72 Resource_base(const std::string &name, const std::string &altname): 72 Resource_base(const std::string &name, const std::string &altname):
73 m_name(name), m_altname(altname) 73 m_name(name), m_altname(altname)
74 { } 74 { }
75 75
76private: 76private:
77 std::string m_name; ///< name of this resource 77 std::string m_name; ///< name of this resource
78 std::string m_altname; ///< alternative name 78 std::string m_altname; ///< alternative name
79}; 79};
80 80
81template <typename T> 81template <typename T>
@@ -99,7 +99,7 @@ public:
99 /// @return true on success 99 /// @return true on success
100 virtual bool save(const char *filename, const char *mergefilename=0); 100 virtual bool save(const char *filename, const char *mergefilename=0);
101 101
102 102
103 103
104 /// Add resource to list, only used in Resource<T> 104 /// Add resource to list, only used in Resource<T>
105 template <class T> 105 template <class T>
@@ -122,21 +122,21 @@ public:
122 void setResourceValue(const std::string &resourcename, const std::string &value); 122 void setResourceValue(const std::string &resourcename, const std::string &value);
123 123
124 /** 124 /**
125 * Will search and cast the resource to Resource<Type>, 125 * Will search and cast the resource to Resource<Type>,
126 * it will throw exception if it fails 126 * it will throw exception if it fails
127 * @return reference to resource type 127 * @return reference to resource type
128 */ 128 */
129 template <typename ResourceType> 129 template <typename ResourceType>
130 Resource<ResourceType> &getResource(const std::string &resource); 130 Resource<ResourceType> &getResource(const std::string &resource);
131 131
132 // this marks the database as "in use" and will avoid reloading 132 // this marks the database as "in use" and will avoid reloading
133 // resources unless it is zero. 133 // resources unless it is zero.
134 // It returns this resource manager. Useful for passing to 134 // It returns this resource manager. Useful for passing to
135 // constructors like Object(m_rm.lock()) 135 // constructors like Object(m_rm.lock())
136 ResourceManager &lock(); 136 ResourceManager &lock();
137 void unlock(); 137 void unlock();
138 // for debugging 138 // for debugging
139 inline int lockDepth() const { return m_db_lock; } 139 int lockDepth() const { return m_db_lock; }
140 void dump() { 140 void dump() {
141 ResourceList::iterator it = m_resourcelist.begin(); 141 ResourceList::iterator it = m_resourcelist.begin();
142 ResourceList::iterator it_end = m_resourcelist.end(); 142 ResourceList::iterator it_end = m_resourcelist.end();
@@ -171,33 +171,33 @@ private:
171 */ 171 */
172template <typename T> 172template <typename T>
173class Resource:public Resource_base, public Accessor<T> { 173class Resource:public Resource_base, public Accessor<T> {
174public: 174public:
175 typedef T Type; 175 typedef T Type;
176 Resource(ResourceManager &rm, T val, 176 Resource(ResourceManager &rm, T val,
177 const std::string &name, const std::string &altname): 177 const std::string &name, const std::string &altname):
178 Resource_base(name, altname), 178 Resource_base(name, altname),
179 m_value(val), m_defaultval(val), 179 m_value(val), m_defaultval(val),
180 m_rm(rm) { 180 m_rm(rm) {
181 m_rm.addResource(*this); // add this to resource handler 181 m_rm.addResource(*this); // add this to resource handler
182 } 182 }
183 virtual ~Resource() { 183 virtual ~Resource() {
184 m_rm.removeResource(*this); // remove this from resource handler 184 m_rm.removeResource(*this); // remove this from resource handler
185 } 185 }
186 186
187 inline void setDefaultValue() { m_value = m_defaultval; } 187 void setDefaultValue() { m_value = m_defaultval; }
188 /// sets resource from string, specialized, must be implemented 188 /// sets resource from string, specialized, must be implemented
189 void setFromString(const char *strval); 189 void setFromString(const char *strval);
190 inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;} 190 Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;}
191 /// specialized, must be implemented 191 /// specialized, must be implemented
192 /// @return string value of resource 192 /// @return string value of resource
193 std::string getString() const; 193 std::string getString() const;
194 194
195 inline operator T() const { return m_value; } 195 operator T() const { return m_value; }
196 inline T& get() { return m_value; } 196 T& get() { return m_value; }
197 inline T& operator*() { return m_value; } 197 T& operator*() { return m_value; }
198 inline const T& operator*() const { return m_value; } 198 const T& operator*() const { return m_value; }
199 inline T *operator->() { return &m_value; } 199 T *operator->() { return &m_value; }
200 inline const T *operator->() const { return &m_value; } 200 const T *operator->() const { return &m_value; }
201private: 201private:
202 T m_value, m_defaultval; 202 T m_value, m_defaultval;
203 ResourceManager &m_rm; 203 ResourceManager &m_rm;
@@ -233,17 +233,17 @@ void ResourceManager::addResource(Resource<T> &r) {
233 233
234 unlock(); 234 unlock();
235} 235}
236 236
237 237
238template <typename ResourceType> 238template <typename ResourceType>
239Resource<ResourceType> &ResourceManager::getResource(const std::string &resname) { 239Resource<ResourceType> &ResourceManager::getResource(const std::string &resname) {
240 Resource_base *res = findResource(resname); 240 Resource_base *res = findResource(resname);
241 if (res == 0) { 241 if (res == 0) {
242 throw ResourceException("Could not find resource \"" + 242 throw ResourceException("Could not find resource \"" +
243 resname + "\""); 243 resname + "\"");
244 } 244 }
245 245
246 Resource<ResourceType> *res_type = 246 Resource<ResourceType> *res_type =
247 dynamic_cast<Resource<ResourceType> *>(res); 247 dynamic_cast<Resource<ResourceType> *>(res);
248 if (res_type == 0) { 248 if (res_type == 0) {
249 throw ResourceException("Could not convert resource \"" + 249 throw ResourceException("Could not convert resource \"" +