TemplateHelpers.h#

Fully qualified name: carb/container/detail/TemplateHelpers.h

File members: carb/container/detail/TemplateHelpers.h

// Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
//
// NVIDIA CORPORATION and its licensors retain all intellectual property
// and proprietary rights in and to this software, related documentation
// and any modifications thereto. Any use, reproduction, disclosure or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA CORPORATION is strictly prohibited.
//

#pragma once

#include "../../cpp/TypeTraits.h"

#include <memory>

namespace carb
{
namespace container
{

namespace detail
{

template <class T, class, template <typename> class... Checks>
struct supports
{
    using type = std::false_type;
};

template <class T, template <typename> class... Checks>
struct supports<T, cpp::void_t<Checks<T>...>, Checks...>
{
    using type = std::true_type;
};

template <class T, template <typename> class... Checks>
using supports_t = typename supports<T, void, Checks...>::type;

template <class A>
using alloc_value_type = typename A::value_type;
template <class A>
using alloc_ptr_t = typename std::allocator_traits<A>::pointer;
template <class A>
using has_allocate = decltype(std::declval<alloc_ptr_t<A>&>() = std::declval<A>().allocate(0));
template <class A>
using has_deallocate = decltype(std::declval<A>().deallocate(std::declval<alloc_ptr_t<A>>(), 0));

template <class T>
using is_allocator = supports_t<T, alloc_value_type, has_allocate, has_deallocate>;

template <class Iterator>
using iterator_value_t = typename std::iterator_traits<Iterator>::value_type;

template <class Iterator>
using iterator_key_t = std::remove_const_t<typename iterator_value_t<Iterator>::first_type>;

template <class Iterator>
using iterator_mapped_t = typename iterator_value_t<Iterator>::second_type;

template <class Iterator>
using iterator_alloc_pair_t = std::pair<std::add_const_t<iterator_key_t<Iterator>>, iterator_mapped_t<Iterator>>;

#if CARB_HAS_CPP17
template <class T>
inline constexpr bool is_allocator_v = is_allocator<T>::value;
#endif

template <class Trait, class T>
struct dependent_bool : cpp::bool_constant<bool(Trait::value)>
{
};

} // namespace detail

} // namespace container
} // namespace carb