diff options
Diffstat (limited to 'src/FbTk/Accessor.hh')
-rw-r--r-- | src/FbTk/Accessor.hh | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/FbTk/Accessor.hh b/src/FbTk/Accessor.hh new file mode 100644 index 0000000..88db1aa --- /dev/null +++ b/src/FbTk/Accessor.hh | |||
@@ -0,0 +1,85 @@ | |||
1 | // Accessor.hh | ||
2 | // Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot 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 FBTK_ACCESSOR_HH | ||
23 | #define FBTK_ACCESSOR_HH | ||
24 | |||
25 | namespace FbTk { | ||
26 | |||
27 | // base class for objects that act like data type T | ||
28 | template <typename T> | ||
29 | class Accessor { | ||
30 | public: | ||
31 | virtual Accessor<T> &operator =(const T &val) = 0; | ||
32 | virtual operator T() const = 0; | ||
33 | }; | ||
34 | |||
35 | // essentially just a reference | ||
36 | template <typename T> | ||
37 | class SimpleAccessor: public Accessor<T> { | ||
38 | public: | ||
39 | SimpleAccessor(T &val): m_val(val) { } | ||
40 | inline Accessor<T> &operator =(const T &val) { m_val = val; return *this; } | ||
41 | inline operator T() const { return m_val; } | ||
42 | |||
43 | private: | ||
44 | T &m_val; | ||
45 | }; | ||
46 | |||
47 | // use object methods as an accessor | ||
48 | template <typename T, typename Receiver> | ||
49 | class ObjectAccessor: public Accessor<T> { | ||
50 | public: | ||
51 | typedef T (Receiver:: *Getter)() const; | ||
52 | typedef void (Receiver:: *Setter)(T &); | ||
53 | ObjectAccessor(Receiver &r, Getter g, Setter s): | ||
54 | m_receiver(r), m_getter(g), m_setter(s) { } | ||
55 | |||
56 | inline operator T() const { return (m_receiver.*m_getter)(); } | ||
57 | inline Accessor<T> &operator =(const T &val) { | ||
58 | (m_receiver.*m_setter)(val); return *this; | ||
59 | } | ||
60 | |||
61 | private: | ||
62 | Receiver &m_receiver; | ||
63 | Getter m_getter; | ||
64 | Setter m_setter; | ||
65 | }; | ||
66 | |||
67 | // same as above but with no set method | ||
68 | template <typename T, typename Receiver> | ||
69 | class ConstObjectAccessor: public Accessor<T> { | ||
70 | public: | ||
71 | typedef T (Receiver:: *Getter)() const; | ||
72 | ConstObjectAccessor(const Receiver &r, Getter g): | ||
73 | m_receiver(r), m_getter(g) { } | ||
74 | |||
75 | inline operator T() const { return (m_receiver.*m_getter)(); } | ||
76 | inline Accessor<T> &operator =(const T &val) { return *this; } | ||
77 | |||
78 | private: | ||
79 | const Receiver &m_receiver; | ||
80 | Getter m_getter; | ||
81 | }; | ||
82 | |||
83 | }; // end namespace FbTk | ||
84 | |||
85 | #endif // FBTK_ACCESSOR_HH | ||