Salome HOME
0a6cedb5a036756bdfef5ba77fd207368f2533d5
[modules/yacs.git] / src / yacsloader / inlineParsers.hxx
1 // Copyright (C) 2006-2016  CEA/DEN, EDF R&D
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef _INLINEPARSERS_HXX_
21 #define _INLINEPARSERS_HXX_
22
23 #include "parserBase.hxx"
24 #include "containerParsers.hxx"
25 #include "dataParsers.hxx"
26 #include "portParsers.hxx"
27 #include "codeParsers.hxx"
28 #include "propertyParsers.hxx"
29
30 #include "Proc.hxx"
31 #include "TypeCode.hxx"
32 #include "InlineNode.hxx"
33 #include "Exception.hxx"
34 #include "Runtime.hxx"
35 #include "Container.hxx"
36 #include "ComponentInstance.hxx"
37 #include "OutputDataStreamPort.hxx"
38 #include "InputDataStreamPort.hxx"
39 #include "ComponentInstance.hxx"
40 #include "factory.hxx"
41
42 extern YACS::ENGINE::Proc* currentProc;
43 extern YACS::ENGINE::Runtime* theRuntime;
44
45 namespace YACS
46 {
47
48 static std::string t1[]={"script","function",""};
49
50 template <class T=YACS::ENGINE::InlineNode*>
51 struct inlinetypeParser:public nodetypeParser<T>
52 {
53   static inlinetypeParser<T> inlineParser;
54
55   virtual void onStart(const XML_Char* el, const XML_Char** attr);
56   virtual void onEnd(const char *el,parser* child)
57     {
58       DEBTRACE( "inlinetypeParser::onEnd: " << el )             
59       std::string element(el);
60       if(element == "kind")kind(((stringtypeParser*)child)->post());
61       else if(element == "script")script(((codetypeParser*)child)->post());
62       else if(element == "function")function(((functypeParser*)child)->post());
63       else if(element == "property")this->property(((propertytypeParser*)child)->post());
64       else if(element == "inport") inport(((inporttypeParser<myinport>*)child)->post());
65       else if(element == "outport") outport(((outporttypeParser<myoutport>*)child)->post());
66     }
67   virtual void buildAttr(const XML_Char** attr)
68     {
69       if (!attr)
70         return;
71       this->required("name",attr);
72       for (int i = 0; attr[i]; i += 2) 
73       {
74         if(std::string(attr[i]) == "name")this->name(attr[i+1]);
75         if(std::string(attr[i]) == "state")this->state(attr[i+1]);
76       }
77     }
78   virtual void pre ()
79     {
80       this->_node=0;
81       _kind="";
82       this->_state="";
83       this->_container="";
84     }
85   virtual void kind (const std::string& name)
86     {
87       DEBTRACE( "inline_kind " << name )             
88       _kind=name;
89     }
90
91   virtual void script (const myfunc& f){}
92   virtual void function (const myfunc& f) {}
93
94   virtual void inport (const myinport& p)
95     {
96       DEBTRACE( "inline_inport: " << p._name <<":"<<p._type)             
97       if(this->_node==0)
98         throw YACS::Exception("Node must be completely defined before defining its ports");
99
100       if(currentProc->typeMap.count(p._type)==0)
101       {
102         //Check if the typecode is defined in the runtime
103         YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
104         if(t==0)
105         {
106           std::string msg="Unknown InPort Type: ";
107           msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
108           throw YACS::Exception(msg);
109         }
110         else
111         {
112           currentProc->typeMap[p._type]=t;
113           t->incrRef();
114         }
115       }
116       this->_node->edAddInputPort(p._name,currentProc->typeMap[p._type]);
117     }
118   virtual void outport (const myoutport& p)
119     {
120       DEBTRACE( "inline_outport: " << p._name <<":"<<p._type)             
121       if(this->_node==0)
122         throw YACS::Exception("Node must be completely defined before defining its ports");
123
124       if(currentProc->typeMap.count(p._type)==0)
125       {
126         YACS::ENGINE::TypeCode* t=theRuntime->getTypeCode(p._type);
127         if(t==0)
128         {
129           std::string msg="Unknown OutPort Type: ";
130           msg=msg+p._type+" for node: "+this->_node->getName()+" port name: "+p._name;
131           throw YACS::Exception(msg);
132         }
133         else
134         {
135           currentProc->typeMap[p._type]=t;
136           t->incrRef();
137         }
138       }
139       this->_node->edAddOutputPort(p._name,currentProc->typeMap[p._type]);
140     }
141   virtual T post()
142     {
143       DEBTRACE( "inline_post " << this->_node->getName() )             
144       if(this->_state == "disabled")this->_node->exDisabledState();
145       /*
146       std::list<OutputPort *>::iterator iter;
147       std::list<OutputPort *> s=_node->getSetOfOutputPort();
148       for(iter=s.begin();iter!=s.end();iter++)
149         {
150           std::cerr << "port name: " << (*iter)->getName() << std::endl;
151           std::cerr << "port kind: " << (*iter)->edGetType()->kind() << std::endl;
152         }
153         */
154       return this->_node;
155     }
156   std::string _kind;
157 };
158
159 template <class T> inlinetypeParser<T> inlinetypeParser<T>::inlineParser;
160
161 template <>
162 void inlinetypeParser<YACS::ENGINE::InlineNode*>::script (const myfunc& f)
163 {
164   DEBTRACE( "inline_script: " << f._code )             
165   _node=theRuntime->createScriptNode(_kind,_name);
166   _node->setScript(f._code);
167 }
168
169 template <>
170 void inlinetypeParser<YACS::ENGINE::InlineNode*>::function (const myfunc& f)
171 {
172   DEBTRACE( "inline_function: " << f._code )             
173   YACS::ENGINE::InlineFuncNode *fnode;
174   fnode=theRuntime->createFuncNode(_kind,_name);
175   fnode->setScript(f._code);
176   fnode->setFname(f._name);
177   _node=fnode;
178 }
179
180 template <class T>
181 void inlinetypeParser<T>::onStart(const XML_Char* el, const XML_Char** attr)
182
183     {
184       DEBTRACE( "inlinetypeParser::onStart: " << el )
185       std::string element(el);
186       parser* pp=&parser::main_parser;
187       this->maxcount("kind",1,element);
188       this->maxcount("script",1,element);
189       this->maxcount("function",1,element);
190       this->maxchoice(t1,1,element);
191       if(element == "kind")pp=&stringtypeParser::stringParser;
192       else if(element == "script")pp=&codetypeParser::codeParser;
193       else if(element == "function")pp=&functypeParser::funcParser;
194       else if(element == "property")pp=&propertytypeParser::propertyParser;
195       else if(element == "inport")pp=&inporttypeParser<>::inportParser;
196       else if(element == "outport")pp=&outporttypeParser<>::outportParser;
197       this->SetUserDataAndPush(pp);
198       pp->init();
199       pp->pre();
200       pp->buildAttr(attr);
201     }
202
203 } // end of namespace YACS
204
205 #endif