libFAUDES

Sections

Index

rtiwrapper.h

Go to the documentation of this file.
00001 /** @file rtiwrapper.h Runtime interface, wrapper from core functions */
00002 
00003     /* FAU Discrete Event Systems Library (libfaudes)
00004 
00005     Copyright (C) 2009 Ruediger Berndt
00006     Copyright (C) 2009 Thomas Moor
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Lesser General Public
00010     License as published by the Free Software Foundation; either
00011     version 2.1 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public
00019     License along with this library; if not, write to the Free Software
00020     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00021 
00022 
00023 #ifndef FAUDES_RTIWRAPPER_H
00024 #define FAUDES_RTIWRAPPER_H
00025 
00026 #include "rtifncts.h"
00027 #include "corefaudes.h"
00028 
00029 
00030 namespace faudes {
00031 
00032 
00033 /*
00034 ********************************************************************
00035 ********************************************************************
00036 ********************************************************************
00037 
00038 Helper to define Function derivates
00039 
00040 ********************************************************************
00041 ********************************************************************
00042 ********************************************************************
00043 */
00044 
00045 
00046 /**
00047  * A TSimpeFunction derives a class from Function to invoke a
00048  * c-function with parameters of specified type. It must be a simple 
00049  * function in that it 
00050  *  - has a unique signature, and
00051  *  - has all but the last parameter const,
00052  *  - has no more than three parameters.
00053  * Technically, a TSimpleFunction is a template
00054  * that emulates variadic parameters in a rather crude way.
00055  *
00056  */
00057 
00058 template<class FTYPE0, void (*CFNCT)(FTYPE0&)>
00059 class TSimpleFunction1 : public Function {  
00060 public:
00061  
00062   // constructor
00063   TSimpleFunction1(const FunctionDefinition* fdef) : Function(fdef) {}; 
00064 
00065   // new on heap (factory)
00066   virtual Function* New() const {return new TSimpleFunction1<FTYPE0,CFNCT>(pFuncDef); };
00067 
00068 protected: 
00069 
00070   // typed parameters
00071   FTYPE0* pp0;
00072 
00073   // type check (variant and parameter number known to be ok)
00074   virtual bool DoTypeCheck(int n) {
00075     bool res=false;
00076     FD_DRTI("TSimpleFunction1::DoTypeCheck(n): " << pFuncDef->Name() << " #" << Variant()->Size());
00077     if(n==0) res=DoTypeCast<FTYPE0>(0,pp0);
00078     FD_DRTI("TSimpleFunction1::DoTypeCheck(n): " << pFuncDef->Name() << " done");
00079     return res;
00080   };
00081 
00082   // execute (parameter cast ok)
00083   virtual void DoExecute(void) {
00084     FD_DRTI("TSimpleFunction1::DoExecute(): " << pFuncDef->Name() << " #" << Variant()->Size());
00085     CFNCT(*pp0);
00086     FD_DRTI("TSimpleFunction1::DoExecute(): " << pFuncDef->Name() << " done");
00087   };
00088 };
00089 
00090 
00091 template<class FTYPE0, class FTYPE1, void (*CFNCT)(const FTYPE0&, FTYPE1&)>
00092 class TSimpleFunction2 : public Function {  
00093 public: 
00094 
00095   // constructor
00096   TSimpleFunction2(const FunctionDefinition* fdef) : Function(fdef) {}; 
00097 
00098   // new on heap (factory)
00099   virtual Function* New() const {return new TSimpleFunction2<FTYPE0,FTYPE1,CFNCT>(pFuncDef); };
00100 
00101 protected: 
00102 
00103   // typed parameters
00104   FTYPE0* pp0;
00105   FTYPE1* pp1;
00106 
00107   // type check (variant and parameter number known to be ok)
00108   virtual bool DoTypeCheck(int n) {
00109     bool res=false;
00110     FD_DRTI("TSimpleFunction2::DoTypeCheck(n): " << pFuncDef->Name() << " #" << Variant()->Size());
00111     switch(n) {
00112     case 0: res=DoTypeCast<FTYPE0>(0,pp0); break;
00113     case 1: res=DoTypeCast<FTYPE1>(1,pp1); break;
00114     default: break;
00115     }
00116     FD_DRTI("TSimpleFunction2::DoTypeCheck(n): " << pFuncDef->Name() << " done");
00117     return res;
00118   };
00119 
00120   // execute (parameter cast ok)
00121   virtual void DoExecute(void) {
00122     FD_DRTI("TSimpleFunction2::DoExecute(): " << pFuncDef->Name() << " #" << Variant()->Size());
00123     CFNCT(*pp0,*pp1);
00124     FD_DRTI("TSimpleFunction2::DoExecute(): " << pFuncDef->Name() << " done");
00125   };
00126 };
00127 
00128 template<class FTYPE0, class FTYPE1, class FTYPE2, void (*CFNCT)(const FTYPE0&, const FTYPE1&, FTYPE2&)>
00129 class TSimpleFunction3 : public Function {  
00130 public: 
00131 
00132   // constructor
00133   TSimpleFunction3(const FunctionDefinition* fdef) : Function(fdef) {}; 
00134 
00135   // new on heap (factory)
00136   virtual Function* New() const {return new TSimpleFunction3<FTYPE0,FTYPE1,FTYPE2,CFNCT>(pFuncDef); };
00137 
00138 protected: 
00139 
00140   // typed parameters
00141   FTYPE0* pp0;
00142   FTYPE1* pp1;
00143   FTYPE2* pp2;
00144 
00145   // type check (variant and parameter number known to be ok)
00146   virtual bool DoTypeCheck(int n) {
00147     bool res=false;
00148     FD_DRTI("TSimpleFunction3::DoTypeCheck(): " << pFuncDef->Name() << " #" << Variant()->Size());
00149     switch(n) {
00150     case 0: res=DoTypeCast<FTYPE0>(0,pp0); break;
00151     case 1: res=DoTypeCast<FTYPE1>(1,pp1); break;
00152     case 2: res=DoTypeCast<FTYPE1>(2,pp2); break;
00153     default: break;
00154     }
00155     FD_DRTI("TSimpleFunction3::DoTypeCheck(): " << pFuncDef->Name() << " done");
00156     return res;
00157   };
00158 
00159   // execute (parameter cast ok)
00160   virtual void DoExecute(void) {
00161     FD_DRTI("TSimpleFunction3::DoExecute(): " << pFuncDef->Name() << " #" << Variant()->Size());
00162     CFNCT(*pp0,*pp1,*pp2);
00163     FD_DRTI("TSimpleFunction3::DoExecute(): " << pFuncDef->Name() << " done");
00164   };
00165 };
00166 
00167 
00168 
00169 /**
00170  * A TNestedFunction derives a class from Function to invoke a
00171  * c-function repeatavely. Technicalls, it is a template class and
00172  * it takes the argument type and the c-function as template parameters.
00173  * The c-function must take three parameters, the first two const.
00174  *
00175  */
00176 
00177 template<class FTYPE, void (*CFNCT)(const FTYPE&, const FTYPE&, FTYPE&)> 
00178 class TNestedFunction : public Function { 
00179 
00180 public: 
00181 
00182   // constructor
00183   TNestedFunction(const FunctionDefinition* fdef) : Function(fdef) {}; 
00184 
00185   // new on heap (factory)
00186   virtual Function* New() const {return new TNestedFunction<FTYPE,CFNCT>(pFuncDef); };
00187 
00188 protected: 
00189 
00190   // typed parameters
00191   std::vector<FTYPE*> pvec;
00192 
00193   // type check (variant and parameter number known to be ok)
00194   virtual bool DoTypeCheck(int n) {
00195     bool res;
00196     FD_DRTI("TNestedFunction::DoTypeCheck(n): " << pFuncDef->Name() << " #" << Variant()->Size());
00197     // allocate typed references
00198     pvec.resize(ParamsSize(),NULL);
00199     // do the cast
00200     res=DoTypeCast<FTYPE>(n,pvec.at(n));
00201     // done
00202     FD_DRTI("TNestedFunction::DoTypeCheck(): " << pFuncDef->Name() << " done");
00203     return res;
00204   };
00205 
00206   // execute (parameter cast ok)
00207   virtual void DoExecute(void) {
00208     FD_DRTI("TNestedFunction::DoExecute(): " << pFuncDef->Name() << " #" << Variant()->Size());
00209     // signature by numer of args
00210     int args=ParamsSize()-1;
00211     int res =ParamsSize()-1;
00212     // cases: #1
00213     if(args==1) {
00214       *(pvec[res])=*pvec.at(0);
00215     }
00216     // cases: #2+
00217     if(args>=2) {
00218       CFNCT(*pvec.at(0),*pvec.at(1),*(pvec[res]));
00219       // loop sum
00220       for(int i=2; i < args; i++) 
00221         CFNCT(*pvec.at(i),*(pvec[res]),*pvec.at(res));
00222     }; 
00223     FD_DRTI("TNestedFunction::DoExecute(): " << pFuncDef->Name() << " done");
00224   }
00225 
00226 };
00227 
00228 
00229 
00230 
00231 
00232 } // namespace
00233 
00234 
00235 #endif // header

libFAUDES 2.14g --- 2009-12-3 --- c++ source docu by doxygen 1.5.6