annotate 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
rev   line source
ian@0 1 // Copyright 2011, Ian Hobson.
ian@0 2 //
ian@0 3 // This file is part of gpsynth.
ian@0 4 //
ian@0 5 // gpsynth is free software: you can redistribute it and/or modify
ian@0 6 // it under the terms of the GNU General Public License as published by
ian@0 7 // the Free Software Foundation, either version 3 of the License, or
ian@0 8 // (at your option) any later version.
ian@0 9 //
ian@0 10 // gpsynth is distributed in the hope that it will be useful,
ian@0 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
ian@0 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ian@0 13 // GNU General Public License for more details.
ian@0 14 //
ian@0 15 // You should have received a copy of the GNU General Public License
ian@0 16 // along with gpsynth in the file COPYING.
ian@0 17 // If not, see http://www.gnu.org/licenses/.
ian@0 18
ian@0 19 // Boost helper functions
ian@0 20
ian@0 21 #pragma once
ian@0 22
ian@0 23 #include "boost/foreach.hpp"
ian@0 24 #include "boost/random/mersenne_twister.hpp"
ian@0 25 #include "boost/random/normal_distribution.hpp"
ian@0 26 #include "boost/random/variate_generator.hpp"
ian@0 27 #include "boost/shared_array.hpp"
ian@0 28 #include "boost/thread.hpp"
ian@0 29
ian@0 30 #include <cstddef>
ian@0 31 #include <ctime>
ian@0 32
ian@0 33 // foreach macro
ian@0 34 #define foreach BOOST_FOREACH
ian@0 35 #define foreach_reverse BOOST_REVERSE_FOREACH
ian@0 36
ian@0 37 namespace bx {
ian@0 38
ian@0 39 // Sleep the current thread for a certain number of milliseconds
ian@0 40 inline void SleepMs(int milliseconds) {
ian@0 41 boost::this_thread::sleep(boost::posix_time::milliseconds(milliseconds));
ian@0 42 }
ian@0 43
ian@0 44 // boost.random helpers
ian@0 45 typedef boost::random::mt19937 EngineType;
ian@0 46 typedef boost::variate_generator<EngineType, boost::normal_distribution<> >
ian@0 47 GaussianGenerator;
ian@0 48
ian@0 49 class Random {
ian@0 50 EngineType engine_;
ian@0 51
ian@0 52 public:
ian@0 53 Random() : engine_(static_cast<unsigned int>(std::time(0))) {}
ian@0 54 EngineType& Engine() { return engine_; }
ian@0 55
ian@0 56 static Random& Singleton() {
ian@0 57 static Random random;
ian@0 58 return random;
ian@0 59 }
ian@0 60 };
ian@0 61
ian@0 62 static GaussianGenerator MakeGaussianGenerator(double mean = 0.0,
ian@0 63 double sigma = 1.0) {
ian@0 64 boost::normal_distribution<> distribution(mean, sigma);
ian@0 65 return GaussianGenerator(Random::Singleton().Engine(), distribution);
ian@0 66 }
ian@0 67
ian@0 68 static EngineType& RandomEngine() {
ian@0 69 return Random::Singleton().Engine();
ian@0 70 }
ian@0 71
ian@0 72
ian@0 73 } // bx namespace