Logo ROOT   6.13/01
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  fOwn = true;
101  }
102 
103  void Free() {
104  if (!fOwn) return; // no operation if pointer is not own
105  //std::cout << "free gslrng " << fRngType << " " << fRng << std::endl;
106  if (fRng != 0) gsl_rng_free(fRng);
107  fRng = 0;
108  }
109 
110 
111  void SetType(const gsl_rng_type * type) {
112  fRngType = type;
113  }
114 
115  void SetDefaultType() {
116  // construct default engine
117  gsl_rng_env_setup();
118  fRngType = gsl_rng_default;
119  }
120 
121  void PrintState() const {
122  gsl_rng_print_state(fRng);
123  }
124 
125  inline gsl_rng * Rng() { return fRng; }
126 
127  inline const gsl_rng * Rng() const { return fRng; }
128 
129 
130 
131 private:
132 
133  bool fOwn; // ownership of contained pointer
134  gsl_rng * fRng;
135  const gsl_rng_type * fRngType;
136 };
137 
138 
139  } // end namespace Math
140 
141 } // end namespace ROOT
142 
143 
144 #endif /* ROOT_Math_GSLRngWrapper */
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
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
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