Skip to content

Commit 70c0018

Browse files
committed
Replace Boost.UUID with a simple UUID4 generator
1 parent 10efba8 commit 70c0018

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ add_library(lslobj OBJECT
9292
src/util/cast.cpp
9393
src/util/inireader.hpp
9494
src/util/inireader.cpp
95+
src/util/uuid.hpp
9596
thirdparty/loguru/loguru.cpp
9697
$<$<BOOL:${LSL_LEGACY_CPP_ABI}>:src/legacy/legacy_abi.cpp src/legacy/legacy_abi.h>
9798
# headers

src/stream_info_impl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#include "stream_info_impl.h"
22
#include "api_config.h"
33
#include "util/cast.hpp"
4+
#include "util/uuid.hpp"
45
#include <algorithm>
5-
#include <boost/uuid/random_generator.hpp>
6-
#include <boost/uuid/uuid_io.hpp>
76
#include <loguru.hpp>
87
#include <sstream>
98
#include <utility>
@@ -258,7 +257,7 @@ void stream_info_impl::uid(const std::string &v) {
258257

259258
const std::string& stream_info_impl::reset_uid()
260259
{
261-
uid(lslboost::uuids::to_string(lslboost::uuids::random_generator()()));
260+
uid(UUID::random().to_string());
262261
return uid_;
263262
}
264263

src/util/uuid.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma once
2+
3+
#include <random>
4+
#include <string>
5+
6+
struct UUID {
7+
uint8_t data[16]{0};
8+
9+
void set_version(uint8_t version = 4) {
10+
data[6] = (data[6] & 0x0f) | static_cast<uint8_t>(version << 4);
11+
}
12+
13+
void set_variant2() { data[8] = (data[8] & 0x3f) | 0x80; }
14+
15+
/// return the UUID formatted as RFC 4122 UUID
16+
std::string to_string() const {
17+
std::string out(36, '0');
18+
auto pos = out.begin();
19+
const uint8_t *curbyte = data;
20+
21+
/// helper: write `n` hex-formatted bytes as hex to a string, incrementing the positions
22+
const auto copybytesashex = [&pos, &curbyte](int n) {
23+
const char tbl[] = "0123456789abcdef";
24+
for (int i = 0; i < n; ++i) {
25+
*pos++ = tbl[*curbyte / 16];
26+
*pos++ = tbl[*curbyte++ & 0x0f];
27+
}
28+
};
29+
30+
copybytesashex(2);
31+
32+
// 4x "abcd-"
33+
for (int i = 0; i < 4; ++i) {
34+
copybytesashex(2);
35+
*pos++ = '-';
36+
}
37+
copybytesashex(6);
38+
return out;
39+
}
40+
41+
/// generate a random UUID4
42+
static UUID random() {
43+
UUID uuid;
44+
45+
// cast data to an array of the RNGs native type, write random bytes directly to it
46+
std::random_device rng;
47+
using rand_t = std::random_device::result_type;
48+
constexpr int rand_elems = sizeof(UUID::data) / sizeof(rand_t);
49+
rand_t *data_view = reinterpret_cast<rand_t*>(uuid.data);
50+
for (int i = 0; i < rand_elems; ++i) data_view[i] = rng();
51+
52+
uuid.set_version(4);
53+
uuid.set_variant2();
54+
return uuid;
55+
}
56+
};

testing/test_int_streaminfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ bool contains(const T(&valid)[N], const T target) {
1515
TEST_CASE("uid", "[basic][streaminfo]") {
1616
lsl::stream_info_impl info;
1717
const std::string uid = info.reset_uid();
18+
INFO(uid)
1819
REQUIRE(uid.length() == 36);
1920
for(auto i=0u; i<uid.size(); ++i)
2021
if(i==8||i==13||i==18||i==23) REQUIRE(uid[i] == '-');

0 commit comments

Comments
 (0)