1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
// -*- C++ -*-
//
// CurrentGenerator.h is a part of ThePEG - Toolkit for HEP Event Generation
// Copyright (C) 1999-2011 Leif Lonnblad
//
// ThePEG is licenced under version 2 of the GPL, see COPYING for details.
// Please respect the MCnet academic guidelines, see GUIDELINES for details.
//
#ifndef ThePEG_CurrentGenerator_H
#define ThePEG_CurrentGenerator_H
// This is the declaration of the CurrentGenerator class.
#include "ThePEG/Repository/EventGenerator.h"
#include "CurrentGenerator.fh"
namespace ThePEG {
/**
* This CurrentGenerator class keeps a static stack of EventGenerators
* which can be used anywhere by any class. When an EventGenerator is
* initialized or run it adds itself to the stack which can be used by
* any other object being initialized or run through the static
* functions of the CurrentGenerator class. If someone
* needs to use an alternative EventGenerator object a new
* CurrentGenerator object can be constructed with a
* pointer to the desired EventGenerator object as argument and that
* object will the be used by the static CurrentGenerator
* functions until the CurrentGenerator object is destructed.
*
* @see EventGenerator
*
*/
class CurrentGenerator {
public:
/**
* Default constructor does nothing.
*/
CurrentGenerator() : generatorPushed(false) {}
/**
* Copy-constructor does nothing.
*/
CurrentGenerator(const CurrentGenerator &)
: generatorPushed(false) {}
/**
* Construct a new object specifying a new EventGenerator, \a eg, to
* be used during this objects lifetime.
*/
CurrentGenerator(const EGPtr & eg) : generatorPushed(false) {
if ( eg ) {
theGeneratorStack.push_back(eg);
generatorPushed = true;
}
}
/**
* The destructor removing the EventGenerator specified in the
* constructor from the stack.
*/
~CurrentGenerator() {
if ( generatorPushed ) theGeneratorStack.pop_back();
}
public:
/**
* Returns true if there is no currently chosen EventGenerator
* object.
*/
static bool isVoid() {
return theGeneratorStack.empty() || !(theGeneratorStack.back());
}
/**
* Return a reference to the currently chosen EventGenerator object.
*/
static EventGenerator & current() {
return *theGeneratorStack.back();
}
/**
* Return a reference to the currently chosen object.
*/
EventGenerator & operator*() const {
return *theGeneratorStack.back();
}
/**
* Return a pointer to the currently chosen object.
*/
EventGenerator * operator->() const {
return theGeneratorStack.back();
}
/**
* Pointer to the stack
*/
static EventGenerator * ptr() {
return theGeneratorStack.back();
}
/**
* Test for existance
*/
operator bool() const {
return ptr();
}
/**
* Test for existance
*/
bool operator!() const {
return !ptr();
}
/**
* Return a pointer to the standard model parameters used by the
* current generator.
*/
static tSMPtr standardModel() {
return current().standardModel();
}
/**
* Return a pointer to the strategy object containing eg. a set of
* non-default particles to be used by the current generator.
*/
static tStrategyPtr strategy() { return current().strategy(); }
/**
* Get the current standard output stream. Return a reference to the
* stream connected to the file for general output of the current
* generator. If no file is connected, the BaseRepository::cout()
* will be used instead.
*/
static ostream & out() { return current().out(); }
/**
* Get the current standard log stream. Return a reference to the
* stream connected to the file for logging information of the
* current generator. If no file is connected, the
* BaseRepository::clog() will be used instead.
*/
static ostream & log() { return current().log(); }
/**
* Get the current standard ref stream. Return a reference to the
* stream connected to the file for references from used objects of
* the current generator. If no file is connected, the
* BaseRepository::cout() will be used instead.
*/
static ostream & ref() { return current().ref(); }
/**
* Get object. Return a garbage collected pointer to a given object
* in the current EventGenerator. If the object is not found, a null
* pointer will be returned.
*/
template <typename T>
static typename Ptr<T>::pointer getPtr(const T & t) {
return current().getPtr(t);
}
/**
* Get object. Return a pointer to an object present in the current
* EventGenerator given its full name. Return the null pointer if
* non-existent.
*/
static IBPtr getPointer(string name) {
return current().getPointer(name);
}
/**
* Get object. Return a pointer to an object of type T present in
* the current EventGenerator given its full name. Return the null
* pointer if non-existent.
*/
template <typename T>
static typename Ptr<T>::pointer getObject(string name) {
return current().getObject<T>(name);
}
/**
* Get default object. Return the default object for class T in the
* current EventGenerator. Returns the null pointer if non-existent.
*/
template <typename T>
static typename Ptr<T>::pointer getDefault() {
return current().getDefault<T>();
}
public:
/**
* Class used to temporarily redirect a given ostream to the misc()
* stream of the current EventGenerator.
*/
class Redirect {
public:
/**
* Constructor taking the stream to be redirected as input. If the
* \a internal flag false the output will be stored in the Event
* Generator and written to the log file in the end of the run. If
* \internal is true the output is instead stored internally in
* this object and is accessible through the str() function until
* the object is destroyed.
*/
Redirect(ostream & os, bool internal = false)
: theStream(&os), theBuffer(os.rdbuf()) {
if ( internal ) theStream->rdbuf(intStream.rdbuf());
else if ( !current().useStdOut() )
theStream->rdbuf(current().misc().rdbuf());
}
/**
* The destructor which restores the original destination of the
* stream.
*/
~Redirect() {
theStream->rdbuf(theBuffer);
}
/**
* If output is stored internally, acces what has been written so
* far.
*/
string str() const {
return intStream.str();
}
/**
* The stream which is redirected.
*/
ostream * theStream;
/**
* The original buffer of the redirected stream.
*/
std::streambuf * theBuffer;
/**
* An internal buffer, the content of which will be discarded when
* the this object is destructed.
*/
ostringstream intStream;
};
private:
/**
* The stack of EventGenerators requested.
*/
static vector<EGPtr> theGeneratorStack;
/**
* True if this object is responsible for pushing a EventGenerator
* onto the stack.
*/
bool generatorPushed;
private:
/**
* Private and non-existent assignment operator.
*/
CurrentGenerator & operator=(const CurrentGenerator &);
};
}
#endif /* ThePEG_CurrentGenerator_H */
|