CARLA
CompositeSerializer.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 "carla/Buffer.h"
10 #include "carla/Memory.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 
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
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
static interpreted_type Deserialize_impl(Data &&data)
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get< InKey > get
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
A compile time structure for mapping two types.
Compile-time map for mapping sensor objects to serializers.
static interpreted_type Deserialize(Buffer &&data)
Deserializes a Buffer by calling the "Deserialize" function of the serializer that generated the Buff...
static Buffer Serialize(Sensor &sensor, Args &&... args)
Serialize the arguments provided into a Buffer by calling to the serializer registered for the given ...
uint64_t GetSensorTypeId() const
Type-id of the sensor that generated the data.
Definition: RawData.h:32
typename detail::CompileTimeTypeMapImpl< sizeof...(Items), Items... >::template get_by_index< Index > get_by_index
A piece of raw data.
Definition: carla/Buffer.h:42
SharedPtr< SensorData > interpreted_type
static interpreted_type Deserialize(size_t index, Data &&data)
static interpreted_type Deserialize_impl(size_t i, Data &&data, std::index_sequence< Is... >)
Wrapper around the raw data generated by a sensor plus some useful meta-information.
Definition: RawData.h:21