CARLA
Loading...
Searching...
No Matches
test/Buffer.cpp
Go to the documentation of this file.
1// Copyright (c) 2026 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#include <climits>
9#include <random>
10
11namespace util {
12namespace buffer {
13
15 if (size == 0u)
16 return make_empty();
17 // independent_bits_engine requires an unsigned result type wider than
18 // char ([rand.req.genl]/1.6); MSVC enforces this with a static_assert.
19 using random_bytes_engine = std::independent_bits_engine<
20 std::random_device,
21 CHAR_BIT,
22 unsigned short>;
23 random_bytes_engine rbe;
24 auto buffer = make_empty(size);
25 std::generate(buffer->begin(), buffer->end(), [&rbe]() {
26 return static_cast<unsigned char>(rbe());
27 });
28 return buffer;
29 }
30
31 std::string to_hex_string(const Buffer &buf, size_t length) {
32 length = std::min(static_cast<size_t>(buf.size()), length);
33 auto buffer = std::make_unique<char[]>(2u * length + 1u);
34 for (auto i = 0u; i < length; ++i)
35 sprintf(&buffer[2u * i], "%02x", buf.data()[i]);
36 if (length < buf.size())
37 return std::string(buffer.get()) + std::string("...");
38 return std::string(buffer.get());
39 }
40
41} // namespace buffer
42} // namespace util
A piece of raw data.
const value_type * data() const noexcept
Direct access to the allocated memory or nullptr if no memory is allocated.
size_type size() const noexcept
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
std::string to_hex_string(const Buffer &buf, size_t length)
shared_buffer make_random(size_t size)