summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorAndres Noetzli <andres.noetzli@gmail.com>2019-05-16 00:18:48 +0000
committerAina Niemetz <aina.niemetz@gmail.com>2019-05-15 17:18:48 -0700
commit62d361071ea54c9b7cba882313ab4dedef6f1286 (patch)
tree2e721a2fb77769a2824955a2b47fdf8b86203da3 /src/util
parentfc8907afc08d7b418471a537f9c23e9964df82df (diff)
Fix iterators in Java API (#3000)
Fixes #2989. SWIG 3 seems to have an issue properly resolving `T::const_iterator::value_type` if that type itself is a `typedef`. This is for example the case in the `UnsatCore` class, which `typedef`s `const_iterator` to `std::vector<Expr>::const_iterator`. As a workaround, this commit changes the `JavaIteratorAdapter` class to take two template parameters, one of which is the `value_type`. The commit also adds a compile-time assertion that `T::const_iterator::value_type` can be converted to `value_type` to avoid nasty surprises. A nice side-effect of this solution is that explicit `typemap`s are not necessary anymore, so they are removed. Additionally, the commit adds a `toString()` method for the Java API of `UnsatCore` and adds examples that show and test the iteration over the unsat core and the statistics. Iterating over `Statistics` now returns instances of `Statistic` instead of `Object[]`, which is a bit cleaner and requires less glue code.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/statistics.i46
1 files changed, 20 insertions, 26 deletions
diff --git a/src/util/statistics.i b/src/util/statistics.i
index 9ff6757d8..8d1086dcc 100644
--- a/src/util/statistics.i
+++ b/src/util/statistics.i
@@ -4,7 +4,6 @@
#ifdef SWIGJAVA
#include "bindings/java_iterator_adapter.h"
-#include "bindings/java_stream_adapters.h"
#endif /* SWIGJAVA */
%}
@@ -24,24 +23,29 @@
%ignore CVC4::StatisticsBase::begin() const;
%ignore CVC4::StatisticsBase::end() const;
%extend CVC4::StatisticsBase {
- CVC4::JavaIteratorAdapter<CVC4::StatisticsBase> iterator() {
- return CVC4::JavaIteratorAdapter<CVC4::StatisticsBase>(*$self);
+ CVC4::JavaIteratorAdapter<CVC4::StatisticsBase,
+ std::pair<std::string, CVC4::SExpr> >
+ iterator()
+ {
+ return CVC4::JavaIteratorAdapter<CVC4::StatisticsBase,
+ std::pair<std::string, CVC4::SExpr> >(
+ *$self);
}
}
// StatisticsBase is "iterable" on the Java side
-%typemap(javainterfaces) CVC4::StatisticsBase "java.lang.Iterable<Object[]>";
+%typemap(javainterfaces) CVC4::StatisticsBase "java.lang.Iterable<Statistic>";
// the JavaIteratorAdapter should not be public, and implements Iterator
-%typemap(javaclassmodifiers) CVC4::JavaIteratorAdapter<CVC4::StatisticsBase> "class";
-%typemap(javainterfaces) CVC4::JavaIteratorAdapter<CVC4::StatisticsBase> "java.util.Iterator<Object[]>";
+%typemap(javaclassmodifiers) CVC4::JavaIteratorAdapter<CVC4::StatisticsBase, std::pair<std::string, CVC4::SExpr> > "class";
+%typemap(javainterfaces) CVC4::JavaIteratorAdapter<CVC4::StatisticsBase, std::pair<std::string, CVC4::SExpr> > "java.util.Iterator<Statistic>";
// add some functions to the Java side (do it here because there's no way to do these in C++)
-%typemap(javacode) CVC4::JavaIteratorAdapter<CVC4::StatisticsBase> "
+%typemap(javacode) CVC4::JavaIteratorAdapter<CVC4::StatisticsBase, std::pair<std::string, CVC4::SExpr> > "
public void remove() {
throw new java.lang.UnsupportedOperationException();
}
- public Object[] next() {
+ public Statistic next() {
if(hasNext()) {
return getNext();
} else {
@@ -50,22 +54,7 @@
}
"
// getNext() just allows C++ iterator access from Java-side next(), make it private
-%javamethodmodifiers CVC4::JavaIteratorAdapter<CVC4::StatisticsBase>::getNext() "private";
-
-// map the types appropriately. for statistics, the "payload" of the iterator is an Object[].
-// These Object arrays are always of two elements, the first is a String and the second an
-// SExpr. (On the C++ side, it is a std::pair<std::string, SExpr>.)
-%typemap(jni) CVC4::StatisticsBase::const_iterator::value_type "jobjectArray";
-%typemap(jtype) CVC4::StatisticsBase::const_iterator::value_type "java.lang.Object[]";
-%typemap(jstype) CVC4::StatisticsBase::const_iterator::value_type "java.lang.Object[]";
-%typemap(javaout) CVC4::StatisticsBase::const_iterator::value_type { return $jnicall; }
-%typemap(out) CVC4::StatisticsBase::const_iterator::value_type {
- $result = jenv->NewObjectArray(2, jenv->FindClass("java/lang/Object"), $null);
- jenv->SetObjectArrayElement($result, 0, jenv->NewStringUTF($1.first.c_str()));
- jclass clazz = jenv->FindClass("edu/nyu/acsys/CVC4/SExpr");
- jmethodID methodid = jenv->GetMethodID(clazz, "<init>", "(JZ)V");
- jenv->SetObjectArrayElement($result, 1, jenv->NewObject(clazz, methodid, reinterpret_cast<uintptr_t>(new CVC4::SExpr($1.second)), true));
- };
+%javamethodmodifiers CVC4::JavaIteratorAdapter<CVC4::StatisticsBase, std::pair<std::string, CVC4::SExpr> >::getNext() "private";
#endif /* SWIGJAVA */
@@ -73,9 +62,14 @@
#ifdef SWIGJAVA
+%include <std_pair.i>
+%include <std_string.i>
+%include <std_vector.i>
+
%include "bindings/java_iterator_adapter.h"
-%include "bindings/java_stream_adapters.h"
+%include "util/sexpr.h"
-%template(JavaIteratorAdapter_StatisticsBase) CVC4::JavaIteratorAdapter<CVC4::StatisticsBase>;
+%template(Statistic) std::pair<std::string, CVC4::SExpr>;
+%template(JavaIteratorAdapter_StatisticsBase) CVC4::JavaIteratorAdapter<CVC4::StatisticsBase, std::pair<std::string, CVC4::SExpr> >;
#endif /* SWIGJAVA */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback