From 0775350fee345e37fb59835dda4d85664346b606 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Tue, 3 May 2011 12:49:05 +0200 Subject: Last round of simplification of Signal/Slot classes - merged all the common stuff from 0,1,2,3 argument versions into one common base class - removed ReturnType template parameter as it was instantiated with "void" everywhere and the current ignores the return value of the callbacks anyway --- src/ButtonTheme.hh | 2 +- src/FbTk/MenuTheme.hh | 2 +- src/FbTk/Signal.hh | 102 +++++++++++++++++----------------------------- src/FbTk/Slot.hh | 70 ++++++++++++++----------------- src/FbTk/Theme.hh | 6 +-- src/FbWinFrameTheme.hh | 2 +- src/Focusable.hh | 8 ++-- src/FocusableTheme.hh | 10 ++--- src/IconbarTheme.hh | 2 +- src/RootTheme.hh | 2 +- src/Screen.hh | 6 +-- src/SlitTheme.hh | 2 +- src/ToolTheme.hh | 2 +- src/ToolbarTheme.hh | 2 +- src/WinButtonTheme.hh | 2 +- src/Workspace.hh | 2 +- src/WorkspaceNameTheme.hh | 2 +- src/tests/testSignals.cc | 18 ++++---- 18 files changed, 103 insertions(+), 139 deletions(-) diff --git a/src/ButtonTheme.hh b/src/ButtonTheme.hh index 77cc159..a293b71 100644 --- a/src/ButtonTheme.hh +++ b/src/ButtonTheme.hh @@ -43,7 +43,7 @@ public: int scale() const { return *m_scale; } // scale factor for inside objects const std::string &name() { return m_name; } - virtual FbTk::Signal &reconfigSig() { return FbTk::Theme::reconfigSig(); } + virtual FbTk::Signal<> &reconfigSig() { return FbTk::Theme::reconfigSig(); } virtual ButtonTheme &operator *() { return *this; } virtual const ButtonTheme &operator *() const { return *this; } diff --git a/src/FbTk/MenuTheme.hh b/src/FbTk/MenuTheme.hh index b575860..d39b5df 100644 --- a/src/FbTk/MenuTheme.hh +++ b/src/FbTk/MenuTheme.hh @@ -130,7 +130,7 @@ public: m_hl_selected_pixmap->pixmap().dontFree(); } - virtual Signal &reconfigSig() { return Theme::reconfigSig(); } + virtual Signal<> &reconfigSig() { return Theme::reconfigSig(); } virtual MenuTheme &operator *() { return *this; } virtual const MenuTheme &operator *() const { return *this; } diff --git a/src/FbTk/Signal.hh b/src/FbTk/Signal.hh index 432bb3f..a7b91ba 100644 --- a/src/FbTk/Signal.hh +++ b/src/FbTk/Signal.hh @@ -22,7 +22,6 @@ #ifndef FBTK_SIGNAL_HH #define FBTK_SIGNAL_HH -#include "RefCount.hh" #include "Slot.hh" #include #include @@ -35,7 +34,7 @@ namespace FbTk { namespace SigImpl { /** - * Parent class for all \c Signal[0...*] classes. + * Parent class for all \c Signal template classes. * It handles the disconnect and holds all the slots. The connect must be * handled by the child class so it can do the type checking. */ @@ -121,93 +120,66 @@ private: unsigned m_emitting; }; -struct EmptyArg {}; - -} // namespace SigImpl - - -/// Specialization for three arguments. -template -class Signal: public SigImpl::SignalHolder { +template +class SignalTemplate: public SignalHolder { public: - void emit(Arg1 arg1, Arg2 arg2, Arg3 arg3) { + template + SlotID connect(const Functor& functor) { + return SignalHolder::connect(SlotPtr( new Slot(functor) )); + } + +protected: + void emit_(Arg1 arg1, Arg2 arg2, Arg3 arg3) { begin_emitting(); for ( Iterator it = begin(); it != end(); ++it ) { if(*it) - static_cast &>(**it)(arg1, arg2, arg3); + static_cast &>(**it)(arg1, arg2, arg3); } end_emitting(); } +}; - template - SlotID connect(const Functor& functor) { - return SignalHolder::connect(SlotPtr( - new SigImpl::Slot3(functor) - )); - } +} // namespace SigImpl + + +/// Base template for three arguments. +template +class Signal: public SigImpl::SignalTemplate { +public: + void emit(Arg1 arg1, Arg2 arg2, Arg3 arg3) + { SigImpl::SignalTemplate::emit_(arg1, arg2, arg3); } }; /// Specialization for two arguments. -template -class Signal: public SigImpl::SignalHolder { +template +class Signal: + public SigImpl::SignalTemplate { public: void emit(Arg1 arg1, Arg2 arg2) { - begin_emitting(); - for ( Iterator it = begin(); it != end(); ++it ) { - if(*it) - static_cast &>(**it)(arg1, arg2); - } - end_emitting(); - } - - template - SlotID connect(const Functor& functor) { - return SignalHolder::connect(SlotPtr( - new SigImpl::Slot2(functor) - )); + SigImpl::SignalTemplate:: + emit_(arg1, arg2, SigImpl::EmptyArg()); } }; /// Specialization for one argument. -template -class Signal: public SigImpl::SignalHolder { +template +class Signal: + public SigImpl::SignalTemplate { public: - void emit(Arg1 arg) { - begin_emitting(); - for ( Iterator it = begin(); it != end(); ++it ) { - if(*it) - static_cast &>(**it)(arg); - } - end_emitting(); - } - - template - SlotID connect(const Functor& functor) { - return SignalHolder::connect(SlotPtr( - new SigImpl::Slot1(functor) - )); + void emit(Arg1 arg1) { + SigImpl::SignalTemplate + ::emit_(arg1, SigImpl::EmptyArg(), SigImpl::EmptyArg()); } }; /// Specialization for no arguments. -template -class Signal: public SigImpl::SignalHolder { +template <> +class Signal: + public SigImpl::SignalTemplate { public: void emit() { - begin_emitting(); - for ( Iterator it = begin(); it != end(); ++it ) { - if(*it) - static_cast &>(**it)(); - } - end_emitting(); - } - - template - SlotID connect(const Functor& functor) { - return SignalHolder::connect(SlotPtr( - new SigImpl::Slot0(functor) - )); + SigImpl::SignalTemplate + ::emit_(SigImpl::EmptyArg(), SigImpl::EmptyArg(), SigImpl::EmptyArg()); } }; diff --git a/src/FbTk/Slot.hh b/src/FbTk/Slot.hh index 3daca4e..ea7ee2f 100644 --- a/src/FbTk/Slot.hh +++ b/src/FbTk/Slot.hh @@ -22,81 +22,72 @@ #ifndef FBTK_SLOT_HH #define FBTK_SLOT_HH +#include "RefCount.hh" + namespace FbTk { /// \namespace Implementation details for signals, do not use anything in this namespace namespace SigImpl { +struct EmptyArg {}; + class SlotBase { public: virtual ~SlotBase() {} }; -template -class SlotBase0: public SlotBase { +template +class SlotTemplate: public SlotBase { public: - virtual ReturnType operator()() = 0; + virtual void operator()(Arg1, Arg2, Arg3) = 0; }; -template -class Slot0: public SlotBase0 { +template +class Slot: public SlotTemplate { public: - virtual ReturnType operator()() { return m_functor(); } + virtual void operator()(Arg1 arg1, Arg2 arg2, Arg3 arg3) + { m_functor(arg1, arg2, arg3); } - Slot0(Functor functor) : m_functor(functor) {} + Slot(Functor functor) : m_functor(functor) {} private: Functor m_functor; }; -template -class SlotBase1: public SlotBase { -public: - virtual ReturnType operator()(Arg1) = 0; -}; - -template -class Slot1: public SlotBase1 { +// specialization for two arguments +template +class Slot: public SlotTemplate { public: - virtual ReturnType operator()(Arg1 arg1) { return m_functor(arg1); } + virtual void operator()(Arg1 arg1, Arg2 arg2, EmptyArg) + { m_functor(arg1, arg2); } - Slot1(Functor functor) : m_functor(functor) {} + Slot(Functor functor) : m_functor(functor) {} private: Functor m_functor; }; -template -class SlotBase2: public SlotBase { -public: - virtual ReturnType operator()(Arg1, Arg2) = 0; -}; - -template -class Slot2: public SlotBase2 { +// specialization for one argument +template +class Slot: public SlotTemplate { public: - virtual ReturnType operator()(Arg1 arg1, Arg2 arg2) { return m_functor(arg1, arg2); } + virtual void operator()(Arg1 arg1, EmptyArg, EmptyArg) + { m_functor(arg1); } - Slot2(Functor functor) : m_functor(functor) {} + Slot(Functor functor) : m_functor(functor) {} private: Functor m_functor; }; -template -class SlotBase3: public SlotBase { -public: - virtual ReturnType operator()(Arg1, Arg2, Arg3) = 0; - virtual ~SlotBase3() {} -}; - -template -class Slot3: public SlotBase3 { +// specialization for no arguments +template +class Slot: public SlotTemplate { public: - virtual ReturnType operator()(Arg1 arg1, Arg2 arg2, Arg3 arg3) - { return m_functor(arg1, arg2, arg3); } + virtual void operator()(EmptyArg, EmptyArg, EmptyArg) + { m_functor(); } - Slot3(Functor functor) : m_functor(functor) {} + Slot(Functor functor) : m_functor(functor) {} private: Functor m_functor; @@ -106,4 +97,5 @@ private: } // namespace FbTk + #endif // FBTK_SLOT_H diff --git a/src/FbTk/Theme.hh b/src/FbTk/Theme.hh index c700f9b..a0f2717 100644 --- a/src/FbTk/Theme.hh +++ b/src/FbTk/Theme.hh @@ -107,13 +107,13 @@ public: template void remove(ThemeItem &item); virtual bool fallback(ThemeItem_base &) { return false; } - Signal &reconfigSig() { return m_reconfig_sig; } + Signal<> &reconfigSig() { return m_reconfig_sig; } private: const int m_screen_num; ItemList m_themeitems; - Signal m_reconfig_sig; + Signal<> m_reconfig_sig; }; /// Proxy interface for themes, so they can be substituted dynamically @@ -122,7 +122,7 @@ class ThemeProxy { public: virtual ~ThemeProxy() { } - virtual Signal &reconfigSig() = 0; + virtual Signal<> &reconfigSig() = 0; virtual BaseTheme &operator *() = 0; virtual const BaseTheme &operator *() const = 0; diff --git a/src/FbWinFrameTheme.hh b/src/FbWinFrameTheme.hh index 613c66a..8e7274c 100644 --- a/src/FbWinFrameTheme.hh +++ b/src/FbWinFrameTheme.hh @@ -79,7 +79,7 @@ public: IconbarTheme &iconbarTheme() { return m_iconbar_theme; } - virtual FbTk::Signal &reconfigSig() { return FbTk::Theme::reconfigSig(); } + virtual FbTk::Signal<> &reconfigSig() { return FbTk::Theme::reconfigSig(); } virtual FbWinFrameTheme &operator *() { return *this; } virtual const FbWinFrameTheme &operator *() const { return *this; } diff --git a/src/Focusable.hh b/src/Focusable.hh index 0aae1a4..72a1dcc 100644 --- a/src/Focusable.hh +++ b/src/Focusable.hh @@ -119,13 +119,13 @@ public: @name signals @{ */ - typedef FbTk::Signal TitleSignal; + typedef FbTk::Signal TitleSignal; /// Used for both title and icon changes. TitleSignal &titleSig() { return m_titlesig; } /// Used for both title and icon changes. const TitleSignal &titleSig() const { return m_titlesig; } - FbTk::Signal &focusSig() { return m_focussig; } - const FbTk::Signal &focusSig() const { return m_focussig; } + FbTk::Signal &focusSig() { return m_focussig; } + const FbTk::Signal &focusSig() const { return m_focussig; } FbTk::Subject &dieSig() { return m_diesig; } const FbTk::Subject &dieSig() const { return m_diesig; } FbTk::Subject &attentionSig() { return m_attentionsig; } @@ -153,7 +153,7 @@ protected: FocusSubject m_diesig, m_attentionsig; private: - FbTk::Signal m_focussig; + FbTk::Signal m_focussig; TitleSignal m_titlesig; }; diff --git a/src/FocusableTheme.hh b/src/FocusableTheme.hh index 6643b43..d379c15 100644 --- a/src/FocusableTheme.hh +++ b/src/FocusableTheme.hh @@ -36,13 +36,13 @@ public: m_win(win), m_focused_theme(focused), m_unfocused_theme(unfocused) { m_signals.join(m_win.focusSig(), - FbTk::MemFunIgnoreArgs(m_reconfig_sig, &FbTk::Signal::emit)); + FbTk::MemFunIgnoreArgs(m_reconfig_sig, &FbTk::Signal<>::emit)); m_win.attentionSig().attach(this); m_signals.join(m_focused_theme.reconfigSig(), - FbTk::MemFun(m_reconfig_sig, &FbTk::Signal::emit)); + FbTk::MemFun(m_reconfig_sig, &FbTk::Signal<>::emit)); m_signals.join(m_unfocused_theme.reconfigSig(), - FbTk::MemFun(m_reconfig_sig, &FbTk::Signal::emit)); + FbTk::MemFun(m_reconfig_sig, &FbTk::Signal<>::emit)); } Focusable &win() { return m_win; } @@ -54,7 +54,7 @@ public: FbTk::ThemeProxy &unfocusedTheme() { return m_unfocused_theme; } const FbTk::ThemeProxy &unfocusedTheme() const { return m_unfocused_theme; } - FbTk::Signal &reconfigSig() { return m_reconfig_sig; } + FbTk::Signal<> &reconfigSig() { return m_reconfig_sig; } virtual BaseTheme &operator *() { return (m_win.isFocused() || m_win.getAttentionState()) ? @@ -70,7 +70,7 @@ private: Focusable &m_win; FbTk::ThemeProxy &m_focused_theme, &m_unfocused_theme; - FbTk::Signal m_reconfig_sig; + FbTk::Signal<> m_reconfig_sig; FbTk::SignalTracker m_signals; }; diff --git a/src/IconbarTheme.hh b/src/IconbarTheme.hh index f43a915..734e008 100644 --- a/src/IconbarTheme.hh +++ b/src/IconbarTheme.hh @@ -41,7 +41,7 @@ public: const FbTk::Texture &texture() const { return *m_texture; } const FbTk::Texture &emptyTexture() const { return *m_empty_texture; } - virtual FbTk::Signal &reconfigSig() { return FbTk::Theme::reconfigSig(); } + virtual FbTk::Signal<> &reconfigSig() { return FbTk::Theme::reconfigSig(); } virtual IconbarTheme &operator *() { return *this; } virtual const IconbarTheme &operator *() const { return *this; } diff --git a/src/RootTheme.hh b/src/RootTheme.hh index 6485448..f39c264 100644 --- a/src/RootTheme.hh +++ b/src/RootTheme.hh @@ -48,7 +48,7 @@ public: GC opGC() const { return m_opgc.gc(); } - virtual FbTk::Signal &reconfigSig() { return FbTk::Theme::reconfigSig(); } + virtual FbTk::Signal<> &reconfigSig() { return FbTk::Theme::reconfigSig(); } virtual RootTheme &operator *() { return *this; } virtual const RootTheme &operator *() const { return *this; } diff --git a/src/Screen.hh b/src/Screen.hh index 4d1d933..96a4d43 100644 --- a/src/Screen.hh +++ b/src/Screen.hh @@ -197,7 +197,7 @@ public: @name Screen signals */ //@{ - typedef FbTk::Signal ScreenSignal; + typedef FbTk::Signal ScreenSignal; /// client list signal ScreenSignal &clientListSig() { return m_clientlist_sig; } /// icon list sig @@ -211,7 +211,7 @@ public: /// current workspace signal ScreenSignal ¤tWorkspaceSig() { return m_currentworkspace_sig; } /// focused window signal - FbTk::Signal &focusedWindowSig() { return m_focusedwindow_sig; } + FbTk::Signal &focusedWindowSig() { return m_focusedwindow_sig; } /// reconfigure signal FbTk::Subject &reconfigureSig() { return m_reconfigure_sig; } ScreenSignal &resizeSig() { return m_resize_sig; } @@ -479,7 +479,7 @@ private: ScreenSubject m_reconfigure_sig; ///< reconfigure signal - FbTk::Signal m_focusedwindow_sig; ///< focused window signal + FbTk::Signal m_focusedwindow_sig; ///< focused window signal ScreenSignal m_resize_sig; ///< resize signal ScreenSignal m_workspace_area_sig; ///< workspace area changed signal ScreenSignal m_iconlist_sig; ///< notify if a window gets iconified/deiconified diff --git a/src/SlitTheme.hh b/src/SlitTheme.hh index 5a1b9d1..86dd498 100644 --- a/src/SlitTheme.hh +++ b/src/SlitTheme.hh @@ -38,7 +38,7 @@ public: int borderWidth() const { return *m_border_width; } int bevelWidth() const { return *m_bevel_width; } - virtual FbTk::Signal &reconfigSig() { return FbTk::Theme::reconfigSig(); } + virtual FbTk::Signal<> &reconfigSig() { return FbTk::Theme::reconfigSig(); } virtual SlitTheme &operator *() { return *this; } virtual const SlitTheme &operator *() const { return *this; } diff --git a/src/ToolTheme.hh b/src/ToolTheme.hh index 8daad7d..5c2352d 100644 --- a/src/ToolTheme.hh +++ b/src/ToolTheme.hh @@ -43,7 +43,7 @@ public: int alpha() const { return m_alpha; } void setAlpha(int alpha) { m_alpha = alpha; } - virtual FbTk::Signal &reconfigSig() { return FbTk::Theme::reconfigSig(); } + virtual FbTk::Signal<> &reconfigSig() { return FbTk::Theme::reconfigSig(); } virtual ToolTheme &operator *() { return *this; } virtual const ToolTheme &operator *() const { return *this; } diff --git a/src/ToolbarTheme.hh b/src/ToolbarTheme.hh index dbb6786..853ea70 100644 --- a/src/ToolbarTheme.hh +++ b/src/ToolbarTheme.hh @@ -44,7 +44,7 @@ public: int height() const { return *m_height; } int buttonSize() const { return *m_button_size; } - virtual FbTk::Signal &reconfigSig() { return FbTk::Theme::reconfigSig(); } + virtual FbTk::Signal<> &reconfigSig() { return FbTk::Theme::reconfigSig(); } virtual ToolbarTheme &operator *() { return *this; } virtual const ToolbarTheme &operator *() const { return *this; } diff --git a/src/WinButtonTheme.hh b/src/WinButtonTheme.hh index f7f2337..4765822 100644 --- a/src/WinButtonTheme.hh +++ b/src/WinButtonTheme.hh @@ -65,7 +65,7 @@ public: FbTk::PixmapWithMask &titlePixmap() { return *m_title_pm; } const FbTk::PixmapWithMask &titlePixmap() const { return *m_title_pm; } - virtual FbTk::Signal &reconfigSig() { return FbTk::Theme::reconfigSig(); } + virtual FbTk::Signal<> &reconfigSig() { return FbTk::Theme::reconfigSig(); } virtual WinButtonTheme &operator *() { return *this; } virtual const WinButtonTheme &operator *() const { return *this; } diff --git a/src/Workspace.hh b/src/Workspace.hh index b62a400..2dabb3e 100644 --- a/src/Workspace.hh +++ b/src/Workspace.hh @@ -85,7 +85,7 @@ private: BScreen &m_screen; Windows m_windowlist; - FbTk::Signal m_clientlist_sig; + FbTk::Signal<> m_clientlist_sig; ClientMenu m_clientmenu; FbTk::FbString m_name; ///< name of this workspace diff --git a/src/WorkspaceNameTheme.hh b/src/WorkspaceNameTheme.hh index 0096682..83cf124 100644 --- a/src/WorkspaceNameTheme.hh +++ b/src/WorkspaceNameTheme.hh @@ -49,7 +49,7 @@ public: return ToolTheme::fallback(item); } - virtual FbTk::Signal &reconfigSig() { return FbTk::Theme::reconfigSig(); } + virtual FbTk::Signal<> &reconfigSig() { return FbTk::Theme::reconfigSig(); } virtual WorkspaceNameTheme &operator *() { return *this; } virtual const WorkspaceNameTheme &operator *() const { return *this; } diff --git a/src/tests/testSignals.cc b/src/tests/testSignals.cc index 75f046e..9b9d151 100644 --- a/src/tests/testSignals.cc +++ b/src/tests/testSignals.cc @@ -80,16 +80,16 @@ int main() { using FbTk::Signal; using FbTk::SignalTracker; - Signal no_arg; + Signal<> no_arg; no_arg.connect( NoArgument() ); - Signal one_arg; + Signal one_arg; one_arg.connect( OneArgument() ); - Signal two_args; + Signal two_args; two_args.connect( TwoArguments() ); - Signal three_args; + Signal three_args; three_args.connect( ThreeArguments() ); // emit test @@ -127,7 +127,7 @@ int main() { no_arg.emit(); string takeThis("Take this"); - Signal ref_arg; + Signal ref_arg; ref_arg.connect(MemFun(obj, &FunctionClass::takeIt)); ref_arg.emit( takeThis ); @@ -145,7 +145,7 @@ int main() { using FbTk::MemFunIgnoreArgs; // Create a signal that emits with three arguments, and connect // sinks that takes less than three arguments. - Signal more_args; + Signal more_args; more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::print)); more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::takeIt)); more_args.connect(MemFunIgnoreArgs(obj, &FunctionClass::showMessage2)); @@ -168,7 +168,7 @@ int main() { destination.attach(&obs); // create a new signal and relay it to the // old subject - FbTk::Signal source; + FbTk::Signal source; FbTk::relaySignal(source, destination); // the new signal should now make the old // subject notify its observers @@ -178,7 +178,7 @@ int main() { // Test argument selector { using namespace FbTk; - Signal source; + Signal source; Printer printer; source.connect(MemFunSelectArg0(printer, &Printer::printInt)); @@ -187,7 +187,7 @@ int main() { source.emit(10, "hello", 3.141592); - Signal source2; + Signal source2; source2.connect(MemFunSelectArg0(printer, &Printer::printString)); source2.connect(MemFunSelectArg1(printer, &Printer::printInt)); source2.emit("world", 37); -- cgit v0.11.2