Logo ROOT  
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 */
25 class GSLRngWrapper {
26 
27 public:
28 
29 
30  /**
31  Default constructor
32  */
33  GSLRngWrapper () :
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  */
74  GSLRngWrapper & operator = (const GSLRngWrapper & rhs) {
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  */
92  ~GSLRngWrapper() {
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 */
r
ROOT::R::TRInterface & r
Definition: Object.C:4
ROOT::Math::GSLRngWrapper
GSLRngWrapper class to wrap gsl_rng structure.
Definition: GSLRngWrapper.h:35
ROOT::Math::GSLRngWrapper::PrintState
void PrintState() const
Definition: GSLRngWrapper.h:136
ROOT::Math::GSLRngWrapper::Allocate
void Allocate()
Definition: GSLRngWrapper.h:111
ROOT::Math::GSLRngWrapper::GSLRngWrapper
GSLRngWrapper()
Default constructor.
Definition: GSLRngWrapper.h:48
ROOT::Math::GSLRngWrapper::Rng
gsl_rng * Rng()
Definition: GSLRngWrapper.h:140
ROOT::Math::GSLRngWrapper::~GSLRngWrapper
~GSLRngWrapper()
Destructor (free the rng if not done before)
Definition: GSLRngWrapper.h:107
ROOT::Math::GSLRngWrapper::fRngType
const gsl_rng_type * fRngType
Definition: GSLRngWrapper.h:150
ROOT::Math::GSLRngWrapper::SetDefaultType
void SetDefaultType()
Definition: GSLRngWrapper.h:130
ROOT::Math::GSLRngWrapper::Free
void Free()
Definition: GSLRngWrapper.h:118
ROOT::Math::GSLRngWrapper::fRng
gsl_rng * fRng
Definition: GSLRngWrapper.h:149
ROOT::Math::GSLRngWrapper::SetType
void SetType(const gsl_rng_type *type)
Definition: GSLRngWrapper.h:126
type
int type
Definition: TGX11.cxx:121
ROOT::Math::GSLRngWrapper::fOwn
bool fOwn
Definition: GSLRngWrapper.h:148
ROOT
VSD Structures.
Definition: StringConv.hxx:21
Math
ROOT::Math::GSLRngWrapper::operator=
GSLRngWrapper & operator=(const GSLRngWrapper &rhs)
Assignment operator.
Definition: GSLRngWrapper.h:89