Commit bf188122 authored by Sebastian Eibl's avatar Sebastian Eibl
Browse files

replaced boost::uuid with RandomUUID

parent f06a59c9
//======================================================================================================================
//
// 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;
}
}
//======================================================================================================================
//
// 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);
}
......@@ -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;
}
......
......@@ -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 )
......
......@@ -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 )
......
//======================================================================================================================
//
// 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);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment