Method.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_Method_h
31#define Pt_Method_h
32
33#include <Pt/Callable.h>
34#include <Pt/Connectable.h>
35#include <Pt/Slot.h>
36
37namespace Pt {
38
46template <typename R, typename ClassT, typename... As>
47class Method : public Callable<R, As...>
48{
49 public:
52 typedef R (ClassT::*MemFuncT)(As...);
53
54 public:
57 Method(ClassT& object, MemFuncT method)
58 : _object(&object)
59 , _method(method)
60 { }
61
64 ClassT& object()
65 {
66 return *_object;
67 }
68
71 const ClassT& object() const
72 {
73 return *_object;
74 }
75
78 const MemFuncT& method() const
79 {
80 return _method;
81 }
82
83 // inherit doc
84 R call(As... args) const
85 {
86 return (_object->*_method)(args...);
87 }
88
89 // inherit doc
90 void invoke(As... args) const
91 {
92 (_object->*_method)(args...);
93 }
94
95 // inherit doc
96 Method* clone() const
97 {
98 return new Method(*this);
99 }
100
101 Method& operator=(const Method& other)
102 {
103 _object = other._object;
104 _method = other._method;
105 return *this;
106 }
107
110 bool operator==(const Method& other) const
111 {
112 return _object == other._object && _method == other._method;
113 }
114
115 private:
116 ClassT* _object;
117 MemFuncT _method;
118};
119
124template <typename R, typename ClassT, typename... As>
125class MethodSlot : public BasicSlot<R, As...>
126{
127 public:
130 explicit MethodSlot(const Method<R, ClassT, As...>& method)
131 : _method(method)
132 { }
133
134 // inherit doc
135 Slot* clone() const
136 {
137 return new MethodSlot(*this);
138 }
139
140 // inherit doc
141 virtual const Callback* callable() const
142 {
143 return &_method;
144 }
145
146 // inherit doc
147 virtual void onConnect(const Connection& connection)
148 {
149 _method.object().onConnectionOpen(connection);
150 }
151
152 // inherit doc
153 virtual void onDisconnect(const Connection& connection)
154 {
155 _method.object().onConnectionClose(connection);
156 }
157
158 // inherit doc
159 virtual bool equals(const Slot& slot) const
160 {
161 const MethodSlot* other = dynamic_cast<const MethodSlot*>(&slot);
162 return other && _method == other->_method;
163 }
164
165 private:
166 Method<R, ClassT, As...> _method;
167};
168
174template <typename R, typename BaseT, typename ClassT, typename... As>
175Method<R, ClassT, As...> callable(ClassT& object, R (BaseT::*method)(As...))
176{
177 return Method<R, ClassT, As...>(object, method);
178}
179
185template <typename R, typename BaseT, typename ClassT, typename... As>
186MethodSlot<R, ClassT, As...> slot(ClassT& object, R (BaseT::*method)(As...))
187{
188 return MethodSlot<R, ClassT, As...>(callable(object, method));
189}
190
191} // namespace Pt
192
193#endif
Base type for various "slot" types.
Definition Slot.h:92
An interface for all callable entities.
Definition Callable.h:48
Method< R, ClassT, As... > callable(ClassT &object, R(BaseT::*method)(As...))
Returns a Method object for the given object/method pair.
Definition Method.h:175
Represents a connection between a Signal/Delegate and a slot.
Definition Connection.h:96
Wraps Method objects so that they can act as Slots.
Definition Method.h:126
virtual bool equals(const Slot &slot) const
Returns true if two slots are equal in value.
Definition Method.h:159
virtual const Callback * callable() const
Returns the callable converted to the type-erased Callback interface.
Definition Method.h:141
virtual void onDisconnect(const Connection &connection)
Notifies of disconnects.
Definition Method.h:153
virtual void onConnect(const Connection &connection)
Notifies of connects.
Definition Method.h:147
MethodSlot(const Method< R, ClassT, As... > &method)
Constructs from callable.
Definition Method.h:130
Slot * clone() const
Clone this object with new.
Definition Method.h:135
Adapter for class methods.
Definition Method.h:48
ClassT & object()
Returns a reference to this object's wrapped ClassT object.
Definition Method.h:64
const MemFuncT & method() const
Returns a reference to the wrapped member function.
Definition Method.h:78
R(ClassT::*) MemFuncT(As...)
The wrapped member function signature.
Definition Method.h:52
bool operator==(const Method &other) const
Returns true if both use the same object and function pointer.
Definition Method.h:110
Method * clone() const
Returns a copy of this instance.
Definition Method.h:96
Method(ClassT &object, MemFuncT method)
Wraps the given object/member pair.
Definition Method.h:57
R call(As... args) const
Calls the callable entity and returns its result.
Definition Method.h:84
void invoke(As... args) const
Invokes the invokable entity with the given arguments.
Definition Method.h:90
const ClassT & object() const
Returns a const reference to the wrapped ClassT object.
Definition Method.h:71
Endpoint of a signal/slot connection.
Definition Slot.h:55
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.
MethodSlot< R, ClassT, As... > slot(ClassT &object, R(BaseT::*method)(As...))
Returns a slot object for the given object/member pair.
Definition Method.h:186
Core module.
Definition Allocator.h:33