Mercurial > hg > gpsynth
diff src/boost_ex.hpp @ 0:add35537fdbb tip
Initial import
author | irh <ian.r.hobson@gmail.com> |
---|---|
date | Thu, 25 Aug 2011 11:05:55 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/boost_ex.hpp Thu Aug 25 11:05:55 2011 +0100 @@ -0,0 +1,73 @@ +// Copyright 2011, Ian Hobson. +// +// This file is part of gpsynth. +// +// gpsynth 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. +// +// gpsynth 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 gpsynth in the file COPYING. +// If not, see http://www.gnu.org/licenses/. + +// Boost helper functions + +#pragma once + +#include "boost/foreach.hpp" +#include "boost/random/mersenne_twister.hpp" +#include "boost/random/normal_distribution.hpp" +#include "boost/random/variate_generator.hpp" +#include "boost/shared_array.hpp" +#include "boost/thread.hpp" + +#include <cstddef> +#include <ctime> + +// foreach macro +#define foreach BOOST_FOREACH +#define foreach_reverse BOOST_REVERSE_FOREACH + +namespace bx { + + // Sleep the current thread for a certain number of milliseconds + inline void SleepMs(int milliseconds) { + boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds)); + } + + // boost.random helpers + typedef boost::random::mt19937 EngineType; + typedef boost::variate_generator<EngineType, boost::normal_distribution<> > + GaussianGenerator; + + class Random { + EngineType engine_; + + public: + Random() : engine_(static_cast<unsigned int>(std::time(0))) {} + EngineType& Engine() { return engine_; } + + static Random& Singleton() { + static Random random; + return random; + } + }; + + static GaussianGenerator MakeGaussianGenerator(double mean = 0.0, + double sigma = 1.0) { + boost::normal_distribution<> distribution(mean, sigma); + return GaussianGenerator(Random::Singleton().Engine(), distribution); + } + + static EngineType& RandomEngine() { + return Random::Singleton().Engine(); + } + + +} // bx namespace