1 // Copyright (C) 2006-2014 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "RuntimeSALOME.hxx"
21 #include "CppComponent.hxx"
22 #include "CppContainer.hxx"
23 #include "TypeCode.hxx"
24 #include "CppNode.hxx"
26 using namespace YACS::ENGINE;
33 #include "YacsTrace.hxx"
35 const char CppComponent::KIND[] = "Cpp";
37 static std::ostream & operator<<(std::ostream & f, const Any & A)
39 const TypeCode * t = A.getType();
48 f << "(type Double) " << A.getDoubleValue();
51 f << "(type Int) " << A.getIntValue();
54 f << "(type String) " << A.getStringValue();
57 f << "(type Bool) " << A.getBoolValue();
63 f << "(type Sequence) ";
66 const SequenceAny * sA = dynamic_cast<const SequenceAny *>(&A);
67 for (i=0; i<sA->size(); i++)
68 f << " " << *((*sA)[i]);
75 std::string CppComponent::getKind() const
77 return CppComponent::KIND;
80 std::string CppComponent::getKindForNode() const
82 return CppComponent::KIND;
85 //! CppComponent constructor
86 CppComponent::CppComponent(const std::string &name) : ComponentInstance(name), __obj(0), __run(0),__terminate(0)
88 _container = getRuntime()->createContainer(CppNode::KIND);
89 /* This part of code has been commented because the load part is supposed to do that !
90 if (!_container->isAlreadyStarted(0))
92 CppContainer * _containerC = dynamic_cast<CppContainer *> (_container);
93 _containerC->createInternalInstance(name, __obj, __run, __terminate);*/
96 //! CppComponent copy constructor
97 CppComponent::CppComponent(const CppComponent& other) : ComponentInstance(other._compoName), __obj(0), __run(0),__terminate(0)
99 _container = getRuntime()->createContainer(CppNode::KIND);
100 /* This part of code has been commented because the load part is supposed to do that !
101 if (!_container->isAlreadyStarted(0))
102 _container->start(0);
103 CppContainer * _containerC = dynamic_cast<CppContainer *> (_container);
104 _containerC->createInternalInstance(_compoName, __obj, __run, __terminate);*/
107 CppComponent::~CppComponent()
109 DEBTRACE("CppComponent::~CppComponent()");
110 if (__terminate) __terminate(&__obj);
112 ((CppContainer *) _container)->unregisterComponentInstance(this);
115 void CppComponent::run (const char * service, int nbIn, int nbOut,
116 Any ** argIn, Any ** argOut) throw (YACS::Exception)
119 returnInfo return_code;
122 std::ostringstream sDebug;
123 sDebug << getInstanceName() << "::" << service << "(";
124 for (i=0; i<nbIn; i++) {
125 sDebug << *(argIn[i]);
132 __run(__obj, service, nbIn, nbOut, argIn, argOut, &return_code);
134 if (return_code.code != 0) {
136 std::cerr << sDebug << " ???" << std::endl;
138 throw YACS::Exception(return_code.message);
144 for (i=0; i<nbOut; i++) {
145 sDebug << " " << *(argOut[i]);
148 DEBTRACE(sDebug.str());
152 //! Unload the component
153 void CppComponent::unload(Task *askingNode)
156 DEBTRACE("CppComponent::unload : not implemented ");
159 //! Is the component instance already loaded ?
160 bool CppComponent::isLoaded(Task *askingNode) const
162 return NULL != __obj;
165 void CppComponent::load(Task *askingNode)
169 _container = getRuntime()->createContainer(CppNode::KIND);
174 CppContainer *containerC(dynamic_cast< CppContainer *> (_container));
176 throw Exception("The type of container should be CPP for component CPP !");
177 containerC->lock();//To be sure
178 if(!_container->isAlreadyStarted(askingNode))
182 _container->start(askingNode);
186 containerC->unLock();
190 containerC->unLock();
191 containerC->lock();//To be sure
193 bool isLoadable(containerC->loadComponentLibrary(_compoName));
195 containerC->createInternalInstance(_compoName, __obj, __run, __terminate);
199 containerC->unLock();
200 throw Exception("CppComponent::load : Error while trying to create a new component.");
202 containerC->unLock();
207 ServiceNode* CppComponent::createNode(const std::string& name)
209 CppNode* node=new CppNode(name);
210 node->setComponent(this);
214 //! Clone the component instance
215 ComponentInstance* CppComponent::clone() const
217 if(_isAttachedOnCloning)
220 return (ComponentInstance*) (this);
223 return new CppComponent(*this);
226 YACS::ENGINE::ComponentInstance *CppComponent::cloneAlways() const
228 return new CppComponent(*this);