libqi-api  2.4.3.28
 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/log.hpp>
18 
19 namespace qi
20 {
21 
23  class TrackableBase {};
24 
41  template<typename T>
42  class Trackable: public TrackableBase
43  {
44  public:
46  Trackable(T* ptr);
48  ~Trackable();
49 
54  boost::shared_ptr<T> lock();
55 
61  boost::weak_ptr<T> weakPtr();
62 
67  void wait();
68  protected:
73  void destroy();
74  private:
75  void _destroyed();
76 
77  boost::shared_ptr<T> _ptr;
78  boost::condition_variable _cond;
79  boost::mutex _mutex;
80  bool _wasDestroyed;
81  };
82 
83  class QI_API PointerLockException: public std::exception
84  {
85  public:
86  virtual const char* what() const throw()
87  {
88  return "Pointer Lock failed";
89  }
90  };
91 
92 #ifdef DOXYGEN
93 
98  template<typename RF, typename AF> boost::function<RF> bind(const AF& fun, ...);
99 #endif
100 
108  template<typename F, typename ARG0>
109  boost::function<F> track(const boost::function<F>& f, const ARG0& arg0);
117  template<typename F, typename ARG0>
118  boost::function<F> trackWithFallback(boost::function<void()> onFail,
119  const boost::function<F>& f, const ARG0& arg0);
120 }
121 
122 # include <qi/detail/trackable.hxx>
123 #endif // _QI_TRACKABLE_HPP_
#define QI_API
Definition: api.hpp:34
boost::function< RF > bind(const AF &fun,...)
boost::shared_ptr< T > lock()
Definition: trackable.hxx:67
Trackable(T *ptr)
Default constructor.
Definition: trackable.hxx:21
void destroy()
Definition: trackable.hxx:28
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...
Definition: trackable.hxx:206
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...
Definition: trackable.hxx:212
~Trackable()
Default destructor.
Definition: trackable.hxx:54
Object tracking by blocking destruction while shared pointers are present.
Definition: trackable.hpp:42
Convenient log macro.
virtual const char * what() const
Definition: trackable.hpp:86
boost::weak_ptr< T > weakPtr()
Definition: trackable.hxx:73
Common base class to templates Trackable for compile-time detection.
Definition: trackable.hpp:23