Skip to content

Commit d069941

Browse files
committed
feat: implement Uuid class with conditional UUID generation and parsing
1 parent 4d76194 commit d069941

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

include/libecs-cpp/Uuid.hpp

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,9 @@
88
class Uuid
99
{
1010
public:
11-
Uuid()
12-
{
13-
UUIDv4::UUIDGenerator<std::mt19937_64> uuidGenerator;
14-
this->id = uuidGenerator.getUUID();
15-
}
16-
Uuid(std::string id)
17-
{
18-
this->id = UUIDv4::UUID(id);
19-
}
20-
std::string Get()
21-
{
22-
return this->id.str();
23-
}
11+
Uuid();
12+
Uuid(std::string id);
13+
std::string Get();
2414
private:
2515
UUIDv4::UUID id;
2616
};
@@ -32,20 +22,9 @@
3222
class Uuid
3323
{
3424
public:
35-
Uuid()
36-
{
37-
uuid_generate(this->id);
38-
}
39-
Uuid(std::string id)
40-
{
41-
uuid_parse(id.c_str(), this->id);
42-
}
43-
std::string Get()
44-
{
45-
char str_id[37] = {0};
46-
uuid_unparse(this->id, str_id);
47-
return std::string(str_id);
48-
}
25+
Uuid();
26+
Uuid(std::string id);
27+
std::string Get();
4928
private:
5029
uuid_t id;
5130
};

src/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ libecs_cpp_la_CXXFLAGS = -std=c++20 -I../include -Werror -Wall -Wno-psabi ${UUID
2121
libecs_cpp_la_LIBADD = ${UUID_LIBS}
2222
endif
2323

24-
libecs_cpp_la_SOURCES = Manager.cpp Container.cpp System.cpp Component.cpp Entity.cpp
24+
libecs_cpp_la_SOURCES = Manager.cpp Container.cpp System.cpp Component.cpp Entity.cpp Uuid.cpp
2525
libecs_cpp_la_LDFLAGS = -no-undefined -pthread
2626

2727
bin_PROGRAMS = example

src/Uuid.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <libecs-cpp/ecs.hpp>
2+
3+
#ifndef DISABLE_BUILTIN_UUID
4+
ecs::Uuid::Uuid()
5+
{
6+
UUIDv4::UUIDGenerator<std::mt19937_64> uuidGenerator;
7+
this->id = uuidGenerator.getUUID();
8+
}
9+
10+
ecs::Uuid::Uuid(std::string id)
11+
{
12+
this->id = UUIDv4::UUID(id);
13+
}
14+
15+
std::string ecs::Uuid::Get()
16+
{
17+
return this->id.str();
18+
}
19+
#else
20+
ecs::Uuid::Uuid()
21+
{
22+
uuid_generate(this->id);
23+
}
24+
25+
ecs::Uuid::Uuid(std::string id)
26+
{
27+
uuid_parse(id.c_str(), this->id);
28+
}
29+
30+
std::string ecs::Uuid::Get()
31+
{
32+
char str_id[37] = {0};
33+
uuid_unparse(this->id, str_id); // 36 characters + 1 for null terminator
34+
return std::string(str_id);
35+
}
36+
#endif

0 commit comments

Comments
 (0)