
Thanks Robert, Your singleton makes stuff really easy. For those interested, code below works like a breeze. Destructor is also called at program exit, so no leaks will occur. Best, Diederick --- #pragma once // includes #include <boost/random.hpp> #include <boost/serialization/singleton.hpp> #include <time.h> using namespace std; namespace bs = boost::serialization; // declaration template<class Dist> class CRandom { 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 engine is wrapped following singleton pattern, so every instantiation of CRandom uses the same engine. Singleton of boost::serialization used */ // forwarding constructors explicit CRandom() : Gen(bs::singleton<boost::mt19937>::get_mutable_instance(), Dist()) { } template<class P> explicit CRandom(const P & p1) : Gen(bs::singleton<boost::mt19937>::get_mutable_instance(), Dist(p1)) { } template<class P> explicit CRandom(const P & p1, const P & p2) : Gen(bs::singleton<boost::mt19937>::get_mutable_instance(), Dist(p1, p2)) { } template<class P> explicit CRandom(const P & p1, const P & p2, const P & p3) : Gen(bs::singleton<boost::mt19937>::get_mutable_instance(), Dist(p1, p2, p3)) { } ~CRandom(void) {}; typedef typename Dist::result_type result_type; result_type operator()() { return Gen(); } template<class T> result_type operator()(T value) { return Gen(value); } private: boost::variate_generator<boost::mt19937&,Dist> Gen; }; // end declaration // shorthand typedefs typedef CRandom<boost::uniform_int<int>> CRandomI; // even int is default type , specify it in case it ever changes typedef CRandom<boost::uniform_real<double>> CRandomR; // even double is default type, specify it in case it ever changes On Sun, Jul 26, 2009 at 1:54 PM, Diederick C. Niehorster<dcnieho@gmail.com> wrote:
Dear Robert, Thanks! I will look into http://www.boost.org/doc/libs/1_39_0/libs/serialization/doc/singleton.html, I should not be reinventing the wheel ;)
Dear Steven, Sharp eye, good point. Now two different numbers get generated and all those destructor calls dissapeared.
It seems one too many dissapeared even (see program output below). Even though MSVC does not complain about memory leaks, at end of program the destructor never gets called. Lets see if Robert's singleton class solves this, otherwise i suppose i should look into using scoped_ptr?
Thanks a lot for your replies, Diederick
------ output: check 0 00000000 ctor 1 after ctor 1 003462E0 random: 8148 dtor 1 ctor 2 after ctor 2 003462E0 random: 1355 Press any key to continue . . .
On Sun, Jul 26, 2009 at 1:16 PM, Steven Watanabe<watanabesj@gmail.com> wrote:
AMDG
Diederick C. Niehorster wrote:
template<class Dist> class CRandom { public: <snip> private: boost::variate_generator<boost::mt19937,Dist> Gen; };
This copies the engine. Try boost::variate_generator<boost::mt19937&,Dist>
In Christ, Steven Watanabe
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users