Program Listing for File pointer_traits.hpp

Return to documentation for file (include/rcpputils/pointer_traits.hpp)

// 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.

#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


template<class T>
struct is_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;
};


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_