From bf1881225edd7f942d0a8620e9c8686e735e56fe Mon Sep 17 00:00:00 2001
From: Sebastian Eibl <sebastian.eibl@fau.de>
Date: Thu, 4 Apr 2019 13:08:51 +0200
Subject: [PATCH] replaced boost::uuid with RandomUUID

---
 src/core/RandomUUID.cpp                 | 74 +++++++++++++++++++++++++
 src/core/RandomUUID.h                   | 61 ++++++++++++++++++++
 src/core/mpi/BufferDataTypeExtensions.h | 23 ++++----
 src/postprocessing/sqlite/SQLite.cpp    |  6 +-
 tests/core/CMakeLists.txt               |  3 +
 tests/core/RandomUUID.cpp               | 67 ++++++++++++++++++++++
 6 files changed, 218 insertions(+), 16 deletions(-)
 create mode 100644 src/core/RandomUUID.cpp
 create mode 100644 src/core/RandomUUID.h
 create mode 100644 tests/core/RandomUUID.cpp

diff --git a/src/core/RandomUUID.cpp b/src/core/RandomUUID.cpp
new file mode 100644
index 000000000..659922e4f
--- /dev/null
+++ b/src/core/RandomUUID.cpp
@@ -0,0 +1,74 @@
+//======================================================================================================================
+//
+//  This file is part of waLBerla. waLBerla is free software: you can
+//  redistribute it and/or modify it under the terms of the GNU General Public
+//  License as published by the Free Software Foundation, either version 3 of
+//  the License, or (at your option) any later version.
+//
+//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
+//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+//  for more details.
+//
+//  You should have received a copy of the GNU General Public License along
+//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
+//
+//! \file RandomUUID.cpp
+//! \author Sebastian Eibl <sebastian.eibl@fau.de>
+//
+//======================================================================================================================
+
+#include "RandomUUID.h"
+
+#include <iomanip>
+#include <random>
+#include <sstream>
+
+namespace walberla {
+
+RandomUUID::RandomUUID()
+{
+   std::random_device rd;
+   std::mt19937 gen( rd() );
+   std::uniform_int_distribution<uint64_t> dis(std::numeric_limits<uint64_t>::min(), std::numeric_limits<uint64_t>::max());
+   a_ = dis(gen);
+   std::random_device rd2;
+   std::mt19937 gen2( rd2() );
+   b_ = dis(gen2);
+}
+
+RandomUUID::RandomUUID( const UIntType a, const UIntType b)
+   : a_(a)
+   , b_(b)
+{}
+
+std::string RandomUUID::toString() const
+{
+   std::stringstream ss;
+   ss << std::hex
+      << std::setfill('0')
+      << std::setw(8) << (a_ >> 32) << "-"
+      << std::setw(4) << ((a_&0xFFFFFFFF)>>16) << "-"
+      << std::setw(4) <<(a_&0xFFFF) << "-"
+      << std::setw(4) << (b_ >> 48) << "-"
+      << std::setw(12) << (b_&0xFFFFFFFFFFFF);
+   return ss.str();
+}
+
+bool operator==(const RandomUUID& lhs, const RandomUUID& rhs)
+{
+   return (lhs.a_ == rhs.a_) && (lhs.b_ == rhs.b_);
+}
+
+bool operator!=(const RandomUUID& lhs, const RandomUUID& rhs)
+{
+   return (lhs.a_ != rhs.a_) || (lhs.b_ != rhs.b_);
+}
+
+std::ostream& operator<<(std::ostream& os, const RandomUUID& uuid)
+{
+   os << uuid.toString();
+   return os;
+}
+
+}
diff --git a/src/core/RandomUUID.h b/src/core/RandomUUID.h
new file mode 100644
index 000000000..45c1c0808
--- /dev/null
+++ b/src/core/RandomUUID.h
@@ -0,0 +1,61 @@
+//======================================================================================================================
+//
+//  This file is part of waLBerla. waLBerla is free software: you can
+//  redistribute it and/or modify it under the terms of the GNU General Public
+//  License as published by the Free Software Foundation, either version 3 of
+//  the License, or (at your option) any later version.
+//
+//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
+//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+//  for more details.
+//
+//  You should have received a copy of the GNU General Public License along
+//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
+//
+//! \file RandomUUID.h
+//! \author Sebastian Eibl <sebastian.eibl@fau.de>
+//
+//======================================================================================================================
+
+#pragma once
+
+#include <ostream>
+#include <string>
+
+namespace walberla {
+
+/**
+ * Replacement for boost::uuids::uuid and boost::uuids::random_generator
+ *
+ * Uses two 64 bit random numbers to create a 128 bit uuid.
+ */
+class RandomUUID
+{
+   friend bool operator==(const RandomUUID& lhs, const RandomUUID& rhs);
+   friend bool operator!=(const RandomUUID& lhs, const RandomUUID& rhs);
+public:
+   using UIntType = uint64_t;
+
+   RandomUUID();
+   RandomUUID(const UIntType a, const UIntType b);
+
+   /**
+    * returns a string representation of the uuid
+    *
+    * format: hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh
+    */
+   std::string toString() const;
+
+   UIntType getFirstUInt() const {return a_;}
+   UIntType getSecondUInt() const {return b_;}
+private:
+   UIntType a_; ///< first part of the uuid
+   UIntType b_; ///< second part of the uuid
+};
+
+bool operator==(const RandomUUID& lhs, const RandomUUID& rhs);
+bool operator!=(const RandomUUID& lhs, const RandomUUID& rhs);
+std::ostream& operator<<(std::ostream& os, const RandomUUID& uuid);
+
+}
diff --git a/src/core/mpi/BufferDataTypeExtensions.h b/src/core/mpi/BufferDataTypeExtensions.h
index 145e1d3aa..955c153ec 100644
--- a/src/core/mpi/BufferDataTypeExtensions.h
+++ b/src/core/mpi/BufferDataTypeExtensions.h
@@ -28,8 +28,7 @@
 #include "core/Conversion.h"
 #include "core/DataTypes.h"
 #include "core/Optional.h"
-
-#include <boost/uuid/uuid.hpp>
+#include "core/RandomUUID.h"
 
 #include <array>
 #include <deque>
@@ -560,32 +559,32 @@ GenericRecvBuffer<T>& operator>>( GenericRecvBuffer<T> & buf, walberla::optional
 }
 
 // ---------------------------------------------------------------------------------------------------------------------
-// --------------------------------------- Boost uuid Support ----------------------------------------------------------
+// --------------------------------------- RandomUUID Support ----------------------------------------------------------
 // ---------------------------------------------------------------------------------------------------------------------
 
 template<>
-struct BufferSizeTrait< boost::uuids::uuid > {
+struct BufferSizeTrait< RandomUUID > {
    static const bool constantSize = true;
    static const uint_t size = 16 + BUFFER_DEBUG_OVERHEAD;
 };
 
-inline SendBuffer & operator<<( SendBuffer & buf, const boost::uuids::uuid & uuid )
+inline SendBuffer & operator<<( SendBuffer & buf, const RandomUUID& uuid )
 {
    buf.addDebugMarker( "uu" );
-   WALBERLA_ASSERT_EQUAL( boost::uuids::uuid::static_size(), 16u );
-   for( auto it = uuid.begin(); it != uuid.end(); ++it )
-      buf << *it;
+   buf << uuid.getFirstUInt();
+   buf << uuid.getSecondUInt();
 
    return buf;
 }
 
 
-inline RecvBuffer & operator>>( RecvBuffer & buf, boost::uuids::uuid & uuid )
+inline RecvBuffer & operator>>( RecvBuffer & buf, RandomUUID& uuid )
 {
    buf.readDebugMarker( "uu" );
-   WALBERLA_ASSERT_EQUAL( boost::uuids::uuid::static_size(), 16u );
-   for( auto it = uuid.begin(); it != uuid.end(); ++it )
-      buf >> *it;
+   RandomUUID::UIntType a;
+   RandomUUID::UIntType b;
+   buf >> a >> b;
+   uuid = RandomUUID(a, b);
 
    return buf;
 }
diff --git a/src/postprocessing/sqlite/SQLite.cpp b/src/postprocessing/sqlite/SQLite.cpp
index 5e27286ef..391f5c9d9 100644
--- a/src/postprocessing/sqlite/SQLite.cpp
+++ b/src/postprocessing/sqlite/SQLite.cpp
@@ -23,9 +23,7 @@
 
 #include "extern/sqlite3.h"
 
-#include <boost/uuid/uuid.hpp>
-#include <boost/uuid/uuid_generators.hpp>
-#include <boost/uuid/uuid_io.hpp>
+#include <core/RandomUUID.h>
 
 #include <sstream>
 
@@ -93,7 +91,7 @@ uint_t storeRunImpl( sqlite3 * dbHandle, std::string & filename,
 
    string insertRunCommand = "INSERT INTO runs (timestamp, uuid ";
    std::stringstream values;
-   auto uuid = boost::uuids::random_generator()();
+   auto uuid = RandomUUID();
    values << " VALUES ( CURRENT_TIMESTAMP, \"" << uuid << "\" ";
    // Add columns for integer properties
    for ( auto i = integerProperties.begin(); i != integerProperties.end(); ++i )
diff --git a/tests/core/CMakeLists.txt b/tests/core/CMakeLists.txt
index bfecc04b9..e0a417390 100644
--- a/tests/core/CMakeLists.txt
+++ b/tests/core/CMakeLists.txt
@@ -189,6 +189,9 @@ waLBerla_execute_test( NAME GridGeneratorTest )
 waLBerla_compile_test( FILES OpenMPWrapperTest.cpp )
 waLBerla_execute_test( NAME OpenMPWrapperTest )
 
+waLBerla_compile_test( FILES RandomUUID.cpp )
+waLBerla_execute_test( NAME RandomUUID )
+
 waLBerla_compile_test( FILES SetTest.cpp )
 waLBerla_execute_test( NAME SetTest )
 
diff --git a/tests/core/RandomUUID.cpp b/tests/core/RandomUUID.cpp
new file mode 100644
index 000000000..a57cd23de
--- /dev/null
+++ b/tests/core/RandomUUID.cpp
@@ -0,0 +1,67 @@
+//======================================================================================================================
+//
+//  This file is part of waLBerla. waLBerla is free software: you can
+//  redistribute it and/or modify it under the terms of the GNU General Public
+//  License as published by the Free Software Foundation, either version 3 of
+//  the License, or (at your option) any later version.
+//
+//  waLBerla is distributed in the hope that it will be useful, but WITHOUT
+//  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+//  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+//  for more details.
+//
+//  You should have received a copy of the GNU General Public License along
+//  with waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
+//
+//! \file RandomUUID.cpp
+//! \author Sebastian Eibl <sebastian.eibl@fau.de>
+//
+//======================================================================================================================
+
+#include <core/debug/TestSubsystem.h>
+#include <core/logging/Logging.h>
+#include <core/mpi/BufferDataTypeExtensions.h>
+#include <core/mpi/Environment.h>
+#include <core/mpi/RecvBuffer.h>
+#include <core/mpi/SendBuffer.h>
+#include <core/RandomUUID.h>
+
+namespace walberla{
+
+int main( int /*argc*/, char** /*argv*/ )
+{
+   debug::enterTestMode();
+
+   RandomUUID uuid1;
+   RandomUUID uuid2;
+
+   WALBERLA_CHECK_EQUAL(uuid1, uuid1);
+   WALBERLA_CHECK_EQUAL(uuid2, uuid2);
+
+   WALBERLA_CHECK_UNEQUAL(uuid1, uuid2);
+   WALBERLA_CHECK_UNEQUAL(uuid2, uuid1);
+
+   WALBERLA_LOG_DEVEL(uuid1);
+
+   WALBERLA_MPI_SECTION()
+   {
+      //pack&unpack test
+      mpi::SendBuffer sb;
+      sb << uuid1 <<uuid2;
+      mpi::RecvBuffer rb(sb);
+      RandomUUID uuid3, uuid4;
+      rb >> uuid3 >> uuid4;
+      WALBERLA_CHECK_EQUAL(uuid1, uuid3);
+      WALBERLA_CHECK_EQUAL(uuid2, uuid4);
+   }
+
+   return EXIT_SUCCESS;
+}
+
+} //namespace walberla
+
+int main( int argc, char** argv )
+{
+   walberla::mpi::Environment env(argc, argv);
+   return walberla::main(argc, argv);
+}
-- 
GitLab