ConstMethod.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_ConstMethod_h
31#define Pt_ConstMethod_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 ConstMethod : public Callable<R, As...>
48{
49 public:
52 typedef R (ClassT::*MemFuncT)(As...) const;
53
54 public:
57 ConstMethod(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
76 // inherit doc
77 R call(As... args) const
78 {
79 return (_object->*_method)(args...);
80 }
81
82 // inherit doc
83 void invoke(As... args) const
84 {
85 (_object->*_method)(args...);
86 }
87
88 // inherit doc
90 {
91 return new ConstMethod(*this);
92 }
93
96 bool operator==(const ConstMethod& other) const
97 {
98 return _object == other._object && _method == other._method;
99 }
100
101 private:
102 ClassT* _object;
103 MemFuncT _method;
104};
105
110template <typename R, typename ClassT, typename... As>
111class ConstMethodSlot : public BasicSlot<R, As...>
112{
113 public:
117 : _method(method)
118 { }
119
120 // inherit doc
121 Slot* clone() const
122 {
123 return new ConstMethodSlot(*this);
124 }
125
126 // inherit doc
127 virtual const Callback* callable() const
128 {
129 return &_method;
130 }
131
132 // inherit doc
133 virtual void onConnect(const Connection& connection)
134 {
135 _method.object().onConnectionOpen(connection);
136 }
137
138 // inherit doc
139 virtual void onDisconnect(const Connection& connection)
140 {
141 _method.object().onConnectionClose(connection);
142 }
143
144 // inherit doc
145 virtual bool equals(const Slot& slot) const
146 {
147 const ConstMethodSlot* other = dynamic_cast<const ConstMethodSlot*>(&slot);
148 return other && _method == other->_method;
149 }
150
151 private:
152 ConstMethod<R, ClassT, As...> _method;
153};
154
160template <typename R, typename BaseT, typename ClassT, typename... As>
161ConstMethod<R, ClassT, As...> callable(ClassT& object, R (BaseT::*method)(As...) const)
162{
163 return ConstMethod<R, ClassT, As...>(object, method);
164}
165
171template <typename R, typename BaseT, typename ClassT, typename... As>
172ConstMethodSlot<R, ClassT, As...> slot(ClassT& object, R (BaseT::*method)(As...) const)
173{
174 return ConstMethodSlot<R, ClassT, As...>(callable(object, method));
175}
176
177} // namespace Pt
178
179#endif
Base type for various "slot" types.
Definition Slot.h:92
An interface for all callable entities.
Definition Callable.h:48
ConstMethod< R, ClassT, As... > callable(ClassT &object, R(BaseT::*method)(As...) const)
Returns a ConstMethod object for the given object/method pair.
Definition ConstMethod.h:161
Represents a connection between a Signal/Delegate and a slot.
Definition Connection.h:96
Wraps ConstMethod objects so that they can act as Slots.
Definition ConstMethod.h:112
virtual bool equals(const Slot &slot) const
Returns true if two slots are equal in value.
Definition ConstMethod.h:145
virtual const Callback * callable() const
Returns the callable converted to the type-erased Callback interface.
Definition ConstMethod.h:127
virtual void onDisconnect(const Connection &connection)
Notifies of disconnects.
Definition ConstMethod.h:139
virtual void onConnect(const Connection &connection)
Notifies of connects.
Definition ConstMethod.h:133
ConstMethodSlot(const ConstMethod< R, ClassT, As... > &method)
Constructs from callable.
Definition ConstMethod.h:116
Slot * clone() const
Clone this object with new.
Definition ConstMethod.h:121
Adapter for const class methods.
Definition ConstMethod.h:48
ClassT & object()
Returns a reference to this object's bound ClassT object.
Definition ConstMethod.h:64
ConstMethod * clone() const
Returns a copy of this instance.
Definition ConstMethod.h:89
R call(As... args) const
Calls the callable entity and returns its result.
Definition ConstMethod.h:77
void invoke(As... args) const
Invokes the invokable entity with the given arguments.
Definition ConstMethod.h:83
R(ClassT::*) MemFuncT(As...) const
The wrapped member function type.
Definition ConstMethod.h:52
const ClassT & object() const
Returns a const reference to this object's bound ClassT object.
Definition ConstMethod.h:71
ConstMethod(ClassT &object, MemFuncT method)
Wraps the given member function of the given object.
Definition ConstMethod.h:57
bool operator==(const ConstMethod &other) const
Returns true if both use the same object and function pointer.
Definition ConstMethod.h:96
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.
Core module.
Definition Allocator.h:33