Function.h
1/*
2 Copyright (C) 2005 by Dr. Marc Boris Duerner
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 As a special exception, you may use this file as part of a free
10 software library without restriction. Specifically, if other files
11 instantiate templates or use macros or inline functions from this
12 file, or you compile this file and link it with other files to
13 produce an executable, this file does not by itself cause the
14 resulting executable to be covered by the GNU General Public
15 License. This exception does not however invalidate any other
16 reasons why the executable file might be covered by the GNU Library
17 General Public License.
18
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, write to the:
26 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 Boston, MA 02110-1301 USA
28*/
29
30#ifndef Pt_Function_h
31#define Pt_Function_h
32
33#include <Pt/Callable.h>
34#include <Pt/Slot.h>
35
36namespace Pt {
37
45template <typename R, typename... As>
46class Function : public Callable<R, As...>
47{
48 public:
51 typedef R (*FuncT)(As...);
52
53 public:
56 explicit Function(FuncT function)
57 : _function(function)
58 { }
59
60 // inherit doc
61 R call(As... args) const
62 {
63 return (*_function)(args...);
64 }
65
66 // inherit doc
67 void invoke(As... args) const
68 {
69 (*_function)(args...);
70 }
71
72 Function* clone() const
73 {
74 return new Function(*this);
75 }
76
79 bool operator==(const Function& other) const
80 {
81 return _function == other._function;
82 }
83
84 private:
85 FuncT _function;
86};
87
92template <typename R, typename... As>
93class FunctionSlot : public BasicSlot<R, As...>
94{
95 public:
98 explicit FunctionSlot(const Function<R, As...>& function)
99 : _function(function)
100 { }
101
102 Slot* clone() const
103 {
104 return new FunctionSlot(*this);
105 }
106
107 // inherit doc
108 virtual const Callback* callable() const
109 {
110 return &_function;
111 }
112
113 virtual void onConnect(const Connection&)
114 { }
115
116 virtual void onDisconnect(const Connection&)
117 { }
118
119 virtual bool equals(const Slot& slot) const
120 {
121 const FunctionSlot* other = dynamic_cast<const FunctionSlot*>(&slot);
122 return other && _function == other->_function;
123 }
124
125 private:
126 Function<R, As...> _function;
127};
128
134template <typename R, typename... As>
135Function<R, As...> callable(R (*function)(As...))
136{
137 return Function<R, As...>(function);
138}
139
145template <typename R, typename... As>
146FunctionSlot<R, As...> slot(R (*function)(As...))
147{
148 return FunctionSlot<R, As...>(callable(function));
149}
150
151} // namespace Pt
152
153
154#endif // Pt_Function_h
Base type for various "slot" types.
Definition Slot.h:92
An interface for all callable entities.
Definition Callable.h:48
Function< R, As... > callable(R(*function)(As...))
Returns a Function wrapper for the given free/static function.
Definition Function.h:135
Represents a connection between a Signal/Delegate and a slot.
Definition Connection.h:96
Wraps Function objects so that they can act as slots.
Definition Function.h:94
virtual bool equals(const Slot &slot) const
Returns true if two slots are equal in value.
Definition Function.h:119
virtual void onDisconnect(const Connection &)
Notifies of disconnects.
Definition Function.h:116
virtual const Callback * callable() const
Returns the callable converted to the type-erased Callback interface.
Definition Function.h:108
virtual void onConnect(const Connection &)
Notifies of connects.
Definition Function.h:113
FunctionSlot(const Function< R, As... > &function)
Constructs from callable.
Definition Function.h:98
Slot * clone() const
Clone this object with new.
Definition Function.h:102
Wraps free functions into a generic callable for use with the signals/slots.
Definition Function.h:47
R(*) FuncT(As...)
The function signature wrapped by this class.
Definition Function.h:51
Function(FuncT function)
Construct from function pointer.
Definition Function.h:56
R call(As... args) const
Calls the callable entity and returns its result.
Definition Function.h:61
bool operator==(const Function &other) const
Returns true if both use the same function pointer.
Definition Function.h:79
void invoke(As... args) const
Invokes the invokable entity with the given arguments.
Definition Function.h:67
Function * clone() const
Returns a copy of this instance.
Definition Function.h:72
Endpoint of a signal/slot connection.
Definition Slot.h:55
FunctionSlot< R, As... > slot(R(*function)(As...))
Returns a slot object for the given free/static function.
Definition Function.h:146
ConstMethodSlot< R, ClassT, As... > slot(ClassT &object, R(BaseT::*method)(As...) const)
Definition ConstMethod.h:172
virtual const Callback * callable() const =0
Returns the callable converted to the type-erased Callback interface.
Core module.
Definition Allocator.h:33