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...
// http://www.devarticles.com/c/a/Cplusplus/C-plus-plus-In-Theory-The-Singleton...
// 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
#include
#include <iostream>
#include "Singleton.h"
// namespaces
namespace s = std;
namespace b = boost;
// declaration
template<class Dist>
class CRandom : public b::variate_generator //
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 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(
Singleton::Instance(unsigned int(time(NULL))),
Dist()
)
{ }
template<class P>
explicit CRandom(const P & p1)
: variate_generator(
Singleton::Instance(unsigned int(time(NULL))),
Dist(p1)
)
{ }
template
explicit CRandom(const P1 & p1, const P2 & p2)
: variate_generator(
Singleton::Instance(unsigned int(time(NULL))),
Dist(p1, p2)
)
{ }
template
explicit CRandom(const P1 & p1, const P2 & p2, const P3 & p3)
: variate_generator(
Singleton::Instance(unsigned int(time(NULL))),
Dist(p1, p2, p3)
)
{ }
};
// end declaration
// shorthand typedefs
typedef CRandom CRandomI; // even int is
default type , specify it in case it ever changes
typedef CRandom 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;
};