2015-10-19 19:45:08 +00:00
|
|
|
// Macro to construct iterable objects.
|
|
|
|
%define sb_iterator(TYPE, PREFIX, VALUE_TYPE)
|
|
|
|
|
2016-06-19 18:53:24 +00:00
|
|
|
#if !SWIGRUBY
|
|
|
|
|
2015-10-19 19:45:08 +00:00
|
|
|
#if SWIGJAVA
|
|
|
|
%typemap(javainterfaces) TYPE##Iterator "java.util.Iterator<"#VALUE_TYPE">"
|
2016-06-19 18:53:24 +00:00
|
|
|
%typemap(javacode) TYPE##Iterator %{
|
2015-10-19 19:45:08 +00:00
|
|
|
@Override
|
|
|
|
public void remove() {
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
}
|
|
|
|
%}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Basic types
|
|
|
|
%inline %{
|
|
|
|
typedef struct {
|
|
|
|
PREFIX##_t *ptr;
|
|
|
|
} TYPE##Iterator;
|
|
|
|
%}
|
|
|
|
|
|
|
|
// Exception to end iteration
|
|
|
|
|
|
|
|
#if SWIGJAVA
|
|
|
|
%exception TYPE##Iterator##::next() {
|
|
|
|
if (!arg1->ptr) {
|
|
|
|
jclass cls = (*jenv)->FindClass(jenv, "java/util/NoSuchElementException");
|
|
|
|
(*jenv)->ThrowNew(jenv, cls, NULL);
|
|
|
|
return $null;
|
|
|
|
}
|
|
|
|
$action;
|
|
|
|
}
|
|
|
|
#elif SWIGPYTHON
|
|
|
|
%exception TYPE##Iterator##::next() {
|
|
|
|
if (!arg1->ptr) {
|
|
|
|
SWIG_SetErrorObj(PyExc_StopIteration, SWIG_Py_Void());
|
|
|
|
SWIG_fail;
|
|
|
|
}
|
|
|
|
$action;
|
|
|
|
}
|
|
|
|
%exception TYPE##Iterator##::__next__() {
|
|
|
|
if (!arg1->ptr) {
|
|
|
|
SWIG_SetErrorObj(PyExc_StopIteration, SWIG_Py_Void());
|
|
|
|
SWIG_fail;
|
|
|
|
}
|
|
|
|
$action;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Implementation of the iterator itself
|
|
|
|
|
|
|
|
%extend TYPE##Iterator {
|
|
|
|
TYPE##Iterator(PREFIX##_t *ptr) {
|
2016-06-19 18:53:24 +00:00
|
|
|
TYPE##Iterator *iter = (TYPE##Iterator *)ckd_malloc(sizeof *iter);
|
2015-10-19 19:45:08 +00:00
|
|
|
iter->ptr = ptr;
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
~TYPE##Iterator() {
|
|
|
|
if ($self->ptr)
|
|
|
|
PREFIX##_free($self->ptr);
|
|
|
|
ckd_free($self);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SWIGJAVA
|
|
|
|
%newobject next;
|
|
|
|
VALUE_TYPE * next() {
|
|
|
|
if ($self->ptr) {
|
|
|
|
VALUE_TYPE *value = ##VALUE_TYPE##_fromIter($self->ptr);
|
|
|
|
$self->ptr = ##PREFIX##_next($self->ptr);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bool hasNext() {
|
|
|
|
return $self->ptr != NULL;
|
|
|
|
}
|
2016-06-19 18:53:24 +00:00
|
|
|
#elif SWIGJAVASCRIPT
|
|
|
|
%newobject next;
|
|
|
|
VALUE_TYPE * next() {
|
|
|
|
if ($self->ptr) {
|
|
|
|
VALUE_TYPE *value = ##VALUE_TYPE##_fromIter($self->ptr);
|
|
|
|
$self->ptr = ##PREFIX##_next($self->ptr);
|
|
|
|
return value;
|
|
|
|
}
|
2015-10-19 19:45:08 +00:00
|
|
|
|
2016-06-19 18:53:24 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#elif SWIGPYTHON
|
2015-10-19 19:45:08 +00:00
|
|
|
// Python2
|
|
|
|
%newobject next;
|
|
|
|
VALUE_TYPE * next() {
|
|
|
|
if ($self->ptr) {
|
|
|
|
VALUE_TYPE *value = ##VALUE_TYPE##_fromIter($self->ptr);
|
|
|
|
$self->ptr = ##PREFIX##_next($self->ptr);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Python3
|
|
|
|
%newobject __next__;
|
|
|
|
VALUE_TYPE * __next__() {
|
|
|
|
if ($self->ptr) {
|
|
|
|
VALUE_TYPE *value = ##VALUE_TYPE##_fromIter($self->ptr);
|
|
|
|
$self->ptr = ##PREFIX##_next($self->ptr);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-06-19 18:53:24 +00:00
|
|
|
#endif // SWIGRUBY
|
|
|
|
|
2015-10-19 19:45:08 +00:00
|
|
|
%enddef
|
|
|
|
|
|
|
|
|
2016-06-19 18:53:24 +00:00
|
|
|
%define sb_iterable(TYPE, ITER_TYPE, PREFIX, INIT_PREFIX, VALUE_TYPE)
|
2015-10-19 19:45:08 +00:00
|
|
|
|
|
|
|
// Methods to retrieve the iterator from the container
|
|
|
|
|
|
|
|
#if SWIGJAVA
|
|
|
|
%typemap(javainterfaces) TYPE "Iterable<"#VALUE_TYPE">"
|
2016-06-19 18:53:24 +00:00
|
|
|
#endif
|
2015-10-19 19:45:08 +00:00
|
|
|
|
2016-06-19 18:53:24 +00:00
|
|
|
%extend TYPE {
|
|
|
|
|
|
|
|
#if SWIGRUBY
|
|
|
|
void each() {
|
|
|
|
##PREFIX##_t *iter = INIT_PREFIX##($self);
|
|
|
|
while (iter) {
|
|
|
|
VALUE_TYPE *value = ##VALUE_TYPE##_fromIter(iter);
|
|
|
|
rb_yield(SWIG_NewPointerObj(SWIG_as_voidptr(value), SWIGTYPE_p_##VALUE_TYPE##, 0 | 0 ));
|
|
|
|
iter = PREFIX##_next(iter);
|
|
|
|
}
|
|
|
|
return;
|
2015-10-19 19:45:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 18:53:24 +00:00
|
|
|
void each(int count) {
|
|
|
|
##PREFIX##_t *iter = INIT_PREFIX##($self);
|
|
|
|
int cnt = 0;
|
|
|
|
while (iter && cnt < count) {
|
|
|
|
VALUE_TYPE *value = ##VALUE_TYPE##_fromIter(iter);
|
|
|
|
rb_yield(SWIG_NewPointerObj(SWIG_as_voidptr(value), SWIGTYPE_p_##VALUE_TYPE##, 0 | 0 ));
|
|
|
|
iter = PREFIX##_next(iter);
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
if (iter)
|
|
|
|
PREFIX##_free(iter);
|
|
|
|
return;
|
2015-10-19 19:45:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 18:53:24 +00:00
|
|
|
#elif SWIGJAVA
|
|
|
|
%newobject iterator;
|
|
|
|
ITER_TYPE##Iterator * iterator() {
|
|
|
|
return new_##ITER_TYPE##Iterator(INIT_PREFIX##($self));
|
2015-10-19 19:45:08 +00:00
|
|
|
}
|
2016-06-19 18:53:24 +00:00
|
|
|
|
|
|
|
#else /* PYTHON, JS */
|
|
|
|
%newobject __iter__;
|
|
|
|
ITER_TYPE##Iterator * __iter__() {
|
|
|
|
return new_##ITER_TYPE##Iterator(INIT_PREFIX##($self));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
2015-10-19 19:45:08 +00:00
|
|
|
|
|
|
|
%enddef
|