Logo ROOT   6.10/09
Reference Guide
GSLRngWrapper.h
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Author: L. Moneta Fri Aug 24 17:20:45 2007
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // Header file for class GSLRngWrapper
12 
13 #ifndef ROOT_Math_GSLRngWrapper
14 #define ROOT_Math_GSLRngWrapper
15 
16 
17 namespace ROOT {
18 
19  namespace Math {
20 
21 
22 /**
23  GSLRngWrapper class to wrap gsl_rng structure
24 */
26 
27 public:
28 
29 
30  /**
31  Default constructor
32  */
34  fOwn(0),
35  fRng(0),
36  fRngType(0)
37  {
38  }
39 
40  /**
41  Constructor with type
42  */
43  GSLRngWrapper(const gsl_rng_type * type) :
44  fOwn(1),
45  fRng(0),
46  fRngType(type)
47  {
48  }
49 
50  /**
51  construct from an existing gsl_rng
52  it is managed externally - so will not be deleted at the end
53  */
54  GSLRngWrapper(const gsl_rng * r ) :
55  fOwn(0),
56  fRngType(0)
57  {
58  fRng = const_cast<gsl_rng *>(r);
59  }
60 
61  /**
62  Copy constructor - clone the GSL object and manage it
63  */
65  fOwn(1),
67  {
68  fRng = gsl_rng_clone(r.fRng);
69  }
70 
71  /**
72  Assignment operator
73  */
75  if (this == &rhs) return *this; // time saving self-test
76  fRngType = rhs.fRngType;
77  int iret = 0;
78  if (fRngType == rhs.fRngType) {
79  iret = gsl_rng_memcpy(fRng, rhs.fRng);
80  if (!iret) return *this;
81  }
82  // otherwise create a new copy
83  if (fOwn) Free();
84  fRng = gsl_rng_clone(rhs.fRng);
85  fOwn = true;
86  return *this;
87  }
88 
89  /**
90  Destructor (free the rng if not done before)
91  */
93  if (fOwn) Free();
94  }
95 
96  void Allocate() {
97  if (fRngType == 0) SetDefaultType();
98  if (fRng != 0 && fOwn) Free();
99  fRng = gsl_rng_alloc( fRngType );
100  }
101 
102  void Free() {
103  if (!fOwn) return; // no operation if pointer is not own
104  //std::cout << "free gslrng " << fRngType << " " << fRng << std::endl;
105  if (fRng != 0) gsl_rng_free(fRng);
106  fRng = 0;
107  }
108 
109 
110  void SetType(const gsl_rng_type * type) {
111  fRngType = type;
112  }
113 
114  void SetDefaultType() {
115  // construct default engine
116  gsl_rng_env_setup();
117  fRngType = gsl_rng_default;
118  }
119 
120  void PrintState() const {
121  gsl_rng_print_state(fRng);
122  }
123 
124  inline gsl_rng * Rng() { return fRng; }
125 
126  inline const gsl_rng * Rng() const { return fRng; }
127 
128 
129 
130 private:
131 
132  bool fOwn; // ownership of contained pointer
133  gsl_rng * fRng;
134  const gsl_rng_type * fRngType;
135 };
136 
137 
138  } // end namespace Math
139 
140 } // end namespace ROOT
141 
142 
143 #endif /* ROOT_Math_GSLRngWrapper */
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
GSLRngWrapper(const gsl_rng *r)
construct from an existing gsl_rng it is managed externally - so will not be deleted at the end ...
Definition: GSLRngWrapper.h:54
const gsl_rng * Rng() const
GSLRngWrapper()
Default constructor.
Definition: GSLRngWrapper.h:33
~GSLRngWrapper()
Destructor (free the rng if not done before)
Definition: GSLRngWrapper.h:92
GSLRngWrapper class to wrap gsl_rng structure.
Definition: GSLRngWrapper.h:25
const gsl_rng_type * fRngType
TRandom2 r(17)
int type
Definition: TGX11.cxx:120
GSLRngWrapper(GSLRngWrapper &r)
Copy constructor - clone the GSL object and manage it.
Definition: GSLRngWrapper.h:64
GSLRngWrapper(const gsl_rng_type *type)
Constructor with type.
Definition: GSLRngWrapper.h:43
void SetType(const gsl_rng_type *type)
Namespace for new Math classes and functions.
GSLRngWrapper & operator=(const GSLRngWrapper &rhs)
Assignment operator.
Definition: GSLRngWrapper.h:74