source: src/RandomNumbers/RandomNumberEngine_Encapsulation.hpp@ 88bb6b

Last change on this file since 88bb6b was 7d46e3, checked in by Frederik Heber <heber@…>, 13 years ago

MEMFIX: RandomNumberEngine_Encapsulation<T>::clone() did leak params allocated in getParameterSet().

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * RandomNumberEngine_Encapsulation.hpp
3 *
4 * Created on: Jan 01, 2011
5 * Author: heber
6 */
7
8#ifndef RANDOMNUMBERENGINE_ENCAPSULATION_HPP_
9#define RANDOMNUMBERENGINE_ENCAPSULATION_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include <typeinfo>
17
18#include <boost/nondet_random.hpp>
19#include <boost/random.hpp>
20#include <boost/random/additive_combine.hpp>
21#include <boost/random/discard_block.hpp>
22#include <boost/random/inversive_congruential.hpp>
23#include <boost/random/lagged_fibonacci.hpp>
24#include <boost/random/linear_congruential.hpp>
25#include <boost/random/linear_feedback_shift.hpp>
26#include <boost/random/mersenne_twister.hpp>
27#include <boost/random/random_number_generator.hpp>
28#include <boost/random/ranlux.hpp>
29#include <boost/random/shuffle_output.hpp>
30#include <boost/random/subtract_with_carry.hpp>
31#include <boost/random/xor_combine.hpp>
32
33#include "CodePatterns/ManipulableClone.hpp"
34#include "RandomNumberEngine.hpp"
35
36class RandomNumberEngineFactory;
37
38#include "RandomNumberEngine_Parameters.hpp"
39
40/** Template class that encapsulates the random number engines from
41 * random::boost.
42 *
43 *
44 * We need one template parameters:
45 * -# the engine - generates uniform random numbers
46 */
47template <class engine>
48class RandomNumberEngine_Encapsulation :
49 public RandomNumberEngine,
50 public ManipulableClone<RandomNumberEngine, RandomNumberEngine_Parameters>
51{
52 /**
53 * Factory is friend such that it can access private cstor when filling its
54 * table
55 */
56 friend class RandomNumberEngineFactory;
57
58public:
59 /** Getter for the whole set of possible parameters.
60 *
61 * @return filled instance of RandomNumberEngine_Parameters
62 */
63 RandomNumberEngine_Parameters* getParameterSet() const
64 {
65 RandomNumberEngine_Parameters *params = new RandomNumberEngine_Parameters();
66 params->getParameters(this);
67 return params;
68 }
69
70 /** Set the engine's seed.
71 *
72 * @param _seed seed to set to
73 */
74 void seed(unsigned int _seed) {
75 value_seed = _seed;
76 engine_type.seed(value_seed);
77 }
78
79 /** Getter for the engine's seed.
80 *
81 * @return seed
82 */
83 unsigned int getseed() const {
84 return value_seed;
85 }
86
87 /** Getter for smallest value the engine produces.
88 *
89 * @return smallest value
90 */
91 double min() const {
92 return engine_type.min();
93 }
94
95 /** Getter for largest value the engine produces.
96 *
97 * @return largest value
98 */
99 double max() const {
100 return engine_type.max();
101 }
102
103 /** Getter for the type name of the internal engine.
104 *
105 */
106 std::string name() {
107 return typeid(engine_type).name();
108 }
109
110 /** Getter for the engine instance.
111 *
112 * @return reference to instance
113 */
114 engine& getEngine() {
115 return engine_type;
116 }
117
118 /** Clones the current instance.
119 *
120 * Implementation of Clone pattern.
121 *
122 * @return interface reference to cloned instance
123 */
124 RandomNumberEngine* clone() const {
125 RandomNumberEngine_Parameters *params = getParameterSet();
126 RandomNumberEngine* MyClone = new RandomNumberEngine_Encapsulation<engine>(*params);
127 delete params;
128 //MyClone->seed(getseed());
129 return MyClone;
130 }
131
132 /** Clones and manipulates the current instance.
133 *
134 * Implementation of ManipulableClone pattern.
135 *
136 * @param _params set of parameters to instantiate manipulated clone with
137 * @return interface reference to cloned&manipulated instance
138 */
139 ManipulableClone<RandomNumberEngine, RandomNumberEngine_Parameters>*
140 manipulatedclone(const RandomNumberEngine_Parameters &_params) const {
141 RandomNumberEngine_Encapsulation<engine>* MyClone = NULL;
142 MyClone = new RandomNumberEngine_Encapsulation<engine>();
143 MyClone->value_seed = _params.seed;
144 MyClone->seed(_params.seed);
145 return MyClone;
146 }
147
148protected:
149 /** Constructor that instantiates a specific random number generator and
150 * distribution.
151 * @param _engine_type instance of the desired engine
152 */
153 RandomNumberEngine_Encapsulation() :
154 value_seed(1)
155 {}
156
157 /** Constructor that instantiates a specific random number generator and
158 * distribution.
159 * @param _engine_type instance of the desired engine
160 */
161 RandomNumberEngine_Encapsulation(const RandomNumberEngine_Parameters &_params) :
162 value_seed(_params.seed)
163 {}
164
165 /** Destructor of the class.
166 *
167 */
168 virtual ~RandomNumberEngine_Encapsulation() {}
169private:
170 engine engine_type;
171 unsigned int value_seed;
172};
173
174#endif /* RANDOMNUMBERENGINE_ENCAPSULATION_HPP_ */
Note: See TracBrowser for help on using the repository browser.