//
// 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;
}
typename std::map<std::string,U*>::iterator end;
};
-#endif /* ITERATORPY3_HXX */
+#endif /* __ITERATORPY3_HXX__ */
#include "OutputDataStreamPort.hxx"
#include "OptimizerLoop.hxx"
#include "HomogeneousPoolContainer.hxx"
+#include "IteratorPy3.hxx"
#include <sstream>
%}
+// ----------------------------------------------------------------------------
+
#if SWIG_VERSION >= 0x010329
%template() std::list<int>;
%template() std::list<std::string>;
#endif
#endif
+// ----------------------------------------------------------------------------
+
#ifdef SWIGPYTHON
%typecheck(SWIG_TYPECHECK_POINTER) YACS::ENGINE::Any*
#endif
+// ----------------------------------------------------------------------------
+
/*
* Exception section
*/
* End of Exception section
*/
+// ----------------------------------------------------------------------------
+
/*
* Ownership section
*/
* End of Reference counting section
*/
+// ----------------------------------------------------------------------------
+
/*
%wrapper %{
namespace swig {
%}
*/
-%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).
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>();
}
}
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();
}
}
};
-%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)
#include "ComponentInstance.hxx"
#include "DataNode.hxx"
#include "PlayGround.hxx"
-#include "IteratorPy3.hxx"
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>;
%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
*/