| 1 |
dnl Copyright 2013, The libsigc++ Development Team |
| 2 |
dnl |
| 3 |
dnl This file is part of libsigc++. |
| 4 |
dnl |
| 5 |
dnl This library is free software; you can redistribute it and/or |
| 6 |
dnl modify it under the terms of the GNU Lesser General Public |
| 7 |
dnl License as published by the Free Software Foundation; either |
| 8 |
dnl version 2.1 of the License, or (at your option) any later version. |
| 9 |
dnl |
| 10 |
dnl This library is distributed in the hope that it will be useful, |
| 11 |
dnl but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
dnl Lesser General Public License for more details. |
| 14 |
dnl |
| 15 |
dnl You should have received a copy of the GNU Lesser General Public |
| 16 |
dnl License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
dnl |
| 18 |
divert(-1) |
| 19 |
|
| 20 |
include(template.macros.m4) |
| 21 |
|
| 22 |
define([TRACK_OBJECT_OPERATOR],[dnl |
| 23 |
/** Invokes the wrapped functor passing on the arguments.dnl |
| 24 |
FOR(1, $1,[ |
| 25 |
* @param _A_arg%1 Argument to be passed on to the functor.]) |
| 26 |
* @return The return value of the functor invocation. |
| 27 |
*/ |
| 28 |
template <LOOP([typename T_arg%1], $1)> |
| 29 |
typename deduce_result_type<LOOP(T_arg%1, $1)>::type |
| 30 |
operator()(LOOP(T_arg%1 _A_arg%1, $1)) |
| 31 |
{ |
| 32 |
return this->functor_.SIGC_WORKAROUND_OPERATOR_PARENTHESES<LOOP([ |
| 33 |
_P_(T_arg%1)], $1)> |
| 34 |
(LOOP(_A_arg%1, $1)); |
| 35 |
} |
| 36 |
|
| 37 |
#ifndef SIGC_TEMPLATE_SPECIALIZATION_OPERATOR_OVERLOAD |
| 38 |
template <LOOP([typename T_arg%1], $1)> |
| 39 |
typename deduce_result_type<LOOP(T_arg%1, $1)>::type |
| 40 |
sun_forte_workaround(LOOP(T_arg%1 _A_arg%1, $1)) |
| 41 |
{ |
| 42 |
return this->functor_.SIGC_WORKAROUND_OPERATOR_PARENTHESES<LOOP([ |
| 43 |
_P_(T_arg%1)], $1)> |
| 44 |
(LOOP(_A_arg%1, $1)); |
| 45 |
} |
| 46 |
#endif |
| 47 |
|
| 48 |
])dnl end TRACK_OBJECT_OPERATOR |
| 49 |
|
| 50 |
dnl track_obj_functor[2..CALL_SIZE]. $1 is assumed to be >= 2. |
| 51 |
define([TRACK_OBJECT_FUNCTOR],[dnl |
| 52 |
/** track_obj_functor$1 wraps a functor and stores $1 references to trackable objects. |
| 53 |
* Use the convenience function track_object() to create an instance of track_obj_functor$1. |
| 54 |
* |
| 55 |
* @tparam T_functor The type of functor to wrap.dnl |
| 56 |
FOR(1,$1,[ |
| 57 |
* @tparam T_obj%1 The type of a trackable object.]) |
| 58 |
* |
| 59 |
* @newin{2,4} |
| 60 |
* |
| 61 |
* @ingroup track_obj |
| 62 |
*/ |
| 63 |
template <typename T_functor, LOOP(typename T_obj%1, $1)> |
| 64 |
class track_obj_functor$1 : public track_obj_functor1<T_functor, T_obj1> |
| 65 |
{ |
| 66 |
public: |
| 67 |
/** Constructs a track_obj_functor$1 object that wraps the passed functor and |
| 68 |
* stores references to the passed trackable objects. |
| 69 |
* @param _A_func Functor.dnl |
| 70 |
FOR(1,$1,[ |
| 71 |
* @param _A_obj%1 Trackable object.]) |
| 72 |
*/ |
| 73 |
track_obj_functor$1(const T_functor& _A_func, LOOP(const T_obj%1& _A_obj%1, $1)) |
| 74 |
: track_obj_functor1<T_functor, T_obj1>(_A_func, _A_obj1)FOR(2,$1,[[, ]obj%1_(_A_obj%1)]) {} |
| 75 |
|
| 76 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 77 |
//protected: |
| 78 |
// public, so that visit_each() can access it.dnl |
| 79 |
FOR(2,$1,[ |
| 80 |
const_limit_reference<T_obj%1> obj%1_;]) |
| 81 |
#endif /* DOXYGEN_SHOULD_SKIP_THIS */ |
| 82 |
|
| 83 |
}; // end class track_obj_functor$1 |
| 84 |
|
| 85 |
])dnl end TRACK_OBJECT_FUNCTOR |
| 86 |
|
| 87 |
define([TRACK_OBJECT_VISIT_EACH],[dnl |
| 88 |
//template specialization of visitor<>::do_visit_each<>(action, functor): |
| 89 |
/** Performs a functor on each of the targets of a functor. |
| 90 |
* The function overload for sigc::track_obj_functor$1 performs a functor |
| 91 |
* on the functor and on the trackable object instances stored in the |
| 92 |
* sigc::track_obj_functor$1 object. |
| 93 |
* |
| 94 |
* @newin{2,4} |
| 95 |
* |
| 96 |
* @ingroup track_obj |
| 97 |
*/ |
| 98 |
template <typename T_functor, LOOP(typename T_obj%1, $1)> |
| 99 |
struct visitor<track_obj_functor$1<T_functor, LOOP(T_obj%1, $1)> > |
| 100 |
{ |
| 101 |
template <typename T_action> |
| 102 |
static void do_visit_each(const T_action& _A_action, |
| 103 |
const track_obj_functor$1<T_functor, LOOP(T_obj%1, $1)>& _A_target) |
| 104 |
{ |
| 105 |
sigc::visit_each(_A_action, _A_target.functor_);dnl |
| 106 |
FOR(1,$1,[ |
| 107 |
sigc::visit_each(_A_action, _A_target.obj%1_);]) |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
])dnl end TRACK_OBJECT_VISIT_EACH |
| 112 |
|
| 113 |
define([TRACK_OBJECT],[dnl |
| 114 |
_DEPRECATE_IFDEF_START |
| 115 |
/** Creates an adaptor of type sigc::track_obj_functor$1 which wraps a functor. |
| 116 |
* @param _A_func Functor that shall be wrapped.dnl |
| 117 |
FOR(1,$1,[ |
| 118 |
* @param _A_obj%1 Trackable object.]) |
| 119 |
* @return Adaptor that executes _A_func() on invocation. |
| 120 |
* |
| 121 |
* @newin{2,4} |
| 122 |
* @deprecated Use sigc::track_object() instead. |
| 123 |
* |
| 124 |
* @ingroup track_obj |
| 125 |
*/ |
| 126 |
template <typename T_functor, LOOP(typename T_obj%1, $1)> |
| 127 |
inline track_obj_functor$1<T_functor, LOOP(T_obj%1, $1)> |
| 128 |
track_obj(const T_functor& _A_func, LOOP(const T_obj%1& _A_obj%1, $1)) |
| 129 |
{ |
| 130 |
return track_obj_functor$1<T_functor, LOOP(T_obj%1, $1)> |
| 131 |
(_A_func, LOOP(_A_obj%1, $1)); |
| 132 |
} |
| 133 |
_DEPRECATE_IFDEF_END |
| 134 |
|
| 135 |
])dnl end TRACK_OBJECT |
| 136 |
|
| 137 |
define([TRACK_OBJECT2],[dnl |
| 138 |
/** Creates an adaptor of type sigc::track_obj_functor$1 which wraps a functor. |
| 139 |
* @param _A_func Functor that shall be wrapped.dnl |
| 140 |
FOR(1,$1,[[ |
| 141 |
* @param _A_obj%1 Trackable object, derived directly or indirectly from sigc::trackable.]]) |
| 142 |
* @return Adaptor that executes _A_func() on invocation. |
| 143 |
* |
| 144 |
* @newin{2,12} |
| 145 |
* |
| 146 |
* @ingroup track_obj |
| 147 |
*/ |
| 148 |
template <typename T_functor, LOOP(typename T_obj%1, $1)> |
| 149 |
inline track_obj_functor$1<T_functor, LOOP(T_obj%1, $1)> |
| 150 |
track_object(const T_functor& _A_func, LOOP(const T_obj%1& _A_obj%1, $1)) |
| 151 |
{ |
| 152 |
static_assert(LOOP([[std::is_base_of<sigc::trackable, T_obj%1>::value]], $1, [ && ]), |
| 153 |
"Each trackable object must be derived from sigc::trackable."); |
| 154 |
|
| 155 |
return track_obj_functor$1<T_functor, LOOP(T_obj%1, $1)> |
| 156 |
(_A_func, LOOP(_A_obj%1, $1)); |
| 157 |
} |
| 158 |
|
| 159 |
])dnl end TRACK_OBJECT2 |
| 160 |
|
| 161 |
divert(0)dnl |
| 162 |
_FIREWALL([ADAPTORS_TRACK_OBJ]) |
| 163 |
#include <sigc++/adaptors/adaptor_trait.h> |
| 164 |
#include <sigc++/limit_reference.h> |
| 165 |
#include <type_traits> |
| 166 |
|
| 167 |
namespace sigc { |
| 168 |
|
| 169 |
/** @defgroup track_obj track_obj(), track_object() |
| 170 |
* sigc::track_object() tracks trackable objects, referenced from a functor. |
| 171 |
* It can be useful when you assign a C++11 lambda expression or a std::function<> |
| 172 |
* to a slot, or connect it to a signal, and the lambda expression or std::function<> |
| 173 |
* contains references to sigc::trackable derived objects. |
| 174 |
* |
| 175 |
* The functor returned by sigc::track_object() is formally an adaptor, but it does |
| 176 |
* not alter the signature, return type or behaviour of the supplied functor. |
| 177 |
* Up to CALL_SIZE objects can be tracked. operator()() can have up to CALL_SIZE arguments. |
| 178 |
* |
| 179 |
* track_obj() is a deprecated alternative to track_object(). |
| 180 |
* |
| 181 |
* @par Example: |
| 182 |
* @code |
| 183 |
* struct bar : public sigc::trackable {}; |
| 184 |
* sigc::signal<void> some_signal; |
| 185 |
* void foo(bar&); |
| 186 |
* { |
| 187 |
* bar some_bar; |
| 188 |
* some_signal.connect([[&some_bar]](){ foo(some_bar); }); |
| 189 |
* // NOT disconnected automatically when some_bar goes out of scope |
| 190 |
* some_signal.connect(sigc::track_object([[&some_bar]](){ foo(some_bar); }, some_bar); |
| 191 |
* // disconnected automatically when some_bar goes out of scope |
| 192 |
* } |
| 193 |
* @endcode |
| 194 |
* |
| 195 |
* @ingroup adaptors |
| 196 |
*/ |
| 197 |
|
| 198 |
/** track_obj_functor1 wraps a functor and stores a reference to a trackable object. |
| 199 |
* Use the convenience function track_object() to create an instance of track_obj_functor1. |
| 200 |
* |
| 201 |
* @tparam T_functor The type of functor to wrap. |
| 202 |
* @tparam T_obj1 The type of a trackable object. |
| 203 |
* |
| 204 |
* @newin{2,4} |
| 205 |
* |
| 206 |
* @ingroup track_obj |
| 207 |
*/ |
| 208 |
template <typename T_functor, typename T_obj1> |
| 209 |
class track_obj_functor1 : public adapts<T_functor> |
| 210 |
{ |
| 211 |
public: |
| 212 |
typedef typename adapts<T_functor>::adaptor_type adaptor_type; |
| 213 |
|
| 214 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 215 |
template <LOOP(typename T_arg%1=void, CALL_SIZE)> |
| 216 |
struct deduce_result_type |
| 217 |
{ typedef typename adaptor_type::template deduce_result_type<LOOP(_P_(T_arg%1), CALL_SIZE)>::type type; }; |
| 218 |
#endif |
| 219 |
typedef typename adaptor_type::result_type result_type; |
| 220 |
|
| 221 |
/** Constructs a track_obj_functor1 object that wraps the passed functor and |
| 222 |
* stores a reference to the passed trackable object. |
| 223 |
* @param _A_func Functor. |
| 224 |
* @param _A_obj1 Trackable object. |
| 225 |
*/ |
| 226 |
track_obj_functor1(const T_functor& _A_func, const T_obj1& _A_obj1) |
| 227 |
: adapts<T_functor>(_A_func), obj1_(_A_obj1) {} |
| 228 |
|
| 229 |
/** Invokes the wrapped functor. |
| 230 |
* @return The return value of the functor invocation. |
| 231 |
*/ |
| 232 |
result_type operator()() |
| 233 |
{ return this->functor_(); } |
| 234 |
|
| 235 |
FOR(1,CALL_SIZE,[[TRACK_OBJECT_OPERATOR(%1)]])dnl |
| 236 |
|
| 237 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 238 |
//protected: |
| 239 |
// public, so that visit_each() can access it. |
| 240 |
const_limit_reference<T_obj1> obj1_; |
| 241 |
#endif /* DOXYGEN_SHOULD_SKIP_THIS */ |
| 242 |
|
| 243 |
}; // end class track_obj_functor1 |
| 244 |
|
| 245 |
FOR(2,CALL_SIZE,[[TRACK_OBJECT_FUNCTOR(%1)]])dnl |
| 246 |
|
| 247 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 248 |
FOR(1,CALL_SIZE,[[TRACK_OBJECT_VISIT_EACH(%1)]])dnl |
| 249 |
#endif // DOXYGEN_SHOULD_SKIP_THIS |
| 250 |
|
| 251 |
FOR(1,CALL_SIZE,[[TRACK_OBJECT(%1)]])dnl |
| 252 |
FOR(1,CALL_SIZE,[[TRACK_OBJECT2(%1)]])dnl |
| 253 |
|
| 254 |
} /* namespace sigc */ |