CARLA
CompileTimeTypeMap.h
Go to the documentation of this file.
1 // Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
7 #pragma once
8 
9 #include <type_traits>
10 #include <utility>
11 
12 namespace carla {
13 namespace sensor {
14 
15 namespace detail {
16 
17  /// Private implementation of the CompileTimeTypeMap.
18  template <size_t Size, typename...>
20 
21  /// Specialization for an empty map, it is retrieved when a key cannot be
22  /// found.
23  template <size_t Size>
25 
26  template <typename InKey>
27  struct get {
28  using type = void;
29  static constexpr size_t index = Size;
30  };
31 
32  template <size_t Index>
33  struct get_by_index {
34  using type = void;
35  using key = void;
36  };
37  };
38 
39  template <size_t Size, typename Key, typename Value, typename... Rest>
40  struct CompileTimeTypeMapImpl<Size, std::pair<Key, Value>, Rest...> {
41 
42  static constexpr size_t current_index() {
43  return Size - 1u - sizeof...(Rest);
44  }
45 
46  // Recursively call this struct until the InKey matches an element.
47  template <typename InKey>
48  struct get {
49  using type = typename std::conditional<
50  std::is_same<InKey, Key>::value,
51  Value,
52  typename CompileTimeTypeMapImpl<Size, Rest...>::template get<InKey>::type
54  static constexpr size_t index =
55  std::is_same<InKey, Key>::value ?
56  current_index() :
57  CompileTimeTypeMapImpl<Size, Rest...>::template get<InKey>::index;
58  };
59 
60  // Recursively call this struct until the Index matches an element.
61  template <size_t Index>
62  struct get_by_index {
63  using type = typename std::conditional<
64  Index == current_index(),
65  Value,
66  typename CompileTimeTypeMapImpl<Size, Rest...>::template get_by_index<Index>::type
68 
69  using key = typename std::conditional<
70  Index == current_index(),
71  Key,
72  typename CompileTimeTypeMapImpl<Size, Rest...>::template get_by_index<Index>::key
74  };
75  };
76 
77 } // namespace detail
78 
79  /// A compile time structure for mapping two types. Lookup elements by Key or
80  /// by Index.
81  ///
82  /// Example usage:
83  ///
84  /// using MyMap = CompileTimeTypeMap<std::pair<A, B>, std::pair<C, D>>;
85  /// using type_B = MyMap::get<A>::type;
86  /// constexpr size_t index_B = MyMap::get<A>::index;
87  /// using type_B_too = MyMap::get_by_index<index_B>::type;
88  ///
89  template <typename... Items>
91 
92  static constexpr size_t size() {
93  return sizeof...(Items);
94  }
95 
96  template <typename InKey>
97  using get = typename detail::CompileTimeTypeMapImpl<sizeof...(Items), Items...>::template get<InKey>;
98 
99  template <size_t Index>
100  using get_by_index = typename detail::CompileTimeTypeMapImpl<sizeof...(Items), Items...>::template get_by_index<Index>;
101  };
102 
103 } // namespace sensor
104 } // namespace carla
static constexpr size_t size()
typename std::conditional< std::is_same< InKey, Key >::value, Value, typename CompileTimeTypeMapImpl< Size, Rest... >::template get< InKey >::type >::type type
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get< InKey > get
Specialization for an empty map, it is retrieved when a key cannot be found.
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
A compile time structure for mapping two types.
typename std::conditional< Index==current_index(), Key, typename CompileTimeTypeMapImpl< Size, Rest... >::template get_by_index< Index >::key >::type key
typename std::conditional< Index==current_index(), Value, typename CompileTimeTypeMapImpl< Size, Rest... >::template get_by_index< Index >::type >::type type
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get_by_index< Index > get_by_index
Private implementation of the CompileTimeTypeMap.