aboutsummaryrefslogtreecommitdiff
path: root/src/ObjectResource.hh
diff options
context:
space:
mode:
authorsimonb <simonb>2007-01-07 14:20:31 (GMT)
committersimonb <simonb>2007-01-07 14:20:31 (GMT)
commit93ec253f0d6790e79017f74ae5413ef0363b5cb1 (patch)
treebb6ae26d09275b5a73503cebc052b30ff8735bb6 /src/ObjectResource.hh
parent440c69afa436150f2a797aa8f192d68090832c5c (diff)
downloadfluxbox-93ec253f0d6790e79017f74ae5413ef0363b5cb1.zip
fluxbox-93ec253f0d6790e79017f74ae5413ef0363b5cb1.tar.bz2
forgot to svn add the files
Diffstat (limited to 'src/ObjectResource.hh')
-rw-r--r--src/ObjectResource.hh145
1 files changed, 145 insertions, 0 deletions
diff --git a/src/ObjectResource.hh b/src/ObjectResource.hh
new file mode 100644
index 0000000..44e52b6
--- /dev/null
+++ b/src/ObjectResource.hh
@@ -0,0 +1,145 @@
1// ObjectResource.hh for Fluxbox
2// Copyright (c) 2007 Henrik Kinnunen (fluxgen at fluxbox dot org)
3// and Simon Bowden (rathnor at users.sourceforge.net)
4//
5// Permission is hereby granted, free of charge, to any person obtaining a
6// copy of this software and associated documentation files (the "Software"),
7// to deal in the Software without restriction, including without limitation
8// the rights to use, copy, modify, merge, publish, distribute, sublicense,
9// and/or sell copies of the Software, and to permit persons to whom the
10// Software is furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21// DEALINGS IN THE SOFTWARE.
22
23// $Id$
24
25#ifndef OBJECTRESOURCE_HH
26#define OBJECTRESOURCE_HH
27
28/* This is a generic resource that can be used as an accessor to a value in an object.
29 The constructors allow to select between:
30 1a. giving an object of ObjectType, OR
31 1b. giving a function returning an object of ObjectType
32
33 2a. a function that sets a value
34 2b. a function that toggles a value
35 */
36
37template <typename ObjectType, typename ValueType>
38class ObjectResource {
39public:
40 typedef ValueType (ObjectType::* getResourceType)() const;
41 typedef void (ObjectType::* setResourceType)(ValueType);
42 typedef void (ObjectType::* toggleResourceType)();
43 typedef ObjectType* (*getResourceObject)();
44
45 ObjectResource(ObjectType *object, getResourceType get, setResourceType set, ValueType a_default) :
46 m_get(get), m_set(set), m_istoggle(false), m_object(object),
47 m_default(a_default), m_use_accessor(false) {
48 }
49
50 ObjectResource(ObjectType *object, getResourceType get, toggleResourceType toggle, ValueType a_default) :
51 m_get(get), m_toggle(toggle), m_istoggle(true), m_object(object),
52 m_default(a_default), m_use_accessor(false) {
53 }
54
55 ObjectResource(getResourceObject object_accessor, getResourceType get, setResourceType set, ValueType a_default) :
56 m_get(get), m_set(set), m_istoggle(false), m_object_accessor(object_accessor),
57 m_default(a_default), m_use_accessor(true) {
58 }
59
60 ObjectResource(getResourceObject object_accessor, getResourceType get, toggleResourceType toggle, ValueType a_default) :
61 m_get(get), m_toggle(toggle), m_istoggle(true), m_object_accessor(object_accessor),
62 m_default(a_default), m_use_accessor(true) {
63 }
64
65 ObjectResource<ObjectType, ValueType>& operator = (const ValueType newvalue) {
66 ObjectType * obj = getObject();
67 if (!obj)
68 return *this;
69
70 if (m_istoggle) {
71 if (newvalue != (operator*)())
72 (obj->*m_toggle)();
73 } else {
74 (obj->*m_set)(newvalue);
75 }
76 return *this;
77 }
78
79 ObjectResource<ObjectType, ValueType>& operator += (const ValueType newvalue) {
80 ObjectType * obj = getObject();
81 if (obj && !m_istoggle)
82 (obj->*m_set)((operator*)()+newvalue);
83 return *this;
84 }
85
86 ObjectResource<ObjectType, ValueType>& operator -= (const ValueType newvalue) {
87 ObjectType * obj = getObject();
88 if (obj && !m_istoggle)
89 (obj->*m_set)((operator*)()-newvalue);
90 return *this;
91 }
92
93 // this is a touch dirty, but it makes us compatible with FbTk::Resource<int> in IntResMenuItem
94 ObjectResource<ObjectType, ValueType>& get() {
95 return *this;
96 }
97
98 ValueType operator*() {
99 ObjectType * obj = getObject();
100 if (!obj)
101 return m_default;
102
103 return (obj->*m_get)();
104 }
105
106 const ValueType operator*() const {
107 ObjectType * obj = getObject();
108 if (!obj)
109 return m_default;
110
111 return (obj->*m_get)();
112 }
113
114private:
115 // choose one get and one set function
116
117 ObjectType * getObject() {
118 if (m_use_accessor)
119 return (*m_object_accessor)();
120 else
121 return m_object;
122 }
123
124 getResourceType m_get;
125
126 union {
127 setResourceType m_set;
128 toggleResourceType m_toggle;
129 };
130
131 bool m_istoggle;
132
133 union {
134 ObjectType *m_object;
135 getResourceObject m_object_accessor;
136 };
137
138 // default is only used when object isn't set (saves crashes)
139 ValueType m_default;
140
141 bool m_use_accessor;
142};
143
144
145#endif // OBJECTRESOURCE_HH