CMS 3D CMS Logo

FortranInstance.h
Go to the documentation of this file.
1 #ifndef gen_FortranInstance_h
2 #define gen_FortranInstance_h
3 
4 #include <string>
5 
6 namespace gen {
7 
8  // the callbacks from Pythia6/Herwig6 which are passed to the FortranInstance
9  extern "C" {
10  void upinit_();
11  void upevnt_();
12  void upveto_(int *);
13  }
14 
16  public:
18  virtual ~FortranInstance() noexcept(false);
19 
20  void call(void (&fn)()) {
22  fn();
23  }
24  template <typename T>
25  T call(T (&fn)()) {
27  return fn();
28  }
29  template <typename A>
30  void call(void (&fn)(A), A a) {
32  fn(a);
33  }
34  template <typename T, typename A>
35  T call(T (&fn)(A), A a) {
37  return fn(a);
38  }
39  template <typename A1, typename A2>
40  void call(void (&fn)(A1, A2), A1 a1, A2 a2) {
42  fn(a1, a2);
43  }
44  template <typename T, typename A1, typename A2>
45  T call(T (&fn)(A1, A2), A1 a1, A2 a2) {
47  return fn(a1, a2);
48  }
49 
50  // if a member is instantiated at the beginning of a method,
51  // it makes sure this FortranInstance instance is kept as
52  // current instance during the execution of the method
53  // This wrapper makes the enter()..leave() cycle exception-safe
54  struct InstanceWrapper {
56  this->instance = instance;
57  instance->enter();
58  }
59 
61 
63  };
64 
65  // set this instance to be the current one
66  // will throw exception when trying to reenter Herwig twice
67  virtual void enter();
68 
69  // leave instance
70  // will throw if the currently running instance does not match
71  virtual void leave();
72 
73  // get the currently running instance (from enterInstance)
74  // intended for callbacks from Fortran
75  template <typename T>
76  static T *getInstance() {
77  T *instance = dynamic_cast<T *>(currentInstance);
78  if (!instance)
80  return instance;
81  }
82 
83  // Fortran callbacks
84  virtual void upInit();
85  virtual void upEvnt();
86  virtual bool upVeto();
87 
89 
90  private:
91  // list all the Fortran callbacks here
92  friend void gen::upinit_();
93  friend void gen::upevnt_();
94  friend void gen::upveto_(int *);
95 
96  // internal methods
97  static void throwMissingInstance();
98 
99  // how many times enter() was called
100  // this is to make sure leave() will release the instance
101  // after the same number of calls
102  // nesting can in theory occur if the Fortran callback calls
103  // into Herwig again
105 
106  // this points to the Herwig instance that is currently being executed
108  };
109 
110 } // namespace gen
111 
112 #endif // gen_FortranInstance_h
gen::FortranInstance::getInstance
static T * getInstance()
Definition: FortranInstance.h:76
gen::FortranInstance
Definition: FortranInstance.h:15
gen::FortranInstance::enter
virtual void enter()
Definition: FortranInstance.cc:43
gen::FortranInstance::call
T call(T(&fn)(A), A a)
Definition: FortranInstance.h:35
funct::false
false
Definition: Factorize.h:29
gen::FortranInstance::call
T call(T(&fn)(A1, A2), A1 a1, A2 a2)
Definition: FortranInstance.h:45
gen::FortranInstance::kFortranInstance
static const std::string kFortranInstance
Definition: FortranInstance.h:88
gen::FortranInstance::leave
virtual void leave()
Definition: FortranInstance.cc:62
wrapper
static HepMC::HEPEVT_Wrapper wrapper
Definition: BeamHaloProducer.cc:47
gen::FortranInstance::upEvnt
virtual void upEvnt()
Definition: FortranInstance.cc:100
testProducerWithPsetDescEmpty_cfi.a2
a2
Definition: testProducerWithPsetDescEmpty_cfi.py:35
gen::FortranInstance::call
void call(void(&fn)(A), A a)
Definition: FortranInstance.h:30
gen::FortranInstance::call
void call(void(&fn)(A1, A2), A1 a1, A2 a2)
Definition: FortranInstance.h:40
gen::FortranInstance::InstanceWrapper
Definition: FortranInstance.h:54
gen::FortranInstance::FortranInstance
FortranInstance()
Definition: FortranInstance.h:17
gen::upveto_
void upveto_(int *)
Definition: FortranInstance.cc:21
gen
Definition: PythiaDecays.h:13
gen::FortranInstance::InstanceWrapper::InstanceWrapper
InstanceWrapper(FortranInstance *instance)
Definition: FortranInstance.h:55
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
gen::upevnt_
void upevnt_()
Definition: FortranInstance.cc:19
gen::FortranInstance::call
void call(void(&fn)())
Definition: FortranInstance.h:20
gen::FortranInstance::InstanceWrapper::~InstanceWrapper
~InstanceWrapper()
Definition: FortranInstance.h:60
a
double a
Definition: hdecay.h:119
gen::FortranInstance::call
T call(T(&fn)())
Definition: FortranInstance.h:25
gen::FortranInstance::~FortranInstance
virtual ~FortranInstance() noexcept(false)
Definition: FortranInstance.cc:31
gen::FortranInstance::instanceNesting
int instanceNesting
Definition: FortranInstance.h:104
A
gen::FortranInstance::throwMissingInstance
static void throwMissingInstance()
Definition: FortranInstance.cc:83
instance
static PFTauRenderPlugin instance
Definition: PFTauRenderPlugin.cc:70
gen::FortranInstance::InstanceWrapper::instance
FortranInstance * instance
Definition: FortranInstance.h:62
T
long double T
Definition: Basic3DVectorLD.h:48
gen::FortranInstance::upInit
virtual void upInit()
Definition: FortranInstance.cc:92
personalPlayback.fn
fn
Definition: personalPlayback.py:515
gen::FortranInstance::upVeto
virtual bool upVeto()
Definition: FortranInstance.cc:108
gen::upinit_
void upinit_()
Definition: FortranInstance.cc:17
gen::FortranInstance::currentInstance
static FortranInstance * currentInstance
Definition: FortranInstance.h:107