mbsimcontrol  4.0.0
signal_function.h
00001 /* Copyright (C) MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or 
00004  * modify it under the terms of the GNU Lesser General Public 
00005  * License as published by the Free Software Foundation; either 
00006  * version 2.1 of the License, or (at your option) any later version. 
00007  *  
00008  * This library is distributed in the hope that it will be useful, 
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00011  * Lesser General Public License for more details. 
00012  *  
00013  * You should have received a copy of the GNU Lesser General Public 
00014  * License along with this library; if not, write to the Free Software 
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: markus.ms.schneider@gmail.com
00018  */
00019 
00020 #ifndef _SIGNAL_FUNCTION_H_
00021 #define _SIGNAL_FUNCTION_H_
00022 
00023 #include "mbsim/functions/function.h"
00024 #include "mbsimControl/signal_.h"
00025 #include "mbsim/element.h"
00026 #include "mbsim/functions/function.h"
00027 
00028 namespace MBSimControl {
00029 
00031   template<typename Sig>
00032   class SignalFunction;
00033 
00035   template<typename Ret, typename Arg>
00036   class SignalFunction<Ret(Arg)> : public MBSim::Function<Ret(Arg)> {
00037     public:
00038       SignalFunction(Signal *ret_=NULL) : ret(ret_) {}
00039 
00040       void setReturnSignal(Signal *ret_);
00041 
00042       virtual Ret operator()(const Arg& a) {
00043         return MBSim::FromVecV<Ret>::cast(ret->getSignal());
00044       }
00045 
00046       void init(MBSim::Element::InitStage stage);
00047 
00048       void initializeUsingXML(xercesc::DOMElement *element);
00049 
00050     protected:
00051       std::string retString;
00052       Signal *ret;
00053   };
00054 
00055   template<typename Ret, typename Arg>
00056   void SignalFunction<Ret(Arg)>::setReturnSignal(Signal *ret_) {
00057     ret=ret_;
00058   }
00059 
00060   template<typename Ret, typename Arg>
00061   void SignalFunction<Ret(Arg)>::initializeUsingXML(xercesc::DOMElement *element) {
00062     MBSim::Function<Ret(Arg)>::initializeUsingXML(element);
00063     xercesc::DOMElement *e;
00064     e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIMCONTROL%"returnSignal");
00065     retString=MBXMLUtils::E(e)->getAttribute("ref");
00066   }
00067 
00068   template<typename Ret, typename Arg>
00069   void SignalFunction<Ret(Arg)>::init(MBSim::Element::InitStage stage) {
00070     if(stage==MBSim::Element::resolveXMLPath) {
00071       if(retString!="")
00072         setReturnSignal(this->template getByPath<Signal>(retString));
00073       MBSim::Function<Ret(Arg)>::init(stage);
00074     }
00075     else
00076       MBSim::Function<Ret(Arg)>::init(stage);
00077   }
00078 
00080   template<typename Ret, typename Arg1, typename Arg2>
00081   class SignalFunction<Ret(Arg1,Arg2)> : public MBSim::Function<Ret(Arg1,Arg2)> {
00082     public:
00083       SignalFunction(Signal *ret_=NULL) : ret(ret_) {}
00084 
00085       void setReturnSignal(Signal *ret_);
00086 
00087       virtual Ret operator()(const Arg1& a1, const Arg2& a2) {
00088         return MBSim::FromVecV<Ret>::cast(ret->getSignal());
00089       }
00090 
00091       void init(MBSim::Element::InitStage stage);
00092 
00093       void initializeUsingXML(xercesc::DOMElement *element);
00094 
00095     protected:
00096       std::string retString;
00097       Signal *ret;
00098   };
00099 
00100   template<typename Ret, typename Arg1, typename Arg2>
00101   void SignalFunction<Ret(Arg1, Arg2)>::setReturnSignal(Signal *ret_) {
00102     ret=ret_;
00103   }
00104 
00105   template<typename Ret, typename Arg1, typename Arg2>
00106   void SignalFunction<Ret(Arg1, Arg2)>::initializeUsingXML(xercesc::DOMElement *element) {
00107     MBSim::Function<Ret(Arg1,Arg2)>::initializeUsingXML(element);
00108     xercesc::DOMElement *e;
00109     e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIMCONTROL%"returnSignal");
00110     retString=MBXMLUtils::E(e)->getAttribute("ref");
00111   }
00112 
00113   template<typename Ret, typename Arg1, typename Arg2>
00114   void SignalFunction<Ret(Arg1, Arg2)>::init(MBSim::Element::InitStage stage) {
00115     if(stage==MBSim::Element::resolveXMLPath) {
00116       if(retString!="")
00117         setReturnSignal(this->template getByPath<Signal>(retString));
00118       MBSim::Function<Ret(Arg1,Arg2)>::init(stage);
00119     }
00120     else
00121       MBSim::Function<Ret(Arg1,Arg2)>::init(stage);
00122   }
00123 
00124 }
00125 
00126 #endif