00001 // @(#)root/mathmore:$Id: GSLRngWrapper.h 33304 2010-04-30 08:39:37Z moneta $ 00002 // Author: L. Moneta Fri Aug 24 17:20:45 2007 00003 00004 /********************************************************************** 00005 * * 00006 * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT * 00007 * * 00008 * * 00009 **********************************************************************/ 00010 00011 // Header file for class GSLRngWrapper 00012 00013 #ifndef ROOT_Math_GSLRngWrapper 00014 #define ROOT_Math_GSLRngWrapper 00015 00016 00017 namespace ROOT { 00018 00019 namespace Math { 00020 00021 00022 /** 00023 GSLRngWrapper class to wrap gsl_rng structure 00024 */ 00025 class GSLRngWrapper { 00026 00027 public: 00028 00029 00030 /** 00031 Default constructor 00032 */ 00033 GSLRngWrapper () : 00034 fOwn(0), 00035 fRng(0), 00036 fRngType(0) 00037 { 00038 } 00039 00040 /** 00041 Constructor with type 00042 */ 00043 GSLRngWrapper(const gsl_rng_type * type) : 00044 fOwn(1), 00045 fRng(0), 00046 fRngType(type) 00047 { 00048 } 00049 00050 /** 00051 construct from an existing gsl_rng 00052 it is managed externally - so will not be deleted at the end 00053 */ 00054 GSLRngWrapper(const gsl_rng * r ) : 00055 fOwn(0), 00056 fRngType(0) 00057 { 00058 fRng = const_cast<gsl_rng *>(r); 00059 } 00060 00061 /** 00062 Copy constructor - pass ownership (need not to be const) 00063 Just copy the pointer and do not manage it 00064 */ 00065 GSLRngWrapper(GSLRngWrapper & r) : 00066 fOwn(r.fOwn), 00067 fRng(r.fRng), 00068 fRngType(r.fRngType) 00069 { 00070 // in case an rng exists must release it 00071 if (fRng && fOwn) r.fOwn = false; 00072 } 00073 00074 00075 /** 00076 Destructor (free the rng if not done before) 00077 */ 00078 ~GSLRngWrapper() { 00079 if (fOwn) Free(); 00080 } 00081 00082 void Allocate() { 00083 if (fRngType == 0) SetDefaultType(); 00084 if (fRng != 0 && fOwn) Free(); 00085 fRng = gsl_rng_alloc( fRngType ); 00086 //std::cout << " allocate " << fRng << std::endl; 00087 } 00088 00089 void Free() { 00090 if (!fOwn) return; // no operation if pointer is not own 00091 //std::cout << "free gslrng " << fRngType << " " << fRng << std::endl; 00092 if (fRng != 0) gsl_rng_free(fRng); 00093 fRng = 0; 00094 } 00095 00096 00097 void SetType(const gsl_rng_type * type) { 00098 fRngType = type; 00099 } 00100 00101 void SetDefaultType() { 00102 // construct default engine 00103 gsl_rng_env_setup(); 00104 fRngType = gsl_rng_default; 00105 } 00106 00107 00108 00109 inline gsl_rng * Rng() { return fRng; } 00110 00111 inline const gsl_rng * Rng() const { return fRng; } 00112 00113 private: 00114 // usually copying is non trivial, so we make this unaccessible 00115 00116 00117 /** 00118 Assignment operator 00119 Disable since if don't want to change an already created wrapper 00120 */ 00121 GSLRngWrapper & operator = (const GSLRngWrapper & rhs) { 00122 if (this == &rhs) return *this; // time saving self-test 00123 return *this; 00124 } 00125 00126 00127 private: 00128 00129 bool fOwn; // ownership of contained pointer 00130 gsl_rng * fRng; 00131 const gsl_rng_type * fRngType; 00132 }; 00133 00134 00135 } // end namespace Math 00136 00137 } // end namespace ROOT 00138 00139 00140 #endif /* ROOT_Math_GSLRngWrapper */