From 60702179b576a1c94182873140f8d69eaa8a11df Mon Sep 17 00:00:00 2001
From: Mathias Paulin <Mathias.Paulin@irit.fr>
Date: Tue, 22 Feb 2022 09:37:39 +0100
Subject: [PATCH] [librender] use custom uuid generator

---
 src/libRender/CMakeLists.txt               |  3 ++-
 src/libRender/RadiumNBR/NodeGraph/Node.cpp | 19 ++++++++++++++++++-
 src/libRender/RadiumNBR/NodeGraph/Node.hpp |  5 +++++
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/libRender/CMakeLists.txt b/src/libRender/CMakeLists.txt
index cf02a5c..d43a3c5 100644
--- a/src/libRender/CMakeLists.txt
+++ b/src/libRender/CMakeLists.txt
@@ -183,7 +183,7 @@ set(resources
 # std::uuid dependency
 # Note, follow the progress of integration of uuid generator in the C++ std lib. When supported by std, remove this dependency
 set(UUID_BUILD_TESTS OFF)
-set(UUID_SYSTEM_GENERATOR ON)
+set(UUID_SYSTEM_GENERATOR OFF)
 set(UUID_USING_CXX20_SPAN OFF) # change this when going to C++20 as language standard for Radium libs
 add_subdirectory(Dependencies/stduuid)
 add_library(std::uuid ALIAS stduuid)
@@ -356,6 +356,7 @@ target_link_libraries(
     PUBLIC
     Radium::Core
     NodeEditor::nodes
+    std::uuid
     ${Qt_LIBRARIES}
     ${libName} # make this PRIVATE ?
     )
diff --git a/src/libRender/RadiumNBR/NodeGraph/Node.cpp b/src/libRender/RadiumNBR/NodeGraph/Node.cpp
index da71325..5907479 100644
--- a/src/libRender/RadiumNBR/NodeGraph/Node.cpp
+++ b/src/libRender/RadiumNBR/NodeGraph/Node.cpp
@@ -3,15 +3,32 @@
 #include <Core/Utils/Log.hpp>
 using namespace Ra::Core::Utils;
 
+bool Node::s_uuidGeneratorInitialized {false};
+uuids::uuid_random_generator* Node::s_uidGenerator{nullptr};
+void Node::createUuidGenerator() {
+    std::random_device rd;
+    auto seed_data = std::array<int, std::mt19937::state_size> {};
+    std::generate(std::begin(seed_data), std::end(seed_data), std::ref(rd));
+    std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
+    std::mt19937 generator(seq);
+    s_uidGenerator = new uuids::uuid_random_generator(generator);
+    s_uuidGeneratorInitialized = true;
+}
+
 /// Generates the uuid of the node
 void Node::generateUuid() {
-    m_uuid = uuids::uuid_system_generator{}();
+    if (!s_uuidGeneratorInitialized) {
+        createUuidGenerator();
+    }
+    m_uuid = (*s_uidGenerator)();
 }
 /// Gets the UUID of the node as a string
 std::string Node::getUuid() const {
     if ( m_uuid.is_nil() ) {
         // generates the uuid (need to remove const attribute ...
         const_cast<Node*>(this)->generateUuid();
+        std::string guuid = "{" + uuids::to_string(m_uuid) + "}";
+        std::cout << "###*** UUId generated for node" << getName() << " : " << guuid << std::endl;
     }
     std::string struuid = "{" + uuids::to_string(m_uuid) + "}";
     return struuid;
diff --git a/src/libRender/RadiumNBR/NodeGraph/Node.hpp b/src/libRender/RadiumNBR/NodeGraph/Node.hpp
index fd71316..2542d15 100644
--- a/src/libRender/RadiumNBR/NodeGraph/Node.hpp
+++ b/src/libRender/RadiumNBR/NodeGraph/Node.hpp
@@ -231,4 +231,9 @@ class NodeBasedRenderer_LIBRARY_API Node
 
     /// Additional data on the node, added by application or gui or ...
     nlohmann::json m_extraJsonData;
+
+    /// generator for uuid
+    static bool s_uuidGeneratorInitialized;
+    static uuids::uuid_random_generator* s_uidGenerator;
+    static void createUuidGenerator();
 };
-- 
GitLab