CARLA
 
载入中...
搜索中...
未找到
CompileTimeTypeMap.h
浏览该文件的文档.
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
12namespace carla {
13namespace sensor {
14
15namespace detail {
16
17 /// CompileTimeTypeMap 的私有实现
18 template <size_t Size, typename...>
20
21 /// 空映射的特化,当找不到键时返回该特化
22 template <size_t Size>
24
25 template <typename InKey>
26 struct get {
27 using type = void;// 默认类型为 void
28 static constexpr size_t index = Size;// 索引为 Size
29 };
30
31 template <size_t Index>
32 struct get_by_index {
33 using type = void;
34 using key = void;
35 };
36 };
37
38 template <size_t Size, typename Key, typename Value, typename... Rest>
39 struct CompileTimeTypeMapImpl<Size, std::pair<Key, Value>, Rest...> {
40
41 static constexpr size_t current_index() {
42 return Size - 1u - sizeof...(Rest);// 计算当前索引
43 }
44
45 // 递归调用该结构体,直到 InKey 匹配到某个元素
46 template <typename InKey>
47 struct get {
48 using type = typename std::conditional<
49 std::is_same<InKey, Key>::value,
50 Value,
51 typename CompileTimeTypeMapImpl<Size, Rest...>::template get<InKey>::type
52 >::type;
53 static constexpr size_t index =
54 std::is_same<InKey, Key>::value ?
55 current_index() :
56 CompileTimeTypeMapImpl<Size, Rest...>::template get<InKey>::index;
57 };
58
59 // 递归调用该结构体,直到 Index 匹配到某个元素
60 template <size_t Index>
61 struct get_by_index {
62 using type = typename std::conditional<
63 Index == current_index(),
64 Value,
65 typename CompileTimeTypeMapImpl<Size, Rest...>::template get_by_index<Index>::type
66 >::type;
67
68 using key = typename std::conditional<
69 Index == current_index(),
70 Key,
71 typename CompileTimeTypeMapImpl<Size, Rest...>::template get_by_index<Index>::key
72 >::type;
73 };
74 };
75
76} // namespace detail
77
78 /// 一个编译时结构,用于映射两种类型。可以通过键或索引查找元素。
79 ///
80 /// Example usage:
81 ///
82 /// using MyMap = CompileTimeTypeMap<std::pair<A, B>, std::pair<C, D>>;
83 /// using type_B = MyMap::get<A>::type;
84 /// constexpr size_t index_B = MyMap::get<A>::index;
85 /// using type_B_too = MyMap::get_by_index<index_B>::type;
86 ///
87 template <typename... Items>
89
90 static constexpr size_t size() {
91 return sizeof...(Items);// 返回映射大小
92 }
93
94 template <typename InKey>
95 using get = typename detail::CompileTimeTypeMapImpl<sizeof...(Items), Items...>::template get<InKey>;
96
97 template <size_t Index>
98 using get_by_index = typename detail::CompileTimeTypeMapImpl<sizeof...(Items), Items...>::template get_by_index<Index>;
99 };
100
101} // namespace sensor
102} // namespace carla
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
一个编译时结构,用于映射两种类型。可以通过键或索引查找元素。
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get_by_index< Index > get_by_index
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get< InKey > get
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 std::conditional< std::is_same< InKey, Key >::value, Value, typename CompileTimeTypeMapImpl< Size, Rest... >::template get< InKey >::type >::type type