File indexing completed on 2024-04-06 12:13:25
0001 #ifndef gen_FortranInstance_h
0002 #define gen_FortranInstance_h
0003
0004 #include <string>
0005
0006 namespace gen {
0007
0008
0009 extern "C" {
0010 void upinit_();
0011 void upevnt_();
0012 void upveto_(int *);
0013 }
0014
0015 class FortranInstance {
0016 public:
0017 FortranInstance() : instanceNesting(0) {}
0018 virtual ~FortranInstance() noexcept(false);
0019
0020 void call(void (&fn)()) {
0021 InstanceWrapper wrapper(this);
0022 fn();
0023 }
0024 template <typename T>
0025 T call(T (&fn)()) {
0026 InstanceWrapper wrapper(this);
0027 return fn();
0028 }
0029 template <typename A>
0030 void call(void (&fn)(A), A a) {
0031 InstanceWrapper wrapper(this);
0032 fn(a);
0033 }
0034 template <typename T, typename A>
0035 T call(T (&fn)(A), A a) {
0036 InstanceWrapper wrapper(this);
0037 return fn(a);
0038 }
0039 template <typename A1, typename A2>
0040 void call(void (&fn)(A1, A2), A1 a1, A2 a2) {
0041 InstanceWrapper wrapper(this);
0042 fn(a1, a2);
0043 }
0044 template <typename T, typename A1, typename A2>
0045 T call(T (&fn)(A1, A2), A1 a1, A2 a2) {
0046 InstanceWrapper wrapper(this);
0047 return fn(a1, a2);
0048 }
0049
0050
0051
0052
0053
0054 struct InstanceWrapper {
0055 InstanceWrapper(FortranInstance *instance) {
0056 this->instance = instance;
0057 instance->enter();
0058 }
0059
0060 ~InstanceWrapper() { instance->leave(); }
0061
0062 FortranInstance *instance;
0063 };
0064
0065
0066
0067 virtual void enter();
0068
0069
0070
0071 virtual void leave();
0072
0073
0074
0075 template <typename T>
0076 static T *getInstance() {
0077 T *instance = dynamic_cast<T *>(currentInstance);
0078 if (!instance)
0079 throwMissingInstance();
0080 return instance;
0081 }
0082
0083
0084 virtual void upInit();
0085 virtual void upEvnt();
0086 virtual bool upVeto();
0087
0088 static const std::string kFortranInstance;
0089
0090 private:
0091
0092 friend void gen::upinit_();
0093 friend void gen::upevnt_();
0094 friend void gen::upveto_(int *);
0095
0096
0097 static void throwMissingInstance();
0098
0099
0100
0101
0102
0103
0104 int instanceNesting;
0105
0106
0107 static FortranInstance *currentInstance;
0108 };
0109
0110 }
0111
0112 #endif