|
Boost Users : |
Subject: Re: [Boost-users] [random] Singleton engine
From: Robert Ramey (ramey_at_[hidden])
Date: 2009-07-29 02:03:43
I'm not getting the "parameter" part. Where is such a parameter supposed to
come from?
If include in the program
Singleton<b::mt19937>::Instance(1)
and somewhere else in the program or in a different execution module
Singleton<b::mt19937>::Instance(2)
What is supposed to happen? Two singletons? If not which one?
Robert Ramey
Diederick C. Niehorster wrote:
> Hi All,
>
> My code has now become what is pasted below. It does the trick and
> allows for singletons with a parameterized constructor.
>
> This leaves the question of whether a singleton library would be a
> useful addition to boost. I would be willing to code and write, though
> this would be a learning process for me.
>
> Best,
> Diederick
> -----
>
> singleton class:
> ---
> #pragma once
>
> // Meyers singleton with support for constructors with 1 parameter
>
> //
> http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-I/
> //
> http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton-Pattern-Part-2/
> //
> http://www.devarticles.com/c/a/Cplusplus/The-Singleton-Pattern-Revisited/
> // http://www.cplusplus.com/forum/beginner/1459/
>
> template <class T>
> class Singleton
> {
> public:
> static T& Instance() {
> static T _instance;
> return _instance;
> }
> template<class P>
> static T& Instance(const P & p) {
> static T _instance(p);
> return _instance;
> }
> private:
> Singleton(); // ctor hidden
> ~Singleton(); // dtor hidden
> Singleton(Singleton const&); // copy ctor hidden
> Singleton& operator=(Singleton const&); // assign op hidden
> };
> ----
> Random number class with default engine:
> ----
> #pragma once
>
> // includes
> #include <boost/random.hpp>
> #include <time.h>
> #include <iostream>
> #include "Singleton.h"
>
> // namespaces
> namespace s = std;
> namespace b = boost;
>
> // declaration
> template<class Dist>
> class CRandom : public b::variate_generator<b::mt19937&,Dist> //
> notice the reference, we do not want to copy the engine
> {
> public:
> /** use for Dist:
> CRandomI Ci(min,max); uses uniform_int<int> - integral
> types CRandomR Cr(min,max); uses uniform_real<double> -
> floating point types
>
> for other ditributions or types, use:
> CRandom<distribution<optional type>> Cr(0--3 params, depending on
> distribution);
> for distributions and params, see:
> http://www.boost.org/doc/libs/1_39_0/libs/random/random-distributions.html
> */
>
> // forwarding constructors
> explicit CRandom()
> : variate_generator<b::mt19937&,Dist>(
> Singleton<b::mt19937>::Instance(unsigned int(time(NULL))),
> Dist()
> )
> { }
> template<class P>
> explicit CRandom(const P & p1)
> : variate_generator<b::mt19937&,Dist>(
> Singleton<b::mt19937>::Instance(unsigned int(time(NULL))),
> Dist(p1)
> )
> { }
> template<class P1, class P2>
> explicit CRandom(const P1 & p1, const P2 & p2)
> : variate_generator<b::mt19937&,Dist>(
> Singleton<b::mt19937>::Instance(unsigned int(time(NULL))),
> Dist(p1, p2)
> )
> { }
> template<class P1, class P2, class P3>
> explicit CRandom(const P1 & p1, const P2 & p2, const P3 & p3)
> : variate_generator<b::mt19937&,Dist>(
> Singleton<b::mt19937>::Instance(unsigned int(time(NULL))),
> Dist(p1, p2, p3)
> )
> { }
> };
> // end declaration
>
>
> // shorthand typedefs
> typedef CRandom<b::uniform_int<int>> CRandomI; // even int is
> default type , specify it in case it ever changes
> typedef CRandom<b::uniform_real<double>> CRandomR; // even double
> is default type, specify it in case it ever changes
>
>
> // boolean: class whose () operator returns a random true or false
> class CRandomB
> {
> public:
> CRandomB(void) :
> _Cr(CRandomR(0.,1.))
> {}
>
> bool operator()() { return _Cr() <= .5; }
>
> private:
> CRandomR _Cr;
> };
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net