CARLA
 
载入中...
搜索中...
未找到
CompositeSerializer.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 "carla/Buffer.h"
10#include "carla/Memory.h"
13
14namespace carla {
15namespace sensor {
16// 类 SensorData 的前向声明,可能在其他地方有完整定义。
17 class SensorData;
18
19 // ===========================================================================
20 // -- CompositeSerializer ----------------------------------------------------
21 // ===========================================================================
22
23 /// 编译时映射表,用于将传感器对象映射到序列化器。
24 ///
25 /// 针对每个传感器,会调用相应的序列化器对其数据进行序列化和反序列化操作。
26 ///
27 /// 请勿直接使用,请使用传感器注册实例(SensorRegistry实例)。
28 template <typename... Items>
29 class CompositeSerializer : public CompileTimeTypeMap<Items...> {
30 using Super = CompileTimeTypeMap<Items...>;
31
32 public:
33// 定义了一个智能指针类型 interpreted_type,指向 SensorData 对象。
35
36 /// Serialize the arguments provided into a Buffer by calling to the
37 /// serializer registered for the given @a Sensor type.
38 template <typename Sensor, typename... Args>
39 static Buffer Serialize(Sensor &sensor, Args &&... args);
40
41 /// Deserializes a Buffer by calling the "Deserialize" function of the
42 /// serializer that generated the Buffer.
43 static interpreted_type Deserialize(Buffer &&data);
44
45 private:
46// 这个模板函数是反序列化的内部实现,根据索引从 Buffer 中反序列化数据。
47 template <size_t Index, typename Data>
48 static interpreted_type Deserialize_impl(Data &&data) {
49 // 获取对应索引的序列化器类型。
50 using Serializer = typename Super::template get_by_index<Index>::type;
51 // 调用序列化器的反序列化函数进行反序列化。
52 return Serializer::Deserialize(std::forward<Data>(data));
53 }
54// 这个模板函数也是反序列化的内部实现,根据索引序列和数据进行反序列化。
55 template <typename Data, size_t... Is>
56 static interpreted_type Deserialize_impl(size_t i, Data &&data, std::index_sequence<Is...>) {
57 // This function is equivalent to creating a switch statement with a case
58 // for each element in the map, the compiler should be able to optimize it
59 // into a jump table. See https://stackoverflow.com/a/46282159/5308925.
60 interpreted_type result;
61 std::initializer_list<int> ({
62 (i == Is ? (result = Deserialize_impl<Is>(std::forward<Data>(data))), 0 : 0)...
63 });
64 return result;
65 }
66// 这个模板函数是反序列化的入口函数,根据索引和数据调用内部实现进行反序列化。
67 template <typename Data>
68 static interpreted_type Deserialize(size_t index, Data &&data) {
69 return Deserialize_impl(
70 index,
71 std::forward<Data>(data),
72 std::make_index_sequence<Super::size()>());
73 }
74 };
75
76 // ===========================================================================
77 // -- CompositeSerializer implementation -------------------------------------
78 // ===========================================================================
79 // 这个模板函数实现了将传感器的数据序列化到 Buffer 的功能。
80 template <typename... Items>
81 template <typename Sensor, typename... Args>
82 inline Buffer CompositeSerializer<Items...>::Serialize(Sensor &sensor, Args &&... args) {
83 // 去除 Sensor 的 const 修饰,得到实际的传感器类型 TheSensor。
84 using TheSensor = typename std::remove_const<Sensor>::type;
85 // 获取对应传感器类型的序列化器类型 Serializer。
86 using Serializer = typename Super::template get<TheSensor*>::type;
87 // 调用序列化器的序列化函数进行序列化,并返回 Buffer。
88 return Serializer::Serialize(sensor, std::forward<Args>(args)...);
89 }
90// 这个函数实现了反序列化 Buffer 中的数据,返回一个指向 SensorData 的智能指针。
91 template <typename... Items>
92 inline typename CompositeSerializer<Items...>::interpreted_type
94 // 将移动后的 Buffer 包装成 RawData 对象。
95 RawData message{std::move(data)};
96 // 从 RawData 中获取传感器类型标识。
97 size_t index = message.GetSensorTypeId();
98 // 根据传感器类型标识调用反序列化函数进行反序列化。
99 return Deserialize(index, std::move(message));
100 }
101
102} // namespace sensor
103} // namespace carla
一块原始数据。 请注意,如果需要更多容量,则会分配一个新的内存块,并 删除旧的内存块。这意味着默认情况下,缓冲区只能增长。要释放内存,使用 clear 或 pop。
编译时映射表,用于将传感器对象映射到序列化器。
static Buffer Serialize(Sensor &sensor, Args &&... args)
Serialize the arguments provided into a Buffer by calling to the serializer registered for the given ...
SharedPtr< SensorData > interpreted_type
static interpreted_type Deserialize_impl(Data &&data)
static interpreted_type Deserialize(Buffer &&data)
Deserializes a Buffer by calling the "Deserialize" function of the serializer that generated the Buff...
static interpreted_type Deserialize(size_t index, Data &&data)
static interpreted_type Deserialize_impl(size_t i, Data &&data, std::index_sequence< Is... >)
包装一个传感器生成的原始数据以及一些有用的元信息。
Definition RawData.h:20
uint64_t GetSensorTypeId() const
生成数据的传感器的类型ID。
Definition RawData.h:32
CARLA模拟器的主命名空间。
Definition Carla.cpp:139
boost::shared_ptr< T > SharedPtr
使用这个SharedPtr(boost::shared_ptr)以保持与boost::python的兼容性, 但未来如果可能的话,我们希望能为std::shared_ptr制作一个Python适配器。
Definition Memory.h:19
一个编译时结构,用于映射两种类型。可以通过键或索引查找元素。
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get_by_index< Index > get_by_index
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get< InKey > get