Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Switch.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_Switch_H
0010 #define ThePEG_Switch_H
0011 // This is the declaration of the Switch, SwitchBase and SwitchOption classes.
0012 
0013 #include "ThePEG/Config/ThePEG.h"
0014 #include "Switch.fh"
0015 #include "Switch.xh"
0016 #include "InterfaceBase.h"
0017 
0018 namespace ThePEG {
0019 
0020 /**
0021  * SwitchOption is used by the Switch class and its base class
0022  * SwitchBase to define valid options in a switch.
0023  *
0024  * For each InterfacedBase class exactly one static Switch object
0025  * should created for each member variable which should be
0026  * interfaced. This object will automatically register itself with the
0027  * BaseRepository class. Also for each Switch object exactly one
0028  * static SwitchOption object should be created for each valid integer
0029  * option.
0030  *
0031  * @see InterfacedBase
0032  * @see InterfacedBase
0033  * @see Named
0034  * 
0035  */
0036 class SwitchOption: public Named {
0037 
0038 public:
0039 
0040   /**
0041    * Standard constructor.
0042    *
0043    * @param theSwitch the Switch object for which this option is
0044    * defined. Note thet the static Switch object must be created
0045    * before this is created.
0046    *
0047    * @param newName the name of the option, may only contain
0048    * letters [a-zA-z0-9_].
0049    *
0050    * @param newDescription a brief description of the option.
0051    *
0052    * @param newValue the integer value corresponding to this option.
0053    */
0054   template<typename EnumT> 
0055   SwitchOption(SwitchBase & theSwitch, string newName,
0056            string newDescription, EnumT newValue);
0057 
0058   /**
0059    * Default constructor.
0060    */
0061   SwitchOption() : theValue(-999) {}
0062 
0063   /**
0064    * The description of this option
0065    */
0066   const string & description() const { return theDescription; }
0067 
0068   /**
0069    * The value of this option.
0070    */
0071   long value() const { return theValue; }
0072 
0073   /**
0074    * The value of this option.
0075    */
0076   operator long () const;
0077 
0078 protected:
0079 
0080 private:
0081 
0082   /**
0083    * The description of this option
0084    */
0085   string theDescription;
0086 
0087   /**
0088    * The value of this option.
0089    */
0090   long theValue;
0091 
0092 };
0093 
0094 /**
0095  * The Switch class and its base class SwitchBase defines an interface
0096  * to a class derived from the InterfacedBase, through which simple
0097  * integer member variables can be manuipulated and set to a
0098  * pre-defined set of values (options). Switch is
0099  * templated on the type of the integer member variable and the type
0100  * of the class, and is derived from the InterfaceBase class via
0101  * <code>SwitchBase</code>.
0102  *
0103  * The Switch class has a set of Named SwitchOptions,
0104  * which limits the values possible to set.
0105  *
0106  * For each InterfacedBase class exactly one static Switch object
0107  * should created for each member variable which should be
0108  * interfaced. This object will automatically register itself with the
0109  * BaseRepository class. Also for each Switch object exactly one
0110  * static SwitchOption object should be created for each valid integer
0111  * option.
0112  *
0113  * @see InterfacedBase
0114  * @see InterfacedBase
0115  * @see Named
0116  * 
0117  */
0118 class SwitchBase: public InterfaceBase {
0119 
0120 public:
0121 
0122   /** A map with SwitchOptions indexed by their values. */
0123   typedef map<long, SwitchOption> OptionMap;
0124   /** A map with SwitchOptions indexed by their names. */
0125   typedef map<string, SwitchOption> StringMap;
0126 
0127   /** SwitchOption is a friend. */
0128   friend class SwitchOption;
0129 
0130 public:
0131 
0132   /**
0133    * Standard constructor.
0134    *
0135    * @param newName the name of the interface, may only contain
0136    * letters [a-zA-z0-9_].
0137    *
0138    * @param newDescription a brief description of the interface.
0139    *
0140    * @param newClassName the name of the corresponding class.
0141    *
0142    * @param newTypeInfo the type_info object of the corresponding
0143    * class.
0144    *
0145    * @param depSafe set to true if calls to this interface for one
0146    * object does not influence other objects.
0147    *
0148    * @param readonly if this is set true the interface will not be
0149    * able to manipulate objects of the corresponding class, but will
0150    * still be able to access information.
0151    */
0152   SwitchBase(string newName, string newDescription,
0153          string newClassName, const type_info & newTypeInfo,
0154          bool depSafe, bool readonly)
0155     : InterfaceBase(newName, newDescription, newClassName, 
0156             newTypeInfo, depSafe, readonly) {}
0157 
0158   /**
0159    * The general interface method overriding the one in
0160    * InterfaceBase. For this class, \a action can be any of "set",
0161    * "get", "def" and "setdef" and \a argument should be a something
0162    * which can be read into an integer variable through a stringstream
0163    * with the standard '>>' operator.
0164    */
0165   virtual string exec(InterfacedBase & ib, string action,
0166             string arguments) const;
0167 
0168   /**
0169    * Return a complete description of this switch.
0170    */
0171   virtual string fullDescription(const InterfacedBase & ib) const;
0172 
0173   /**
0174    * Return a code for the type of this switch.
0175    */
0176   virtual string type() const;
0177 
0178   /**
0179    * Set the member variable of \a ib to \a val.
0180    */
0181   virtual void set(InterfacedBase & ib, long val)
0182     const = 0;
0183 
0184   /**
0185    * Return the value of the member variable of \a ib.
0186    */
0187   virtual long get(const InterfacedBase & ib)
0188     const = 0;
0189 
0190   /**
0191    * Return the default value for the member variable of \a ib.
0192    */
0193   virtual long def(const InterfacedBase & ib)
0194     const = 0;
0195 
0196   /**
0197    * Set the member variable of \a ib to its default value.
0198    */
0199   void setDef(InterfacedBase & i) const {
0200     set(i, def(i));
0201   }
0202 
0203   /**
0204    * Check if \a val is among the listed options.
0205    */
0206   bool check(long newValue) const { return member(theOptions, newValue); }
0207 
0208   /**
0209    * Return the map relating options to their values
0210    */
0211   const OptionMap & options() const { return theOptions; }
0212 
0213   /**
0214    * Return a string describing the type of interface to be included
0215    * in the Doxygen documentation.
0216    */
0217   virtual string doxygenType() const;
0218 
0219   /**
0220    * Return a string with the option index and its associated tag.
0221    */
0222   string opttag(long opt) const;
0223 
0224 protected:
0225 
0226   /**
0227    * Register a new option.
0228    */
0229   void registerOption(const SwitchOption & o) {
0230     theOptions[o.value()] = o;
0231     theOptionNames[o.name()] = o;
0232   }
0233 
0234 private:
0235 
0236   /**
0237    * The map relating options to their values
0238    */
0239   OptionMap theOptions;
0240 
0241   /**
0242    * The map relating options to their names
0243    */
0244   StringMap theOptionNames;
0245 
0246 };
0247 
0248 /**
0249  * The Switch class and its base class SwitchBase defines an interface
0250  * to a class derived from the InterfacedBase, through which simple
0251  * integer member variables can be manuipulated and set to a
0252  * pre-defined set of values (options). Switch is templated on the
0253  * type of the integer member variable (also enums and bool are
0254  * allowed) and the type of the class, and is derived from the
0255  * InterfaceBase class via SwitchBase.
0256  *
0257  * The Switch class has a set of Named SwitchOptions,
0258  * which limits the values possible to set.
0259  *
0260  * For each InterfacedBase class exactly one static Switch object
0261  * should created for each member variable which should be
0262  * interfaced. This object will automatically register itself with the
0263  * BaseRepository class. Also for each Switch object exactly one
0264  * static SwitchOption object should be created for each valid integer
0265  * option.
0266  *
0267  * @see InterfacedBase
0268  * @see InterfacedBase
0269  * @see Named
0270  * 
0271  */
0272 template <typename T, typename Int>
0273 class Switch: public SwitchBase {
0274 
0275 public:
0276 
0277   /**
0278    * The declaration of member functions which can be used by this
0279    * Switch interface for the 'set' action.
0280    */
0281   typedef void (T::*SetFn)(Int);
0282   /**
0283    * The declaration of member functions which can be used by this
0284    * Switch interface for the 'get' action.
0285    */
0286   typedef Int (T::*GetFn)() const;
0287 
0288   /**
0289    * Declaration of a direct pointer to the member variable.
0290    */
0291   typedef Int T::* Member;
0292 
0293 public:
0294 
0295   /**
0296    * Standard constructor.
0297    *
0298    * @param newName the name of the interface, may only contain
0299    * letters [a-zA-z0-9_].
0300    *
0301    * @param newDescription a brief description of the interface.
0302    *
0303    * @param newMember a pointer to the member variable. May be null if
0304    * corresponding set/get functions are provided.
0305    *
0306    * @param newDef the default value for the member variable.
0307    *
0308    * @param depSafe set to true if calls to this interface for one
0309    * object does not influence other objects.
0310    *
0311    * @param readonly if this is set true the interface will not be
0312    * able to manipulate objects of the corresponding class, but will
0313    * still be able to access information.
0314    *
0315    * @param newSetFn optional pointer to the member function for the
0316    * 'set' action.
0317    *
0318    * @param newGetFn optional pointer to the member function for the
0319    * 'get' action.
0320    *
0321    * @param newDefFn optional pointer to the member function for the
0322    * 'def' action.
0323    */
0324   Switch(string newName, string newDescription,
0325      Member newMember, Int newDef, bool depSafe = false,
0326      bool readonly = false, SetFn newSetFn = 0, GetFn newGetFn = 0,
0327      GetFn newDefFn = 0)
0328     : SwitchBase(newName, newDescription, ClassTraits<T>::className(),
0329          typeid(T), depSafe, readonly),
0330       theMember(newMember), theDef(newDef), theSetFn(newSetFn),
0331       theGetFn(newGetFn), theDefFn(newDefFn) {}
0332   
0333   /**
0334    * Set the member variable of \a ib to \a val.
0335    */
0336   virtual void set(InterfacedBase & ib, long val) const
0337    ;
0338 
0339   /**
0340    * Return the value of the member variable of \a ib.
0341    */
0342   virtual long get(const InterfacedBase & ib) const;
0343 
0344   /**
0345    * Return the default value for the member variable of \a ib.
0346    */
0347   virtual long def(const InterfacedBase & ib) const;
0348 
0349   /**
0350    * Give a pointer to a member function to be used by 'set()'.
0351    */
0352   void setSetFunction(SetFn sf) { theSetFn = sf; }
0353 
0354   /**
0355    * Give a pointer to a member function to be used by 'get()'.
0356    */
0357   void setGetFunction(GetFn gf) { theGetFn = gf; }
0358 
0359   /**
0360    * Give a pointer to a member function to be used by 'def()'.
0361    */
0362   void setDefaultFunction(GetFn df) { theDefFn = df; }
0363 
0364   /**
0365    * Print a description to be included in the Doxygen documentation
0366    * to the given \a stream.
0367    */
0368   virtual void doxygenDescription(ostream & stream) const;
0369 
0370 private:
0371 
0372   /**
0373    * The pointer to the member variable.
0374    */
0375   Member theMember;
0376 
0377   /**
0378    * Default value to be used if no corresponding member function
0379    * pointers are given.
0380    */
0381   Int theDef;
0382 
0383   /**
0384    * A pointer to a member function to be used by 'set()'.
0385    */
0386   SetFn theSetFn;
0387 
0388   /**
0389    * Pointer to member function to be used by get().
0390    */
0391   GetFn theGetFn;  
0392 
0393   /**
0394    * Pointer to member function to be used by def().
0395    */
0396   GetFn theDefFn;  
0397 
0398 };
0399 
0400 }
0401 
0402 #ifndef ThePEG_TEMPLATES_IN_CC_FILE
0403 #include "Switch.tcc"
0404 #endif
0405 
0406 #endif /* ThePEG_Switch_H */