ThePEG  1.8.0
ClassDescription.h
1 // -*- C++ -*-
2 //
3 // ClassDescription.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_ClassDescription_H
10 #define ThePEG_ClassDescription_H
11 
12 #include "ThePEG/Config/ThePEG.h"
13 #include "ClassDescription.fh"
14 #include "ThePEG/Utilities/Named.h"
15 #include "ThePEG/Persistency/PersistentOStream.fh"
16 #include "ThePEG/Persistency/PersistentIStream.fh"
17 #include "ClassTraits.h"
18 #include "DescriptionList.h"
19 
20 namespace ThePEG {
21 
63 class ClassDescriptionBase: public Named {
64 
65 public:
66 
68  typedef vector<const ClassDescriptionBase *> DescriptionVector;
69 
70 protected:
71 
81  ClassDescriptionBase(string newName,
82  const type_info & newInfo,
83  int newVersion,
84  string newLibrary,
85  bool abst)
86  : Named(newName), theVersion(newVersion), theLibrary(newLibrary),
87  theInfo(newInfo), isAbstract(abst), done(false) {}
88 
89 public:
90 
94  virtual ~ClassDescriptionBase();
95 
99  const type_info & info() const { return theInfo; }
100 
104  int version() const { return theVersion; }
105 
110  string library() const { return theLibrary; }
111 
115  bool check() const { return done; }
116 
121  const DescriptionVector & descriptions() const { return theBaseClasses; }
122 
126  virtual void setup() = 0;
127 
131  virtual BPtr create() const = 0;
132 
139  virtual void output(tcBPtr b, PersistentOStream & os) const = 0;
140 
148  virtual void input(tBPtr b, PersistentIStream & is, int oldVersion) const = 0;
149 
154  bool isA(const ClassDescriptionBase & base) const;
155 
159  bool abstract() const { return isAbstract; }
160 
161 protected:
162 
169  void baseClasses(DescriptionVector::iterator first,
170  DescriptionVector::iterator last)
171  {
172  theBaseClasses = DescriptionVector(first, last);
173  done = true;
174  }
175 
176 private:
177 
182 
186  string theLibrary;
187 
191  const type_info & theInfo;
192 
197 
202 
206  bool done;
207 
208 };
209 
214 template <typename T, int IBase,
215  typename B = typename BaseClassTrait<T,IBase>::NthBase>
218  static void addBases(vector<const ClassDescriptionBase *> & c){
219  const ClassDescriptionBase * b = DescriptionList::find(typeid(B));
220  if ( !b ) return;
221  c.push_back(b);
223  }
224 };
225 
232 template <typename T, int IBase>
233 struct ClassDescriptionHelper<T, IBase, int> {
235  static void addBases(vector<const ClassDescriptionBase *> & ) {}
236 };
237 
244 template <typename T>
246 
247 public:
248 
251 
252 public:
253 
259  : ClassDescriptionBase(Traits::className(), typeid(T), Traits::version(),
260  Traits::library(), abst)
261  {
263  T::Init();
264  }
265 
266 
271 
275  virtual void setup() {
276  DescriptionVector bases;
278  baseClasses(bases.begin(), bases.end());
279  }
280 
281 };
282 
287 template <typename T>
289 
290 public:
291 
294 
295 public:
296 
301 
306  virtual BPtr create() const {
307  throw std::logic_error("Tried to instantiate virtual class " + Named::name());
308  }
309 
316  virtual void output(tcBPtr b, PersistentOStream & os) const {
318  }
319 
327  virtual void input(tBPtr b, PersistentIStream & is,
328  int oldVersion) const {
329  Traits::input(Traits::cast(b), is, oldVersion);
330  }
331 
332 };
333 
338 template <typename T>
340 
341 public:
342 
345 
346 public:
347 
352 
356  virtual BPtr create() const { return Traits::create(); }
357 
364  virtual void output(tcBPtr b, PersistentOStream & os) const {
366  }
367 
375  virtual void input(tBPtr b, PersistentIStream & is,
376  int oldVersion) const {
377  Traits::input(Traits::cast(b), is, oldVersion);
378  }
379 
380 };
381 
386 template <typename T>
388 
389 public:
390 
393 
394 public:
395 
400 
404  virtual BPtr create() const { return Traits::create(); }
405 
409  virtual void output(tcBPtr, PersistentOStream &) const {}
410 
414  virtual void input(tBPtr, PersistentIStream &, int) const {}
415 
416 };
417 
422 template <typename T>
424 
425 public:
426 
429 
430 public:
431 
436 
441  virtual BPtr create() const {
442  throw std::logic_error("Tried to instantiate virtual class " + Named::name());
443  }
444 
448  virtual void output(tcBPtr, PersistentOStream & ) const {}
449 
453  virtual void input(tBPtr, PersistentIStream &, int) const {}
454 
455 };
456 
457 }
458 
459 #define ThePEG_DECLARE_CLASS_DESCRIPTION(Class) \
460  \
461 static ClassDescription<Class> init ## Class \
462 
463 #define ThePEG_DECLARE_ABSTRACT_CLASS_DESCRIPTION(Class) \
464  \
465 static AbstractClassDescription<Class> init ## Class \
466 
467 #define ThePEG_DECLARE_NOPIO_CLASS_DESCRIPTION(Class) \
468  \
469 static NoPIOClassDescription<Class> init ## Class \
470 
471 #define ThePEG_DECLARE_ABSTRACT_NOPIO_CLASS_DESCRIPTION(Class) \
472  \
473 static AbstractNoPIOClassDescription<Class> init ## Class \
474 
475 #define ThePEG_IMPLEMENT_CLASS_DESCRIPTION(Class) \
476 ClassDescription<Class> Class::init ## Class \
477 
478 #define ThePEG_IMPLEMENT_ABSTRACT_CLASS_DESCRIPTION(Class) \
479 AbstractClassDescription<Class> Class::init ## Class \
480 
481 #define ThePEG_IMPLEMENT_NOPIO_CLASS_DESCRIPTION(Class) \
482 NoPIOClassDescription<Class> Class::init ## Class \
483 
484 #define ThePEG_IMPLEMENT_ABSTRACT_NOPIO_CLASS_DESCRIPTION(Class) \
485 AbstractNoPIOClassDescription<Class> Class::init ## Class \
486 
487 #endif /* ThePEG_ClassDescription_H */