CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FortranInstance.h
Go to the documentation of this file.
1 #ifndef gen_FortranInstance_h
2 #define gen_FortranInstance_h
3 
4 namespace gen {
5 
6 // the callbacks from Pythia6/Herwig6 which are passed to the FortranInstance
7 extern "C" {
8  void upinit_();
9  void upevnt_();
10  void upveto_(int*);
11 }
12 
14  public:
16  virtual ~FortranInstance();
17 
18  void call(void(&fn)())
19  { InstanceWrapper wrapper(this); fn(); }
20  template<typename T> T call(T(&fn)())
21  { InstanceWrapper wrapper(this); return fn(); }
22  template<typename A> void call(void(&fn)(A), A a)
23  { InstanceWrapper wrapper(this); fn(a); }
24  template<typename T, typename A> T call(T(&fn)(A), A a)
25  { InstanceWrapper wrapper(this); return fn(a); }
26  template<typename A1, typename A2> void call(void(&fn)(A1, A2), A1 a1, A2 a2)
27  { InstanceWrapper wrapper(this); fn(a1, a2); }
28  template<typename T, typename A1, typename A2> T call(T(&fn)(A1, A2), A1 a1, A2 a2)
29  { InstanceWrapper wrapper(this); return fn(a1, a2); }
30 
31  // if a member is instantiated at the beginning of a method,
32  // it makes sure this FortranInstance instance is kept as
33  // current instance during the execution of the method
34  // This wrapper makes the enter()..leave() cycle exception-safe
35  struct InstanceWrapper {
37  { this->instance = instance; instance->enter(); }
38 
40  { instance->leave(); }
41 
43  };
44 
45  // set this instance to be the current one
46  // will throw exception when trying to reenter Herwig twice
47  virtual void enter();
48 
49  // leave instance
50  // will throw if the currently running instance does not match
51  virtual void leave();
52 
53  // get the currently running instance (from enterInstance)
54  // intended for callbacks from Fortran
55  template<typename T>
56  static T *getInstance()
57  {
58  T *instance = dynamic_cast<T*>(currentInstance);
59  if (!instance)
61  return instance;
62  }
63 
64  // Fortran callbacks
65  virtual void upInit();
66  virtual void upEvnt();
67  virtual bool upVeto();
68 
69  private:
70  // list all the Fortran callbacks here
71  friend void gen::upinit_();
72  friend void gen::upevnt_();
73  friend void gen::upveto_(int*);
74 
75  // internal methods
76  static void throwMissingInstance();
77 
78  // how many times enter() was called
79  // this is to make sure leave() will release the instance
80  // after the same number of calls
81  // nesting can in theory occur if the Fortran callback calls
82  // into Herwig again
84 
85  // this points to the Herwig instance that is currently being executed
87 };
88 
89 } // namespace gen
90 
91 #endif // gen_FortranInstance_h
void call(void(&fn)())
static PFTauRenderPlugin instance
static void throwMissingInstance()
void upinit_()
void upevnt_()
InstanceWrapper(FortranInstance *instance)
T call(T(&fn)(A1, A2), A1 a1, A2 a2)
virtual void enter()
static T * getInstance()
T call(T(&fn)(A), A a)
static FortranInstance * currentInstance
void call(void(&fn)(A), A a)
virtual void leave()
void call(void(&fn)(A1, A2), A1 a1, A2 a2)
double a
Definition: hdecay.h:121
void upveto_(int *)
long double T
static HepMC::HEPEVT_Wrapper wrapper