Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // DescribeClass.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_DescribeClass_H
0010 #define ThePEG_DescribeClass_H
0011 
0012 #include "ThePEG/Utilities/ClassDescription.h"
0013 
0014 namespace ThePEG {
0015 
0016 /**
0017  * Helper class for multiple base classes. If a class to be described
0018  * by DescribeClass has multiple base classes, B1 and B2, they should
0019  * be encoded by BaseClasses<B1,B2>. This can be done for up to four
0020  * base classes.
0021  */
0022 template <typename BaseT1, typename BaseT2 = int,
0023           typename BaseT3 = int, typename BaseT4 = int>
0024 struct BaseClasses {};
0025 
0026 /**
0027  * Traits class used by DescribeCLassT for transparent handling of one
0028  * base class or a several base classes encoded by BaseClasses.
0029  */
0030 template <typename T>
0031 struct BaseClassesTraits {
0032 
0033   /** The first base class */
0034   typedef T Base1;
0035 
0036   /** The second base class */
0037   typedef int Base2;
0038 
0039   /** The third base class */
0040   typedef int Base3;
0041 
0042   /** The fourth base class */
0043   typedef int Base4;
0044 
0045 };
0046 
0047 /**
0048  * Traits class used by DescribeCLassT for transparent handling of one
0049  * base class or a several base classes encoded by BaseClasses.
0050  */
0051 template <typename BaseT1, typename BaseT2, typename BaseT3, typename BaseT4>
0052 struct BaseClassesTraits< BaseClasses<BaseT1,BaseT2,BaseT3,BaseT4> > {
0053 
0054   /** The first base class */
0055   typedef BaseT1 Base1;
0056 
0057   /** The second base class */
0058   typedef BaseT2 Base2;
0059 
0060   /** The third base class */
0061   typedef BaseT3 Base3;
0062 
0063   /** The fourth base class */
0064   typedef BaseT4 Base4;
0065 
0066 };
0067 
0068 /**
0069  * Helper class used by DescribeClassT for transparent handling of
0070  * classes with and without persistent I/O functions.
0071  */
0072 template <typename T, bool NoPIO>
0073 struct DescribeClassPIOHelper {
0074 
0075   /**
0076    * Call standard output function.
0077    */
0078   static void output(const T & t, PersistentOStream & os) {
0079     t.persistentOutput(os);
0080   }
0081 
0082   /**
0083    * Call standard input function.
0084    */
0085   static void input(T & t, PersistentIStream & is, int oldVersion) {
0086     t.persistentInput(is, oldVersion);
0087   }
0088 
0089 };
0090 
0091 /**
0092  * Helper class used by DescribeClassT for transparent handling of
0093  * classes with and without persistent I/O functions.
0094  */
0095 template <typename T>
0096 struct DescribeClassPIOHelper<T,true> {
0097 
0098   /**
0099    * Do nothing as T has no persistent I/O functions.
0100    */
0101   static void output(const T &, PersistentOStream &) {}
0102 
0103   /**
0104    * Do nothing as T has no persistent I/O functions.
0105    */
0106   static void input(T &, PersistentIStream &, int) {}
0107 
0108 };
0109 
0110 /** 
0111  * Helper class used by DescribeClassT for transparent handling of
0112  * abstract and concrete classes.
0113  */
0114 template <typename T, bool abstract>
0115 struct DescribeClassAbstractHelper {
0116 
0117   /**
0118    * Default-creat an object.
0119    */
0120   static typename ThePEG::Ptr<T>::pointer create() {
0121     return new_ptr(T());
0122   }
0123 };
0124 
0125 /** 
0126  * Helper class used y DescribeClassT for transparent handling of
0127  * abstract and concrete classes.
0128  */
0129 template <typename T>
0130 struct DescribeClassAbstractHelper<T,true> {
0131 
0132   /**
0133    * Throw an exception as the class is bastract.
0134    */
0135   static typename ThePEG::Ptr<T>::pointer create() {
0136     throw std::logic_error("Tried to instantiate abstract class " +
0137                ClassTraits<T>::className());
0138    }
0139 
0140 };
0141 
0142 
0143 
0144 
0145 /**
0146  * DescribeClassT and its derived companion classes DescribeClass
0147  * DescribeAbstractClass, DescribeNoPIOClass and
0148  * DescribeAbstractNoPIOClass, is a simplified interface to the type
0149  * information system in ThePEG. For simple classes there is no need
0150  * to specialize the ClassTraits and BaseClassTrait classes and to
0151  * have a static member variable of ClassDescription in the class (as
0152  * in the full ThePEG type info system). Instead it is enough to have
0153  * one statically initialized variable of one of the DescraibeClass
0154  * classes for each class. The Abstract and NoPIO versions of this
0155  * class should be used for abstract classes and classes without
0156  * persistent I/O functions respectively.
0157  */
0158 template <typename T, typename BaseT, bool Abstract = false, bool NoPIO = false>
0159 class DescribeClassT: public ClassDescriptionBase {
0160 
0161 public:
0162 
0163   ThePEG_DECLARE_TEMPLATE_POINTERS(T,TPtr);
0164   ThePEG_DECLARE_POINTERS(Base,BPtr);
0165 
0166   /**
0167    * Constructor taking the name of the class, the dynamic library
0168    * where it is located and an optional version number as argument.
0169    */
0170   DescribeClassT(string cname, string lib, int vers = 0)
0171     : ClassDescriptionBase(cname, typeid(T), vers, lib, Abstract) {
0172     DescriptionList::Register(*this);
0173     T::Init();
0174   }
0175 
0176   /**
0177    * The descructor.
0178    */
0179   virtual ~DescribeClassT() {}
0180 
0181   /**
0182    * Set up the base class information for this object.
0183    */
0184   virtual void setup() {
0185     DescriptionVector bases;
0186     const ClassDescriptionBase * b =
0187       DescriptionList::find(typeid(typename BaseClassesTraits<BaseT>::Base1));
0188     if ( b ) bases.push_back(b);
0189     b = DescriptionList::find(typeid(typename BaseClassesTraits<BaseT>::Base2));
0190     if ( b ) bases.push_back(b);
0191     b = DescriptionList::find(typeid(typename BaseClassesTraits<BaseT>::Base3));
0192     if ( b ) bases.push_back(b);
0193     b = DescriptionList::find(typeid(typename BaseClassesTraits<BaseT>::Base4));
0194     if ( b ) bases.push_back(b);
0195     baseClasses(bases.begin(), bases.end());
0196   }
0197 
0198   /**
0199    * Default-create an object.
0200    */
0201   virtual BPtr create() const {
0202     return DescribeClassAbstractHelper<T,Abstract>::create();
0203   }
0204 
0205   /**
0206    * Call standard output function.
0207    */
0208   virtual void output(tcBPtr o, PersistentOStream & os) const {
0209     tcTPtr t = dynamic_ptr_cast<tcTPtr>(o);
0210     DescribeClassPIOHelper<T,NoPIO>::output(*t, os);
0211   }
0212 
0213   /**
0214    * Call standard input function.
0215    */
0216   virtual void input(tBPtr o, PersistentIStream & is, int oldVersion) const {
0217     tTPtr t = dynamic_ptr_cast<tTPtr>(o);
0218     DescribeClassPIOHelper<T,NoPIO>::input(*t, is, oldVersion);
0219   }
0220 
0221 };
0222    
0223 /**
0224  * DescribeClass and its companion classes DescribeAbstractClass,
0225  * DescribeNoPIOClass and DescribeAbstractNoPIOClass, is a simplified
0226  * interface to type information system in ThePEG. For simple classes
0227  * there is no need to specialize the ClassTraits and BaseClassTrait
0228  * classes and to have a static member variable of ClassDescription in
0229  * the class (as in the full ThePEG type info system). Instead it is
0230  * enough to have one statically initialized variable of one of the
0231  * DescraibeClass classes for each class. The Abstract and NoPIO
0232  * versions of this class may be used for abstract classes and
0233  * classes without persistent I/O functions respectively.
0234  *
0235  * The template arguments are as follows:
0236  * T       : the class being described. 
0237  * BaseT   : The base class of T. If several base classes these should be
0238  *           encoded s template arguments to the BaseClasses class
0239  *           (at most four base classes can be encoded).
0240  * Abstract: shoule be set to true if class T is abstract.
0241  * NoPIO   : shoule be set to true if class T does not persistent I/O functions.
0242  */
0243 template <typename T, typename BaseT = int,
0244   bool Abstract = false, bool NoPIO = false>
0245 class DescribeClass: public DescribeClassT<T,BaseT,Abstract,NoPIO> {
0246 
0247 public:
0248 
0249   /**
0250    * Constructor taking the name of the class, the dynamic library
0251    * where it is located and an optional version number as argument.
0252    */
0253   DescribeClass(string cname, string lib, int vers = 0)
0254     : DescribeClassT<T,BaseT,Abstract,NoPIO>(cname, lib, vers) {}
0255 
0256 };
0257    
0258 /**
0259  * DescribeClass and its companion classes DescribeAbstractClass,
0260  * DescribeNoPIOClass and DescribeAbstractNoPIOClass, is a simplified
0261  * interface to type information system in ThePEG. For simple classes
0262  * there is no need to specialize the ClassTraits and BaseClassTrait
0263  * classes and to have a static member variable of ClassDescription in
0264  * the class (as in the full ThePEG type info system). Instead it is
0265  * enough to have one statically initialized variable of one of the
0266  * DescraibeClass classes for each class. The Abstract and NoPIO
0267  * versions of this class may be used for abstract classes and
0268  * classes without persistent I/O functions respectively.
0269  *
0270  * The template arguments are as follows:
0271  * T       : the class being described. 
0272  * BaseT   : The base class of T. If several base classes these should be
0273  *           encoded s template arguments to the BaseClasses class
0274  *           (at most four base classes can be encoded).
0275  */
0276 template <typename T, typename BaseT = int>
0277 class DescribeNoPIOClass: public DescribeClassT<T,BaseT,false,true> {
0278 
0279 public:
0280 
0281   /**
0282    * Constructor taking the name of the class, the dynamic library
0283    * where it is located and an optional version number as argument.
0284    */
0285   DescribeNoPIOClass(string cname, string lib, int vers = 0)
0286     : DescribeClassT<T,BaseT,false,true>(cname, lib, vers) {}
0287 
0288 };
0289    
0290 /**
0291  * DescribeClass and its companion classes DescribeAbstractClass,
0292  * DescribeNoPIOClass and DescribeAbstractNoPIOClass, is a simplified
0293  * interface to type information system in ThePEG. For simple classes
0294  * there is no need to specialize the ClassTraits and BaseClassTrait
0295  * classes and to have a static member variable of ClassDescription in
0296  * the class (as in the full ThePEG type info system). Instead it is
0297  * enough to have one statically initialized variable of one of the
0298  * DescraibeClass classes for each class. The Abstract and NoPIO
0299  * versions of this class may be used for abstract classes and
0300  * classes without persistent I/O functions respectively.
0301  *
0302  * The template arguments are as follows:
0303  * T       : the class being described. 
0304  * BaseT   : The base class of T. If several base classes these should be
0305  *           encoded s template arguments to the BaseClasses class
0306  *           (at most four base classes can be encoded).
0307  */
0308 template <typename T, typename BaseT = int>
0309 class DescribeAbstractClass: public DescribeClassT<T,BaseT,true,false> {
0310 
0311 public:
0312 
0313   /**
0314    * Constructor taking the name of the class, the dynamic library
0315    * where it is located and an optional version number as argument.
0316    */
0317   DescribeAbstractClass(string cname, string lib, int vers = 0)
0318     :DescribeClassT<T,BaseT,true,false>(cname, lib, vers) {}
0319 
0320 };
0321    
0322 /**
0323  * DescribeClass and its companion classes DescribeAbstractClass,
0324  * DescribeNoPIOClass and DescribeAbstractNoPIOClass, is a simplified
0325  * interface to type information system in ThePEG. For simple classes
0326  * there is no need to specialize the ClassTraits and BaseClassTrait
0327  * classes and to have a static member variable of ClassDescription in
0328  * the class (as in the full ThePEG type info system). Instead it is
0329  * enough to have one statically initialized variable of one of the
0330  * DescraibeClass classes for each class. The Abstract and NoPIO
0331  * versions of this class may be used for abstract classes and
0332  * classes without persistent I/O functions respectively.
0333  *
0334  * The template arguments are as follows:
0335  * T       : the class being described. 
0336  * BaseT   : The base class of T. If several base classes these should be
0337  *           encoded s template arguments to the BaseClasses class
0338  *           (at most four base classes can be encoded).
0339  */
0340 template <typename T, typename BaseT = int>
0341 class DescribeAbstractNoPIOClass: public DescribeClassT<T,BaseT,true,true> {
0342 
0343 public:
0344 
0345   /**
0346    * Constructor taking the name of the class, the dynamic library
0347    * where it is located and an optional version number as argument.
0348    */
0349   DescribeAbstractNoPIOClass(string cname, string lib, int vers = 0)
0350     :DescribeClassT<T,BaseT,true,true>(cname, lib, vers) {}
0351 
0352 };
0353    
0354 }
0355 
0356 
0357 #endif /* ThePEG_DescribeClass_H */