diff options
author | Mark Tiefenbruck <mark@fluxbox.org> | 2007-12-27 21:55:24 (GMT) |
---|---|---|
committer | Mark Tiefenbruck <mark@fluxbox.org> | 2007-12-27 21:55:24 (GMT) |
commit | b5c354b994bc06667abe35e2d528c0f025703c4e (patch) | |
tree | 081fd3207053a06cb50931ccec3237bc614f10eb /src/FbTk/DefaultValue.hh | |
parent | 1f5cd12facc662de240b36bf3c5c14f40adf391b (diff) | |
download | fluxbox_pavel-b5c354b994bc06667abe35e2d528c0f025703c4e.zip fluxbox_pavel-b5c354b994bc06667abe35e2d528c0f025703c4e.tar.bz2 |
architecture astronomy
Diffstat (limited to 'src/FbTk/DefaultValue.hh')
-rw-r--r-- | src/FbTk/DefaultValue.hh | 54 |
1 files changed, 14 insertions, 40 deletions
diff --git a/src/FbTk/DefaultValue.hh b/src/FbTk/DefaultValue.hh index 4e7eb49..95b534a 100644 --- a/src/FbTk/DefaultValue.hh +++ b/src/FbTk/DefaultValue.hh | |||
@@ -22,58 +22,32 @@ | |||
22 | #ifndef FBTK_DEFAULTVALUE_HH | 22 | #ifndef FBTK_DEFAULTVALUE_HH |
23 | #define FBTK_DEFAULTVALUE_HH | 23 | #define FBTK_DEFAULTVALUE_HH |
24 | 24 | ||
25 | namespace FbTk { | 25 | #include "Accessor.hh" |
26 | |||
27 | // classes for overriding default values without having to listen for changes | ||
28 | template <typename T> | ||
29 | class DefaultValue { | ||
30 | public: | ||
31 | DefaultValue(const T &def): | ||
32 | m_default(def), m_actual(def), m_use_default(true) { } | ||
33 | 26 | ||
34 | inline const T &get() const { return m_use_default ? m_default : m_actual; } | 27 | namespace FbTk { |
35 | inline void set(const T &val) { m_use_default = false; m_actual = val; } | ||
36 | inline void restoreDefault() { m_use_default = true; } | ||
37 | inline bool isDefault() const { return m_use_default; } | ||
38 | |||
39 | inline DefaultValue<T> &operator =(const T &val) { | ||
40 | set(val); return *this; | ||
41 | } | ||
42 | 28 | ||
43 | inline operator T() const { return get(); } | 29 | // class for overriding default values and restoring them later |
44 | 30 | ||
45 | private: | 31 | // Ret = type of value that gets returned |
46 | const T &m_default; | 32 | // Def = type of default value -- may be Accessor<Ret> &, for example |
47 | T m_actual; | 33 | template <typename Ret, typename Def=Ret &> |
48 | bool m_use_default; | 34 | class DefaultValue: public Accessor<Ret> { |
49 | }; | ||
50 | |||
51 | // designed for use with built-in types T, thus no need to return references | ||
52 | template <typename T, typename Receiver> | ||
53 | class DefaultAccessor { | ||
54 | public: | 35 | public: |
55 | typedef T (Receiver:: *Accessor)() const; | 36 | DefaultValue(const Def def): |
56 | DefaultAccessor(const Receiver &r, Accessor a): | 37 | m_default(def), m_actual(def), m_use_default(true) { } |
57 | m_receiver(r), m_accessor(a), m_actual((r.*a)()), | ||
58 | m_use_default(true) { } | ||
59 | 38 | ||
60 | inline const T get() const { | ||
61 | return m_use_default ? (m_receiver.*m_accessor)() : m_actual; | ||
62 | } | ||
63 | inline void set(const T &val) { m_use_default = false; m_actual = val; } | ||
64 | inline void restoreDefault() { m_use_default = true; } | 39 | inline void restoreDefault() { m_use_default = true; } |
65 | inline bool isDefault() const { return m_use_default; } | 40 | inline bool isDefault() const { return m_use_default; } |
66 | 41 | ||
67 | inline DefaultAccessor<T, Receiver> &operator =(const T &val) { | 42 | inline DefaultValue<Ret, Def> &operator =(const Ret &val) { |
68 | set(val); return *this; | 43 | m_use_default = false; m_actual = val; return *this; |
69 | } | 44 | } |
70 | 45 | ||
71 | inline operator T() const { return get(); } | 46 | inline operator Ret() const { return m_use_default ? m_default : m_actual; } |
72 | 47 | ||
73 | private: | 48 | private: |
74 | const Receiver &m_receiver; | 49 | const Def m_default; |
75 | Accessor m_accessor; | 50 | Ret m_actual; |
76 | T m_actual; | ||
77 | bool m_use_default; | 51 | bool m_use_default; |
78 | }; | 52 | }; |
79 | 53 | ||