]> SALOME platform Git repositories - modules/yacs.git/commitdiff
Salome HOME
try fix REFCOUNT_TEMPLATE...
authorPaul RASCLE <paul.rascle@edf.fr>
Wed, 3 Jan 2018 21:15:03 +0000 (22:15 +0100)
committerPaul RASCLE <paul.rascle@edf.fr>
Sun, 7 Jan 2018 14:21:06 +0000 (15:21 +0100)
src/engine_swig/CMakeLists.txt
src/engine_swig/IteratorPy3.hxx
src/engine_swig/engtypemaps.i
src/engine_swig/pilot.i

index af9df8b3c2f84b578ab5675f109d7c374c624064..320824aa66ce9d8ed713bcde57c58c42f3fcf6aa 100644 (file)
@@ -106,7 +106,8 @@ ENDFOREACH()
 
 SET(SWIGINCLUDES ${ENGINEINCL}  
     "${PROJECT_SOURCE_DIR}/src/bases/yacsconfig.h" 
-    "ExecutorSwig.hxx")
+    "ExecutorSwig.hxx"
+    "IteratorPy3.hxx")
 
 SET(SWIG_MODULE_pilot_EXTRA_DEPS engtypemaps.i ${SWIGINCLUDES})
 SWIG_ADD_MODULE(pilot python pilot.i ExecutorSwig.cxx)
index dd480dfda571e56e5160a08e0225bd8ac3d0fb67..dd39f1e8958fe289c11dccf93647902407752fd0 100644 (file)
 //
 // Author: Nicolas GEIMER (EDF)
 
-#ifndef ITERATORPY3_HXX_
-#define ITERATORPY3_HXX_
+#ifndef __ITERATORPY3_HXX__
+#define __ITERATORPY3_HXX__
 
-template <typename U> class StopIterator {};
-template <typename U> class Iterator {
+template <typename U> class StopIteratorPy3 {};
+
+template <typename U> class IteratorPy3 {
     public:
-        Iterator( typename std::map<std::string,U*>::iterator _cur, typename std::map<std::string,U*>::iterator _end) : cur(_cur), end(_end) {}
-        Iterator* __iter__()
+        IteratorPy3( typename std::map<std::string,U*>::iterator _cur, typename std::map<std::string,U*>::iterator _end) : cur(_cur), end(_end) {}
+        ~IteratorPy3() {}
+        IteratorPy3* __iter__()
         {
             return this;
         }
@@ -33,5 +35,5 @@ template <typename U> class Iterator {
         typename std::map<std::string,U*>::iterator end;
 };
 
-#endif /* ITERATORPY3_HXX */
+#endif /* __ITERATORPY3_HXX__ */
 
index 0d1675b333117ca234940b16336963e98d9a002d..0771a1c7eedd893b1011b732dd869ed38b0abaae 100644 (file)
@@ -62,6 +62,7 @@ catch (const CORBA::SystemException& ex) { \
 #include "OutputDataStreamPort.hxx"
 #include "OptimizerLoop.hxx"
 #include "HomogeneousPoolContainer.hxx"
+#include "IteratorPy3.hxx"
 
 #include <sstream>
 
@@ -251,6 +252,8 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons
 
 %}
 
+// ----------------------------------------------------------------------------
+
 #if SWIG_VERSION >= 0x010329
 %template()        std::list<int>;
 %template()        std::list<std::string>;
@@ -307,6 +310,8 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons
 #endif
 #endif
 
+// ----------------------------------------------------------------------------
+
 #ifdef SWIGPYTHON
 
 %typecheck(SWIG_TYPECHECK_POINTER) YACS::ENGINE::Any*
@@ -652,6 +657,8 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons
 
 #endif
 
+// ----------------------------------------------------------------------------
+
 /*
  * Exception section
  */
@@ -773,6 +780,8 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons
  * End of Exception section
  */
 
+// ----------------------------------------------------------------------------
+
 /*
  * Ownership section
  */
@@ -803,6 +812,8 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons
  * End of Reference counting section
  */
 
+// ----------------------------------------------------------------------------
+
 /*
 %wrapper %{
   namespace swig {
@@ -830,11 +841,18 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons
 %}
 */
 
-%define REFCOUNT_TEMPLATE(tname, T...)
+// ----------------------------------------------------------------------------
+
+%include "IteratorPy3.hxx"
+%include "exception.i"
+
+%define REFCOUNT_TEMPLATE(tname, T)
 /*
  This macro is a special wrapping for map with value type which derives from RefCounter.
  To overload standard SWIG wrapping we define a full specialization of std::map
- with %extend for 4 basic methods : getitem, setitem, delitem and keys.
+ with %extend for 5 basic methods : getitem, setitem, delitem, keys and iter.
+ We also provide an iterator and %extend with the __next__ method : required in python 3.
+ (see https://docs.python.org/3/library/stdtypes.html#iterator-types)
  Then we complete the interface by deriving the shadow wrapper from
  the python mixin class (UserDict.DictMixin / collections.MutableMapping with Python 3).
  Do not forget to declare the new shadow class to SWIG with tname_swigregister(tname).
@@ -842,30 +860,34 @@ static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, cons
  call decrRef (see feature("unref") for RefCounter).
 */
 
-%include "exception.i"
-%exception Iterator::next {
+%exception IteratorPy3<T>::__next__
+{
   try
   {
     $action  // calls %extend function next() below
   }
-  catch (StopIterator)
+  catch (StopIteratorPy3<T>)
   {
     PyErr_SetString(PyExc_StopIteration, "End of iterator");
     return NULL;
   }
 }
 
-%extend Iterator
+%extend IteratorPy3<T>
 {
-  std::map<std::string,U*>& next()
+//  std::pair<const std::string,T*>& __next__()
+  std::string __next__()
   {
     if ($self->cur != $self->end)
     {
       // dereference the iterator and return reference to the object,
       // after that it increments the iterator
-      return *$self->cur++;
+      //return *self->cur++;
+      std::string key= self->cur->first;
+      *self->cur++;
+      return key;
     }
-    throw StopIterator();
+    throw StopIteratorPy3<T>();
   }
 }
 
@@ -901,28 +923,30 @@ public:
   void __delitem__(std::string name)
     {
       std::map<std::string, T* >::iterator i = self->find(name);
-      if (i != self->end()){
+      if (i != self->end())
+      {
         i->second->decrRef();
         self->erase(i);
       }
       else
         throw std::out_of_range("key not found");
     }
-  PyObject* keys() {
+  PyObject* keys()
+    {
       int pysize = self->size();
       PyObject* keyList = PyList_New(pysize);
       std::map<std::string, T* >::const_iterator i = self->begin();
-      for (int j = 0; j < pysize; ++i, ++j) {
+      for (int j = 0; j < pysize; ++i, ++j)
+      {
         PyList_SET_ITEM(keyList, j, PyUnicode_FromString(i->first.c_str()));
       }
       return keyList;
     }
-  Iterator<T> __iter__()
+  IteratorPy3<T> __iter__()
   {
-    // return a constructed Iterator object
-    return typename Iterator($self->begin(), $self->end());
+    // return a constructed IteratorPy3 object
+    return IteratorPy3<T>($self->begin(), $self->end());
   }
-
   int __len__()
   {
       int pysize = self->size();
@@ -930,12 +954,13 @@ public:
   }
 }
 };
-%rename(__next__) Iterator::next;
 %newobject std::map<std::string,T* >::__getitem__;
 %newobject std::map<std::string,T* >::__iter__;
-%template()   std::pair<std::string, T* >;
-%template(tname)    std::map<std::string, T* >;
-%pythoncode{
+%template(tname##it)  IteratorPy3<T >;
+%template()           std::pair<std::string, T* >;
+%template(tname)      std::map<std::string, T* >;
+%pythoncode
+{
 from collections import MutableMapping    
 class tname(tname,MutableMapping):pass
 tname##_swigregister(tname)
index c79d4909c681d4e6f3995fbceee4a822e6e7eb3b..dc2f9bb2da79b99ad1fa4f95a35c30196f6e347e 100644 (file)
@@ -59,7 +59,6 @@
 #include "ComponentInstance.hxx"
 #include "DataNode.hxx"
 #include "PlayGround.hxx"
-#include "IteratorPy3.hxx"
   
 using namespace YACS::ENGINE;
 
@@ -107,14 +106,14 @@ using namespace YACS::ENGINE;
 %template()              std::pair<YACS::ENGINE::OutPort *,YACS::ENGINE::InPort *>;
 %template()              std::pair<YACS::ENGINE::InPort *,YACS::ENGINE::OutPort *>;
 %template()              std::pair< std::string, int >;
-%template()              Iterator<YACS::ENGINE::TypeCode *>;
+%template(ItPy3TC)       IteratorPy3<YACS::ENGINE::TypeCode *>;
 //%template(TCmap)         std::map<std::string, YACS::ENGINE::TypeCode *>;
 REFCOUNT_TEMPLATE(TCmap,YACS::ENGINE::TypeCode)
 %template(NODEmap)       std::map<std::string, YACS::ENGINE::Node *>;
 %template(INODEmap)      std::map<std::string, YACS::ENGINE::InlineNode *>;
 %template(SNODEmap)      std::map<std::string, YACS::ENGINE::ServiceNode *>;
 //%template(CONTAINmap)    std::map<std::string, YACS::ENGINE::Container *>;
-%template()              Iterator<YACS::ENGINE::Container * >;
+%template(ItPy3Cont)     IteratorPy3<YACS::ENGINE::Container * >;
 REFCOUNT_TEMPLATE(CONTAINmap,YACS::ENGINE::Container)
 %template(strvec)        std::vector<std::string>;
 %template(uivec)         std::vector<unsigned int>;
@@ -133,9 +132,11 @@ REFCOUNT_TEMPLATE(CONTAINmap,YACS::ENGINE::Container)
 %template(compomap)      std::map<std::string, YACS::ENGINE::ComponentDefinition *>;
 %template()              std::pair<std::string, std::string>;
 %template(propmap)       std::map<std::string, std::string>;
-%template()              Iterator<YACS::ENGINE::ComponentInstance *>;
+%template(ItPy3Comp)     IteratorPy3<YACS::ENGINE::ComponentInstance *>;
 REFCOUNT_TEMPLATE(CompoInstmap,YACS::ENGINE::ComponentInstance)
 
+%include "exception.i"
+
 /*
  * End of Template section
  */