thepeg is hosted by Hepforge, IPPP Durham
ThePEG  2.2.1
LoopGuard.h
1 // -*- C++ -*-
2 //
3 // LoopGuard.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_LoopGuard_H
10 #define ThePEG_LoopGuard_H
11 // This is the declaration of the LoopGuard class.
12 
13 namespace ThePEG {
14 
26 template <typename ExceptionT = Exception,
27  typename MessageT = const char *>
28 class LoopGuard {
29 
30 public:
31 
37  LoopGuard(const MessageT & mess, long maxc = 1000000 )
38  : count(0), maxCount(maxc), message(mess) {}
39 
44  void operator()()
45  {
46  if ( ++count > maxCount ) throw ExceptionT(message);
47  }
48 
49 private:
50 
54  long count;
55 
59  long maxCount;
60 
65  const MessageT & message;
66 
67 private:
68 
72  LoopGuard();
73 
77  LoopGuard(const LoopGuard &);
78 
79 };
80 
94 template <typename ExceptionT>
95 class LoopGuard<ExceptionT,void> {
96 
97 public:
98 
104  LoopGuard(long maxc = 1000000 )
105  : count(0), maxCount(maxc) {}
106 
111  void operator()()
112  {
113  if ( ++count > maxCount ) throw ExceptionT();
114  }
115 
116 private:
117 
121  long count;
122 
126  long maxCount;
127 
128 private:
129 
133  LoopGuard();
134 
138  LoopGuard(const LoopGuard &);
139 
140 };
141 
142 }
143 
144 #endif /* ThePEG_LoopGuard_H */
LoopGuard()
Default constructor is private and not implemented.
void operator()()
Increase the iteration count and throw an ExceptionT if the maximum number of iterations is exceeded...
Definition: LoopGuard.h:44
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28
long maxCount
The maximum number of counts allowed.
Definition: LoopGuard.h:126
long count
The number of counts so far.
Definition: LoopGuard.h:121
void operator()()
Increase the iteration count and throw an ExceptionT if the maximum number of iterations is exceeded...
Definition: LoopGuard.h:111
A LoopGuard object can be used to throw an exception if a loop is iterated too many times...
Definition: LoopGuard.h:28
LoopGuard(long maxc=1000000)
Create a loop guard object which will throw an exception of type ExceptionT, constructed with &#39;mess&#39; ...
Definition: LoopGuard.h:104
long maxCount
The maximum number of counts allowed.
Definition: LoopGuard.h:59
LoopGuard(const MessageT &mess, long maxc=1000000)
Create a loop guard object which will throw an exception of type ExceptionT, constructed with &#39;mess&#39; ...
Definition: LoopGuard.h:37
long count
The number of counts so far.
Definition: LoopGuard.h:54
const MessageT & message
The message with which the thrown ExceptionT object will be initialized.
Definition: LoopGuard.h:65