CARLA
test/Buffer.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 
11 #include <algorithm>
12 #include <memory>
13 #include <ostream>
14 #include <string>
15 
16 namespace util {
17 namespace buffer {
18 
19  using carla::Buffer;
20 
21  using shared_buffer = std::shared_ptr<Buffer>;
22  using const_shared_buffer = std::shared_ptr<const Buffer>;
23 
24  static inline shared_buffer make_empty(size_t size = 0u) {
25  return size == 0u ?
26  std::make_shared<Buffer>() :
27  std::make_shared<Buffer>(size);
28  }
29 
30  shared_buffer make_random(size_t size);
31 
32  template <typename T>
33  static inline shared_buffer make(const T &buffer) {
34  return std::make_shared<Buffer>(boost::asio::buffer(buffer));
35  }
36 
37  static inline std::string as_string(const Buffer &buf) {
38  return {reinterpret_cast<const char *>(buf.data()), buf.size()};
39  }
40 
41  std::string to_hex_string(const Buffer &buf, size_t length = 16u);
42 
43 } // namespace buffer
44 } // namespace util
45 
46 namespace carla {
47 
48  static inline std::ostream &operator<<(std::ostream &out, const Buffer &buf) {
49  out << "[" << buf.size() << " bytes] " << util::buffer::to_hex_string(buf);
50  return out;
51  }
52 
53  static inline bool operator==(const Buffer &lhs, const Buffer &rhs) {
54  return
55  (lhs.size() == rhs.size()) &&
56  std::equal(lhs.begin(), lhs.end(), rhs.begin());
57  }
58 
59  static inline bool operator!=(const Buffer &lhs, const Buffer &rhs) {
60  return !(lhs == rhs);
61  }
62 
63 } // 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
static shared_buffer make(const T &buffer)
Definition: test/Buffer.h:33
static shared_buffer make_empty(size_t size=0u)
Definition: test/Buffer.h:24
static std::string as_string(const Buffer &buf)
Definition: test/Buffer.h:37
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
static bool operator==(const Buffer &lhs, const Buffer &rhs)
Definition: test/Buffer.h:53
std::shared_ptr< Buffer > shared_buffer
Definition: test/Buffer.h:21
static bool operator!=(const Buffer &lhs, const Buffer &rhs)
Definition: test/Buffer.h:59
const_iterator end() const noexcept
Definition: carla/Buffer.h:233
const_iterator begin() const noexcept
Definition: carla/Buffer.h:221
std::shared_ptr< const Buffer > const_shared_buffer
Definition: test/Buffer.h:22
std::string to_hex_string(const Buffer &buf, size_t length)
Definition: test/Buffer.cpp:30
static std::ostream & operator<<(std::ostream &out, const Buffer &buf)
Definition: test/Buffer.h:48
A piece of raw data.
Definition: carla/Buffer.h:42
shared_buffer make_random(size_t size)
Definition: test/Buffer.cpp:17
size_type size() const noexcept
Definition: carla/Buffer.h:197