diff options
author | fluxgen <fluxgen> | 2002-01-18 00:47:33 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2002-01-18 00:47:33 (GMT) |
commit | 5d72cb458406fc42f7ee68d8f8dbdf99d6f9a192 (patch) | |
tree | 60783e5381495907c00a8ca3ba074a1429dfd5b5 /src/Resource.hh | |
parent | 34f9c5e44685a56baf66b38dda30bc062548cc66 (diff) | |
download | fluxbox_pavel-5d72cb458406fc42f7ee68d8f8dbdf99d6f9a192.zip fluxbox_pavel-5d72cb458406fc42f7ee68d8f8dbdf99d6f9a192.tar.bz2 |
added Resource.cc/.hh
Diffstat (limited to 'src/Resource.hh')
-rw-r--r-- | src/Resource.hh | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/Resource.hh b/src/Resource.hh new file mode 100644 index 0000000..38bea30 --- /dev/null +++ b/src/Resource.hh | |||
@@ -0,0 +1,107 @@ | |||
1 | // Resource.hh | ||
2 | // Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | #ifndef _RESOURCE_HH_ | ||
23 | #define _RESOURCE_HH_ | ||
24 | |||
25 | #include "NotCopyable.hh" | ||
26 | #include <string> | ||
27 | #include <list> | ||
28 | |||
29 | class Resource_base:private NotCopyable | ||
30 | { | ||
31 | public: | ||
32 | |||
33 | virtual void setFromString(char const *strval)=0; | ||
34 | virtual void setDefaultValue()=0; | ||
35 | |||
36 | virtual std::string getString()=0; | ||
37 | inline std::string& getAltName() { return m_altname; } | ||
38 | inline std::string& getName() { return m_name; } | ||
39 | |||
40 | protected: | ||
41 | Resource_base(const std::string &name, const std::string &altname): | ||
42 | m_name(name), m_altname(altname) | ||
43 | { | ||
44 | |||
45 | } | ||
46 | virtual ~Resource_base(){ }; | ||
47 | private: | ||
48 | std::string m_name, m_altname; | ||
49 | }; | ||
50 | |||
51 | class ResourceManager; | ||
52 | |||
53 | template <typename T> | ||
54 | class Resource:public Resource_base | ||
55 | { | ||
56 | public: | ||
57 | Resource(ResourceManager &rm, T val, | ||
58 | const std::string &name, const std::string &altname): | ||
59 | Resource_base(name, altname), | ||
60 | m_value(val), m_defaultval(val), | ||
61 | m_rm(rm) | ||
62 | { | ||
63 | m_rm.addResource(*this); | ||
64 | } | ||
65 | ~Resource() { | ||
66 | m_rm.removeResource(*this); | ||
67 | } | ||
68 | |||
69 | inline void setDefaultValue() { m_value = m_defaultval; } | ||
70 | void setFromString(const char *strval); | ||
71 | inline Resource<T>& operator = (const T& newvalue) { m_value = newvalue; return *this;} | ||
72 | |||
73 | std::string getString(); | ||
74 | inline T& operator*(void) { return m_value; } | ||
75 | inline T *operator->(void) { return &m_value; } | ||
76 | private: | ||
77 | T m_value, m_defaultval; | ||
78 | ResourceManager &m_rm; | ||
79 | }; | ||
80 | |||
81 | class ResourceManager | ||
82 | { | ||
83 | public: | ||
84 | typedef std::list<Resource_base *> ResourceList; | ||
85 | |||
86 | ResourceManager(){ } | ||
87 | |||
88 | bool load(const char *filename); | ||
89 | bool save(const char *filename, const char *mergefilename=0); | ||
90 | template <class T> | ||
91 | void addResource(Resource<T> &r) { | ||
92 | m_resourcelist.push_back(&r); | ||
93 | m_resourcelist.unique(); | ||
94 | } | ||
95 | template <class T> | ||
96 | void removeResource(Resource<T> &r) { | ||
97 | m_resourcelist.remove(&r); | ||
98 | } | ||
99 | private: | ||
100 | static inline void ensureXrmIsInitialize(); | ||
101 | static bool m_init; | ||
102 | ResourceList m_resourcelist; | ||
103 | |||
104 | }; | ||
105 | |||
106 | |||
107 | #endif //_RESOURCE_HH_ | ||