WeakBind.hh
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018 Open Source Robotics Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16*/
17
18#ifndef GAZEBO_COMMON_WEAKBIND_HH_
19#define GAZEBO_COMMON_WEAKBIND_HH_
20
21// This fixes compiler warnings, see #3147 and #3160
22#ifndef BOOST_BIND_GLOBAL_PLACEHOLDERS
23#define BOOST_BIND_GLOBAL_PLACEHOLDERS
24#endif
25#include <boost/bind.hpp>
26#include <boost/shared_ptr.hpp>
27
28namespace gazebo
29{
30 namespace common
31 {
32 namespace details
33 {
36 template <typename Func, typename T>
38 {
39 public: using WeakPtr = boost::weak_ptr<T>;
40
42 private: Func func;
43
46 private: WeakPtr ptr;
47
52 public: WeakBinder(Func _func, WeakPtr _ptr) :
53 func(_func),
54 ptr(_ptr)
55 {}
56
58 public: template <typename... Args> auto operator()(Args&&... _args)
59 -> typename std::enable_if<
60 !std::is_void<
61 decltype(this->func(std::forward<Args>(_args)...))
62 >::value,
63 decltype(this->func(std::forward<Args>(_args)...))
64 >::type
65 {
66 auto ptrLock = this->ptr.lock();
67 if (ptrLock)
68 {
69 return this->func(std::forward<Args>(_args)...);
70 }
71 else
72 {
73 return {}; // NOLINT(readability/braces)
74 }
75 }
76
78 public: template <typename... Args> auto operator()(Args&&... _args)
79 -> typename std::enable_if<
80 std::is_void<
81 decltype(this->func(std::forward<Args>(_args)...))
82 >::value,
83 void
84 >::type
85 {
86 auto ptrLock = this->ptr.lock();
87 if (ptrLock)
88 {
89 this->func(std::forward<Args>(_args)...);
90 }
91 }
92 };
93
94 template <typename Func, typename T>
95 WeakBinder<Func, T> makeWeakBinder(Func func, boost::weak_ptr<T> ptr)
96 {
97 return WeakBinder<Func, T>(func, ptr);
98 }
99 }
100
113 template <typename T, typename Func, typename... Args>
114 auto weakBind(Func _func, boost::shared_ptr<T> _ptr, Args... _args)
115 #if __cplusplus < 201402L
116 -> decltype(details::makeWeakBinder(
117 boost::bind(_func, _ptr.get(), _args...),
118 boost::weak_ptr<T>(_ptr)))
119 #endif
120 {
122 boost::bind(_func, _ptr.get(), _args...),
123 boost::weak_ptr<T>(_ptr));
124 }
126 }
127}
128
129#endif
common
Definition FuelModelDatabase.hh:42
Function object wrapper used by common::weakBind.
Definition WeakBind.hh:38
boost::weak_ptr< T > WeakPtr
Definition WeakBind.hh:39
WeakBinder(Func _func, WeakPtr _ptr)
Constructor.
Definition WeakBind.hh:52
auto operator()(Args &&... _args) -> typename std::enable_if< std::is_void< decltype(this->func(std::forward< Args >(_args)...)) >::value, void >::type
Return void version.
Definition WeakBind.hh:78
auto operator()(Args &&... _args) -> typename std::enable_if< !std::is_void< decltype(this->func(std::forward< Args >(_args)...)) >::value, decltype(this->func(std::forward< Args >(_args)...)) >::type
Return non-void version.
Definition WeakBind.hh:58
auto weakBind(Func _func, boost::shared_ptr< T > _ptr, Args... _args) -> decltype(details::makeWeakBinder(boost::bind(_func, _ptr.get(), _args...), boost::weak_ptr< T >(_ptr)))
Definition WeakBind.hh:114
WeakBinder< Func, T > makeWeakBinder(Func func, boost::weak_ptr< T > ptr)
Definition WeakBind.hh:95
Forward declarations for the common classes.
Definition Animation.hh:27