1 // Copyright (C) 2006-2008 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.
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
19 #include "RuntimeSALOME.hxx"
20 #include "CppComponent.hxx"
21 #include "CppContainer.hxx"
22 #include "TypeCode.hxx"
23 #include "CppNode.hxx"
24 #include "DynLibLoader.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)
83 _container = getRuntime()->createContainer(CppNode::KIND);
84 if (!_container->isAlreadyStarted())
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), __run(other.__run),
93 __terminate(other.__terminate), __obj(0)
95 _container = getRuntime()->createContainer(CppNode::KIND);
96 if (!_container->isAlreadyStarted())
99 CppContainer * _containerC = dynamic_cast<CppContainer *> (_container);
100 _containerC->createInternalInstance(_compoName, __obj, __run, __terminate);
103 CppComponent::~CppComponent()
105 DEBTRACE("CppComponent::~CppComponent()");
106 if (__terminate) __terminate(&__obj);
108 ((CppContainer *) _container)->unregisterComponentInstance(this);
111 void CppComponent::run (const char * service, int nbIn, int nbOut,
112 Any ** argIn, Any ** argOut) throw (YACS::Exception)
115 returnInfo return_code;
118 std::ostringstream sDebug;
119 sDebug << _name << "::" << service << "(";
120 for (i=0; i<nbIn; i++) {
121 sDebug << *(argIn[i]);
128 __run(__obj, service, nbIn, nbOut, argIn, argOut, &return_code);
130 if (return_code.code != 0) {
132 std::cerr << sDebug << " ???" << std::endl;
134 throw YACS::Exception(return_code.message);
140 for (i=0; i<nbOut; i++) {
141 sDebug << " " << *(argOut[i]);
144 DEBTRACE(sDebug.str());
148 //! Unload the component
149 void CppComponent::unload()
152 DEBTRACE("CppComponent::unload : not implemented ");
155 //! Is the component instance already loaded ?
156 bool CppComponent::isLoaded()
158 return NULL != __obj;
161 void CppComponent::load()
164 _container = getRuntime()->createContainer(CppNode::KIND);
169 CppContainer * containerC= dynamic_cast< CppContainer *> (_container);
171 containerC->lock();//To be sure
172 if(!_container->isAlreadyStarted())
180 containerC->unLock();
184 containerC->unLock();
185 containerC->lock();//To be sure
187 YACS::BASES::DynLibLoader D(_compoName + "Local");
189 bool isLoadable = containerC->loadComponentLibrary(_compoName);
191 containerC->createInternalInstance(_compoName, __obj, __run, __terminate);
195 containerC->unLock();
196 throw Exception("CppComponent::load : Error while trying to create a new component.");
198 containerC->unLock();
204 ServiceNode* CppComponent::createNode(const std::string& name)
206 CppNode* node=new CppNode(name);
207 node->setComponent(this);
211 //! Clone the component instance
212 ComponentInstance* CppComponent::clone() const
214 if(_isAttachedOnCloning)
217 return (ComponentInstance*) (this);
220 return new CppComponent(*this);