Salome HOME
17a16d9d3595f5d734d84cc259710ff24cf6b81b
[modules/med.git] / src / MEDCouplingCorba_Swig / MEDCouplingCorbaTypemaps.i
1 // Copyright (C) 2007-2023  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 // Author : Anthony Geay (CEA/DEN)
20
21 template<class TServant>
22 static PyObject *buildServantAndActivateCore(const typename TServant::CppType *pt, std::string& iorOut)
23 {
24   int argc=0;
25   TServant *serv=new TServant(pt);
26   CORBA::ORB_var orb=CORBA::ORB_init(argc,0);
27   CORBA::Object_var obj=orb->resolve_initial_references("RootPOA");
28   PortableServer::POA_var poa=PortableServer::POA::_narrow(obj);
29   PortableServer::POAManager_var mgr=poa->the_POAManager();
30   mgr->activate();
31   CORBA::Object_var ret=serv->_this();
32   char *ior=orb->object_to_string(ret);
33   iorOut=ior;
34   PyObject *iorPython=PyString_FromString(ior);
35   PyObject* pdict=PyDict_New();
36   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
37   PyRun_String("import MEDCouplingCorbaServant_idl", Py_single_input, pdict, pdict);
38   PyRun_String("import CORBA", Py_single_input, pdict, pdict);
39   PyRun_String("orbTmp15634=CORBA.ORB_init([''])",Py_single_input,pdict, pdict);
40   PyObject* orbPython=PyDict_GetItemString(pdict,"orbTmp15634");
41   PyObject *corbaObj=PyObject_CallMethod(orbPython,const_cast<char*>("string_to_object"),const_cast<char*>("O"),iorPython);
42   Py_DECREF(pdict);
43   Py_DECREF(iorPython);
44   CORBA::string_free(ior);
45   return corbaObj;
46 }
47
48 template<class TServant>
49 static PyObject *buildServantAndActivate(const typename TServant::CppType *pt)
50 {
51   std::string dummy;
52   return buildServantAndActivateCore<TServant>(pt,dummy);
53 }
54
55 template<class TServant>
56 static PyObject *buildServantAndActivate2(const typename TServant::CppType *pt)
57 {
58   std::string ret1Cpp;
59   PyObject *ret0Py=buildServantAndActivateCore<TServant>(pt,ret1Cpp);
60   PyObject *ret=PyTuple_New(2);
61   PyTuple_SetItem(ret,0,ret0Py);
62   PyTuple_SetItem(ret,1,PyString_FromString(ret1Cpp.c_str()));
63   return ret;
64 }