Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:32

0001 // -*- C++ -*-
0002 //
0003 // LoopGuard.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef ThePEG_LoopGuard_H
0010 #define ThePEG_LoopGuard_H
0011 // This is the declaration of the LoopGuard class.
0012 
0013 namespace ThePEG {
0014 
0015 /**
0016  * A LoopGuard object can be used to throw an exception if a loop is
0017  * iterated too many times. It is used by constructing an object
0018  * before the loop giving the maximum number of iterations allowed and
0019  * a message to be used as argument to the constructor of the
0020  * exception to be thrown. Inside the loop the parenthesis is called
0021  * without argument, which will increment and check an internal counter.
0022  * 
0023  *
0024  * @see Level
0025  */
0026 template <typename ExceptionT = Exception,
0027           typename MessageT = const char *>
0028 class LoopGuard {
0029 
0030 public:
0031 
0032   /**
0033    * Create a loop guard object which will throw an exception of type
0034    * ExceptionT, constructed with 'mess' as argument, if the maximum
0035    * number of iterations is exceeded.
0036    */
0037   LoopGuard(const MessageT & mess, long maxc = 1000000 )
0038     : count(0), maxCount(maxc), message(mess) {}
0039 
0040   /**
0041    * Increase the iteration count and throw an ExceptionT if the
0042    * maximum number of iterations is exceeded.
0043    */
0044   void operator()()
0045   {
0046     if ( ++count > maxCount ) throw ExceptionT(message);
0047   }
0048 
0049 private:
0050 
0051   /**
0052    * The number of counts so far.
0053    */
0054   long count;
0055 
0056   /**
0057    * The maximum number of counts allowed.
0058    */
0059   long maxCount;
0060 
0061   /**
0062    * The message with which the thrown ExceptionT object will be
0063    * initialized.
0064    */
0065   const MessageT & message;
0066 
0067 private:
0068 
0069   /**
0070    * Default constructor is private and not implemented.
0071    */
0072   LoopGuard();
0073 
0074   /**
0075    * Copy constructor is private and not implemented.
0076    */
0077   LoopGuard(const LoopGuard &);
0078 
0079 };
0080 
0081 /**
0082  * A LoopGuard object can be used to throw an exception if a loop is
0083  * iterated too many times. It is used by constructing an object
0084  * before the loop giving the maximum number of iterations allowed and
0085  * a message to be used as argument to the constructor of the
0086  * exception to be thrown. Inside the loop the parenthesis is called
0087  * without argument, which will increment and check an internal
0088  * counter.  This specialization is for the case where the exception
0089  * class cannot be created with a message.
0090  * 
0091  *
0092  * @see Level
0093  */
0094 template <typename ExceptionT>
0095 class LoopGuard<ExceptionT,void> {
0096 
0097 public:
0098 
0099   /**
0100    * Create a loop guard object which will throw an exception of type
0101    * ExceptionT, constructed with 'mess' as argument, if the maximum
0102    * number of iterations is exceeded.
0103    */
0104   LoopGuard(long maxc = 1000000 )
0105     : count(0), maxCount(maxc) {}
0106 
0107   /**
0108    * Increase the iteration count and throw an ExceptionT if the
0109    * maximum number of iterations is exceeded.
0110    */
0111   void operator()()
0112   {
0113     if ( ++count > maxCount ) throw ExceptionT();
0114   }
0115 
0116 private:
0117 
0118   /**
0119    * The number of counts so far.
0120    */
0121   long count;
0122 
0123   /**
0124    * The maximum number of counts allowed.
0125    */
0126   long maxCount;
0127 
0128 private:
0129 
0130   /**
0131    * Default constructor is private and not implemented.
0132    */
0133   LoopGuard();
0134 
0135   /**
0136    * Copy constructor is private and not implemented.
0137    */
0138   LoopGuard(const LoopGuard &);
0139 
0140 };
0141 
0142 }
0143 
0144 #endif /* ThePEG_LoopGuard_H */