shared_ptr_compat.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * fkie_message_filters
4  * Copyright © 2018-2020 Fraunhofer FKIE
5  * Author: Timo Röhling
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ****************************************************************************/
20 
21 #ifndef INCLUDE_FKIE_MESSAGE_FILTERS_HELPERS_SHARED_PTR_COMPAT_H_
22 #define INCLUDE_FKIE_MESSAGE_FILTERS_HELPERS_SHARED_PTR_COMPAT_H_
23 
24 #include <memory>
25 #include <boost/shared_ptr.hpp>
26 
27 namespace fkie_message_filters
28 {
29 namespace helpers
30 {
31 
32 template<class SP>
33 struct Holder
34 {
35  SP sp;
36  explicit Holder (const SP& p) : sp(p) {}
37  void operator()(...) { sp.reset(); }
38 };
39 
40 template<class T>
41 std::shared_ptr<T> to_std_shared_ptr (const boost::shared_ptr<T>& p)
42 {
43  using H = Holder<std::shared_ptr<T>>;
44  if (H* h = boost::get_deleter<H, T>(p))
45  {
46  return h->sp;
47  }
48  else
49  {
50  return std::shared_ptr<T>(p.get(), Holder<boost::shared_ptr<T>>(p));
51  }
52 }
53 
54 template<class T>
55 std::shared_ptr<T> to_std_shared_ptr (const std::shared_ptr<T>& p)
56 {
57  return p;
58 }
59 
60 template<class T>
61 boost::shared_ptr<T> to_boost_shared_ptr (const std::shared_ptr<T>& p)
62 {
63  using H = Holder<boost::shared_ptr<T>>;
64  if (H* h = std::get_deleter<H, T>(p))
65  {
66  return h->sp;
67  }
68  else
69  {
71  }
72 }
73 
74 template<class T>
76 {
77  return p;
78 }
79 
80 template<class T, class U>
82 {
83  to = from;
84 }
85 
86 template<class T, class U>
87 void convert_shared_ptr (const boost::shared_ptr<T>& from, std::shared_ptr<U>& to)
88 {
89  to = to_std_shared_ptr(from);
90 }
91 
92 template<class T, class U>
93 void convert_shared_ptr (const std::shared_ptr<T>& from, std::shared_ptr<U>& to)
94 {
95  to = from;
96 }
97 
98 template<class T, class U>
99 void convert_shared_ptr (const std::shared_ptr<T>& from, boost::shared_ptr<U>& to)
100 {
101  to = to_boost_shared_ptr(from);
102 }
103 
104 template<class SP, class T>
105 SP convert_shared_ptr (const std::shared_ptr<T>& p)
106 {
107  SP sp;
108  convert_shared_ptr<T, typename SP::element_type>(p, sp);
109  return sp;
110 }
111 
112 template<class SP, class T>
114 {
115  SP sp;
116  convert_shared_ptr<T, typename SP::element_type>(p, sp);
117  return sp;
118 }
119 
120 } // namespace helpers
121 } // namespace fkie_message_filters
122 
123 #endif /* INCLUDE_FKIE_MESSAGE_FILTERS_HELPERS_SHARED_PTR_COMPAT_H_ */
void convert_shared_ptr(const boost::shared_ptr< T > &from, boost::shared_ptr< U > &to)
boost::shared_ptr< T > to_boost_shared_ptr(const std::shared_ptr< T > &p)
Primary namespace.
Definition: buffer.h:33
std::shared_ptr< T > to_std_shared_ptr(const boost::shared_ptr< T > &p)


fkie_message_filters
Author(s): Timo Röhling
autogenerated on Mon Feb 28 2022 22:21:43