CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
sensor
CompositeSerializer.h
Go to the documentation of this file.
1
// Copyright (c) 2026 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
"
11
#include "
carla/sensor/CompileTimeTypeMap.h
"
12
#include "
carla/sensor/RawData.h
"
13
14
namespace
carla
{
15
namespace
sensor
{
16
17
class
SensorData
;
18
19
// ===========================================================================
20
// -- CompositeSerializer ----------------------------------------------------
21
// ===========================================================================
22
23
/// Compile-time map for mapping sensor objects to serializers. The
24
/// appropriate serializer is called for each sensor to serialize and
25
/// deserialize its data.
26
///
27
/// Do not use directly, use the SensorRegistry instantiation.
28
template
<
typename
... Items>
29
class
CompositeSerializer
:
public
CompileTimeTypeMap
<Items...> {
30
using
Super
=
CompileTimeTypeMap
<Items...>;
31
32
public
:
33
34
using
interpreted_type
=
SharedPtr<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
47
template
<
size_t
Index,
typename
Data>
48
static
interpreted_type
Deserialize_impl
(Data &&
data
) {
49
using
Serializer =
typename
Super::template
get_by_index<Index>::type
;
50
return
Serializer::Deserialize(std::forward<Data>(
data
));
51
}
52
53
template
<
typename
Data,
size_t
... Is>
54
static
interpreted_type
Deserialize_impl
(
size_t
i, Data &&
data
, std::index_sequence<Is...>) {
55
// This function is equivalent to creating a switch statement with a case
56
// for each element in the map, the compiler should be able to optimize it
57
// into a jump table. See https://stackoverflow.com/a/46282159/5308925.
58
interpreted_type
result;
59
std::initializer_list<int> ({
60
(i == Is ? (result =
Deserialize_impl<Is>
(std::forward<Data>(
data
))), 0 : 0)...
61
});
62
return
result;
63
}
64
65
template
<
typename
Data>
66
static
interpreted_type
Deserialize
(
size_t
index, Data &&
data
) {
67
return
Deserialize_impl
(
68
index,
69
std::forward<Data>(
data
),
70
std::make_index_sequence<
Super::size
()>());
71
}
72
};
73
74
// ===========================================================================
75
// -- CompositeSerializer implementation -------------------------------------
76
// ===========================================================================
77
78
template
<
typename
... Items>
79
template
<
typename
Sensor,
typename
... Args>
80
inline
Buffer
CompositeSerializer<Items...>::Serialize
(Sensor &
sensor
, Args &&... args) {
81
using
TheSensor =
typename
std::remove_const<Sensor>::type;
82
using
Serializer =
typename
Super::template
get<TheSensor*>::type
;
83
return
Serializer::Serialize(
sensor
, std::forward<Args>(args)...);
84
}
85
86
template
<
typename
... Items>
87
inline
typename
CompositeSerializer
<Items...>::interpreted_type
88
CompositeSerializer<Items...>::Deserialize
(
Buffer
&&
data
) {
89
RawData
message{std::move(
data
)};
90
size_t
index = message.
GetSensorTypeId
();
91
return
Deserialize
(index, std::move(message));
92
}
93
94
}
// namespace sensor
95
}
// namespace carla
CompileTimeTypeMap.h
Memory.h
RawData.h
Buffer.h
carla::Buffer
A piece of raw data.
Definition
carla/Buffer.h:42
carla::sensor::CompositeSerializer
Compile-time map for mapping sensor objects to serializers.
Definition
CompositeSerializer.h:29
carla::sensor::CompositeSerializer::Super
CompileTimeTypeMap< Items... > Super
Definition
CompositeSerializer.h:30
carla::sensor::CompositeSerializer::Serialize
static Buffer Serialize(Sensor &sensor, Args &&... args)
Serialize the arguments provided into a Buffer by calling to the serializer registered for the given ...
Definition
CompositeSerializer.h:80
carla::sensor::CompositeSerializer::interpreted_type
SharedPtr< SensorData > interpreted_type
Definition
CompositeSerializer.h:34
carla::sensor::CompositeSerializer::Deserialize_impl
static interpreted_type Deserialize_impl(Data &&data)
Definition
CompositeSerializer.h:48
carla::sensor::CompositeSerializer::Deserialize
static interpreted_type Deserialize(Buffer &&data)
Deserializes a Buffer by calling the "Deserialize" function of the serializer that generated the Buff...
Definition
CompositeSerializer.h:88
carla::sensor::CompositeSerializer::Deserialize
static interpreted_type Deserialize(size_t index, Data &&data)
Definition
CompositeSerializer.h:66
carla::sensor::CompositeSerializer::Deserialize_impl
static interpreted_type Deserialize_impl(size_t i, Data &&data, std::index_sequence< Is... >)
Definition
CompositeSerializer.h:54
carla::sensor::RawData
Wrapper around the raw data generated by a sensor plus some useful meta-information.
Definition
RawData.h:21
carla::sensor::RawData::GetSensorTypeId
uint64_t GetSensorTypeId() const
Type-id of the sensor that generated the data.
Definition
RawData.h:32
carla::sensor::SensorData
Base class for all the objects containing data generated by a sensor.
Definition
SensorData.h:22
carla::sensor::data
Definition
ROS2.h:33
carla::sensor
Definition
client/detail/Client.h:58
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
carla::SharedPtr
std::shared_ptr< T > SharedPtr
Definition
Memory.h:15
carla::sensor::CompileTimeTypeMap
A compile time structure for mapping two types.
Definition
CompileTimeTypeMap.h:90
carla::sensor::CompileTimeTypeMap< Items... >::get_by_index
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get_by_index< Index > get_by_index
Definition
CompileTimeTypeMap.h:100
carla::sensor::CompileTimeTypeMap< Items... >::size
static constexpr size_t size()
Definition
CompileTimeTypeMap.h:92
carla::sensor::CompileTimeTypeMap< Items... >::get
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get< InKey > get
Definition
CompileTimeTypeMap.h:97
Generated by
1.18.0