diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-05-19 10:32:42 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2011-09-14 17:39:10 (GMT) |
commit | 7bca844581a38fbf46f8a3761cbd5e31bd9005d6 (patch) | |
tree | 14c1a7ceae330b4bcb3310284e508269cc2b0044 /src/FbTk/Slot.hh | |
parent | 0a40d1caf3180538d83c7016939d91c7beaf6c2c (diff) | |
download | fluxbox_paul-7bca844581a38fbf46f8a3761cbd5e31bd9005d6.zip fluxbox_paul-7bca844581a38fbf46f8a3761cbd5e31bd9005d6.tar.bz2 |
Prepare the Slot classes to be used independently of Signals
Added some polish around them and, to mark this special occasion, moved them out of the SigImpl
namespace.
PS: This partially reverts commit 0775350fee345e37fb59835dda4d85664346b606, since I had to
reintroduce ReturnType template parameter, because it will be used in other places. But Signal classes
remain without the ReturnType, because I still cannot imagine how would it be used.
Diffstat (limited to 'src/FbTk/Slot.hh')
-rw-r--r-- | src/FbTk/Slot.hh | 90 |
1 files changed, 59 insertions, 31 deletions
diff --git a/src/FbTk/Slot.hh b/src/FbTk/Slot.hh index ea7ee2f..71bf6ab 100644 --- a/src/FbTk/Slot.hh +++ b/src/FbTk/Slot.hh | |||
@@ -22,7 +22,7 @@ | |||
22 | #ifndef FBTK_SLOT_HH | 22 | #ifndef FBTK_SLOT_HH |
23 | #define FBTK_SLOT_HH | 23 | #define FBTK_SLOT_HH |
24 | 24 | ||
25 | #include "RefCount.hh" | 25 | #include "NotCopyable.hh" |
26 | 26 | ||
27 | namespace FbTk { | 27 | namespace FbTk { |
28 | 28 | ||
@@ -31,71 +31,99 @@ namespace SigImpl { | |||
31 | 31 | ||
32 | struct EmptyArg {}; | 32 | struct EmptyArg {}; |
33 | 33 | ||
34 | class SlotBase { | 34 | /** A base class for all slots. It's purpose is to provide a virtual destructor and to enable the |
35 | * Signal class to hold a pointer to a generic slot. | ||
36 | */ | ||
37 | class SlotBase: private FbTk::NotCopyable { | ||
35 | public: | 38 | public: |
36 | virtual ~SlotBase() {} | 39 | virtual ~SlotBase() {} |
37 | }; | 40 | }; |
38 | 41 | ||
39 | template<typename Arg1, typename Arg2, typename Arg3> | 42 | } // namespace SigImpl |
40 | class SlotTemplate: public SlotBase { | 43 | |
44 | /** Declares a pure virtual function call operator with a specific number of arguments (depending | ||
45 | * on the template specialization). This allows us to "call" any functor in an opaque way. | ||
46 | */ | ||
47 | template<typename ReturnType, typename Arg1 = SigImpl::EmptyArg, | ||
48 | typename Arg2 = SigImpl::EmptyArg, typename Arg3 = SigImpl::EmptyArg> | ||
49 | class Slot: public SigImpl::SlotBase { | ||
50 | public: | ||
51 | virtual ReturnType operator()(Arg1, Arg2, Arg3) = 0; | ||
52 | }; | ||
53 | |||
54 | /// Specialization for two arguments | ||
55 | template<typename ReturnType, typename Arg1, typename Arg2> | ||
56 | class Slot<ReturnType, Arg1, Arg2, SigImpl::EmptyArg>: public SigImpl::SlotBase { | ||
57 | public: | ||
58 | virtual ReturnType operator()(Arg1, Arg2) = 0; | ||
59 | }; | ||
60 | |||
61 | /// Specialization for one argument | ||
62 | template<typename ReturnType, typename Arg1> | ||
63 | class Slot<ReturnType, Arg1, SigImpl::EmptyArg, SigImpl::EmptyArg>: public SigImpl::SlotBase { | ||
64 | public: | ||
65 | virtual ReturnType operator()(Arg1) = 0; | ||
66 | }; | ||
67 | |||
68 | /// Specialization for no arguments | ||
69 | template<typename ReturnType> | ||
70 | class Slot<ReturnType, SigImpl::EmptyArg, SigImpl::EmptyArg, SigImpl::EmptyArg>: public SigImpl::SlotBase { | ||
41 | public: | 71 | public: |
42 | virtual void operator()(Arg1, Arg2, Arg3) = 0; | 72 | virtual ReturnType operator()() = 0; |
43 | }; | 73 | }; |
44 | 74 | ||
45 | template<typename Arg1, typename Arg2, typename Arg3, typename Functor> | 75 | /** A class which knows how to call a specific functor. It inherits from Slot and implemetents |
46 | class Slot: public SlotTemplate<Arg1, Arg2, Arg3> { | 76 | * the function call operator |
77 | */ | ||
78 | template<typename Functor, typename ReturnType, typename Arg1 = SigImpl::EmptyArg, | ||
79 | typename Arg2 = SigImpl::EmptyArg, typename Arg3 = SigImpl::EmptyArg> | ||
80 | class SlotImpl: public Slot<ReturnType, Arg1, Arg2, Arg3> { | ||
47 | public: | 81 | public: |
48 | virtual void operator()(Arg1 arg1, Arg2 arg2, Arg3 arg3) | 82 | virtual ReturnType operator()(Arg1 arg1, Arg2 arg2, Arg3 arg3) |
49 | { m_functor(arg1, arg2, arg3); } | 83 | { return m_functor(arg1, arg2, arg3); } |
50 | 84 | ||
51 | Slot(Functor functor) : m_functor(functor) {} | 85 | SlotImpl(Functor functor) : m_functor(functor) {} |
52 | 86 | ||
53 | private: | 87 | private: |
54 | Functor m_functor; | 88 | Functor m_functor; |
55 | }; | 89 | }; |
56 | 90 | ||
57 | // specialization for two arguments | 91 | /// Specialization for two arguments |
58 | template<typename Arg1, typename Arg2, typename Functor> | 92 | template<typename Functor, typename ReturnType, typename Arg1, typename Arg2> |
59 | class Slot<Arg1, Arg2, EmptyArg, Functor>: public SlotTemplate<Arg1, Arg2, EmptyArg> { | 93 | class SlotImpl<Functor, ReturnType, Arg1, Arg2, SigImpl::EmptyArg>: public Slot<ReturnType, Arg1, Arg2> { |
60 | public: | 94 | public: |
61 | virtual void operator()(Arg1 arg1, Arg2 arg2, EmptyArg) | 95 | virtual ReturnType operator()(Arg1 arg1, Arg2 arg2) { return m_functor(arg1, arg2); } |
62 | { m_functor(arg1, arg2); } | ||
63 | 96 | ||
64 | Slot(Functor functor) : m_functor(functor) {} | 97 | SlotImpl(Functor functor) : m_functor(functor) {} |
65 | 98 | ||
66 | private: | 99 | private: |
67 | Functor m_functor; | 100 | Functor m_functor; |
68 | }; | 101 | }; |
69 | 102 | ||
70 | // specialization for one argument | 103 | /// Specialization for one argument |
71 | template<typename Arg1, typename Functor> | 104 | template<typename Functor, typename ReturnType, typename Arg1> |
72 | class Slot<Arg1, EmptyArg, EmptyArg, Functor>: public SlotTemplate<Arg1, EmptyArg, EmptyArg> { | 105 | class SlotImpl<Functor, ReturnType, Arg1, SigImpl::EmptyArg, SigImpl::EmptyArg>: public Slot<ReturnType, Arg1> { |
73 | public: | 106 | public: |
74 | virtual void operator()(Arg1 arg1, EmptyArg, EmptyArg) | 107 | virtual ReturnType operator()(Arg1 arg1) { return m_functor(arg1); } |
75 | { m_functor(arg1); } | ||
76 | 108 | ||
77 | Slot(Functor functor) : m_functor(functor) {} | 109 | SlotImpl(Functor functor) : m_functor(functor) {} |
78 | 110 | ||
79 | private: | 111 | private: |
80 | Functor m_functor; | 112 | Functor m_functor; |
81 | }; | 113 | }; |
82 | 114 | ||
83 | // specialization for no arguments | 115 | /// Specialization for no arguments |
84 | template<typename Functor> | 116 | template<typename Functor, typename ReturnType> |
85 | class Slot<EmptyArg, EmptyArg, EmptyArg, Functor>: public SlotTemplate<EmptyArg, EmptyArg, EmptyArg> { | 117 | class SlotImpl<Functor, ReturnType, SigImpl::EmptyArg, SigImpl::EmptyArg, SigImpl::EmptyArg>: public Slot<ReturnType> { |
86 | public: | 118 | public: |
87 | virtual void operator()(EmptyArg, EmptyArg, EmptyArg) | 119 | virtual ReturnType operator()() { return m_functor(); } |
88 | { m_functor(); } | ||
89 | 120 | ||
90 | Slot(Functor functor) : m_functor(functor) {} | 121 | SlotImpl(Functor functor) : m_functor(functor) {} |
91 | 122 | ||
92 | private: | 123 | private: |
93 | Functor m_functor; | 124 | Functor m_functor; |
94 | }; | 125 | }; |
95 | 126 | ||
96 | } // namespace SigImpl | ||
97 | |||
98 | } // namespace FbTk | 127 | } // namespace FbTk |
99 | 128 | ||
100 | |||
101 | #endif // FBTK_SLOT_H | 129 | #endif // FBTK_SLOT_H |