[python] fix std unordered containers

This commit is contained in:
Takashi Tamura 2017-01-30 11:54:46 +09:00
commit db8bc1c9c7
8 changed files with 126 additions and 51 deletions

View file

@ -195,6 +195,42 @@ namespace swig {
}
};
template<typename OutIterator,
typename ValueType = typename std::iterator_traits<OutIterator>::value_type,
typename FromOper = from_oper<ValueType> >
class SwigPyForwardIteratorOpen_T : public SwigPyIterator_T<OutIterator>
{
public:
FromOper from;
typedef OutIterator out_iterator;
typedef ValueType value_type;
typedef SwigPyIterator_T<out_iterator> base;
typedef SwigPyForwardIteratorOpen_T<OutIterator, ValueType, FromOper> self_type;
SwigPyForwardIteratorOpen_T(out_iterator curr, PyObject *seq)
: SwigPyIterator_T<OutIterator>(curr, seq)
{
}
PyObject *value() const {
return from(static_cast<const value_type&>(*(base::current)));
}
SwigPyIterator *copy() const
{
return new self_type(*this);
}
SwigPyIterator *incr(size_t n = 1)
{
while (n--) {
++base::current;
}
return this;
}
};
template<typename OutIterator,
typename ValueType = typename std::iterator_traits<OutIterator>::value_type,
typename FromOper = from_oper<ValueType> >
@ -237,6 +273,52 @@ namespace swig {
return this;
}
};
template<typename OutIterator,
typename ValueType = typename std::iterator_traits<OutIterator>::value_type,
typename FromOper = from_oper<ValueType> >
class SwigPyForwardIteratorClosed_T : public SwigPyIterator_T<OutIterator>
{
public:
FromOper from;
typedef OutIterator out_iterator;
typedef ValueType value_type;
typedef SwigPyIterator_T<out_iterator> base;
typedef SwigPyForwardIteratorClosed_T<OutIterator, ValueType, FromOper> self_type;
SwigPyForwardIteratorClosed_T(out_iterator curr, out_iterator first, out_iterator last, PyObject *seq)
: SwigPyIterator_T<OutIterator>(curr, seq), begin(first), end(last)
{
}
PyObject *value() const {
if (base::current == end) {
throw stop_iteration();
} else {
return from(static_cast<const value_type&>(*(base::current)));
}
}
SwigPyIterator *copy() const
{
return new self_type(*this);
}
SwigPyIterator *incr(size_t n = 1)
{
while (n--) {
if (base::current == end) {
throw stop_iteration();
} else {
++base::current;
}
}
return this;
}
private:
out_iterator begin;
out_iterator end;
};
template<typename OutIterator,
typename ValueType = typename std::iterator_traits<OutIterator>::value_type,
@ -297,6 +379,14 @@ namespace swig {
out_iterator end;
};
template<typename OutIter>
inline SwigPyIterator*
make_output_forward_iterator(const OutIter& current, const OutIter& begin,const OutIter& end, PyObject *seq = 0)
{
return new SwigPyForwardIteratorClosed_T<OutIter>(current, begin, end, seq);
}
template<typename OutIter>
inline SwigPyIterator*
make_output_iterator(const OutIter& current, const OutIter& begin,const OutIter& end, PyObject *seq = 0)
@ -304,6 +394,13 @@ namespace swig {
return new SwigPyIteratorClosed_T<OutIter>(current, begin, end, seq);
}
template<typename OutIter>
inline SwigPyIterator*
make_output_forward_iterator(const OutIter& current, PyObject *seq = 0)
{
return new SwigPyForwardIteratorOpen_T<OutIter>(current, seq);
}
template<typename OutIter>
inline SwigPyIterator*
make_output_iterator(const OutIter& current, PyObject *seq = 0)