From 613d66b9847e9e4338a37b22b230d3177872a43c Mon Sep 17 00:00:00 2001 From: Joanna Hulboj Date: Wed, 29 Mar 2017 13:43:50 +0100 Subject: Use std::random instead of boost::random Test-Infotmation: Unit tests pass OK. Change-Id: I1b6d3711d97daa86cccbb44aecb1b4025f2fcc8a diff --git a/Swiften/Base/BoostRandomGenerator.cpp b/Swiften/Base/BoostRandomGenerator.cpp deleted file mode 100644 index d893d4d..0000000 --- a/Swiften/Base/BoostRandomGenerator.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2012-2016 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#include - -#include -#include - -#include -#include - -namespace Swift { - -BoostRandomGenerator::BoostRandomGenerator() { - // FIXME: Not a good seed - generator.seed(static_cast(std::time(nullptr))); -} - -int BoostRandomGenerator::generateRandomInteger(int maximum) { - boost::uniform_int<> distribution(0, maximum); - return distribution(generator); -} - -} diff --git a/Swiften/Base/BoostRandomGenerator.h b/Swiften/Base/BoostRandomGenerator.h deleted file mode 100644 index ff715c3..0000000 --- a/Swiften/Base/BoostRandomGenerator.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (c) 2012-2016 Isode Limited. - * All rights reserved. - * See the COPYING file for more information. - */ - -#pragma once - -#include - -#include -#include -#include - -namespace Swift { - class SWIFTEN_API BoostRandomGenerator : public RandomGenerator { - public: - BoostRandomGenerator(); - - int generateRandomInteger(int max) SWIFTEN_OVERRIDE; - - private: - boost::mt19937 generator; - }; -} diff --git a/Swiften/Base/RandomGenerator.cpp b/Swiften/Base/RandomGenerator.cpp index f0d8804..e43c01a 100644 --- a/Swiften/Base/RandomGenerator.cpp +++ b/Swiften/Base/RandomGenerator.cpp @@ -8,8 +8,6 @@ namespace Swift { -RandomGenerator::~RandomGenerator() { - -} +RandomGenerator::~RandomGenerator() = default; } diff --git a/Swiften/Base/SConscript b/Swiften/Base/SConscript index de502c2..1f48f19 100644 --- a/Swiften/Base/SConscript +++ b/Swiften/Base/SConscript @@ -1,7 +1,6 @@ Import("swiften_env") objects = swiften_env.SwiftenObject([ - "BoostRandomGenerator.cpp", "ByteArray.cpp", "DateTime.cpp", "Error.cpp", @@ -16,6 +15,7 @@ objects = swiften_env.SwiftenObject([ "SafeAllocator.cpp", "SafeByteArray.cpp", "SimpleIDGenerator.cpp", + "StdRandomGenerator.cpp", "String.cpp", "URL.cpp", "sleep.cpp", diff --git a/Swiften/Base/StdRandomGenerator.cpp b/Swiften/Base/StdRandomGenerator.cpp new file mode 100644 index 0000000..8b1dee0 --- /dev/null +++ b/Swiften/Base/StdRandomGenerator.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2012-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#include + +#include +#include + +namespace { + template + typename std::enable_if::type createSeededRandomEngine() { + typename T::result_type random_data[N]; + std::random_device source; + std::generate(std::begin(random_data), std::end(random_data), std::ref(source)); + std::seed_seq seeds(std::begin(random_data), std::end(random_data)); + return T(seeds); + } +} + +namespace Swift { + +StdRandomGenerator::StdRandomGenerator() : generator(createSeededRandomEngine()) { +} + +int StdRandomGenerator::generateRandomInteger(int maximum) { + std::uniform_int_distribution<> distribution(0, maximum); + return distribution(generator); +} + +} diff --git a/Swiften/Base/StdRandomGenerator.h b/Swiften/Base/StdRandomGenerator.h new file mode 100644 index 0000000..4cc5e95 --- /dev/null +++ b/Swiften/Base/StdRandomGenerator.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2012-2017 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. + */ + +#pragma once + +#include + +#include +#include +#include + +namespace Swift { + class SWIFTEN_API StdRandomGenerator : public RandomGenerator { + public: + StdRandomGenerator(); + + int generateRandomInteger(int max) SWIFTEN_OVERRIDE; + + private: + std::mt19937 generator; + }; +} diff --git a/Swiften/Network/PlatformDomainNameServiceQuery.cpp b/Swiften/Network/PlatformDomainNameServiceQuery.cpp index 5cffcdb..a5d180b 100644 --- a/Swiften/Network/PlatformDomainNameServiceQuery.cpp +++ b/Swiften/Network/PlatformDomainNameServiceQuery.cpp @@ -29,8 +29,8 @@ #include #include -#include #include +#include #include using namespace Swift; @@ -172,7 +172,7 @@ void PlatformDomainNameServiceQuery::runBlocking() { } #endif - BoostRandomGenerator generator; + StdRandomGenerator generator; DomainNameServiceQuery::sortResults(records, generator); //std::cout << "Sending out " << records.size() << " SRV results " << std::endl; eventLoop->postEvent(boost::bind(boost::ref(onResult), records), shared_from_this()); diff --git a/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp b/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp index 664a87b..22ee9d7 100644 --- a/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp +++ b/Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp @@ -11,10 +11,10 @@ #include #include -#include #include #include #include +#include #include #include #include @@ -38,7 +38,7 @@ static const std::string CLIENT_NODE = "http://swift.im"; static std::shared_ptr eventLoop; static std::shared_ptr networkFactories; -BoostRandomGenerator randGen; +StdRandomGenerator randGen; enum Candidate { InBandBytestream = 1, diff --git a/Swiften/QA/FileTransferTest/FileTransferTest.cpp b/Swiften/QA/FileTransferTest/FileTransferTest.cpp index b627b30..ebdb36a 100644 --- a/Swiften/QA/FileTransferTest/FileTransferTest.cpp +++ b/Swiften/QA/FileTransferTest/FileTransferTest.cpp @@ -10,10 +10,11 @@ #include #include -#include + #include #include #include +#include #include #include #include @@ -37,7 +38,7 @@ static const std::string CLIENT_NODE = "http://swift.im"; static std::shared_ptr eventLoop; static std::shared_ptr networkFactories; -BoostRandomGenerator randGen; +StdRandomGenerator randGen; enum Candidate { InBandBytestream = 1, -- cgit v0.10.2-6-g49f6