| 1 |
dnl Copyright 2002, The libsigc++ Development Team |
| 2 |
dnl |
| 3 |
dnl This library is free software; you can redistribute it and/or |
| 4 |
dnl modify it under the terms of the GNU Lesser General Public |
| 5 |
dnl License as published by the Free Software Foundation; either |
| 6 |
dnl version 2.1 of the License, or (at your option) any later version. |
| 7 |
dnl |
| 8 |
dnl This library is distributed in the hope that it will be useful, |
| 9 |
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
dnl Lesser General Public License for more details. |
| 12 |
dnl |
| 13 |
dnl You should have received a copy of the GNU Lesser General Public |
| 14 |
dnl License along with this library; if not, write to the Free Software |
| 15 |
dnl Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 |
dnl |
| 17 |
divert(-1) |
| 18 |
|
| 19 |
include(template.macros.m4) |
| 20 |
|
| 21 |
define([SLOT_N],[dnl |
| 22 |
/** Converts an arbitrary functor to a unified type which is opaque. |
| 23 |
* sigc::slot itself is a functor or to be more precise a closure. It contains |
| 24 |
* a single, arbitrary functor (or closure) that is executed in operator()(). |
| 25 |
* |
| 26 |
* The template arguments determine the function signature of operator()(): |
| 27 |
* - @e T_return The return type of operator()().dnl |
| 28 |
FOR(1,$1,[ |
| 29 |
* - @e T_arg%1 Argument type used in the definition of operator()(). The default @p nil means no argument.]) |
| 30 |
* |
| 31 |
* To use simply assign the desired functor to the slot. If the functor |
| 32 |
* is not compatible with the parameter list defined with the template |
| 33 |
* arguments compiler errors are triggered. When called the slot |
| 34 |
* will invoke the functor with minimal copies. |
| 35 |
* block() and unblock() can be used to block the functor's invocation |
| 36 |
* from operator()() temporarily. |
| 37 |
* |
| 38 |
* You should use the more convenient unnumbered sigc::slot template. |
| 39 |
* |
| 40 |
* @deprecated Please use the syntax similar to that used by std::function<>: |
| 41 |
* @code |
| 42 |
* sigc::slot<void(bool, int)> some_slot; |
| 43 |
* @endcode |
| 44 |
* |
| 45 |
* @ingroup slot |
| 46 |
*/ |
| 47 |
template <LIST(class T_return, LOOP(class T_arg%1, $1))> |
| 48 |
class slot$1 |
| 49 |
: public slot_base |
| 50 |
{ |
| 51 |
public: |
| 52 |
typedef T_return result_type; |
| 53 |
FOR(1, $1,[ typedef _R_(T_arg%1) arg%1_type_; |
| 54 |
]) |
| 55 |
|
| 56 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 57 |
private: |
| 58 |
typedef internal::slot_rep rep_type; |
| 59 |
public: |
| 60 |
typedef T_return (*call_type)(LIST(rep_type*, LOOP(arg%1_type_, $1))); |
| 61 |
#endif |
| 62 |
|
| 63 |
/** Invoke the contained functor unless slot is in blocking state.dnl |
| 64 |
FOR(1, $1,[ |
| 65 |
* @param _A_a%1 Argument to be passed on to the functor.]) |
| 66 |
* @return The return value of the functor invocation. |
| 67 |
*/ |
| 68 |
inline T_return operator()(LOOP(arg%1_type_ _A_a%1, $1)) const |
| 69 |
{ |
| 70 |
if (!empty() && !blocked()) |
| 71 |
return (sigc::internal::function_pointer_cast<call_type>(slot_base::rep_->call_))(LIST(slot_base::rep_, LOOP(_A_a%1, $1))); |
| 72 |
return T_return(); |
| 73 |
} |
| 74 |
|
| 75 |
inline slot$1() {} |
| 76 |
|
| 77 |
/** Constructs a slot from an arbitrary functor. |
| 78 |
* @param _A_func The desired functor the new slot should be assigned to. |
| 79 |
*/ |
| 80 |
template <class T_functor> |
| 81 |
slot$1(const T_functor& _A_func) |
| 82 |
: slot_base(new internal::typed_slot_rep<T_functor>(_A_func)) |
| 83 |
{ |
| 84 |
//The slot_base:: is necessary to stop the HP-UX aCC compiler from being confused. murrayc. |
| 85 |
slot_base::rep_->call_ = internal::slot_call$1<LIST(T_functor, T_return, LOOP(T_arg%1, $1))>::address(); |
| 86 |
} |
| 87 |
|
| 88 |
/** Constructs a slot, copying an existing one. |
| 89 |
* @param src The existing slot to copy. |
| 90 |
*/ |
| 91 |
slot$1(const slot$1& src) |
| 92 |
: slot_base(src) |
| 93 |
{} |
| 94 |
|
| 95 |
/** Constructs a slot, moving an existing one. |
| 96 |
* If @p src is connected to a parent (e.g. a signal), it is copied, not moved. |
| 97 |
* @param src The existing slot to move or copy. |
| 98 |
*/ |
| 99 |
slot$1(slot$1&& src) |
| 100 |
: slot_base(std::move(src)) |
| 101 |
{} |
| 102 |
|
| 103 |
/** Overrides this slot, making a copy from another slot. |
| 104 |
* @param src The slot from which to make a copy. |
| 105 |
* @return @p this. |
| 106 |
*/ |
| 107 |
slot$1& operator=(const slot$1& src) |
| 108 |
{ |
| 109 |
slot_base::operator=(src); |
| 110 |
return *this; |
| 111 |
} |
| 112 |
|
| 113 |
/** Overrides this slot, making a move from another slot. |
| 114 |
* If @p src is connected to a parent (e.g. a signal), it is copied, not moved. |
| 115 |
* @param src The slot from which to move or copy. |
| 116 |
* @return @p this. |
| 117 |
*/ |
| 118 |
slot$1& operator=(slot$1&& src) |
| 119 |
{ |
| 120 |
slot_base::operator=(std::move(src)); |
| 121 |
return *this; |
| 122 |
} |
| 123 |
}; |
| 124 |
|
| 125 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 126 |
//template specialization of visitor<>::do_visit_each<>(action, functor): |
| 127 |
/** Performs a functor on each of the targets of a functor. |
| 128 |
* The function overloads for sigc::slot$1 are similar to the function |
| 129 |
* overloads for sigc::slot. See the description of those overloads. |
| 130 |
* |
| 131 |
* @ingroup slot |
| 132 |
*/ |
| 133 |
template <LIST(typename T_return, LOOP(typename T_arg%1, $1))> |
| 134 |
struct visitor<slot$1<LIST(T_return, LOOP(T_arg%1, $1))>> |
| 135 |
{ |
| 136 |
static void do_visit_each(const internal::limit_derived_target<trackable*, internal::slot_do_bind>& _A_action, |
| 137 |
const slot$1<LIST(T_return, LOOP(T_arg%1, $1))>& _A_target) |
| 138 |
{ |
| 139 |
if (_A_target.rep_ && _A_target.rep_->parent_ == nullptr) |
| 140 |
_A_target.rep_->set_parent(_A_action.action_.rep_, &internal::slot_rep::notify); |
| 141 |
} |
| 142 |
|
| 143 |
static void do_visit_each(const internal::limit_derived_target<trackable*, internal::slot_do_unbind>& _A_action, |
| 144 |
const slot$1<LIST(T_return, LOOP(T_arg%1, $1))>& _A_target) |
| 145 |
{ |
| 146 |
if (_A_target.rep_ && _A_target.rep_->parent_ == _A_action.action_.rep_) |
| 147 |
_A_target.rep_->set_parent(nullptr, nullptr); |
| 148 |
} |
| 149 |
|
| 150 |
template <typename T_action> |
| 151 |
static void do_visit_each(const T_action& _A_action, |
| 152 |
const slot$1<LIST(T_return, LOOP(T_arg%1, $1))>& _A_target) |
| 153 |
{ |
| 154 |
_A_action(_A_target); |
| 155 |
} |
| 156 |
}; |
| 157 |
#endif // DOXYGEN_SHOULD_SKIP_THIS |
| 158 |
|
| 159 |
]) |
| 160 |
define([SLOT],[dnl |
| 161 |
ifelse($1, $2,[dnl |
| 162 |
// Because slot is opaque, visit_each() will not visit its internal members. |
| 163 |
// Those members are not reachable by visit_each() after the slot has been |
| 164 |
// constructed. But when a slot contains another slot, the outer slot will become |
| 165 |
// the parent of the inner slot, with similar results. See the description of |
| 166 |
// slot's specialization of the visitor struct. |
| 167 |
/** Convenience wrapper for the numbered sigc::slot# templates. |
| 168 |
* Slots convert arbitrary functors to unified types which are opaque. |
| 169 |
* sigc::slot itself is a functor or to be more precise a closure. It contains |
| 170 |
* a single, arbitrary functor (or closure) that is executed in operator()(). |
| 171 |
* |
| 172 |
* The template arguments determine the function signature of operator()(): |
| 173 |
* - @e T_return The return type of operator()().dnl |
| 174 |
FOR(1,$1,[ |
| 175 |
* - @e T_arg%1 Argument type used in the definition of operator()(). The default @p nil means no argument.]) |
| 176 |
* |
| 177 |
* To use, simply assign the desired functor to the slot. If the functor |
| 178 |
* is not compatible with the parameter list defined with the template |
| 179 |
* arguments, compiler errors are triggered. When called, the slot |
| 180 |
* will invoke the functor with minimal copies. |
| 181 |
* block() and unblock() can be used to temporarily block the functor's |
| 182 |
* invocation from operator()(). |
| 183 |
* |
| 184 |
* @par Example: |
| 185 |
* @code |
| 186 |
* void foo(int) {} |
| 187 |
* sigc::slot<void, int> s = sigc::ptr_fun(&foo); |
| 188 |
* s(19); |
| 189 |
* @endcode |
| 190 |
* |
| 191 |
* sigc::slot<> is similar to std::function<>. If you're going to assign the |
| 192 |
* resulting functor to a sigc::slot or connect it to a sigc::signal, it's better |
| 193 |
* not to use std::function. It would become an unnecessary extra wrapper. |
| 194 |
* |
| 195 |
* @deprecated Please use the syntax similar to that used by std::function<>: |
| 196 |
* @code |
| 197 |
* sigc::slot<void(bool, int)> some_slot; |
| 198 |
* @endcode |
| 199 |
* |
| 200 |
* @ingroup slot |
| 201 |
*/ |
| 202 |
template <LIST(class T_return, LOOP(class T_arg%1 = nil, $1))> |
| 203 |
class slot],[dnl |
| 204 |
|
| 205 |
/** Convenience wrapper for the numbered sigc::slot$1 template. |
| 206 |
* See the base class for useful methods. |
| 207 |
* This is the template specialization of the unnumbered sigc::slot |
| 208 |
* template for $1 argument(s), specialized for different numbers of arguments |
| 209 |
* This is possible because the template has default (nil) template types. |
| 210 |
* |
| 211 |
* @deprecated Please use the syntax similar to that used by std::function<>: |
| 212 |
* @code |
| 213 |
* sigc::slot<void(bool, int)> some_slot; |
| 214 |
* @endcode |
| 215 |
* |
| 216 |
dnl * |
| 217 |
dnl * @ingroup slot |
| 218 |
*/ |
| 219 |
template <LIST(class T_return, LOOP(class T_arg%1, $1))> |
| 220 |
class slot <LIST(T_return, LIST(LOOP(T_arg%1, $1), LOOP(nil, CALL_SIZE - $1)))>]) |
| 221 |
: public slot$1<LIST(T_return, LOOP(T_arg%1, $1))> |
| 222 |
{ |
| 223 |
public: |
| 224 |
typedef slot$1<LIST(T_return, LOOP(T_arg%1, $1))> parent_type; |
| 225 |
|
| 226 |
inline slot() {} |
| 227 |
|
| 228 |
/** Constructs a slot from an arbitrary functor. |
| 229 |
* @param _A_func The desired functor the new slot should be assigned to. |
| 230 |
*/ |
| 231 |
template <class T_functor> |
| 232 |
slot(const T_functor& _A_func) |
| 233 |
: parent_type(_A_func) {} |
| 234 |
|
| 235 |
// Without static_cast parent_type(const T_functor& _A_func) |
| 236 |
// is called instead of the copy constructor. |
| 237 |
/** Constructs a slot, copying an existing one. |
| 238 |
* @param src The existing slot to copy. |
| 239 |
*/ |
| 240 |
slot(const slot& src) |
| 241 |
: parent_type(static_cast<const parent_type&>(src)) {} |
| 242 |
|
| 243 |
// Without static_cast parent_type(const T_functor& _A_func) |
| 244 |
// is called instead of the move constructor. |
| 245 |
/** Constructs a slot, moving an existing one. |
| 246 |
* If @p src is connected to a parent (e.g. a signal), it is copied, not moved. |
| 247 |
* @param src The existing slot to move or copy. |
| 248 |
*/ |
| 249 |
slot(slot&& src) |
| 250 |
: parent_type(std::move(static_cast<parent_type&>(src))) {} |
| 251 |
|
| 252 |
/** Overrides this slot, making a copy from another slot. |
| 253 |
* @param src The slot from which to make a copy. |
| 254 |
* @return @p this. |
| 255 |
*/ |
| 256 |
slot& operator=(const slot& src) |
| 257 |
{ |
| 258 |
parent_type::operator=(src); |
| 259 |
return *this; |
| 260 |
} |
| 261 |
|
| 262 |
/** Overrides this slot, making a move from another slot. |
| 263 |
* If @p src is connected to a parent (e.g. a signal), it is copied, not moved. |
| 264 |
* @param src The slot from which to move or copy. |
| 265 |
* @return @p this. |
| 266 |
*/ |
| 267 |
slot& operator=(slot&& src) |
| 268 |
{ |
| 269 |
parent_type::operator=(std::move(src)); |
| 270 |
return *this; |
| 271 |
} |
| 272 |
}; |
| 273 |
|
| 274 |
ifelse($1, $2,[dnl |
| 275 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 276 |
//template specialization of visitor<>::do_visit_each<>(action, functor): |
| 277 |
/** Performs a functor on each of the targets of a functor. |
| 278 |
* |
| 279 |
* There are three function overloads for sigc::slot. |
| 280 |
* |
| 281 |
* The first two overloads are very specialized. They handle the (probably unusual) |
| 282 |
* case when the functor, stored in a slot, contains a slot. They are invoked from |
| 283 |
* the constructor, destructor or destroy() method of typed_slot_rep. |
| 284 |
* The first overload, called from the constructor of the outer slot, sets |
| 285 |
* the outer slot as the parent of the inner slot. The second overload, called from |
| 286 |
* the destructor or destroy() of the outer slot, unsets the parent of the inner slot. |
| 287 |
* When an object referenced from the inner slot is deleted, the inner slot calls |
| 288 |
* its slot_rep::disconnect(), which calls the outer slot's slot_rep::notify(). |
| 289 |
* The outer slot is informed just as if one of its directly referenced objects |
| 290 |
* had been deleted. Result: The outer slot is disconnected from its parent, |
| 291 |
* if any (for instance a sigc::signal). |
| 292 |
* See https://bugzilla.gnome.org/show_bug.cgi?id=755003 |
| 293 |
* |
| 294 |
* The third overload is identical to do_visit_each() in visitor's primary template. |
| 295 |
* |
| 296 |
* @ingroup slot |
| 297 |
*/ |
| 298 |
template <LIST(typename T_return, LOOP(typename T_arg%1, $1))> |
| 299 |
struct visitor<slot<LIST(T_return, LOOP(T_arg%1, $1))>> |
| 300 |
{ |
| 301 |
static void do_visit_each(const internal::limit_derived_target<trackable*, internal::slot_do_bind>& _A_action, |
| 302 |
const slot<LIST(T_return, LOOP(T_arg%1, $1))>& _A_target) |
| 303 |
{ |
| 304 |
if (_A_target.rep_ && _A_target.rep_->parent_ == nullptr) |
| 305 |
_A_target.rep_->set_parent(_A_action.action_.rep_, &internal::slot_rep::notify); |
| 306 |
} |
| 307 |
|
| 308 |
static void do_visit_each(const internal::limit_derived_target<trackable*, internal::slot_do_unbind>& _A_action, |
| 309 |
const slot<LIST(T_return, LOOP(T_arg%1, $1))>& _A_target) |
| 310 |
{ |
| 311 |
if (_A_target.rep_ && _A_target.rep_->parent_ == _A_action.action_.rep_) |
| 312 |
_A_target.rep_->set_parent(nullptr, nullptr); |
| 313 |
} |
| 314 |
|
| 315 |
template <typename T_action> |
| 316 |
static void do_visit_each(const T_action& _A_action, |
| 317 |
const slot<LIST(T_return, LOOP(T_arg%1, $1))>& _A_target) |
| 318 |
{ |
| 319 |
_A_action(_A_target); |
| 320 |
} |
| 321 |
}; |
| 322 |
#endif // DOXYGEN_SHOULD_SKIP_THIS |
| 323 |
]) |
| 324 |
]) |
| 325 |
define([SLOT_CALL],[dnl |
| 326 |
/** Abstracts functor execution. |
| 327 |
* call_it() invokes a functor of type @e T_functor with a list of |
| 328 |
* parameters whose types are given by the template arguments. |
| 329 |
* address() forms a function pointer from call_it(). |
| 330 |
* |
| 331 |
* The following template arguments are used: |
| 332 |
* - @e T_functor The functor type. |
| 333 |
* - @e T_return The return type of call_it().dnl |
| 334 |
FOR(1,$1,[ |
| 335 |
* - @e T_arg%1 Argument type used in the definition of call_it().]) |
| 336 |
* |
| 337 |
*/ |
| 338 |
template<LIST(class T_functor, class T_return, LOOP(class T_arg%1, $1))> |
| 339 |
struct slot_call$1 |
| 340 |
{ |
| 341 |
/** Invokes a functor of type @p T_functor. |
| 342 |
* @param rep slot_rep object that holds a functor of type @p T_functor.dnl |
| 343 |
FOR(1, $1,[ |
| 344 |
* @param _A_a%1 Argument to be passed on to the functor.]) |
| 345 |
* @return The return values of the functor invocation. |
| 346 |
*/ |
| 347 |
static T_return call_it(LIST(slot_rep* rep, LOOP(_R_(T_arg%1) a_%1, $1))) |
| 348 |
{ |
| 349 |
typedef typed_slot_rep<T_functor> typed_slot; |
| 350 |
typed_slot *typed_rep = static_cast<typed_slot*>(rep);dnl |
| 351 |
ifelse($1,0,[ |
| 352 |
return (typed_rep->functor_)(); |
| 353 |
],[ |
| 354 |
return (typed_rep->functor_).SIGC_WORKAROUND_OPERATOR_PARENTHESES<LOOP([_R_(T_arg%1)],$1)> |
| 355 |
(LOOP(a_%1, $1)); |
| 356 |
])dnl |
| 357 |
} |
| 358 |
|
| 359 |
/** Forms a function pointer from call_it(). |
| 360 |
* @return A function pointer formed from call_it(). |
| 361 |
*/ |
| 362 |
static hook address() |
| 363 |
{ return sigc::internal::function_pointer_cast<hook>(&call_it); } |
| 364 |
}; |
| 365 |
|
| 366 |
]) |
| 367 |
|
| 368 |
divert(0)dnl |
| 369 |
_FIREWALL([FUNCTORS_SLOT]) |
| 370 |
#include <sigc++/trackable.h> |
| 371 |
#include <sigc++/visit_each.h> |
| 372 |
#include <sigc++/adaptors/adaptor_trait.h> |
| 373 |
#include <sigc++/functors/slot_base.h> |
| 374 |
|
| 375 |
//TODO: See comment in functor_trait.h. |
| 376 |
#if defined(nil) && defined(SIGC_PRAGMA_PUSH_POP_MACRO) |
| 377 |
#define SIGC_NIL_HAS_BEEN_PUSHED 1 |
| 378 |
#pragma push_macro("nil") |
| 379 |
#undef nil |
| 380 |
#endif |
| 381 |
|
| 382 |
namespace sigc { |
| 383 |
|
| 384 |
namespace internal { |
| 385 |
|
| 386 |
// Conversion between different types of function pointers with |
| 387 |
// reinterpret_cast can make gcc8 print a warning. |
| 388 |
// https://github.com/libsigcplusplus/libsigcplusplus/issues/1 |
| 389 |
// https://github.com/libsigcplusplus/libsigcplusplus/issues/8 |
| 390 |
/** Returns the supplied function pointer, cast to a pointer to another function type. |
| 391 |
* |
| 392 |
* When a single reinterpret_cast between function pointer types causes a |
| 393 |
* compiler warning or error, this function may work. |
| 394 |
* |
| 395 |
* Qualify calls with namespace names: sigc::internal::function_pointer_cast<>(). |
| 396 |
* If you don't, indirect calls from another library that also contains a |
| 397 |
* function_pointer_cast<>() (perhaps glibmm), can be ambiguous due to ADL |
| 398 |
* (argument-dependent lookup). |
| 399 |
*/ |
| 400 |
template <typename T_out, typename T_in> |
| 401 |
inline T_out function_pointer_cast(T_in in) |
| 402 |
{ |
| 403 |
// The double reinterpret_cast suppresses a warning from gcc8 with the |
| 404 |
// -Wcast-function-type option. |
| 405 |
return reinterpret_cast<T_out>(reinterpret_cast<void (*)()>(in)); |
| 406 |
} |
| 407 |
|
| 408 |
/** A typed slot_rep. |
| 409 |
* A typed slot_rep holds a functor that can be invoked from |
| 410 |
* slot::operator()(). visit_each() is used to visit the functor's |
| 411 |
* targets that inherit trackable recursively and register the |
| 412 |
* notification callback. Consequently the slot_rep object will be |
| 413 |
* notified when some referred object is destroyed or overwritten. |
| 414 |
*/ |
| 415 |
template <class T_functor> |
| 416 |
struct typed_slot_rep : public slot_rep |
| 417 |
{ |
| 418 |
typedef typed_slot_rep<T_functor> self; |
| 419 |
|
| 420 |
/* Use an adaptor type so that arguments can be passed as const references |
| 421 |
* through explicit template instantiation from slot_call#::call_it() */ |
| 422 |
typedef typename adaptor_trait<T_functor>::adaptor_type adaptor_type; |
| 423 |
|
| 424 |
/** The functor contained by this slot_rep object. */ |
| 425 |
adaptor_type functor_; |
| 426 |
|
| 427 |
/** Constructs an invalid typed slot_rep object. |
| 428 |
* The notification callback is registered using visit_each(). |
| 429 |
* @param functor The functor contained by the new slot_rep object. |
| 430 |
*/ |
| 431 |
inline typed_slot_rep(const T_functor& functor) |
| 432 |
: slot_rep(nullptr, &destroy, &dup), functor_(functor) |
| 433 |
{ sigc::visit_each_type<trackable*>(slot_do_bind(this), functor_); } |
| 434 |
|
| 435 |
inline typed_slot_rep(const typed_slot_rep& cl) |
| 436 |
: slot_rep(cl.call_, &destroy, &dup), functor_(cl.functor_) |
| 437 |
{ sigc::visit_each_type<trackable*>(slot_do_bind(this), functor_); } |
| 438 |
|
| 439 |
typed_slot_rep& operator=(const typed_slot_rep& src) = delete; |
| 440 |
|
| 441 |
typed_slot_rep(typed_slot_rep&& src) = delete; |
| 442 |
typed_slot_rep& operator=(typed_slot_rep&& src) = delete; |
| 443 |
|
| 444 |
inline ~typed_slot_rep() |
| 445 |
{ |
| 446 |
call_ = nullptr; |
| 447 |
destroy_ = nullptr; |
| 448 |
sigc::visit_each_type<trackable*>(slot_do_unbind(this), functor_); |
| 449 |
} |
| 450 |
|
| 451 |
/** Detaches the stored functor from the other referred trackables and destroys it. |
| 452 |
* This does not destroy the base slot_rep object. |
| 453 |
*/ |
| 454 |
static void* destroy(void* data) |
| 455 |
{ |
| 456 |
self* self_ = static_cast<self*>(reinterpret_cast<slot_rep*>(data)); |
| 457 |
self_->call_ = nullptr; |
| 458 |
self_->destroy_ = nullptr; |
| 459 |
sigc::visit_each_type<trackable*>(slot_do_unbind(self_), self_->functor_); |
| 460 |
self_->functor_.~adaptor_type(); |
| 461 |
/* don't call disconnect() here: destroy() is either called |
| 462 |
* a) from the parent itself (in which case disconnect() leads to a segfault) or |
| 463 |
* b) from a parentless slot (in which case disconnect() does nothing) |
| 464 |
*/ |
| 465 |
return nullptr; |
| 466 |
} |
| 467 |
|
| 468 |
/** Makes a deep copy of the slot_rep object. |
| 469 |
* Deep copy means that the notification callback of the new |
| 470 |
* slot_rep object is registered in the referred trackables. |
| 471 |
* @return A deep copy of the slot_rep object. |
| 472 |
*/ |
| 473 |
static void* dup(void* data) |
| 474 |
{ |
| 475 |
slot_rep* a_rep = reinterpret_cast<slot_rep*>(data); |
| 476 |
return static_cast<slot_rep*>(new self(*static_cast<self*>(a_rep))); |
| 477 |
} |
| 478 |
}; |
| 479 |
|
| 480 |
FOR(0,CALL_SIZE,[[SLOT_CALL(%1)]])dnl |
| 481 |
|
| 482 |
/** Abstracts functor execution. |
| 483 |
* call_it() invokes a functor of type @e T_functor with a list of |
| 484 |
* parameters whose types are given by the template arguments. |
| 485 |
* address() forms a function pointer from call_it(). |
| 486 |
* |
| 487 |
* The following template arguments are used: |
| 488 |
* - @e T_functor The functor type. |
| 489 |
* - @e T_return The return type of call_it(). |
| 490 |
* - @e T_arg Argument types used in the definition of call_it(). |
| 491 |
* |
| 492 |
*/ |
| 493 |
template<class T_functor, class T_return, class... T_arg> |
| 494 |
struct slot_call |
| 495 |
{ |
| 496 |
/** Invokes a functor of type @p T_functor. |
| 497 |
* @param rep slot_rep object that holds a functor of type @p T_functor. |
| 498 |
* @param _A_a Arguments to be passed on to the functor. |
| 499 |
* @return The return values of the functor invocation. |
| 500 |
*/ |
| 501 |
static T_return call_it(slot_rep* rep, type_trait_take_t<T_arg>... a_) |
| 502 |
{ |
| 503 |
using typed_slot = typed_slot_rep<T_functor>; |
| 504 |
typed_slot *typed_rep = static_cast<typed_slot*>(rep); |
| 505 |
return (typed_rep->functor_).SIGC_WORKAROUND_OPERATOR_PARENTHESES<type_trait_take_t<T_arg>...> |
| 506 |
(a_...); |
| 507 |
} |
| 508 |
|
| 509 |
/** Forms a function pointer from call_it(). |
| 510 |
* @return A function pointer formed from call_it(). |
| 511 |
*/ |
| 512 |
static hook address() |
| 513 |
{ return sigc::internal::function_pointer_cast<hook>(&call_it); } |
| 514 |
}; |
| 515 |
|
| 516 |
/** Abstracts functor execution. |
| 517 |
* call_it() invokes a functor without parameters of type @e T_functor. |
| 518 |
* address() forms a function pointer from call_it(). |
| 519 |
* |
| 520 |
* This is a specialization for functors without parameters. |
| 521 |
* |
| 522 |
* The following template arguments are used: |
| 523 |
* - @e T_functor The functor type. |
| 524 |
* - @e T_return The return type of call_it(). |
| 525 |
* |
| 526 |
*/ |
| 527 |
template<class T_functor, class T_return> |
| 528 |
struct slot_call<T_functor, T_return> |
| 529 |
{ |
| 530 |
/** Invokes a functor of type @p T_functor. |
| 531 |
* @param rep slot_rep object that holds a functor of type @p T_functor. |
| 532 |
* @return The return values of the functor invocation. |
| 533 |
*/ |
| 534 |
static T_return call_it(slot_rep* rep) |
| 535 |
{ |
| 536 |
using typed_slot = typed_slot_rep<T_functor>; |
| 537 |
typed_slot *typed_rep = static_cast<typed_slot*>(rep); |
| 538 |
return (typed_rep->functor_)(); |
| 539 |
} |
| 540 |
|
| 541 |
/** Forms a function pointer from call_it(). |
| 542 |
* @return A function pointer formed from call_it(). |
| 543 |
*/ |
| 544 |
static hook address() |
| 545 |
{ return sigc::internal::function_pointer_cast<hook>(&call_it); } |
| 546 |
}; |
| 547 |
|
| 548 |
} /* namespace internal */ |
| 549 |
|
| 550 |
|
| 551 |
FOR(0,CALL_SIZE,[[SLOT_N(%1,CALL_SIZE)]]) |
| 552 |
SLOT(CALL_SIZE,CALL_SIZE) |
| 553 |
FOR(0,eval(CALL_SIZE-1),[[SLOT(%1,CALL_SIZE)]]) |
| 554 |
|
| 555 |
/** Converts an arbitrary functor to a unified type which is opaque. |
| 556 |
* sigc::slot itself is a functor or, to be more precise, a closure. It contains |
| 557 |
* a single, arbitrary functor (or closure) that is executed in operator()(). |
| 558 |
* |
| 559 |
* The template arguments determine the function signature of operator()(): |
| 560 |
* - @e T_return The return type of operator()(). |
| 561 |
* - @e T_arg Argument types used in the definition of operator()(). |
| 562 |
* |
| 563 |
* For instance, to declare a slot that returns void and takes two parameters |
| 564 |
* of bool and int: |
| 565 |
* @code |
| 566 |
* sigc::slot<void(bool, int)> some_slot; |
| 567 |
* @endcode |
| 568 |
* |
| 569 |
* Alternatively, you may use this deprecated syntax: |
| 570 |
* @code |
| 571 |
* sigc::slot<void, bool, int> some_slot; |
| 572 |
* @endcode |
| 573 |
* |
| 574 |
* To use, simply assign the desired functor to the slot. If the functor |
| 575 |
* is not compatible with the parameter list defined with the template |
| 576 |
* arguments then compiler errors are triggered. When called, the slot |
| 577 |
* will invoke the functor with minimal copies. |
| 578 |
* block() and unblock() can be used to block the functor's invocation |
| 579 |
* from operator()() temporarily. |
| 580 |
* |
| 581 |
* @ingroup slot |
| 582 |
*/ |
| 583 |
template <class T_return, class... T_arg> |
| 584 |
class slot<T_return(T_arg...)> |
| 585 |
: public slot_base |
| 586 |
{ |
| 587 |
public: |
| 588 |
using result_type = T_return; |
| 589 |
//TODO: using arg_type_ = type_trait_take_t<T_arg>; |
| 590 |
|
| 591 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 592 |
private: |
| 593 |
using rep_type = internal::slot_rep; |
| 594 |
public: |
| 595 |
using call_type = T_return (*)(rep_type*, type_trait_take_t<T_arg>...); |
| 596 |
#endif |
| 597 |
|
| 598 |
/** Invoke the contained functor unless slot is in blocking state. |
| 599 |
* @param _A_a Arguments to be passed on to the functor. |
| 600 |
* @return The return value of the functor invocation. |
| 601 |
*/ |
| 602 |
inline T_return operator()(type_trait_take_t<T_arg>... _A_a) const |
| 603 |
{ |
| 604 |
if (!empty() && !blocked()) |
| 605 |
return (sigc::internal::function_pointer_cast<call_type>(slot_base::rep_->call_))(slot_base::rep_, _A_a...); |
| 606 |
return T_return(); |
| 607 |
} |
| 608 |
|
| 609 |
inline slot() {} |
| 610 |
|
| 611 |
/** Constructs a slot from an arbitrary functor. |
| 612 |
* @param _A_func The desired functor the new slot should be assigned to. |
| 613 |
*/ |
| 614 |
template <class T_functor> |
| 615 |
slot(const T_functor& _A_func) |
| 616 |
: slot_base(new internal::typed_slot_rep<T_functor>(_A_func)) |
| 617 |
{ |
| 618 |
//The slot_base:: is necessary to stop the HP-UX aCC compiler from being confused. murrayc. |
| 619 |
slot_base::rep_->call_ = internal::slot_call<T_functor, T_return, T_arg...>::address(); |
| 620 |
} |
| 621 |
|
| 622 |
/** Constructs a slot, copying an existing one. |
| 623 |
* @param src The existing slot to copy. |
| 624 |
*/ |
| 625 |
slot(const slot& src) |
| 626 |
: slot_base(src) |
| 627 |
{} |
| 628 |
|
| 629 |
/** Constructs a slot, moving an existing one. |
| 630 |
* If @p src is connected to a parent (e.g. a signal), it is copied, not moved. |
| 631 |
* @param src The existing slot to move or copy. |
| 632 |
*/ |
| 633 |
slot(slot&& src) |
| 634 |
: slot_base(std::move(src)) |
| 635 |
{} |
| 636 |
|
| 637 |
/** Overrides this slot, making a copy from another slot. |
| 638 |
* @param src The slot from which to make a copy. |
| 639 |
* @return @p this. |
| 640 |
*/ |
| 641 |
slot& operator=(const slot& src) |
| 642 |
{ |
| 643 |
slot_base::operator=(src); |
| 644 |
return *this; |
| 645 |
} |
| 646 |
|
| 647 |
/** Overrides this slot, making a move from another slot. |
| 648 |
* If @p src is connected to a parent (e.g. a signal), it is copied, not moved. |
| 649 |
* @param src The slot from which to move or copy. |
| 650 |
* @return @p this. |
| 651 |
*/ |
| 652 |
slot& operator=(slot&& src) |
| 653 |
{ |
| 654 |
slot_base::operator=(std::move(src)); |
| 655 |
return *this; |
| 656 |
} |
| 657 |
}; |
| 658 |
|
| 659 |
} /* namespace sigc */ |
| 660 |
|
| 661 |
#ifdef SIGC_NIL_HAS_BEEN_PUSHED |
| 662 |
#undef SIGC_NIL_HAS_BEEN_PUSHED |
| 663 |
#pragma pop_macro("nil") |
| 664 |
#endif |