rcpputils  master
C++ API providing common utilities and data structures.
pointer_traits.hpp
Go to the documentation of this file.
1 // Copyright 2019 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
19 #ifndef RCPPUTILS__POINTER_TRAITS_HPP_
20 #define RCPPUTILS__POINTER_TRAITS_HPP_
21 
22 #include <memory>
23 #include <type_traits>
24 
25 namespace rcpputils
26 {
27 
28 namespace details
29 {
30 
31 template<class T>
32 struct is_smart_pointer_helper : std::false_type
33 {};
34 
35 template<class T>
36 struct is_smart_pointer_helper<std::shared_ptr<T>>: std::true_type
37 {};
38 
39 template<class T>
40 struct is_smart_pointer_helper<std::unique_ptr<T>>: std::true_type
41 {};
42 
43 template<class T>
44 struct is_smart_pointer : is_smart_pointer_helper<typename std::remove_cv<T>::type>
45 {};
46 
47 template<
48  class T,
49  bool is_smart_pointer
50 >
51 struct remove_pointer
52 {
53  using type = typename std::remove_pointer<T>::type;
54 };
55 
56 template<class T>
57 struct remove_pointer<T, true>
58 {
59  using type = typename std::remove_pointer<
60  decltype(std::declval<typename std::remove_volatile<T>::type>().get())>::type;
61 };
62 } // namespace details
63 
65 
87 template<class T>
88 struct is_pointer
89 {
92  details::is_smart_pointer<typename std::remove_reference<T>::type>::value;
93 };
94 
96 
103 template<class T>
105 {
106  using type = typename details::remove_pointer<
107  typename std::remove_reference<T>::type, details::is_smart_pointer<T>::value>::type;
108 };
109 
110 } // namespace rcpputils
111 
112 #endif // RCPPUTILS__POINTER_TRAITS_HPP_
std::false_type
rcpputils
Definition: asserts.hpp:37
rcpputils::remove_pointer::type
typename details::remove_pointer< typename std::remove_reference< T >::type, details::is_smart_pointer< T >::value >::type type
Definition: pointer_traits.hpp:107
rcpputils::is_pointer
Type traits for validating if T is of type pointer or smart pointer.
Definition: pointer_traits.hpp:88
rcpputils::remove_pointer
Type traits for deducing the data type of T from a pointer or smart pointer.
Definition: pointer_traits.hpp:104
std::remove_reference
std::remove_pointer
std
std::remove_volatile
std::is_pointer
rcpputils::is_pointer::value
static constexpr bool value
Indicates whether this object is a pointer or smart pointer.
Definition: pointer_traits.hpp:91
std::declval
T declval(T... args)