ThePEG  1.8.0
LoopGuard.h
1 // -*- C++ -*-
2 //
3 // LoopGuard.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2011 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 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 */