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 //! CppComponent constructor
81 CppComponent::CppComponent(const std::string &name) : ComponentInstance(name), __obj(0), __run(0),__terminate(0)
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))
87 CppContainer * _containerC = dynamic_cast<CppContainer *> (_container);
88 _containerC->createInternalInstance(name, __obj, __run, __terminate);*/
91 //! CppComponent copy constructor
92 CppComponent::CppComponent(const CppComponent& other) : ComponentInstance(other._compoName), __obj(0), __run(0),__terminate(0)
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))
98 CppContainer * _containerC = dynamic_cast<CppContainer *> (_container);
99 _containerC->createInternalInstance(_compoName, __obj, __run, __terminate);*/
102 CppComponent::~CppComponent()
104 DEBTRACE("CppComponent::~CppComponent()");
105 if (__terminate) __terminate(&__obj);
107 ((CppContainer *) _container)->unregisterComponentInstance(this);
110 void CppComponent::run (const char * service, int nbIn, int nbOut,
111 Any ** argIn, Any ** argOut) throw (YACS::Exception)
114 returnInfo return_code;
117 std::ostringstream sDebug;
118 sDebug << getInstanceName() << "::" << service << "(";
119 for (i=0; i<nbIn; i++) {
120 sDebug << *(argIn[i]);
127 __run(__obj, service, nbIn, nbOut, argIn, argOut, &return_code);
129 if (return_code.code != 0) {
131 std::cerr << sDebug << " ???" << std::endl;
133 throw YACS::Exception(return_code.message);
139 for (i=0; i<nbOut; i++) {
140 sDebug << " " << *(argOut[i]);
143 DEBTRACE(sDebug.str());
147 //! Unload the component
148 void CppComponent::unload(Task *askingNode)
151 DEBTRACE("CppComponent::unload : not implemented ");
154 //! Is the component instance already loaded ?
155 bool CppComponent::isLoaded(Task *askingNode) const
157 return NULL != __obj;
160 void CppComponent::load(Task *askingNode)
164 _container = getRuntime()->createContainer(CppNode::KIND);
169 CppContainer *containerC(dynamic_cast< CppContainer *> (_container));
171 throw Exception("The type of container should be CPP for component CPP !");
172 containerC->lock();//To be sure
173 if(!_container->isAlreadyStarted(askingNode))
177 _container->start(askingNode);
181 containerC->unLock();
185 containerC->unLock();
186 containerC->lock();//To be sure
188 bool isLoadable(containerC->loadComponentLibrary(_compoName));
190 containerC->createInternalInstance(_compoName, __obj, __run, __terminate);
194 containerC->unLock();
195 throw Exception("CppComponent::load : Error while trying to create a new component.");
197 containerC->unLock();
202 ServiceNode* CppComponent::createNode(const std::string& name)
204 CppNode* node=new CppNode(name);
205 node->setComponent(this);
209 //! Clone the component instance
210 ComponentInstance* CppComponent::clone() const
212 if(_isAttachedOnCloning)
215 return (ComponentInstance*) (this);
218 return new CppComponent(*this);