libqi-api  release-2.5.3-2016-11-18
 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 <qi/macro.hpp>
18 # include <qi/log.hpp>
19 
20 namespace qi
21 {
22 
24  class TrackableBase {};
25 
42  template<typename T>
43  class Trackable: public TrackableBase
44  {
45  public:
47  Trackable();
49  QI_API_DEPRECATED_MSG(Use default constructor instead) Trackable(T* ptr);
50  ~Trackable();
51 
57  boost::weak_ptr<T> weakPtr();
58 
63  void wait();
64  protected:
69  void destroy();
70  private:
71  void _destroyed();
72 
73  boost::shared_ptr<T> _ptr;
74  boost::condition_variable _cond;
75  boost::mutex _mutex;
76  bool _wasDestroyed;
77  };
78 
79  class QI_API PointerLockException: public std::exception
80  {
81  public:
82  virtual const char* what() const throw()
83  {
84  return "Pointer Lock failed";
85  }
86  };
87 
88 #ifdef DOXYGEN
89 
94  template<typename RF, typename AF> boost::function<RF> bind(const AF& fun, ...);
95 #endif
96 
104  template<typename F, typename ARG0>
105  boost::function<F> track(boost::function<F> f, const ARG0& arg0);
113  template<typename F, typename ARG0>
114  boost::function<F> trackWithFallback(boost::function<void()> onFail, boost::function<F> f, const ARG0& arg0);
115 }
116 
117 # include <qi/detail/trackable.hxx>
118 #endif // _QI_TRACKABLE_HPP_
#define QI_API
Definition: api.hpp:33
QI_API_DEPRECATED_MSG(Use default constructor instead) Trackable(T *ptr)
void destroy()
Definition: trackable.hxx:37
Various macros for qi. (deprecated, export API, disallow copy, ..) <includename>qi/macro.hpp</includename> .
auto track(F &&f, Arg0 &&arg0) -> decltype(trackWithFallback(detail::throwPointerLockException, std::forward< F >(f), std::forward< Arg0 >(arg0)))
Definition: trackable.hxx:410
Object tracking by blocking destruction while shared pointers are present.
Definition: trackable.hpp:43
Convenient log macro.
virtual const char * what() const
Definition: trackable.hpp:82
auto trackWithFallback(boost::function< void()> onFail, F &&f, Arg0 &&arg0) -> decltype(detail::BindTransform< Arg0 >::wrap(std::forward< Arg0 >(arg0), std::forward< F >(f), std::move(onFail)))
Definition: trackable.hxx:404
std::enable_if< std::is_function< RF >::value, boost::function< RF > >::type bind(AF &&fun, Arg0 &&arg0, Args &&...args)
Definition: trackable.hxx:327
Trackable()
Default constructor.
Definition: trackable.hxx:22
boost::weak_ptr< T > weakPtr()
Definition: trackable.hxx:76
Common base class to templates Trackable for compile-time detection.
Definition: trackable.hpp:24