CARLA
detail/Stream.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/Debug.h"
11 #include "carla/streaming/Token.h"
12 
13 #include <memory>
14 
15 namespace carla {
16 namespace streaming {
17 namespace detail {
18 
19  class Dispatcher;
20 
21  template <typename StreamStateT>
22  class Stream {
23  public:
24 
25  Stream() = delete;
26 
27  Stream(const Stream &) = default;
28  Stream(Stream &&) = default;
29 
30  Stream &operator=(const Stream &) = default;
31  Stream &operator=(Stream &&) = default;
32 
33  /// Token associated with this stream. This token can be used by a client to
34  /// subscribe to this stream.
35  Token token() const {
36  return _shared_state->token();
37  }
38 
39  /// Pull a buffer from the buffer pool associated to this stream. Discarded
40  /// buffers are re-used to avoid memory allocations.
41  ///
42  /// @note Re-using buffers is optimized for the use case in which all the
43  /// messages sent through the stream are big and have (approximately) the
44  /// same size.
46  return _shared_state->MakeBuffer();
47  }
48 
49  /// Flush @a buffers down the stream. No copies are made.
50  template <typename... Buffers>
51  void Write(Buffers &&... buffers) {
52  _shared_state->Write(std::move(buffers)...);
53  }
54 
55  /// Make a copy of @a data and flush it down the stream.
56  template <typename T>
57  Stream &operator<<(const T &data) {
58  auto buffer = MakeBuffer();
59  buffer.copy_from(data);
60  Write(std::move(buffer));
61  return *this;
62  }
63 
64  private:
65 
66  friend class detail::Dispatcher;
67 
68  Stream(std::shared_ptr<StreamStateT> state)
69  : _shared_state(std::move(state)) {
70  DEBUG_ASSERT(_shared_state != nullptr);
71  }
72 
73  std::shared_ptr<StreamStateT> _shared_state;
74  };
75 
76 } // namespace detail
77 } // namespace streaming
78 } // namespace carla
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
Stream(std::shared_ptr< StreamStateT > state)
Definition: detail/Stream.h:68
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
Stream & operator<<(const T &data)
Make a copy of data and flush it down the stream.
Definition: detail/Stream.h:57
A token that uniquely identify a stream.
Definition: Token.h:17
Token token() const
Token associated with this stream.
Definition: detail/Stream.h:35
Keeps the mapping between streams and sessions.
Definition: Dispatcher.h:25
Buffer MakeBuffer()
Pull a buffer from the buffer pool associated to this stream.
Definition: detail/Stream.h:45
A piece of raw data.
Definition: carla/Buffer.h:39
Stream & operator=(const Stream &)=default
std::shared_ptr< StreamStateT > _shared_state
Definition: detail/Stream.h:73
void Write(Buffers &&... buffers)
Flush buffers down the stream. No copies are made.
Definition: detail/Stream.h:51