mbsimcontrol 4.0.0 MBSim Control Module

signal_function.h
1 /* Copyright (C) MBSim Development Team
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 *
17 * Contact: markus.ms.schneider@gmail.com
18 */
19
20 #ifndef _SIGNAL_FUNCTION_H_
21 #define _SIGNAL_FUNCTION_H_
22
23 #include "mbsim/functions/function.h"
24 #include "mbsimControl/signal_.h"
25 #include "mbsim/element.h"
26 #include "mbsim/functions/function.h"
27 #include "mbsim/utils/utils.h"
28
29 namespace MBSimControl {
30
32 template<typename Sig>
33 class SignalFunction;
34
36 template<typename Ret, typename Arg>
37 class SignalFunction<Ret(Arg)> : public MBSim::Function<Ret(Arg)> {
38 public:
39 SignalFunction(Signal *ret_=nullptr) : ret(ret_) {}
40
41 void setReturnSignal(Signal *ret_);
42
43 std::pair<int, int> getRetSize() const override { return std::make_pair(ret->getSignalSize(),1); }
44
45 Ret operator()(const Arg& a) override {
46 return MBSim::FromVecV<Ret>::cast(ret->evalSignal());
47 }
48
49 void init(MBSim::Element::InitStage stage, const MBSim::InitConfigSet &config) override;
50
51 void initializeUsingXML(xercesc::DOMElement *element) override;
52
53 MBSim::Element* getDependency() const override { return ret; }
54
55 protected:
56 std::string retString;
57 Signal *ret;
58 };
59
60 template<typename Ret, typename Arg>
61 void SignalFunction<Ret(Arg)>::setReturnSignal(Signal *ret_) {
62 ret=ret_;
63 }
64
65 template<typename Ret, typename Arg>
66 void SignalFunction<Ret(Arg)>::initializeUsingXML(xercesc::DOMElement *element) {
67 MBSim::Function<Ret(Arg)>::initializeUsingXML(element);
68 xercesc::DOMElement *e;
69 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIMCONTROL%"returnSignal");
70 retString=MBXMLUtils::E(e)->getAttribute("ref");
71 }
72
73 template<typename Ret, typename Arg>
74 void SignalFunction<Ret(Arg)>::init(MBSim::Element::InitStage stage, const MBSim::InitConfigSet &config) {
75 if(stage==MBSim::Element::resolveStringRef) {
76 if(not retString.empty())
77 setReturnSignal(this->template getByPath<Signal>(retString));
78 if(not ret)
79 MBSim::Element::throwError("Signal is not given!");
80 MBSim::Function<Ret(Arg)>::init(stage, config);
81 }
82 else
83 MBSim::Function<Ret(Arg)>::init(stage, config);
84 }
85
87 template<typename Ret, typename Arg1, typename Arg2>
88 class SignalFunction<Ret(Arg1,Arg2)> : public MBSim::Function<Ret(Arg1,Arg2)> {
89 public:
90 SignalFunction(Signal *ret_=nullptr) : ret(ret_) {}
91
92 void setReturnSignal(Signal *ret_);
93
94 std::pair<int, int> getRetSize() const override { return std::make_pair(ret->getSignalSize(),1); }
95
96 Ret operator()(const Arg1& a1, const Arg2& a2) override {
97 return MBSim::FromVecV<Ret>::cast(ret->evalSignal());
98 }
99
100 void init(MBSim::Element::InitStage stage, const MBSim::InitConfigSet &config) override;
101
102 void initializeUsingXML(xercesc::DOMElement *element) override;
103
104 MBSim::Element* getDependency() const override { return ret; }
105
106 protected:
107 std::string retString;
108 Signal *ret;
109 };
110
111 template<typename Ret, typename Arg1, typename Arg2>
112 void SignalFunction<Ret(Arg1, Arg2)>::setReturnSignal(Signal *ret_) {
113 ret=ret_;
114 }
115
116 template<typename Ret, typename Arg1, typename Arg2>
117 void SignalFunction<Ret(Arg1, Arg2)>::initializeUsingXML(xercesc::DOMElement *element) {
118 MBSim::Function<Ret(Arg1,Arg2)>::initializeUsingXML(element);
119 xercesc::DOMElement *e;
120 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIMCONTROL%"returnSignal");
121 retString=MBXMLUtils::E(e)->getAttribute("ref");
122 }
123
124 template<typename Ret, typename Arg1, typename Arg2>
125 void SignalFunction<Ret(Arg1, Arg2)>::init(MBSim::Element::InitStage stage, const MBSim::InitConfigSet &config) {
126 if(stage==MBSim::Element::resolveStringRef) {
127 if(not retString.empty())
128 setReturnSignal(this->template getByPath<Signal>(retString));
129 if(not ret)
130 MBSim::Element::throwError("Signal is not given!");
131 MBSim::Function<Ret(Arg1,Arg2)>::init(stage, config);
132 }
133 else
134 MBSim::Function<Ret(Arg1,Arg2)>::init(stage, config);
135 }
136
137 }
138
139 #endif
MBSimControl::SignalFunction
A function which get its return value from a signal.
Definition: signal_function.h:33
MBSim::Element::resolveStringRef
resolveStringRef
MBSimControl::Signal
Signal.
Definition: signal_.h:38
MBSim::Element
MBSim::FromVecV
MBSim::Element::InitStage
InitStage
MBSim::Function