Salome HOME
Ready to update Executor to take into account of the new type of containers.
[modules/yacs.git] / src / runtime / CppComponent.cxx
1 // Copyright (C) 2006-2014  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 #include "RuntimeSALOME.hxx"
21 #include "CppComponent.hxx"
22 #include "CppContainer.hxx"
23 #include "TypeCode.hxx"
24 #include "CppNode.hxx"
25
26 using namespace YACS::ENGINE;
27  
28 #include <iostream>
29 #include <sstream>
30 #include <cstdlib>
31
32 //#define _DEVDEBUG_
33 #include "YacsTrace.hxx"
34
35 const char CppComponent::KIND[] = "Cpp";
36
37 static std::ostream & operator<<(std::ostream & f, const Any & A)
38 {
39     const TypeCode * t = A.getType();
40     if (NULL == t)
41        f << "(type NULL)";
42     else 
43        switch (t->kind()) {
44          case NONE :
45             f << "(type None)";
46             break;
47          case Double :
48             f << "(type Double) " << A.getDoubleValue();
49             break;
50          case Int :
51             f << "(type Int) " << A.getIntValue();
52             break;
53          case String :
54             f << "(type String) " << A.getStringValue();
55             break;
56          case Bool :
57             f << "(type Bool) " << A.getBoolValue();
58             break;
59          case Objref :
60             f << "(type Objref)";
61             break;
62          case Sequence :
63             f << "(type Sequence) ";
64             {
65               int i;
66               const SequenceAny * sA = dynamic_cast<const SequenceAny *>(&A);
67               for (i=0; i<sA->size(); i++)
68                 f << " " << *((*sA)[i]);
69             }
70             break;
71        }
72     return f;
73 }
74
75 std::string CppComponent::getKind() const
76 {
77    return CppComponent::KIND;
78 }
79
80 //! CppComponent constructor
81 CppComponent::CppComponent(const std::string &name) : ComponentInstance(name), __obj(0), __run(0),__terminate(0)
82 {
83   _container = getRuntime()->createContainer(CppNode::KIND);
84   /* This part of code has been commented because the load part is supposed to do that !
85   if (!_container->isAlreadyStarted(0))
86     _container->start(0);
87   CppContainer * _containerC = dynamic_cast<CppContainer *> (_container);
88   _containerC->createInternalInstance(name, __obj, __run, __terminate);*/
89 }
90
91 //! CppComponent copy constructor
92 CppComponent::CppComponent(const CppComponent& other) : ComponentInstance(other._compoName), __obj(0), __run(0),__terminate(0)
93 {
94   _container = getRuntime()->createContainer(CppNode::KIND);
95   /* This part of code has been commented because the load part is supposed to do that !
96   if (!_container->isAlreadyStarted(0))
97     _container->start(0);
98   CppContainer * _containerC = dynamic_cast<CppContainer *> (_container);  
99    _containerC->createInternalInstance(_compoName, __obj, __run, __terminate);*/
100 }
101
102 CppComponent::~CppComponent()
103 {
104   DEBTRACE("CppComponent::~CppComponent()");
105   if (__terminate) __terminate(&__obj);
106   if (_container)
107     ((CppContainer *) _container)->unregisterComponentInstance(this);
108 }
109
110 void CppComponent::run (const char * service, int nbIn, int nbOut,
111                         Any ** argIn, Any ** argOut) throw (YACS::Exception)
112 {
113   int i;
114   returnInfo return_code;
115     
116 #ifdef _DEVDEBUG_
117   std::ostringstream sDebug;
118   sDebug << getInstanceName() << "::" << service << "(";
119   for (i=0; i<nbIn; i++) {
120      sDebug << *(argIn[i]);
121      if (i<nbIn-1)
122         sDebug << ", ";
123   }
124   sDebug << ")";
125 #endif
126
127   __run(__obj, service, nbIn, nbOut, argIn, argOut, &return_code);
128     
129   if (return_code.code != 0) {
130 #ifdef _DEVDEBUG_
131     std::cerr << sDebug << " ???" << std::endl;
132 #endif
133     throw YACS::Exception(return_code.message);
134   }
135
136 #ifdef _DEVDEBUG_
137   if (nbOut > 0) {
138     sDebug << "->";
139     for (i=0; i<nbOut; i++) {
140        sDebug << " " << *(argOut[i]);
141     }
142   }
143   DEBTRACE(sDebug.str());
144 #endif
145 }
146
147 //! Unload the component 
148 void CppComponent::unload(Task *askingNode)
149 {
150   //Not implemented
151   DEBTRACE("CppComponent::unload : not implemented ");
152 }
153
154 //! Is the component instance already loaded ?
155 bool CppComponent::isLoaded(Task *askingNode) const
156 {
157   return NULL != __obj;
158 }
159  
160 void CppComponent::load(Task *askingNode)
161 {
162   if (!_container)
163     {
164       _container = getRuntime()->createContainer(CppNode::KIND);
165     }
166
167   if(_container)
168     {
169       CppContainer *containerC(dynamic_cast< CppContainer *> (_container));
170       if(!containerC)
171         throw Exception("The type of container should be CPP for component CPP !");
172       containerC->lock();//To be sure
173       if(!_container->isAlreadyStarted(askingNode))
174         {
175           try
176           {
177               _container->start(askingNode);
178           }
179           catch(Exception& e)
180           {
181               containerC->unLock();
182               throw e;
183           }
184         }
185       containerC->unLock();
186       containerC->lock();//To be sure
187
188       bool isLoadable(containerC->loadComponentLibrary(_compoName));
189       if (isLoadable) 
190         containerC->createInternalInstance(_compoName, __obj, __run, __terminate);
191
192       if(NULL == __obj)
193         {
194           containerC->unLock();
195           throw Exception("CppComponent::load : Error while trying to create a new component.");
196         }
197       containerC->unLock();
198       return;
199     }
200 }
201
202 ServiceNode* CppComponent::createNode(const std::string& name)
203 {
204    CppNode* node=new CppNode(name);
205    node->setComponent(this);
206    return node;
207 }
208
209 //! Clone the component instance
210 ComponentInstance* CppComponent::clone() const
211 {
212   if(_isAttachedOnCloning)
213     {
214       incrRef();
215       return (ComponentInstance*) (this);
216     }
217   else
218     return new CppComponent(*this);
219 }
220