CARLA
test/Buffer.cpp
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 #include "Buffer.h"
8 
9 #include <boost/random/independent_bits.hpp>
10 
11 #include <climits>
12 #include <random>
13 
14 namespace util {
15 namespace buffer {
16 
17  shared_buffer make_random(size_t size) {
18  if (size == 0u)
19  return make_empty();
20  using random_bytes_engine = boost::random::independent_bits_engine<
21  std::random_device,
22  CHAR_BIT,
23  unsigned char>;
24  random_bytes_engine rbe;
25  auto buffer = make_empty(size);
26  std::generate(buffer->begin(), buffer->end(), std::ref(rbe));
27  return buffer;
28  }
29 
30  std::string to_hex_string(const Buffer &buf, size_t length) {
31  length = std::min(static_cast<size_t>(buf.size()), length);
32  auto buffer = std::make_unique<char[]>(2u * length + 1u);
33  for (auto i = 0u; i < length; ++i)
34  sprintf(&buffer[2u * i], "%02x", buf.data()[i]);
35  if (length < buf.size())
36  return std::string(buffer.get()) + std::string("...");
37  return std::string(buffer.get());
38  }
39 
40 } // namespace buffer
41 } // namespace util
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_empty(size_t size=0u)
Definition: test/Buffer.h:24
std::shared_ptr< Buffer > shared_buffer
Definition: test/Buffer.h:21
double min(double v1, double v2)
Definition: Simplify.h:294
std::string to_hex_string(const Buffer &buf, size_t length)
Definition: test/Buffer.cpp:30
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