thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
Current.h
1 // -*- C++ -*-
2 //
3 // Current.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2019 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
8 //
9 #ifndef ThePEG_Current_H
10 #define ThePEG_Current_H
11 // This is the declaration of the Current class.
12 
13 namespace ThePEG {
14 
38 template <typename T>
39 class Current {
40 
41 public:
42 
46  Current() : pushed(false) {}
47 
52  : pushed(false) {}
53 
59  Current(T * t) : pushed(false) {
60  if ( t ) {
61  theStack.push_back(t);
62  pushed = true;
63  }
64  }
65 
71  if ( pushed ) theStack.pop_back();
72  }
73 
74 public:
75 
79  static bool isVoid() {
80  return theStack.empty() || !(theStack.back());
81  }
82 
86  static T & current() {
87  return *theStack.back();
88  }
89 
93  T & operator*() const {
94  return *theStack.back();
95  }
96 
100  T * operator->() const {
101  return theStack.back();
102  }
103 
107  static T * ptr() {
108  return theStack.back();
109  }
110 
114  operator bool() const {
115  return ptr();
116  }
117 
121  bool operator!() const {
122  return !ptr();
123  }
124 
125 private:
126 
130  static vector<T *> theStack;
131 
136  bool pushed;
137 
138 private:
139 
143  Current<T> & operator=(const Current<T> &) = delete;
144 
145 };
146 
147 template <typename T>
148 std::vector<T *> ThePEG::Current<T>::theStack;
149 
150 }
151 
152 #endif /* ThePEG_Current_H */
Current()
Default constructor does nothing.
Definition: Current.h:46
~Current()
The destructor removing the object specified in the constructor from the stack.
Definition: Current.h:70
Current< T > & operator=(const Current< T > &)=delete
Private and non-existent assignment operator.
Current(T *t)
Construct a new object specifying a new object, o, to be used during this objects lifetime...
Definition: Current.h:59
static vector< T * > theStack
The stack of objects requested.
Definition: Current.h:130
Current(const Current< T > &)
Copy-constructor does nothing.
Definition: Current.h:51
T & operator*() const
Return a reference to the currently chosen object.
Definition: Current.h:93
The Current class keeps a static stack of objects of the templated class, which can be used anywhere ...
Definition: Current.h:39
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
bool operator!() const
Test for existance.
Definition: Current.h:121
static bool isVoid()
Returns true if there is no currently chosen object.
Definition: Current.h:79
static T & current()
Return a reference to the currently chosen object.
Definition: Current.h:86
bool pushed
True if this object is responsible for pushing an object onto the stack.
Definition: Current.h:136
static T * ptr()
Pointer to the stack.
Definition: Current.h:107
T * operator->() const
Return a pointer to the currently chosen object.
Definition: Current.h:100