diff options
author | Joanna Hulboj <joanna.hulboj@isode.com> | 2017-03-29 12:43:50 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2017-03-31 09:31:21 (GMT) |
commit | 613d66b9847e9e4338a37b22b230d3177872a43c (patch) | |
tree | f8412cb315bb273e6a6d6e3e1a63b803a6aad3a9 | |
parent | 7663ca75731c4313dddbcde4d85f10383644a67a (diff) | |
download | swift-613d66b9847e9e4338a37b22b230d3177872a43c.zip swift-613d66b9847e9e4338a37b22b230d3177872a43c.tar.bz2 |
Use std::random instead of boost::random
Test-Infotmation:
Unit tests pass OK.
Change-Id: I1b6d3711d97daa86cccbb44aecb1b4025f2fcc8a
-rw-r--r-- | Swiften/Base/BoostRandomGenerator.cpp | 27 | ||||
-rw-r--r-- | Swiften/Base/RandomGenerator.cpp | 4 | ||||
-rw-r--r-- | Swiften/Base/SConscript | 2 | ||||
-rw-r--r-- | Swiften/Base/StdRandomGenerator.cpp | 33 | ||||
-rw-r--r-- | Swiften/Base/StdRandomGenerator.h (renamed from Swiften/Base/BoostRandomGenerator.h) | 10 | ||||
-rw-r--r-- | Swiften/Network/PlatformDomainNameServiceQuery.cpp | 4 | ||||
-rw-r--r-- | Swiften/QA/ConcurrentFileTransferTest/ConcurrentFileTransferTest.cpp | 4 | ||||
-rw-r--r-- | Swiften/QA/FileTransferTest/FileTransferTest.cpp | 5 |
8 files changed, 47 insertions, 42 deletions
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 <Swiften/Base/BoostRandomGenerator.h> - -#include <ctime> -#include <numeric> - -#include <boost/random/uniform_int.hpp> -#include <boost/random/variate_generator.hpp> - -namespace Swift { - -BoostRandomGenerator::BoostRandomGenerator() { - // FIXME: Not a good seed - generator.seed(static_cast<unsigned int>(std::time(nullptr))); -} - -int BoostRandomGenerator::generateRandomInteger(int maximum) { - boost::uniform_int<> distribution(0, maximum); - return distribution(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 <Swiften/Base/StdRandomGenerator.h> + +#include <algorithm> +#include <functional> + +namespace { + template<class T = std::mt19937, std::size_t N = T::state_size> + typename std::enable_if<!!N, T>::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/BoostRandomGenerator.h b/Swiften/Base/StdRandomGenerator.h index ff715c3..4cc5e95 100644 --- a/Swiften/Base/BoostRandomGenerator.h +++ b/Swiften/Base/StdRandomGenerator.h @@ -1,25 +1,25 @@ /* - * Copyright (c) 2012-2016 Isode Limited. + * Copyright (c) 2012-2017 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once -#include <boost/random/mersenne_twister.hpp> +#include <random> #include <Swiften/Base/API.h> #include <Swiften/Base/Override.h> #include <Swiften/Base/RandomGenerator.h> namespace Swift { - class SWIFTEN_API BoostRandomGenerator : public RandomGenerator { + class SWIFTEN_API StdRandomGenerator : public RandomGenerator { public: - BoostRandomGenerator(); + StdRandomGenerator(); int generateRandomInteger(int max) SWIFTEN_OVERRIDE; private: - boost::mt19937 generator; + 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 <Swiften/Base/ByteArray.h> #include <Swiften/EventLoop/EventLoop.h> -#include <Swiften/Base/BoostRandomGenerator.h> #include <Swiften/Base/Log.h> +#include <Swiften/Base/StdRandomGenerator.h> #include <Swiften/Network/PlatformDomainNameResolver.h> 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 <boost/filesystem.hpp> #include <boost/numeric/conversion/cast.hpp> -#include <Swiften/Base/BoostRandomGenerator.h> #include <Swiften/Base/Debug.h> #include <Swiften/Base/Log.h> #include <Swiften/Base/sleep.h> +#include <Swiften/Base/StdRandomGenerator.h> #include <Swiften/Client/Client.h> #include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/Disco/ClientDiscoManager.h> @@ -38,7 +38,7 @@ static const std::string CLIENT_NODE = "http://swift.im"; static std::shared_ptr<SimpleEventLoop> eventLoop; static std::shared_ptr<BoostNetworkFactories> 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 <boost/filesystem.hpp> #include <boost/numeric/conversion/cast.hpp> -#include <Swiften/Base/BoostRandomGenerator.h> + #include <Swiften/Base/Debug.h> #include <Swiften/Base/Log.h> #include <Swiften/Base/sleep.h> +#include <Swiften/Base/StdRandomGenerator.h> #include <Swiften/Client/Client.h> #include <Swiften/Client/ClientXMLTracer.h> #include <Swiften/Disco/ClientDiscoManager.h> @@ -37,7 +38,7 @@ static const std::string CLIENT_NODE = "http://swift.im"; static std::shared_ptr<SimpleEventLoop> eventLoop; static std::shared_ptr<BoostNetworkFactories> networkFactories; -BoostRandomGenerator randGen; +StdRandomGenerator randGen; enum Candidate { InBandBytestream = 1, |