Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // ClassTraits.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_ClassTraits_H
0010 #define ThePEG_ClassTraits_H
0011 // This is the declaration of the ClassTraitsBase, ClassTraits and
0012 // BaseClassTraits classes.
0013 
0014 
0015 #include "ThePEG/Config/ThePEG.h"
0016 // #include "ClassTraits.fh"
0017 // #include "ClassTraits.xh"
0018 #include "ThePEG/Persistency/PersistentOStream.fh"
0019 #include "ThePEG/Persistency/PersistentIStream.fh"
0020 #include "DescriptionList.h"
0021 
0022 namespace ThePEG {
0023 
0024 /**
0025  * ClassTraitsType is an empty, non-polymorphic, base class. It is
0026  * used as a base class of all class traits classes in order to group
0027  * them together in the documentation. It currently serves no other
0028  * purpose.
0029  */
0030 struct ClassTraitsType: public TraitsType {};
0031 
0032 /**
0033  * The templated ClassTraitsBase class defines a set of default
0034  * information about classes used by ThePEG. By default, the
0035  * ClassTraits simply inherits from ClassTraitsBase, but it can be
0036  * specialized to override the static member functions.
0037  *
0038  * The information that should be made available is:<BR>
0039  * <code>create()</code> creates an object of the described class,<BR>
0040  * <code>className()</code> returns the platform-independent name of
0041  * the class,<BR> <code>version()</code> return the version
0042  * number,<BR> <code>output()</code> output the members of the class
0043  * to a PersistentOStream,<BR> <code>input()</code> reads the members
0044  * of the class from a PersistentIStream and<BR> <code>cast()</code>
0045  * dynamically cast a pointer to a public base class to a pointer to
0046  * the class.
0047  *
0048  * @see PersistentOStream
0049  * @see PersistentIStream
0050  */
0051 template <typename T>
0052 struct ClassTraitsBase: public ClassTraitsType {
0053 
0054   ThePEG_DECLARE_TEMPLATE_POINTERS(T,TPtr);
0055   ThePEG_DECLARE_POINTERS(Base,BPtr);
0056 
0057   /**
0058    * Create a T object and return a smart pointer to it.
0059    */
0060   static TPtr create() { return TPtr::Create(); }
0061 
0062   /**
0063    * Return the name of class T.
0064    */
0065   //  static string className() { return T::className(); }
0066   static string className() {
0067     return DescriptionList::className(typeid(T));
0068   }
0069 
0070   /**
0071    * Return the version of class T
0072    */
0073   static int version() {
0074     return DescriptionList::version(typeid(T));
0075   }
0076 
0077 
0078   /**
0079    * The name of a file containing the dynamic library where the class
0080    * T is implemented. It may also include several, space separated,
0081    * libraries if the class T depends on other classes (base classes
0082    * excepted). In this case the listed libraries will be dynamically
0083    * linked in the order they are specified.
0084    */
0085   static string library() {
0086     return DescriptionList::library(typeid(T));
0087   }
0088 
0089   /**
0090    * Write the T part of an object to a persistent stream.
0091    */
0092   static void output(tcTPtr t, PersistentOStream & os) {
0093     t->persistentOutput(os);
0094   }
0095 
0096   /**
0097    * Read the T part of an object from a persistent stream.
0098    */
0099   static void input(tTPtr t, PersistentIStream & is, int oldVersion) {
0100     t->persistentInput(is, oldVersion);
0101   }
0102 
0103   /**
0104    * Perform a dynamic cast from the given pointer to a pointer to T.
0105    */
0106   static TPtr cast(BPtr b) { return dynamic_ptr_cast<TPtr>(b); }  
0107 
0108   /**
0109    * Perform a dynamic cast from the given const pointer to a pointer
0110    * to const T.
0111    */
0112   static cTPtr cast(cBPtr b) { return dynamic_ptr_cast<cTPtr>(b); }
0113 
0114   /**
0115    * Perform a dynamic cast from the given transient pointer to a
0116    * transient pointer to T.
0117    */
0118   static tTPtr cast(tBPtr b) { return dynamic_ptr_cast<tTPtr>(b); }  
0119 
0120   /**
0121    * Perform a dynamic cast from the given transient const pointer to
0122    * a transient pointer to const T.
0123    */
0124   static tcTPtr cast(tcBPtr b) { return dynamic_ptr_cast<tcTPtr>(b); }
0125 
0126 };
0127 
0128 /**
0129  * The default concrete implementation of ClassTraitsBase. This
0130  * templated class may be specialized for any class if the default
0131  * implementation is not sufficient.
0132  */
0133 template <typename T>
0134 struct ClassTraits: public ClassTraitsBase<T> {};
0135 
0136 /**
0137  * ClassTraits specialization for double
0138  */
0139 template<>
0140 struct ClassTraits<double> : public ClassTraitsBase<double> {
0141   static string className() { return "double"; }
0142 };
0143 
0144 /**
0145  * BaseClassTraits describes the base classes of the templated class.
0146  * BaseClassTraits should be specialized once for each
0147  * base class of a class to be described. The specializations should
0148  * contain a typedef so that
0149  * <code>BaseClassTraits<T>::NthBase</code> is made an alias for
0150  * the first base class of <code>T</code>,
0151  * <code>BaseClassTraits<T>::NthBase</code> the second base
0152  * class and so on. The typedef defaults to <code>int</code> which means
0153  * no base class.
0154  */
0155 template <typename Derived, int BaseN>
0156 struct BaseClassTrait: public ClassTraitsType {
0157   /**
0158    * The type of the <code>BaseN</code>'th base class (int means there
0159    * are no further base classes).
0160    */
0161   typedef int NthBase;
0162 };
0163 
0164 }
0165 
0166 #define ThePEG_DECLARE_BASE_CLASS_TRAITS_1(Class,Base)                     \
0167 /** This template specialization informs ThePEG about the                  \
0168  *  base classes of Class. */                                              \
0169 template <>                                                                \
0170 struct BaseClassTrait<Class,1>: public ClassTraitsType {                   \
0171   /** Typedef of the first base class of Class. */                         \
0172   typedef Base NthBase;                                                    \
0173 };                                                                         \
0174 
0175 #define ThePEG_DECLARE_BASE_CLASS_TRAITS_2(Class,Base1,Base2)              \
0176 /** This template specialization informs ThePEG about the                  \
0177  *  base classes of Class. */                                              \
0178 template <>                                                                \
0179 struct BaseClassTrait<Class,1>: public ClassTraitsType {                   \
0180   /** Typedef of the first base class of Class. */                         \
0181   typedef Base1 NthBase;                                                   \
0182 };                                                                         \
0183 /** This template specialization informs ThePEG about the                  \
0184  *  base classes of Class. */                                              \
0185 template <>                                                                \
0186 struct BaseClassTrait<Class,2>: public ClassTraitsType {                   \
0187   /** Typedef of the second base class of Class. */                        \
0188   typedef Base2 NthBase;                                                   \
0189 };                                                                         \
0190 
0191 #define ThePEG_DECLARE_NAMED_DYNAMIC_CLASS_TRAITS_(Class,Name,Lib)         \
0192 /** This template specialization informs ThePEG about the name of          \
0193  *  the Class class and the shared object where it is defined. */          \
0194 template <>                                                                \
0195 struct ClassTraits<Class>:                                                 \
0196     public ClassTraitsBase<Class> {                                        \
0197   /** Return a platform-independent class name */                          \
0198   static string className() { return Name; }                               \
0199   /**                                                                      \
0200    * The name of a file containing the dynamic library where the class     \
0201    * T is implemented. It may also include several, space separated,       \
0202    * libraries if the class T depends on other classes (base classes       \
0203    * excepted). In this case the listed libraries will be dynamically      \
0204    * linked in the order they are specified.                               \
0205    */                                                                      \
0206   static string library() { return Lib; }                                  \
0207 }                                                                          \
0208 
0209 
0210 
0211 #define ThePEG_DECLARE_CLASS_TRAITS(Class,Base)                           \
0212 ThePEG_DECLARE_BASE_CLASS_TRAITS_1(Class,Base)                            \
0213 ThePEG_DECLARE_NAMED_DYNAMIC_CLASS_TRAITS_(Class,"ThePEG::" #Class,"")    \
0214 
0215 #define ThePEG_DECLARE_DYNAMIC_CLASS_TRAITS(Class,Base,Lib)               \
0216 ThePEG_DECLARE_BASE_CLASS_TRAITS_1(Class,Base)                            \
0217 ThePEG_DECLARE_NAMED_DYNAMIC_CLASS_TRAITS_(Class,"ThePEG::" #Class,Lib)   \
0218 
0219 #define ThePEG_DECLARE_CLASS_TRAITS_2(Class,Base1,Base2)                  \
0220 ThePEG_DECLARE_BASE_CLASS_TRAITS_2(Class,Base1,Base2)                     \
0221 ThePEG_DECLARE_NAMED_DYNAMIC_CLASS_TRAITS_(Class,"ThePEG/::" #Class,"")    \
0222 
0223 #define ThePEG_DECLARE_DYNAMIC_CLASS_TRAITS_2(Class,Base1,Base2,Lib)      \
0224 ThePEG_DECLARE_BASE_CLASS_TRAITS_2(Class,Base1,Base2)                     \
0225 ThePEG_DECLARE_NAMED_DYNAMIC_CLASS_TRAITS_(Class,"ThePEG::" #Class,Lib)   \
0226 
0227 #define ThePEG_DECLARE_NAMED_CLASS_TRAITS(Class,Base,Name)                \
0228 ThePEG_DECLARE_BASE_CLASS_TRAITS_1(Class,Base)                            \
0229 ThePEG_DECLARE_NAMED_DYNAMIC_CLASS_TRAITS_(Class,Name,"")                 \
0230 
0231 #define ThePEG_DECLARE_NAMED_DYNAMIC_CLASS_TRAITS(Class,Base,Name,Lib)    \
0232 ThePEG_DECLARE_BASE_CLASS_TRAITS_1(Class,Base)                            \
0233 ThePEG_DECLARE_NAMED_DYNAMIC_CLASS_TRAITS_(Class,Name,Lib)                \
0234 
0235 #define ThePEG_DECLARE_NAMED_CLASS_TRAITS_2(Class,Base1,Base2,Name)       \
0236 ThePEG_DECLARE_BASE_CLASS_TRAITS_2(Class,Base1,Base2)                     \
0237 ThePEG_DECLARE_NAMED_DYNAMIC_CLASS_TRAITS_(Class,Name,"")                 \
0238 
0239 #endif /* ThePEG_ClassTraits_H */