8 #ifndef _QI_DETAILS_TRACKABLE_HXX_
9 #define _QI_DETAILS_TRACKABLE_HXX_
11 #include <boost/function.hpp>
12 #include <boost/bind.hpp>
13 #include <boost/type_traits/is_base_of.hpp>
14 #include <boost/type_traits/remove_pointer.hpp>
15 #include <boost/weak_ptr.hpp>
16 #include <boost/function_types/result_type.hpp>
22 : _wasDestroyed(false)
24 _ptr = boost::shared_ptr<T>(ptr,
boost::bind(&Trackable::_destroyed, _1));
37 boost::mutex::scoped_lock lock(_mutex);
38 while (!_wasDestroyed)
48 boost::mutex::scoped_lock lock(_mutex);
60 qiLogError(
"qi.Trackable") <<
"Trackable destroyed without calling destroy()";
75 return boost::weak_ptr<T>(_ptr);
83 template<
typename WT,
typename ST,
typename F>
87 typedef typename boost::function_types::result_type<F>::type
Result;
88 LockAndCall(
const WT& arg, boost::function<F> func, boost::function<
void()> onFail)
94 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
95 QI_GEN_MAYBE_TEMPLATE_OPEN(comma) \
97 QI_GEN_MAYBE_TEMPLATE_CLOSE(comma) \
98 Result operator()(ADECL) \
100 ST s = _wptr.lock(); \
113 boost::function<F>
_f;
125 static boost::function<F>
wrap(
const T& arg, boost::function<F> func, boost::function<
void()> onFail)
131 template<
typename T>
struct BindTransform<boost::weak_ptr<T>, false >
134 static T*
transform(
const boost::weak_ptr<T>& arg)
138 return arg.lock().get();
141 static boost::function<F>
wrap(
const boost::weak_ptr<T>& arg, boost::function<F> func, boost::function<
void()> onFail)
143 return LockAndCall<boost::weak_ptr<T>, boost::shared_ptr<T>, F>(arg, func, onFail);
156 static boost::function<F>
wrap(T*
const & arg, boost::function<F> func, boost::function<
void()> onFail)
169 template<
typename RF,
typename AF>
170 boost::function<RF>
bind(
const AF& fun)
174 #define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma) \
175 template <typename RF, typename AF, typename ARG0 comma ATYPEDECL> \
176 boost::function<RF> bind(const AF& fun, const ARG0& arg0 comma ADECL) \
178 typedef typename detail::BindTransform< \
180 boost::is_base_of<TrackableBase, typename boost::remove_pointer<ARG0>::type>::value> Transform; \
181 typename Transform::type transformed = Transform::transform(arg0); \
182 boost::function<RF> f = boost::bind(fun, transformed comma AUSE); \
183 return Transform::wrap(arg0, f, detail::throwPointerLockException); \
185 template <typename RF, typename AF, typename ARG0 comma ATYPEDECL> \
186 boost::function<RF> bindWithFallback(const boost::function<void()>& onFail, \
188 const ARG0& arg0 comma ADECL) \
190 typedef typename detail::BindTransform< \
192 boost::is_base_of<TrackableBase, typename boost::remove_pointer<ARG0>::type>::value> Transform; \
193 typename Transform::type transformed = Transform::transform(arg0); \
194 boost::function<RF> f = boost::bind(fun, transformed comma AUSE); \
195 return Transform::wrap(arg0, f, onFail); \
197 template <typename RF, typename AF, typename ARG0 comma ATYPEDECL> \
198 boost::function<RF> bindSilent(const AF& fun, const ARG0& arg0 comma ADECL) \
200 return bindWithFallback<RF, AF, ARG0 comma ATYPES>(boost::function<void()>(), fun, arg0 comma AUSE); \
205 template<
typename F,
typename ARG0>
206 boost::function<F>
track(
const boost::function<F>& f,
const ARG0& arg0)
211 template<
typename F,
typename ARG0>
213 const boost::function<F>& f,
const ARG0& arg0)
216 return Transform::wrap(arg0, f, onFail);
218 template<
typename F,
typename ARG0>
219 boost::function<F>
trackSilent(
const boost::function<F>& f,
const ARG0& arg0)
221 return trackWithFallback<F, ARG0>(boost::function<void()>(), f, arg0);
225 #endif // _QI_DETAILS_TRACKABLE_HXX_
void destroy()
Stop and flush the logging system.
boost::function_types::result_type< F >::type Result
boost::function< RF > bind(const AF &fun,...)
boost::shared_ptr< T > lock()
void throwPointerLockException()
Trackable(T *ptr)
Default constructor.
#define qiLogError(...)
Log in error mode.
boost::function< void()> _onFail
LockAndCall(const WT &arg, boost::function< F > func, boost::function< void()> onFail)
boost::function< F > track(const boost::function< F > &f, const ARG0 &arg0)
Wrap given function f with a tracking check on arg0, which must be a weak pointer or a Trackable inst...
boost::function< F > trackSilent(const boost::function< F > &f, const ARG0 &arg0)
boost::function< F > trackWithFallback(boost::function< void()> onFail, const boost::function< F > &f, const ARG0 &arg0)
Wrap given function f with a tracking check on arg0, which must be a weak pointer or a Trackable inst...
~Trackable()
Default destructor.
Object tracking by blocking destruction while shared pointers are present.
#define genCall(n, ATYPEDECL, ATYPES, ADECL, AUSE, comma)
boost::weak_ptr< T > weakPtr()