CARLA
BufferView.h
Go to the documentation of this file.
1 // Copyright (c) 2023 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/Exception.h"
12 #include "carla/Logging.h"
13 
14 #include <boost/asio/buffer.hpp>
15 
16 #include <cstdint>
17 #include <limits>
18 #include <memory>
19 #include <type_traits>
20 
21 #ifdef LIBCARLA_INCLUDED_FROM_UE4
23 #include "Containers/Array.h"
25 #endif // LIBCARLA_INCLUDED_FROM_UE4
26 
27 namespace carla {
28 
29  class BufferPool;
30 
31  /// Creating a constant view from an existing buffer
32  class BufferView : public std::enable_shared_from_this<BufferView> {
33 
34  // =========================================================================
35  /// @name Member types
36  // =========================================================================
37  /// @{
38 
39  public:
40 
41  using value_type = unsigned char;
42  using size_type = uint32_t;
43  using const_iterator = const value_type *;
44 
45  /// @}
46  // =========================================================================
47  /// @name Construction and destruction
48  // =========================================================================
49  /// @{
50 
51  public:
52 
53  BufferView() = delete;
54  BufferView(const BufferView &) = delete;
55 
56  static std::shared_ptr<BufferView> CreateFrom(Buffer &&buffer) {
57  return std::shared_ptr<BufferView>(new BufferView(std::move(buffer)));
58  }
59 
60  private:
61 
62  BufferView(Buffer &&rhs) noexcept
63  : _buffer(std::move(rhs)) {}
64 
65  /// @}
66  // =========================================================================
67  /// @name Data access
68  // =========================================================================
69  /// @{
70 
71  public:
72 
73  /// Access the byte at position @a i.
74  const value_type &operator[](size_t i) const {
75  return _buffer.data()[i];
76  }
77 
78  /// Direct access to the allocated memory or nullptr if no memory is
79  /// allocated.
80  const value_type *data() const noexcept {
81  return _buffer.data();
82  }
83 
84  /// Make a boost::asio::buffer from this buffer.
85  ///
86  /// @warning Boost.Asio buffers do not own the data, it's up to the caller
87  /// to not delete the memory that this buffer holds until the asio buffer is
88  /// no longer used.
89  boost::asio::const_buffer cbuffer() const noexcept {
90  return {_buffer.data(), _buffer.size()};
91  }
92 
93  /// @copydoc cbuffer()
94  boost::asio::const_buffer buffer() const noexcept {
95  return cbuffer();
96  }
97 
98  /// @}
99  // =========================================================================
100  /// @name Capacity
101  // =========================================================================
102  /// @{
103 
104  public:
105 
106  bool empty() const noexcept {
107  return _buffer.size() == 0u;
108  }
109 
110  size_type size() const noexcept {
111  return _buffer.size();
112  }
113 
114  static constexpr size_type max_size() noexcept {
115  return (std::numeric_limits<size_type>::max)();
116  }
117 
118  size_type capacity() const noexcept {
119  return _buffer.capacity();
120  }
121 
122  /// @}
123  // =========================================================================
124  /// @name Iterators
125  // =========================================================================
126  /// @{
127 
128  public:
129 
130  const_iterator cbegin() const noexcept {
131  return _buffer.data();
132  }
133 
134  const_iterator begin() const noexcept {
135  return _buffer.cbegin();
136  }
137 
138  const_iterator cend() const noexcept {
139  return _buffer.cbegin() + _buffer.size();
140  }
141 
142  const_iterator end() const noexcept {
143  return _buffer.cend();
144  }
145 
146  private:
147 
149  };
150 
151  using SharedBufferView = std::shared_ptr<BufferView>;
152 
153 } // namespace carla
const value_type * data() const noexcept
Direct access to the allocated memory or nullptr if no memory is allocated.
Definition: carla/Buffer.h:156
const value_type & operator[](size_t i) const
Access the byte at position i.
Definition: BufferView.h:74
uint32_t size_type
Definition: BufferView.h:42
const Buffer _buffer
Definition: BufferView.h:148
Creating a constant view from an existing buffer.
Definition: BufferView.h:32
const_iterator begin() const noexcept
Definition: BufferView.h:134
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
const value_type * data() const noexcept
Direct access to the allocated memory or nullptr if no memory is allocated.
Definition: BufferView.h:80
static constexpr size_type max_size() noexcept
Definition: BufferView.h:114
boost::asio::const_buffer cbuffer() const noexcept
Make a boost::asio::buffer from this buffer.
Definition: BufferView.h:89
static std::shared_ptr< BufferView > CreateFrom(Buffer &&buffer)
Definition: BufferView.h:56
size_type size() const noexcept
Definition: BufferView.h:110
const_iterator cend() const noexcept
Definition: BufferView.h:138
const_iterator cend() const noexcept
Definition: carla/Buffer.h:229
const_iterator cbegin() const noexcept
Definition: BufferView.h:130
size_type capacity() const noexcept
Definition: carla/Buffer.h:205
const_iterator cbegin() const noexcept
Definition: carla/Buffer.h:217
boost::asio::const_buffer buffer() const noexcept
Make a boost::asio::buffer from this buffer.
Definition: BufferView.h:94
const_iterator end() const noexcept
Definition: BufferView.h:142
A piece of raw data.
Definition: carla/Buffer.h:42
bool empty() const noexcept
Definition: BufferView.h:106
std::shared_ptr< BufferView > SharedBufferView
Definition: BufferView.h:151
unsigned char value_type
Definition: BufferView.h:41
BufferView(Buffer &&rhs) noexcept
Definition: BufferView.h:62
size_type size() const noexcept
Definition: carla/Buffer.h:197
const value_type * const_iterator
Definition: BufferView.h:43
size_type capacity() const noexcept
Definition: BufferView.h:118