File: pointer_traits.hpp

package info (click to toggle)
ros2-rcpputils 2.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 524 kB
  • sloc: cpp: 5,160; xml: 31; ansic: 20; makefile: 4
file content (112 lines) | stat: -rw-r--r-- 3,362 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2019 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/*! \file pointer_traits.hpp
 * \brief Additional type traits support for smart pointers
 */

#ifndef RCPPUTILS__POINTER_TRAITS_HPP_
#define RCPPUTILS__POINTER_TRAITS_HPP_

#include <memory>
#include <type_traits>

namespace rcpputils
{

namespace details
{

template<class T>
struct is_smart_pointer_helper : std::false_type
{};

template<class T>
struct is_smart_pointer_helper<std::shared_ptr<T>>: std::true_type
{};

template<class T>
struct is_smart_pointer_helper<std::unique_ptr<T>>: std::true_type
{};

template<class T>
struct is_smart_pointer : is_smart_pointer_helper<typename std::remove_cv<T>::type>
{};

template<
  class T,
  bool is_smart_pointer
>
struct remove_pointer
{
  using type = typename std::remove_pointer<T>::type;
};

template<class T>
struct remove_pointer<T, true>
{
  using type = typename std::remove_pointer<
    decltype(std::declval<typename std::remove_volatile<T>::type>().get())>::type;
};
}  // namespace details

/// Type traits for validating if T is of type pointer or smart pointer
/**
 * In comparison to the existing type trait for pointer in the stdlib `std::is_pointer<T>`
 * https://en.cppreference.com/w/cpp/types/is_pointer this trait is enhancing it for
 * checking of smart pointer types as well.
 * The valid pointer types are T*, std::shared_pointer<T> and std::unique_ptr<T>
 *
 * Potential use cases are for static assert when passing a template parameter requiring this
 * parameter to be of type pointer without specifying which type of pointer (raw, smart).
 *
 * ```
 * class MyType
 * {
 *   template<class T>
 *   MyType(T && arg)
 *   {
 *     static_assert(rcpputils::is_pointer<decltype(arg)>::value, "arg has to be a pointer");
 *
 *     arg->do_stuff();  // with the assert above, this call is guaranteed to work.
 *   }
 * };
 * ```
 */
template<class T>
struct is_pointer
{
  /// Indicates whether this object is a pointer or smart pointer.
  static constexpr bool value = std::is_pointer<typename std::remove_reference<T>::type>::value ||
    details::is_smart_pointer<typename std::remove_reference<T>::type>::value;
};

/// Type traits for deducing the data type of T from a pointer or smart pointer.
/**
 * In comparison to the existing type trait for pointer in the stdlib `std::remove_pointer<T>`
 * https://en.cppreference.com/w/cpp/types/remove_pointer this trait is enhancing it for
 * checking of smart pointer types as well.
 * The valid pointer types are T*, std::shared_pointer<T> and std::unique_ptr<T>
 *
 */
template<class T>
struct remove_pointer
{
  using type = typename details::remove_pointer<
    typename std::remove_reference<T>::type, details::is_smart_pointer<T>::value>::type;
};

}  // namespace rcpputils

#endif  // RCPPUTILS__POINTER_TRAITS_HPP_