libqi-api  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
trackable.hpp
Go to the documentation of this file.
1 #pragma once
2 /*
3  * Copyright (c) 2013 Aldebaran Robotics. All rights reserved.
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the COPYING file.
6  */
7 
8 
9 #ifndef _QI_TRACKABLE_HPP_
10 # define _QI_TRACKABLE_HPP_
11 
12 # include <boost/thread/mutex.hpp>
13 # include <boost/shared_ptr.hpp>
14 # include <boost/thread/condition_variable.hpp>
15 # include <boost/function.hpp>
16 
17 # include <ka/typetraits.hpp>
18 # include <qi/macro.hpp>
19 # include <ka/macroregular.hpp>
20 # include <qi/log.hpp>
21 
22 namespace qi
23 {
24 
26  class TrackableBase {};
27 
44  template<typename T>
45  class Trackable: public TrackableBase
46  {
47  public:
49  Trackable();
51  QI_API_DEPRECATED_MSG(Use default constructor instead) Trackable(T* ptr);
52  ~Trackable();
53 
59  boost::weak_ptr<T> weakPtr() const;
60 
65  void wait();
66  protected:
71  void destroy();
72  private:
73  void _destroyed();
74 
75  boost::shared_ptr<T> _ptr;
76  boost::condition_variable _cond;
77  boost::mutex _mutex;
78  bool _wasDestroyed;
79  };
80 
81  class QI_API PointerLockException: public std::exception
82  {
83  public:
84  virtual const char* what() const throw()
85  {
86  return "Pointer Lock failed";
87  }
88  };
89 
90 #ifdef DOXYGEN
91 
96  template<typename RF, typename AF> boost::function<RF> bind(const AF& fun, ...);
97 #endif
98 
106  template<typename F, typename ARG0>
107  boost::function<F> track(boost::function<F> f, const ARG0& arg0);
115  template<typename F, typename ARG0>
116  boost::function<F> trackWithFallback(boost::function<void()> onFail, boost::function<F> f, const ARG0& arg0);
117 
123  template<typename Proc, typename T>
125  {
126  Proc _fallback;
128  // Regular:
129  KA_GENERATE_FRIEND_REGULAR_OPS_2(TrackWithFallbackTransfo, _fallback, _trackable)
130  // PolymorphicTransformation:
131  template<typename F>
132  auto operator()(F&& f) const -> decltype(
133  trackWithFallback(_fallback, std::forward<F>(f), _trackable))
134  {
135  return trackWithFallback(_fallback, std::forward<F>(f), _trackable);
136  }
137  };
138 
142  template<typename Proc, typename T>
144  {
145  return {std::forward<Proc>(fallback), t};
146  }
147 
153  template<typename T>
155  {
157  // Regular:
158  KA_GENERATE_FRIEND_REGULAR_OPS_1(TrackSilentTransfo, _trackable)
159  // PolymorphicTransformation:
160  template<typename F>
161  auto operator()(F&& f) const -> decltype(
162  trackSilent(std::forward<F>(f), _trackable))
163  {
164  return trackSilent(std::forward<F>(f), _trackable);
165  }
166  };
167 
171  template<typename T>
173  {
174  return {t};
175  }
176 }
177 
178 # include <qi/detail/trackable.hxx>
179 #endif // _QI_TRACKABLE_HPP_
auto trackWithFallback(boost::function< void()> onFail, F &&f, T &&toTrack) -> decltype(detail::BindTransform< T >::wrap(std::forward< T >(toTrack), std::forward< F >(f), std::move(onFail)))
Definition: trackable.hxx:413
#define QI_API
Definition: api.hpp:33
auto track(F &&f, T &&toTrack) -> decltype(trackWithFallback(detail::throwPointerLockException, std::forward< F >(f), std::forward< T >(toTrack)))
Definition: trackable.hxx:420
QI_API_DEPRECATED_MSG(Use default constructor instead) Trackable(T *ptr)
TrackWithFallbackTransfo< ka::Decay< Proc >, T > trackWithFallbackTransfo(Proc &&fallback, T *t)
Definition: trackable.hpp:143
TrackSilentTransfo< T > trackSilentTransfo(T *t)
Definition: trackable.hpp:172
void destroy()
Definition: trackable.hxx:37
Various macros for qi. (deprecated, export API, disallow copy, ..) <includename>qi/macro.hpp</includename> .
boost::weak_ptr< T > weakPtr() const
Definition: trackable.hxx:76
Object tracking by blocking destruction while shared pointers are present.
Definition: trackable.hpp:45
Convenient log macro.
virtual const char * what() const
Definition: trackable.hpp:84
auto trackSilent(F &&f, T &&toTrack) -> decltype(trackWithFallback(
Definition: trackable.hxx:427
std::enable_if< std::is_function< RF >::value, boost::function< RF > >::type bind(AF &&fun, Arg0 &&arg0, Args &&...args)
Definition: trackable.hxx:308
Trackable()
Default constructor.
Definition: trackable.hxx:22
Common base class to templates Trackable for compile-time detection.
Definition: trackable.hpp:26