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
This commit is contained in:
Pavel Labath 2011-05-03 12:49:05 +02:00
parent bef2039d2c
commit 0775350fee
18 changed files with 106 additions and 142 deletions

View file

@ -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<void> &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; }

View file

@ -130,7 +130,7 @@ public:
m_hl_selected_pixmap->pixmap().dontFree();
}
virtual Signal<void> &reconfigSig() { return Theme::reconfigSig(); }
virtual Signal<> &reconfigSig() { return Theme::reconfigSig(); }
virtual MenuTheme &operator *() { return *this; }
virtual const MenuTheme &operator *() const { return *this; }

View file

@ -22,7 +22,6 @@
#ifndef FBTK_SIGNAL_HH
#define FBTK_SIGNAL_HH
#include "RefCount.hh"
#include "Slot.hh"
#include <algorithm>
#include <list>
@ -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 {};
template <typename Arg1, typename Arg2, typename Arg3>
class SignalTemplate: public SignalHolder {
public:
template<typename Functor>
SlotID connect(const Functor& functor) {
return SignalHolder::connect(SlotPtr( new Slot<Arg1, Arg2, Arg3, Functor>(functor) ));
}
protected:
void emit_(Arg1 arg1, Arg2 arg2, Arg3 arg3) {
begin_emitting();
for ( Iterator it = begin(); it != end(); ++it ) {
if(*it)
static_cast<SigImpl::SlotTemplate<Arg1, Arg2, Arg3> &>(**it)(arg1, arg2, arg3);
}
end_emitting();
}
};
} // namespace SigImpl
/// Specialization for three arguments.
template <typename ReturnType,
typename Arg1 = SigImpl::EmptyArg, typename Arg2 = SigImpl::EmptyArg, typename Arg3 = SigImpl::EmptyArg >
class Signal: public SigImpl::SignalHolder {
/// Base template for three arguments.
template <typename Arg1 = SigImpl::EmptyArg, typename Arg2 = SigImpl::EmptyArg, typename Arg3 = SigImpl::EmptyArg >
class Signal: public SigImpl::SignalTemplate<Arg1, Arg2, Arg3> {
public:
void emit(Arg1 arg1, Arg2 arg2, Arg3 arg3) {
begin_emitting();
for ( Iterator it = begin(); it != end(); ++it ) {
if(*it)
static_cast<SigImpl::SlotBase3<ReturnType, Arg1, Arg2, Arg3> &>(**it)(arg1, arg2, arg3);
}
end_emitting();
}
template<typename Functor>
SlotID connect(const Functor& functor) {
return SignalHolder::connect(SlotPtr(
new SigImpl::Slot3<ReturnType, Arg1, Arg2, Arg3, Functor>(functor)
));
}
void emit(Arg1 arg1, Arg2 arg2, Arg3 arg3)
{ SigImpl::SignalTemplate<Arg1, Arg2, Arg3>::emit_(arg1, arg2, arg3); }
};
/// Specialization for two arguments.
template <typename ReturnType, typename Arg1, typename Arg2>
class Signal<ReturnType, Arg1, Arg2, SigImpl::EmptyArg>: public SigImpl::SignalHolder {
template <typename Arg1, typename Arg2>
class Signal<Arg1, Arg2, SigImpl::EmptyArg>:
public SigImpl::SignalTemplate<Arg1, Arg2, SigImpl::EmptyArg> {
public:
void emit(Arg1 arg1, Arg2 arg2) {
begin_emitting();
for ( Iterator it = begin(); it != end(); ++it ) {
if(*it)
static_cast<SigImpl::SlotBase2<ReturnType, Arg1, Arg2> &>(**it)(arg1, arg2);
}
end_emitting();
}
template<typename Functor>
SlotID connect(const Functor& functor) {
return SignalHolder::connect(SlotPtr(
new SigImpl::Slot2<ReturnType, Arg1, Arg2, Functor>(functor)
));
SigImpl::SignalTemplate<Arg1, Arg2, SigImpl::EmptyArg>::
emit_(arg1, arg2, SigImpl::EmptyArg());
}
};
/// Specialization for one argument.
template <typename ReturnType, typename Arg1>
class Signal<ReturnType, Arg1, SigImpl::EmptyArg, SigImpl::EmptyArg>: public SigImpl::SignalHolder {
template <typename Arg1>
class Signal<Arg1, SigImpl::EmptyArg, SigImpl::EmptyArg>:
public SigImpl::SignalTemplate<Arg1, SigImpl::EmptyArg, SigImpl::EmptyArg> {
public:
void emit(Arg1 arg) {
begin_emitting();
for ( Iterator it = begin(); it != end(); ++it ) {
if(*it)
static_cast<SigImpl::SlotBase1<ReturnType, Arg1> &>(**it)(arg);
}
end_emitting();
}
template<typename Functor>
SlotID connect(const Functor& functor) {
return SignalHolder::connect(SlotPtr(
new SigImpl::Slot1<ReturnType, Arg1, Functor>(functor)
));
void emit(Arg1 arg1) {
SigImpl::SignalTemplate<Arg1, SigImpl::EmptyArg, SigImpl::EmptyArg>
::emit_(arg1, SigImpl::EmptyArg(), SigImpl::EmptyArg());
}
};
/// Specialization for no arguments.
template <typename ReturnType>
class Signal<ReturnType, SigImpl::EmptyArg, SigImpl::EmptyArg, SigImpl::EmptyArg>: public SigImpl::SignalHolder {
template <>
class Signal<SigImpl::EmptyArg, SigImpl::EmptyArg, SigImpl::EmptyArg>:
public SigImpl::SignalTemplate<SigImpl::EmptyArg, SigImpl::EmptyArg, SigImpl::EmptyArg> {
public:
void emit() {
begin_emitting();
for ( Iterator it = begin(); it != end(); ++it ) {
if(*it)
static_cast<SigImpl::SlotBase0<ReturnType> &>(**it)();
}
end_emitting();
}
template<typename Functor>
SlotID connect(const Functor& functor) {
return SignalHolder::connect(SlotPtr(
new SigImpl::Slot0<ReturnType, Functor>(functor)
));
SigImpl::SignalTemplate<SigImpl::EmptyArg, SigImpl::EmptyArg, SigImpl::EmptyArg>
::emit_(SigImpl::EmptyArg(), SigImpl::EmptyArg(), SigImpl::EmptyArg());
}
};

View file

@ -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<typename ReturnType>
class SlotBase0: public SlotBase {
template<typename Arg1, typename Arg2, typename Arg3>
class SlotTemplate: public SlotBase {
public:
virtual ReturnType operator()() = 0;
virtual void operator()(Arg1, Arg2, Arg3) = 0;
};
template<typename ReturnType, typename Functor>
class Slot0: public SlotBase0<ReturnType> {
template<typename Arg1, typename Arg2, typename Arg3, typename Functor>
class Slot: public SlotTemplate<Arg1, Arg2, Arg3> {
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<typename ReturnType, typename Arg1>
class SlotBase1: public SlotBase {
// specialization for two arguments
template<typename Arg1, typename Arg2, typename Functor>
class Slot<Arg1, Arg2, EmptyArg, Functor>: public SlotTemplate<Arg1, Arg2, EmptyArg> {
public:
virtual ReturnType operator()(Arg1) = 0;
};
virtual void operator()(Arg1 arg1, Arg2 arg2, EmptyArg)
{ m_functor(arg1, arg2); }
template<typename ReturnType, typename Arg1, typename Functor>
class Slot1: public SlotBase1<ReturnType, Arg1> {
public:
virtual ReturnType operator()(Arg1 arg1) { return m_functor(arg1); }
Slot1(Functor functor) : m_functor(functor) {}
Slot(Functor functor) : m_functor(functor) {}
private:
Functor m_functor;
};
template<typename ReturnType, typename Arg1, typename Arg2>
class SlotBase2: public SlotBase {
// specialization for one argument
template<typename Arg1, typename Functor>
class Slot<Arg1, EmptyArg, EmptyArg, Functor>: public SlotTemplate<Arg1, EmptyArg, EmptyArg> {
public:
virtual ReturnType operator()(Arg1, Arg2) = 0;
};
virtual void operator()(Arg1 arg1, EmptyArg, EmptyArg)
{ m_functor(arg1); }
template<typename ReturnType, typename Arg1, typename Arg2, typename Functor>
class Slot2: public SlotBase2<ReturnType, Arg1, Arg2> {
public:
virtual ReturnType operator()(Arg1 arg1, Arg2 arg2) { return m_functor(arg1, arg2); }
Slot2(Functor functor) : m_functor(functor) {}
Slot(Functor functor) : m_functor(functor) {}
private:
Functor m_functor;
};
template<typename ReturnType, typename Arg1, typename Arg2, typename Arg3>
class SlotBase3: public SlotBase {
// specialization for no arguments
template<typename Functor>
class Slot<EmptyArg, EmptyArg, EmptyArg, Functor>: public SlotTemplate<EmptyArg, EmptyArg, EmptyArg> {
public:
virtual ReturnType operator()(Arg1, Arg2, Arg3) = 0;
virtual ~SlotBase3() {}
};
virtual void operator()(EmptyArg, EmptyArg, EmptyArg)
{ m_functor(); }
template<typename ReturnType, typename Arg1, typename Arg2, typename Arg3, typename Functor>
class Slot3: public SlotBase3<ReturnType, Arg1, Arg2, Arg3> {
public:
virtual ReturnType operator()(Arg1 arg1, Arg2 arg2, Arg3 arg3)
{ return m_functor(arg1, arg2, arg3); }
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

View file

@ -107,13 +107,13 @@ public:
template <typename T>
void remove(ThemeItem<T> &item);
virtual bool fallback(ThemeItem_base &) { return false; }
Signal<void> &reconfigSig() { return m_reconfig_sig; }
Signal<> &reconfigSig() { return m_reconfig_sig; }
private:
const int m_screen_num;
ItemList m_themeitems;
Signal<void> 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<void> &reconfigSig() = 0;
virtual Signal<> &reconfigSig() = 0;
virtual BaseTheme &operator *() = 0;
virtual const BaseTheme &operator *() const = 0;

View file

@ -79,7 +79,7 @@ public:
IconbarTheme &iconbarTheme() { return m_iconbar_theme; }
virtual FbTk::Signal<void> &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; }

View file

@ -119,13 +119,13 @@ public:
@name signals
@{
*/
typedef FbTk::Signal<void, const std::string&, Focusable&> TitleSignal;
typedef FbTk::Signal<const std::string&, Focusable&> 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<void, Focusable&> &focusSig() { return m_focussig; }
const FbTk::Signal<void, Focusable&> &focusSig() const { return m_focussig; }
FbTk::Signal<Focusable&> &focusSig() { return m_focussig; }
const FbTk::Signal<Focusable&> &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<void, Focusable&> m_focussig;
FbTk::Signal<Focusable&> m_focussig;
TitleSignal m_titlesig;
};

View file

@ -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<void>::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<void>::emit));
FbTk::MemFun(m_reconfig_sig, &FbTk::Signal<>::emit));
m_signals.join(m_unfocused_theme.reconfigSig(),
FbTk::MemFun(m_reconfig_sig, &FbTk::Signal<void>::emit));
FbTk::MemFun(m_reconfig_sig, &FbTk::Signal<>::emit));
}
Focusable &win() { return m_win; }
@ -54,7 +54,7 @@ public:
FbTk::ThemeProxy<BaseTheme> &unfocusedTheme() { return m_unfocused_theme; }
const FbTk::ThemeProxy<BaseTheme> &unfocusedTheme() const { return m_unfocused_theme; }
FbTk::Signal<void> &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<BaseTheme> &m_focused_theme, &m_unfocused_theme;
FbTk::Signal<void> m_reconfig_sig;
FbTk::Signal<> m_reconfig_sig;
FbTk::SignalTracker m_signals;
};

View file

@ -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<void> &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; }

View file

@ -48,7 +48,7 @@ public:
GC opGC() const { return m_opgc.gc(); }
virtual FbTk::Signal<void> &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; }

View file

@ -197,7 +197,7 @@ public:
@name Screen signals
*/
//@{
typedef FbTk::Signal<void, BScreen&> ScreenSignal;
typedef FbTk::Signal<BScreen&> ScreenSignal;
/// client list signal
ScreenSignal &clientListSig() { return m_clientlist_sig; }
/// icon list sig
@ -211,7 +211,7 @@ public:
/// current workspace signal
ScreenSignal &currentWorkspaceSig() { return m_currentworkspace_sig; }
/// focused window signal
FbTk::Signal<void, BScreen&, FluxboxWindow*, WinClient*> &focusedWindowSig() { return m_focusedwindow_sig; }
FbTk::Signal<BScreen&, FluxboxWindow*, WinClient*> &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<void, BScreen&, FluxboxWindow*, WinClient*> m_focusedwindow_sig; ///< focused window signal
FbTk::Signal<BScreen&, FluxboxWindow*, WinClient*> 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

View file

@ -38,7 +38,7 @@ public:
int borderWidth() const { return *m_border_width; }
int bevelWidth() const { return *m_bevel_width; }
virtual FbTk::Signal<void> &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; }

View file

@ -43,7 +43,7 @@ public:
int alpha() const { return m_alpha; }
void setAlpha(int alpha) { m_alpha = alpha; }
virtual FbTk::Signal<void> &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; }

View file

@ -44,7 +44,7 @@ public:
int height() const { return *m_height; }
int buttonSize() const { return *m_button_size; }
virtual FbTk::Signal<void> &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; }

View file

@ -65,7 +65,7 @@ public:
FbTk::PixmapWithMask &titlePixmap() { return *m_title_pm; }
const FbTk::PixmapWithMask &titlePixmap() const { return *m_title_pm; }
virtual FbTk::Signal<void> &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; }

View file

@ -85,7 +85,7 @@ private:
BScreen &m_screen;
Windows m_windowlist;
FbTk::Signal<void> m_clientlist_sig;
FbTk::Signal<> m_clientlist_sig;
ClientMenu m_clientmenu;
FbTk::FbString m_name; ///< name of this workspace

View file

@ -49,7 +49,7 @@ public:
return ToolTheme::fallback(item);
}
virtual FbTk::Signal<void> &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; }

View file

@ -80,16 +80,16 @@ int main() {
using FbTk::Signal;
using FbTk::SignalTracker;
Signal<void> no_arg;
Signal<> no_arg;
no_arg.connect( NoArgument() );
Signal<void, int> one_arg;
Signal<int> one_arg;
one_arg.connect( OneArgument() );
Signal<void, int, const string&> two_args;
Signal<int, const string&> two_args;
two_args.connect( TwoArguments() );
Signal<void, int, const string&, double> three_args;
Signal<int, const string&, double> three_args;
three_args.connect( ThreeArguments() );
// emit test
@ -127,7 +127,7 @@ int main() {
no_arg.emit();
string takeThis("Take this");
Signal<void, string&> ref_arg;
Signal<string&> 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<void, string, string, float> more_args;
Signal<string, string, float> 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<void, string> source;
FbTk::Signal<string> 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<void, int, string, float> source;
Signal<int, string, float> source;
Printer printer;
source.connect(MemFunSelectArg0(printer, &Printer::printInt));
@ -187,7 +187,7 @@ int main() {
source.emit(10, "hello", 3.141592);
Signal<void, string, int> source2;
Signal<string, int> source2;
source2.connect(MemFunSelectArg0(printer, &Printer::printString));
source2.connect(MemFunSelectArg1(printer, &Printer::printInt));
source2.emit("world", 37);