CARLA
 
载入中...
搜索中...
未找到
detail/Stream.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/Debug.h"
12
13#include <memory>
14
15namespace carla {
16namespace streaming {
17namespace 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 auto state = _shared_state;
47 return state->MakeBuffer();
48 }
49
50 /// Flush @a buffers down the stream. No copies are made.
51 template <typename... Buffers>
52 void Write(Buffers &&... buffers) {
53 _shared_state->Write(std::move(buffers)...);
54 }
55
56 /// Make a copy of @a data and flush it down the stream.
57 template <typename T>
58 Stream &operator<<(const T &data) {
59 auto buffer = MakeBuffer();
60 buffer.copy_from(data);
61 Write(std::move(buffer));
62 return *this;
63 }
64
66 {
67 return _shared_state ? _shared_state->AreClientsListening() : false;
68 }
69
70 private:
71
72 friend class detail::Dispatcher;
73
74 Stream(std::shared_ptr<StreamStateT> state)
75 : _shared_state(std::move(state)) {
76 DEBUG_ASSERT(_shared_state != nullptr);
77 }
78
79 std::shared_ptr<StreamStateT> _shared_state;
80 };
81
82} // namespace detail
83} // namespace streaming
84} // namespace carla
#define DEBUG_ASSERT(predicate)
Definition Debug.h:66
A piece of raw data.
A token that uniquely identify a stream.
Definition Token.h:17
Keeps the mapping between streams and sessions.
Definition Dispatcher.h:27
Stream & operator<<(const T &data)
Make a copy of data and flush it down the stream.
Token token() const
Token associated with this stream.
Stream & operator=(const Stream &)=default
Stream(std::shared_ptr< StreamStateT > state)
std::shared_ptr< StreamStateT > _shared_state
void Write(Buffers &&... buffers)
Flush buffers down the stream. No copies are made.
Stream & operator=(Stream &&)=default
Buffer MakeBuffer()
Pull a buffer from the buffer pool associated to this stream.
Stream(const Stream &)=default
This file contains definitions of common data structures used in traffic manager.
Definition Carla.cpp:133