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 /Swiften/Base | |
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
Diffstat (limited to 'Swiften/Base')
-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 |
5 files changed, 40 insertions, 36 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; }; } |