From 8be0e0dbbffc037e5485eb26ff7dc4ed0382d302 Mon Sep 17 00:00:00 2001 From: crouzet Date: Fri, 8 Jul 2011 09:43:52 +0000 Subject: [PATCH] new files for parallel components --- module_generator/hxx_para_tmpl.py | 206 ++++++++++ module_generator/hxx_tmpl_gui.py | 602 ++++++++++++++++++++++++++++++ module_generator/hxxparacompo.py | 446 ++++++++++++++++++++++ 3 files changed, 1254 insertions(+) create mode 100644 module_generator/hxx_para_tmpl.py create mode 100644 module_generator/hxx_tmpl_gui.py create mode 100644 module_generator/hxxparacompo.py diff --git a/module_generator/hxx_para_tmpl.py b/module_generator/hxx_para_tmpl.py new file mode 100644 index 0000000..5545dd3 --- /dev/null +++ b/module_generator/hxx_para_tmpl.py @@ -0,0 +1,206 @@ +# Copyright (C) 2009-2010 EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +try: + from string import Template +except: + from compat import Template,set + +cxxCompo=""" +// this cxx file was generated by yacsgen +#include "${component}_i.hxx" +#include "${cxx_include_file}" +using namespace std; +#include +#include +#include +#include "SenderFactory.hxx" +#include "MultiCommException.hxx" +#include "ReceiverFactory.hxx" +#include "SALOME_Matrix_i.hxx" +#include "MatrixClient.hxx" +#include "Utils_CorbaException.hxx" +#include "MEDCouplingFieldDouble.hxx" + +typedef struct +{ + bool exception; + string msg; +} except_st; + + +//DEFS +${servicesdef} +//ENDDEF + +//============================================================================= +/*! + * standard constructor + */ +//============================================================================= +${component}_i::${component}_i(CORBA::ORB_ptr orb, + PortableServer::POA_ptr poa, + PortableServer::ObjectId * contId, + const char *instanceName, + const char *interfaceName, + bool regist) : + ParaMEDMEMComponent_i(orb,poa,contId,instanceName,interfaceName,regist),${inheritedconstructor}cppCompo_(new ${component}) +{ + MESSAGE("activate object"); + _thisObj = this ; + _id = _poa->activate_object(_thisObj); +} + +${component}_i::~${component}_i() +{ +} + +${servicesimpl} + +${thread_impl} + +extern "C" +{ + PortableServer::ObjectId * ${component}Engine_factory( + CORBA::ORB_ptr orb, + PortableServer::POA_ptr poa, + PortableServer::ObjectId * contId, + const char *instanceName, + const char *interfaceName) + { + + bool regist; + int numproc; + + MPI_Comm_rank( MPI_COMM_WORLD, &numproc ); + if( numproc == 0 ) + regist = true; + else + regist = false; + ${component}_i * my${component} = new ${component}_i(orb, poa, contId, instanceName, interfaceName, regist); + return my${component}->getId(); + } +} +""" +cxxCompo=Template(cxxCompo) + +hxxCompo=""" +//this file was generated by yacsgen +#ifndef __${component}_hxx2salome__ +#define __${component}_hxx2salome__ + +#include +#include "Utils_CorbaException.hxx" +#include CORBA_SERVER_HEADER(${module}) +#include "Utils_CorbaException.hxx" +#include // for std::auto_ptr + +${compodefs} + +// thread functions declaration +${thread_func_decl} + +// thread structures declaration +${thread_str_decl} + +class ${component}; // forward declaration + +class ${component}_i: ${inheritedclass} + public POA_${module}_ORB::${component}_Gen, + public ParaMEDMEM::ParaMEDMEMComponent_i +{ + +public: + ${component}_i(CORBA::ORB_ptr orb, + PortableServer::POA_ptr poa, + PortableServer::ObjectId * contId, + const char *instanceName, + const char *interfaceName, + bool regist); + virtual ~${component}_i(); + +${servicesdef} + +// (re)defined methods of Driver + +private: + std::auto_ptr<${component}> cppCompo_; + +}; + + +extern "C" + PortableServer::ObjectId * ${component}Engine_factory( + CORBA::ORB_ptr orb, + PortableServer::POA_ptr poa, + PortableServer::ObjectId * contId, + const char *instanceName, + const char *interfaceName); + + +#endif +""" +hxxCompo=Template(hxxCompo) + +cxxService=""" +${ret} ${component}_i::${service}(${parameters}) throw (SALOME::SALOME_Exception) +{ + beginService("${component}_i::${service}"); + BEGIN_OF("${component}_i::${service}"); + except_st *est; + void *ret_th; + pthread_t *th; + try + { +${body} + endService("${component}_i::${service}"); + END_OF("${component}_i::${service}"); + + } + catch (std::exception& ex) + { + THROW_SALOME_CORBA_EXCEPTION( ex.what(), SALOME::INTERNAL_ERROR ); + } +} +""" +cxxService=Template(cxxService) + + +compoMakefile=""" + +dist_lib${component}Engine_la_SOURCES = \ + ${component}_i.cxx + +lib${component}Engine_la_CXXFLAGS = -I$$(top_builddir)/idl $$(SALOME_INCLUDES) $$(MPI_INCLUDES) ${includes} +lib${component}Engine_la_LIBADD = ${libs} -L$$(top_builddir)/idl -lSalomeIDL${module} $${SALOME_LIBS} -lSalomeMPIContainer -lparamedmemcompo $$(FLIBS) + + +""" + +#, SALOME_MED::MED_Gen_Driver, SALOME::MultiCommClass +interfaceidlhxx=""" + interface ${component}_Gen: ${inherited} + { +${services} + }; +""" +interfaceidlhxx=Template(interfaceidlhxx) + + +compoMakefile=Template(compoMakefile) diff --git a/module_generator/hxx_tmpl_gui.py b/module_generator/hxx_tmpl_gui.py new file mode 100644 index 0000000..79ddd00 --- /dev/null +++ b/module_generator/hxx_tmpl_gui.py @@ -0,0 +1,602 @@ +# Copyright (C) 2009-2010 EDF R&D +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# + +try: + from string import Template +except: + from compat import Template,set + +hxxgui_cxx=""" +#include "${component_name}GUI.h" + +#include +#include +#include +#include +#include +#include + +#include + +#define COMPONENT_NAME "${component_name}" + +using namespace std; + +// Constructor +${component_name}GUI::${component_name}GUI() : + SalomeApp_Module( COMPONENT_NAME ), // Module name + LightApp_Module( COMPONENT_NAME ) +{ + // Initializations + default_bool = false; + default_int = 0; + default_spinInt = 0; + default_spinDbl = 0.; + default_selection = QString(""); + + // List for the selector + selector_strings.clear(); + selector_strings.append( tr( "PREF_LIST_TEXT_0" ) ); + selector_strings.append( tr( "PREF_LIST_TEXT_1" ) ); + selector_strings.append( tr( "PREF_LIST_TEXT_2" ) ); +} + +// Gets a reference to the module's engine +${component_name}_ORB::${component_name}_Gen_ptr ${component_name}GUI::Init${component_name}Gen( SalomeApp_Application* app ) +{ + Engines::Component_var comp = app->lcc()->FindOrLoad_Component( "FactoryServer",COMPONENT_NAME ); + ${component_name}_ORB::${component_name}_Gen_ptr clr = ${component_name}_ORB::${component_name}_Gen::_narrow(comp); + ASSERT(!CORBA::is_nil(clr)); + return clr; +} + +// Module's initialization +void ${component_name}GUI::initialize( CAM_Application* app ) +{ + // Get handle to Application, Desktop and Resource Manager + SalomeApp_Module::initialize( app ); + + Init${component_name}Gen( dynamic_cast( app ) ); + + QWidget* aParent = app->desktop(); + + SUIT_ResourceMgr* aResourceMgr = application()->resourceMgr(); + + // GUI items + // --> Create actions: 190 is linked to item in "File" menu + // and 901 is linked to both specific menu and toolbar + createAction( 190, tr( "TLT_MY_NEW_ITEM" ), QIcon(), tr( "MEN_MY_NEW_ITEM" ), tr( "STS_MY_NEW_ITEM" ), 0, aParent, false, + this, SLOT( OnMyNewItem() ) ); + + QPixmap aPixmap = aResourceMgr->loadPixmap( COMPONENT_NAME,tr( "ICON_${component_name}" ) ); + createAction( 901, tr( "TLT_${component_name}_ACTION" ), QIcon( aPixmap ), tr( "MEN_${component_name}_ACTION" ), tr( "STS_${component_name}_ACTION" ), 0, aParent, false, + this, SLOT( OnCallAction() ) ); + + // --> Create item in "File" menu + int aMenuId; + aMenuId = createMenu( tr( "MEN_FILE" ), -1, -1 ); + createMenu( separator(), aMenuId, -1, 10 ); + aMenuId = createMenu( tr( "MEN_FILE_${component_name}" ), aMenuId, -1, 10 ); + createMenu( 190, aMenuId ); + + // --> Create specific menu + aMenuId = createMenu( tr( "MEN_${component_name}" ), -1, -1, 30 ); + createMenu( 901, aMenuId, 10 ); + + // --> Create toolbar item + int aToolId = createTool ( tr( "TOOL_${component_name}" ) ); + createTool( 901, aToolId ); +} + +// Module's engine IOR +QString ${component_name}GUI::engineIOR() const +{ + CORBA::String_var anIOR = getApp()->orb()->object_to_string( Init${component_name}Gen( getApp() ) ); + return QString( anIOR.in() ); +} + +// Module's activation +bool ${component_name}GUI::activateModule( SUIT_Study* theStudy ) +{ + bool bOk = SalomeApp_Module::activateModule( theStudy ); + + setMenuShown( true ); + setToolShown( true ); + + return bOk; +} + +// Module's deactivation +bool ${component_name}GUI::deactivateModule( SUIT_Study* theStudy ) +{ + setMenuShown( false ); + setToolShown( false ); + + return SalomeApp_Module::deactivateModule( theStudy ); +} + +// Default windows +void ${component_name}GUI::windows( QMap& theMap ) const +{ + theMap.clear(); + theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea ); + theMap.insert( SalomeApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea ); +} + +// Action slot: Launched with action 190 +void ${component_name}GUI::OnMyNewItem() +{ + SUIT_MessageBox::warning( getApp()->desktop(),tr( "INF_${component_name}_TITLE" ), tr( "INF_${component_name}_TEXT" ), tr( "BUT_OK" ) ); +} + +// Action slot: Launched with action 901 +void ${component_name}GUI::OnCallAction() +{ + // Create a ${component_name} component + ${component_name}_ORB::${component_name}_Gen_ptr ${component_name}gen = ${component_name}GUI::Init${component_name}Gen( getApp() ); + + // Do the job... + // + // ${component_name}gen->method( arg1, arg2, ... ); + + // Open a dialog showing Preferences values (just to display something) + + // ****** Direct access to preferences: implementation at 12/12/05 ****** + // Comment out this section when "preferencesChanged" called back + SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr(); + + default_bool = mgr->booleanValue(COMPONENT_NAME, "default_bool", false); + + default_int = mgr->integerValue(COMPONENT_NAME, "default_integer", 3); + + default_spinInt = mgr->integerValue(COMPONENT_NAME, "default_spinint", 4); + + default_spinDbl = mgr->doubleValue(COMPONENT_NAME, "default_spindbl", 4.5); + + int selectorIndex = mgr->integerValue(COMPONENT_NAME, "default_selector"); + default_selection = (0<=selectorIndex && selectorIndex<=selector_strings.count() ? selector_strings[selectorIndex]: QString("None")); + // ****** End of section to be commented out ****** + + QString SUC = ( default_bool ? QString( tr ("INF_${component_name}_CHECK") ) : QString( tr("INF_${component_name}_UNCHECK") ) ) ; + + QString textResult = QString( tr( "RES_${component_name}_TEXT" ) ).arg(SUC).arg(default_int).arg(default_spinInt).arg(default_spinDbl).arg(default_selection); + SUIT_MessageBox::information( getApp()->desktop(), tr( "RES_${component_name}_TITLE" ), textResult, tr( "BUT_OK" ) ); +} + +void ${component_name}GUI::createPreferences() +{ + // A sample preference dialog + + // One only tab + int genTab = addPreference( tr( "PREF_TAB_GENERAL" ) ); + + // One only group + int defaultsGroup = addPreference( tr( "PREF_GROUP_DEFAULTS" ), genTab ); + + // A checkbox + addPreference( tr( "PREF_DEFAULT_BOOL" ), defaultsGroup, LightApp_Preferences::Bool, COMPONENT_NAME, "default_bool" ); + + // An entry for integer + addPreference( tr( "PREF_DEFAULT_INTEGER" ), defaultsGroup, LightApp_Preferences::Integer, COMPONENT_NAME, "default_integer" ); + + // An integer changed by spinbox + int spinInt = addPreference( tr( "PREF_DEFAULT_SPININT" ), defaultsGroup, LightApp_Preferences::IntSpin, COMPONENT_NAME, "default_spinint" ); + setPreferenceProperty( spinInt, "min", 0 ); + setPreferenceProperty( spinInt, "max", 20 ); + setPreferenceProperty( spinInt, "step", 2 ); + + // A Double changed by spinbox + int spinDbl = addPreference( tr( "PREF_DEFAULT_SPINDBL" ), defaultsGroup, LightApp_Preferences::DblSpin, COMPONENT_NAME, "default_spindbl" ); + setPreferenceProperty( spinDbl, "min", 1 ); + setPreferenceProperty( spinDbl, "max", 10 ); + setPreferenceProperty( spinDbl, "step", 0.1 ); + + // A choice in a list + int options = addPreference( tr( "PREF_DEFAULT_SELECTOR" ), defaultsGroup, LightApp_Preferences::Selector, COMPONENT_NAME, "default_selector" ); + QList indices; + indices.append( 0 ); + indices.append( 1 ); + indices.append( 2 ); + setPreferenceProperty( options, "strings", selector_strings ); + setPreferenceProperty( options, "indexes", indices ); +} + +void ${component_name}GUI::preferencesChanged( const QString& sect, const QString& name ) +{ +// ****** This is normal way: Not yet called back at 12/12/05 ****** + SUIT_ResourceMgr* mgr = SUIT_Session::session()->resourceMgr(); + if( sect==COMPONENT_NAME ) + { + if( name=="default_bool" ) + default_bool = mgr->booleanValue(COMPONENT_NAME, "default_bool", false); + if( name=="default_integer" ) + default_int = mgr->integerValue(COMPONENT_NAME, "default_integer", 3); + if( name=="default_spinint" ) + default_spinInt = mgr->integerValue(COMPONENT_NAME, "default_spinint", 4); + if( name=="default_spindbl" ) + default_spinDbl = mgr->doubleValue(COMPONENT_NAME, "default_spindbl", 4.5); + if( name=="default_selector" ) + { + int selectorIndex = mgr->integerValue(COMPONENT_NAME, "default_selector"); + default_selection = (0<=selectorIndex && selectorIndex<=selector_strings.count() ? selector_strings[selectorIndex]: QString("None")); + } + } +} + +// Export the module +extern "C" { + CAM_Module* createModule() + { + return new ${component_name}GUI(); + } +} +""" +hxxgui_cxx=Template(hxxgui_cxx) + +hxxgui_h=""" +#ifndef _${component_name}GUI_H_ +#define _${component_name}GUI_H_ + +#include + +#include +#include CORBA_CLIENT_HEADER(${component_name}) + +class SalomeApp_Application; +class ${component_name}GUI: public SalomeApp_Module +{ + Q_OBJECT + +public: + ${component_name}GUI(); + + void initialize( CAM_Application* ); + QString engineIOR() const; + void windows( QMap& ) const; + + static ${component_name}_ORB::${component_name}_Gen_ptr Init${component_name}Gen( SalomeApp_Application* ); + + virtual void createPreferences(); + virtual void preferencesChanged( const QString&, const QString& ); + +public slots: + bool deactivateModule( SUIT_Study* ); + bool activateModule( SUIT_Study* ); + +protected slots: + void OnMyNewItem(); + void OnCallAction(); + +private: + bool default_bool; + int default_int; + int default_spinInt; + double default_spinDbl; + QString default_selection; + + QStringList selector_strings; + +}; + +#endif +""" +hxxgui_h=Template(hxxgui_h) +hxxgui_icon_ts=""" + + + + @default + + ICON_${component_name} + Exec${component_name}.png + + + +""" +hxxgui_icon_ts=Template(hxxgui_icon_ts) +hxxgui_message_en=""" + + + + @default + + TLT_MY_NEW_ITEM + A ${component_name} owned menu item + + + MEN_MY_NEW_ITEM + My menu + + + STS_MY_NEW_ITEM + Display a simple dialog + + + TLT_${component_name}_ACTION + Open ${component_name} dialog + + + MEN_FILE + File + + + MEN_FILE_${component_name} + ${component_name} menu + + + MEN_${component_name} + ${component_name} + + + TOOL_${component_name} + ${component_name} + + + + ${component_name}GUI + + BUT_OK + OK + + + BUT_CANCEL + Cancel + + + INF_${component_name}_TITLE + ${component_name} Information + + + INF_${component_name}_TEXT + This is just a test + + + INF_${component_name}_CHECK + checked + + + INF_${component_name}_UNCHECK + Unchecked + + + RES_${component_name}_TITLE + Sample ${component_name} dialog + + + RES_${component_name}_TEXT + Preferences are: \n\tCheckbox: %1\n\tInteger: %2\n\tInteger2: %3\n\tDouble: %4\n\tText: %5 + + + PREF_TAB_GENERAL + General + + + PREF_GROUP_DEFAULTS + Default Values + + + PREF_DEFAULT_BOOL + Check me + + + PREF_DEFAULT_INTEGER + Enter an integer : + + + PREF_DEFAULT_SPININT + Click arrows (integer) : + + + PREF_DEFAULT_SPINDBL + Click arrows (double) + + + PREF_DEFAULT_SELECTOR + Select an option + + + PREF_LIST_TEXT_0 + first option + + + PREF_LIST_TEXT_1 + second option + + + PREF_LIST_TEXT_2 + third option + + + +""" +hxxgui_message_en=Template(hxxgui_message_en) +hxxgui_message_fr=""" + + + + @default + + TLT_MY_NEW_ITEM + Un article de menu propre à ${component_name} + + + MEN_MY_NEW_ITEM + Mon menu + + + STS_MY_NEW_ITEM + Affiche une boite de dialogue simple + + + TLT_${component_name}_ACTION + Ouvre la boite de dialogue de ${component_name} + + + MEN_FILE + File + + + MEN_FILE_${component_name} + Menu de ${component_name} + + + MEN_${component_name} + ${component_name} + + + TOOL_${component_name} + ${component_name} + + + + ${component_name}GUI + + BUT_OK + OK + + + BUT_CANCEL + Annuler + + + INF_${component_name}_TITLE + Information ${component_name} + + + INF_${component_name}_TEXT + Ceci est un simple test + + + INF_${component_name}_CHECK + coche + + + INF_${component_name}_UNCHECK + decoche + + + RES_${component_name}_TITLE + Dialogue example de ${component_name} + + + RES_${component_name}_TEXT + Les preferences sont : \n\tCase a cocher : %1\n\tEntier : %2\n\tEntier2 : %3\n\tDouble : %4\n\tTexte : %5 + + + PREF_TAB_GENERAL + General + + + PREF_GROUP_DEFAULTS + valeur par defaut + + + PREF_DEFAULT_BOOL + Cochez-moi + + + PREF_DEFAULT_INTEGER + Entrez un entier : + + + PREF_DEFAULT_SPININT + cliquez sur les fleches (entier) + + + PREF_DEFAULT_SPINDBL + cliquez sur les fleches (double) + + + PREF_DEFAULT_SELECTOR + Choisissez une option + + + PREF_LIST_TEXT_0 + premiere option + + + PREF_LIST_TEXT_1 + deuxieme option + + + PREF_LIST_TEXT_2 + troisieme option + + + +""" +hxxgui_message_fr=Template(hxxgui_message_fr) +hxxgui_config=""" +language=en +""" +hxxgui_config=Template(hxxgui_config) +hxxgui_xml_en=""" + + + + + + + + + + + + + + + + + + + + + + + + + +""" +hxxgui_xml_en=Template(hxxgui_xml_en) +hxxgui_xml_fr=""" + + + + + + + + + + + + + + + + + + + + + + +""" +hxxgui_xml_fr=Template(hxxgui_xml_fr) + diff --git a/module_generator/hxxparacompo.py b/module_generator/hxxparacompo.py new file mode 100644 index 0000000..12d9bc7 --- /dev/null +++ b/module_generator/hxxparacompo.py @@ -0,0 +1,446 @@ +# Copyright (C) 2009-2010 CEA DEN +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +# +""" + Module that generates SALOME c++ Component from a non SALOME c++ component (its header and its shares library) +""" + +debug=1 +import os +from gener import Component, Invalid +from hxx_para_tmpl import cxxService, hxxCompo, cxxCompo, compoMakefile +from module_generator import Service +import string +from tempfile import mkstemp +from yacstypes import corba_rtn_type,moduleTypes + +class HXX2SALOMEParaComponent(Component): + def __init__(self, hxxfile , cpplib , cpp_path ): + # search a file within a directory tree + import fnmatch + def search_file(pattern, root): + matches = [] + for path, dirs, files in os.walk(os.path.abspath(root)): + for filename in fnmatch.filter(files, pattern): + matches.append(os.path.join(path, filename)) + return matches + + hxxfileful=search_file(hxxfile,cpp_path) + cpplibful=search_file(cpplib,cpp_path) + assert len(hxxfileful) > 0 ,'Error in HXX2SALOMEComponent : file ' + hxxfile + ' not found in ' + cpp_path + assert len(cpplibful) > 0 ,'Error in HXX2SALOMEComponent : file ' + cpplib + ' not found in ' + cpp_path + + self.hxxfile=hxxfile # to include it in servant implementation + + # grab name of c++ component + from hxx_awk import parse01,parse1,parse2,parse3 + cmd1="""awk '$1 == "class" && $0 !~ /;/ {print $2}' """ + hxxfileful[0] + """|awk -F: '{printf "%s",$1}' """ + f=os.popen(cmd1) + class_name=f.readlines()[0] + name=class_name + print "classname=",class_name + + if cpplib[:3]=="lib" and cpplib[-3:]==".so": + cpplibname=cpplib[3:-3] # get rid of lib and .so, to use within makefile.am + else: + cpplibname=class_name+"CXX" # the default name + + f.close() + + # create temporary awk files + (fd01,p01n)=mkstemp() + f01=os.fdopen(fd01,"w") + f01.write(parse01) + f01.close() + + (fd1,p1n)=mkstemp() + f1=os.fdopen(fd1,"w") + f1.write(parse1) + f1.close() + + (fd2,p2n)=mkstemp() + f2=os.fdopen(fd2,"w") + f2.write(parse2) + f2.close() + + (fd3,p3n)=mkstemp() + f3=os.fdopen(fd3,"w") + f3.write(parse3) + f3.close() + + # awk parsing of hxx files - result written in file parse_type_result + cmd2="cat " + hxxfileful[0] + " | awk -f " + p01n + """ | sed 's/virtual //g' | sed 's/MEDMEM_EXPORT//g' | sed 's/throw.*;/;/g' | awk -f """ + p1n + " | awk -f " + p2n + " | awk -v class_name=" + class_name + " -f " + p3n + os.system(cmd2) + os.remove(p01n) + os.remove(p1n) + os.remove(p2n) + os.remove(p3n) + + # Retrieve the information which was generated in the file parse_type_result. + # The structure of the file is : + # + # Function return_type function_name + # [arg1_type arg1_name] + # [arg2_type arg2_name] + # ... + # The service names are stored in list_of_services + # The information relative to a service (called service_name) is stored in the dictionnary service_definition[service_name] + from hxx_awk import cpp2idl_mapping + from hxx_awk import cpp2yacs_mapping + cpp2yacs_mapping["const ParaMEDMEM::MEDCouplingFieldDouble*"]="SALOME_MED/MPIMEDCouplingFieldDoubleCorbaInterface" + cpp2yacs_mapping["const ParaMEDMEM::MEDCouplingFieldDouble&"]="SALOME_MED/MPIMEDCouplingFieldDoubleCorbaInterface" + cpp2yacs_mapping["ParaMEDMEM::MEDCouplingFieldDouble*&"]="SALOME_MED/MPIMEDCouplingFieldDoubleCorbaInterface" + cpp2yacs_mapping["ParaMEDMEM::MEDCouplingFieldDouble*"]="SALOME_MED/MPIMEDCouplingFieldDoubleCorbaInterface" + list_of_services=[] + list_of_services_with_in_fields=[] + service_definition={} + result_parsing=open("parse_type_result","r") + for line in result_parsing.readlines(): + line=line[0:-1] # get rid of trailing \n + words = string.split(line,';') + + if len(words) >=3 and words[0] == "Function": # detect a new service + function_name=words[2] + if function_name != "getInputFieldTemplate": + list_of_services.append(function_name) + service_definition[function_name]={} + service_definition[function_name]["ret"]=words[1] # return type + service_definition[function_name]["inports"]=[] + service_definition[function_name]["outports"]=[] + service_definition[function_name]["ports"]=[] + service_definition[function_name]["impl"]=[] + service_definition[function_name]["thread_func_decl"]=[] + service_definition[function_name]["thread_str_decl"]=[] + + if len(words) == 2 and function_name != "getInputFieldTemplate": # an argument type and argument name of a previous service + typename=words[0] + argname=words[1] + service_definition[list_of_services[-1]]["ports"].append( (argname,typename) ) # store in c++ order the arg names + + # separate in from out parameters + inout=cpp2idl_mapping[typename][0:2] + assert inout=="in" or inout=="ou",'Error in table cpp2idl_mapping' + if inout == "in": + service_definition[list_of_services[-1]]["inports"].append( (argname,typename) ) + if typename=="const ParaMEDMEM::MEDCouplingFieldDouble*": + list_of_services_with_in_fields.append(list_of_services[-1]) + else: + service_definition[list_of_services[-1]]["outports"].append( (argname,typename) ) + for servNane in list_of_services_with_in_fields: + service_definition[servNane]["inports"].append(("coupling","const char*")) + service_definition[servNane]["ports"].append(("coupling","const char*")) + + if service_definition.has_key('getInputFieldTemplate'): + del service_definition['getInputFieldTemplate'] + # + # generate implementation of c++ servant + # store it in service_definition[serv]["impl"] + # + from hxx_awk import cpp_impl_a,cpp_impl_b,cpp_impl_c # these tables contain the part of code which depends upon c++ types + cpp_impl_b["ParaMEDMEM::MEDCouplingFieldDouble*"]="""\tParaMEDMEM::MPIMEDCouplingFieldDoubleServant * _rtn_field_i = new ParaMEDMEM::MPIMEDCouplingFieldDoubleServant(_orb,this,_rtn_cpp); +\t_rtn_cpp->decrRef(); +\tSALOME_MED::MPIMEDCouplingFieldDoubleCorbaInterface_ptr _rtn_ior = _rtn_field_i->_this();\n""" + cpp_impl_a["const ParaMEDMEM::MEDCouplingFieldDouble*"]="\tParaMEDMEM::MEDCouplingFieldDouble* _%(arg)s=cppCompo_->getInputFieldTemplate();\n\t_setInputField(coupling,%(arg)s,_%(arg)s);\n" + + from yacstypes import corbaTypes,corbaOutTypes + format_thread_signature="void * th_%s(void * st);" # this thread declaration will be included in servant's header + format_thread_struct="typedef struct {\n int ip;\n Engines::IORTab* tior;\n%(arg_decl)s} thread_%(serv_name)s_str;" # this thread declaration will be included in servant's header + format_thread_create=""" +// create threads to forward to other processes the service invocation + if(_numproc == 0) + { + th = new pthread_t[_nbproc]; + for(int ip=1;ip<_nbproc;ip++) + { + %(init_thread_str)s + pthread_create(&(th[ip]),NULL,th_%(serv_name)s,(void*)st); + } + } +""" + s_thread_join=""" +// waiting for all threads to complete + if(_numproc == 0) + { + for(int ip=1;ip<_nbproc;ip++) + { + pthread_join(th[ip],&ret_th); + est = (except_st*)ret_th; + if(est->exception) + { + ostringstream msg; + msg << "[" << ip << "] " << est->msg; + THROW_SALOME_CORBA_EXCEPTION(msg.str().c_str(),SALOME::INTERNAL_ERROR); + } + delete est; + } + delete[] th; + } +""" + format_thread_impl=""" +void *th_%(serv_name)s(void *s) +{ + ostringstream msg; + thread_%(serv_name)s_str *st = (thread_%(serv_name)s_str*)s; + except_st *est = new except_st; + est->exception = false; + + try + { + %(module)s_ORB::%(component_name)s_Gen_var compo = %(module)s_ORB::%(component_name)s_Gen::_narrow((*(st->tior))[st->ip]); + compo->%(serv_name)s(%(arg_thread_invocation)s); + } + catch(const SALOME::SALOME_Exception &ex) + { + est->exception = true; + est->msg = ex.details.text; + } + catch(const CORBA::Exception &ex) + { + est->exception = true; + msg << "CORBA::Exception: " << ex; + est->msg = msg.str(); + } + delete st; + return((void*)est); +} + +""" + + self.thread_impl="" # the implementation of the thread functions used to invoque services on slave processors + for serv in list_of_services: + if debug: + print "service : ",serv + print " inports -> ",service_definition[serv]["inports"] + print " outports -> ",service_definition[serv]["outports"] + print " return -> ",service_definition[serv]["ret"] + + # Part 0 : specific treatments for parallel components (call threads to forward the invocation to the service to all processes) + service_definition[serv]["thread_func_decl"]=format_thread_signature % serv + arg_declaration="" + arg_thread_invocation="" + init_thread_str="thread_%s_str *st = new thread_%s_str;" % (serv,serv) + init_thread_str+="\n st->ip = ip;" + init_thread_str+="\n st->tior = _tior;" + for (argname,argtype) in service_definition[serv]["inports"]: + arg_declaration+=" "+corbaTypes[cpp2yacs_mapping[argtype]]+" "+argname+";\n" + init_thread_str+="\n st->"+argname+" = "+argname+";" + for (argname,argtype) in service_definition[serv]["outports"]: + arg_declaration+=" "+corbaOutTypes[cpp2yacs_mapping[argtype]]+" "+argname+";\n" + init_thread_str+="\n st->"+argname+" = "+argname+";" + for (argname,argtype) in service_definition[serv]["ports"]: + arg_thread_invocation+="st->"+argname+", " + if len(arg_thread_invocation)>0: + arg_thread_invocation=arg_thread_invocation[0:-2] # get rid of trailing comma + service_definition[serv]["thread_str_decl"]=format_thread_struct % { "serv_name" : serv, "arg_decl" : arg_declaration } + s_thread_call=format_thread_create % { "serv_name" : serv , "init_thread_str" : init_thread_str} + # within format_thread_impl the treatment of %(module) is postponed in makecxx() because we don't know here the module name + self.thread_impl+=format_thread_impl % {"serv_name" : serv , "arg_thread_invocation" : arg_thread_invocation , "component_name" : name, "module" : "%(module)s" } + + # Part 1 : Argument pre-processing + s_argument_processing="//\tArguments processing\n" + for (argname,argtype) in service_definition[serv]["inports"] + service_definition[serv]["outports"]: + format=cpp_impl_a[argtype] + s_argument_processing += format % {"arg" : argname } + if s_argument_processing=="//\tArguments processing\n": # if there was no args + s_argument_processing="" + + # if an argument called name is of type const char*, this argument is transmitted to getInputFieldTemplate() + # => we insert "name" between the bracket of getInputFieldTemplate() + indice_getInputFieldTemplate=s_argument_processing.find ("cppCompo_->getInputFieldTemplate();") + if s_argument_processing.find ("const std::string _name") != -1 and indice_getInputFieldTemplate != -1: + ind_insertion=indice_getInputFieldTemplate+33 + s_argument_processing=s_argument_processing[:ind_insertion]+"name"+s_argument_processing[ind_insertion:-1] + + # Part 2 : Call to the underlying c++ function + s_call_cpp_function="//\tCall cpp component\n\t" + rtn_type=service_definition[serv]["ret"] + if rtn_type == "void" : # if return type is void, the call syntax is different + s_call_cpp_function += "cppCompo_->%s(" % serv + else: + s_call_cpp_function += "%s _rtn_cpp = cppCompo_->%s(" % (rtn_type ,serv ) + + for (argname,argtype) in service_definition[serv]["ports"]: + # special treatment for some arguments + post="" + pre="" + if string.find(cpp_impl_a[argtype],"auto_ptr" ) != -1 : + post=".get()" # for auto_ptr argument, retrieve the raw pointer behind + if argtype == "const MEDMEM::MESH&" or argtype == "const MEDMEM::SUPPORT&" : + pre="*" # we cannot create MESHClient on the stack (private constructor), so we create it on the heap and dereference it + post+="," # separator between arguments + if(argname!="coupling"): # coupling is a special argument added for input fields - it should not be transferred to the underlying c++ component + s_call_cpp_function += " %s_%s%s" % ( pre,argname,post) + if s_call_cpp_function[-1]==',': + s_call_cpp_function=s_call_cpp_function[0:-1] # get rid of trailing comma + s_call_cpp_function=s_call_cpp_function+');\n' + + # Part 3.a : Out Argument Post-processing + s_argument_postprocessing="//\tPost-processing & return\n" + for (argname,argtype) in service_definition[serv]["outports"]: + format=cpp_impl_c[argtype] + s_argument_postprocessing += format % {"arg" : argname, "module" : "%(module)s" } # the treatment of %(module) is postponed in makecxx() + # because we don't know here the module name + # Part 3.b : In Argument Post-processing + for (argname,argtype) in service_definition[serv]["inports"]: + if cpp_impl_c.has_key(argtype): # not all in types require a treatment + format=cpp_impl_c[argtype] + s_argument_postprocessing += format % {"arg" : argname, "module" : "%(module)s" } # id : treatment of %(module) is postponed in makecxx + + # Part 3.c : return processing + s_rtn_processing=cpp_impl_b[rtn_type] + if rtn_type != "void": + s_rtn_processing += "\treturn _rtn_ior;" + + service_definition[serv]["impl"] = s_thread_call + s_argument_processing + s_call_cpp_function + s_thread_join + s_argument_postprocessing + s_rtn_processing + if debug: + print "implementation :\n",service_definition[serv]["impl"] + + # + # Create a list of Service objects (called services), and give it to Component constructor + # + services=[] + self.use_medmem=False + self.use_medcoupling=False + self.thread_func_decl=[] + self.thread_str_decl=[] + for serv in list_of_services: + # for inports and outports, Service class expects a list of tuples, each tuple containing the name and the yacs type of the port + # thus we need to convert c++ types to yacs types (we use for that the cpp2yacs_mapping table + inports=[] + for i in range( len(service_definition[serv]["inports"]) ): + inports.append( [service_definition[serv]["inports"][i][0], cpp2yacs_mapping[service_definition[serv]["inports"][i][1]] ] ) + outports=[] + for i in range( len(service_definition[serv]["outports"]) ): + outports.append( [service_definition[serv]["outports"][i][0], cpp2yacs_mapping[service_definition[serv]["outports"][i][1]] ] ) + + Return="void" + if service_definition[serv]["ret"] != "void": + Return=cpp2yacs_mapping[service_definition[serv]["ret"]] + + # find out if component uses medmem types and/or medcoupling types + for (argname,argtype) in inports + outports + [("return",Return)]: + if moduleTypes[argtype]=="MED": + if argtype.count("Coupling")>0: + self.use_medcoupling=True + else: + self.use_medmem=True + break + + code=service_definition[serv]["impl"] + if debug: + print "service : ",serv + print " inports -> ",service_definition[serv]["inports"] + print " converted inports -> ",inports + print " outports -> ",service_definition[serv]["outports"] + print " converted outports -> ",outports + print " Return -> ",service_definition[serv]["ret"] + print " converted Return -> ",Return + + services.append(Service(serv, + inport=inports, + outport=outports, + ret=Return, + defs="", + body=code, + ) ) + self.thread_func_decl.append(service_definition[serv]["thread_func_decl"]) + self.thread_str_decl.append(service_definition[serv]["thread_str_decl"]) + Includes="-I${"+name+"CPP_ROOT_DIR}/include" + Libs="-L${"+name+"CPP_ROOT_DIR}/lib -l"+cpplibname + Compodefs="" + Inheritedclass="" + self.inheritedconstructor="" + Compodefs=""" +#include CORBA_SERVER_HEADER(MEDCouplingCorbaServantTest) +#include "MPIMEDCouplingFieldDoubleServant.hxx" +""" + + Component.__init__(self, name, services, impl="CPP", libs=Libs, + rlibs="", includes=Includes, kind="lib", + sources=None,inheritedclass=Inheritedclass, + compodefs=Compodefs) + + def makeCompo(self, gen): + """generate files for C++ component + + return a dict where key is the file name and value is the content of the file + """ + cxxfile = "%s_i.cxx" % self.name + hxxfile = "%s_i.hxx" % self.name + return {"Makefile.am":gen.makeMakefile(self.getMakefileItems(gen)), + cxxfile:self.makecxx(gen), + hxxfile:self.makehxx(gen) + } + + def getMakefileItems(self,gen): + makefileItems={"header":""" +include $(top_srcdir)/adm_local/make_common_starter.am + +"""} + makefileItems["lib_LTLIBRARIES"]=["lib"+self.name+"Engine.la"] + makefileItems["salomeinclude_HEADERS"]=["%s_i.hxx" % self.name] + makefileItems["body"]=compoMakefile.substitute(module=gen.module.name, + component=self.name, + libs=self.libs, + includes=self.includes) + return makefileItems + + def makehxx(self, gen): + """return a string that is the content of .hxx file + """ + services = [] + for serv in self.services: + service = " %s %s(" % (corba_rtn_type(serv.ret,gen.module.name),serv.name) + service = service+gen.makeArgs(serv)+") throw (SALOME::SALOME_Exception);" + services.append(service) + servicesdef = "\n".join(services) + + inheritedclass=self.inheritedclass + thread_func_decl="\n".join(self.thread_func_decl) + thread_str_decl="\n".join(self.thread_str_decl) + if debug: + print "thread_func_decl : " + print thread_func_decl + print "thread_str_decl : " + print thread_str_decl + + if self.inheritedclass: + inheritedclass= " public virtual " + self.inheritedclass + "," + + return hxxCompo.substitute(component=self.name, module=gen.module.name, thread_func_decl=thread_func_decl, + thread_str_decl=thread_str_decl, servicesdef=servicesdef, inheritedclass=inheritedclass, + compodefs=self.compodefs) + + def makecxx(self, gen, exe=0): + """return a string that is the content of .cxx file + """ + services = [] + inits = [] + defs = [] + for serv in self.services: + defs.append(serv.defs) + service = cxxService.substitute(component=self.name, service=serv.name,ret=corba_rtn_type(serv.ret,gen.module.name), + parameters=gen.makeArgs(serv), + body=serv.body % {"module":gen.module.name+"_ORB"} ) + services.append(service) + return cxxCompo.substitute(component=self.name, cxx_include_file=self.hxxfile, + inheritedconstructor=self.inheritedconstructor, + servicesdef="\n".join(defs), + servicesimpl="\n".join(services), + thread_impl=self.thread_impl % {"module":gen.module.name} ) + -- 2.39.2