Merge branch '1.4.x'
[cvc5.git] / src / util / record.i
1 %{
2 #include "util/record.h"
3
4 #ifdef SWIGJAVA
5
6 #include "bindings/java_iterator_adapter.h"
7 #include "bindings/java_stream_adapters.h"
8
9 #endif /* SWIGJAVA */
10 %}
11
12 %rename(equals) CVC4::RecordSelect::operator==(const RecordSelect&) const;
13 %ignore CVC4::RecordSelect::operator!=(const RecordSelect&) const;
14
15 %rename(equals) CVC4::RecordUpdate::operator==(const RecordUpdate&) const;
16 %ignore CVC4::RecordUpdate::operator!=(const RecordUpdate&) const;
17
18 %rename(equals) CVC4::Record::operator==(const Record&) const;
19 %ignore CVC4::Record::operator!=(const Record&) const;
20 %rename(getField) CVC4::Record::operator[](size_t) const;
21
22 %rename(apply) CVC4::RecordHashFunction::operator()(const Record&) const;
23 %rename(apply) CVC4::RecordSelectHashFunction::operator()(const RecordSelect&) const;
24 %rename(apply) CVC4::RecordUpdateHashFunction::operator()(const RecordUpdate&) const;
25
26 %ignore CVC4::operator<<(std::ostream&, const Record&);
27 %ignore CVC4::operator<<(std::ostream&, const RecordSelect&);
28 %ignore CVC4::operator<<(std::ostream&, const RecordUpdate&);
29
30 #ifdef SWIGJAVA
31
32 // These Object arrays are always of two elements, the first is a String and the second a
33 // Type. (On the C++ side, it is a std::pair<std::string, Type>.)
34 %typemap(jni) std::pair<std::string, CVC4::Type> "jobjectArray";
35 %typemap(jtype) std::pair<std::string, CVC4::Type> "java.lang.Object[]";
36 %typemap(jstype) std::pair<std::string, CVC4::Type> "java.lang.Object[]";
37 %typemap(javaout) std::pair<std::string, CVC4::Type> { return $jnicall; }
38 %typemap(out) std::pair<std::string, CVC4::Type> {
39 $result = jenv->NewObjectArray(2, jenv->FindClass("java/lang/Object"), $null);
40 jenv->SetObjectArrayElement($result, 0, jenv->NewStringUTF($1.first.c_str()));
41 jclass clazz = jenv->FindClass("edu/nyu/acsys/CVC4/Type");
42 jmethodID methodid = jenv->GetMethodID(clazz, "<init>", "(JZ)V");
43 jenv->SetObjectArrayElement($result, 1, jenv->NewObject(clazz, methodid, reinterpret_cast<long>(new CVC4::Type($1.second)), true));
44 };
45
46 // Instead of Record::begin() and end(), create an
47 // iterator() method on the Java side that returns a Java-style
48 // Iterator.
49 %ignore CVC4::Record::begin() const;
50 %ignore CVC4::Record::end() const;
51 %extend CVC4::Record {
52 CVC4::Type find(std::string name) const {
53 CVC4::Record::const_iterator i;
54 for(i = $self->begin(); i != $self->end(); ++i) {
55 if((*i).first == name) {
56 return (*i).second;
57 }
58 }
59 return CVC4::Type();
60 }
61
62 CVC4::JavaIteratorAdapter<CVC4::Record> iterator() {
63 return CVC4::JavaIteratorAdapter<CVC4::Record>(*$self);
64 }
65 }
66
67 // Record is "iterable" on the Java side
68 %typemap(javainterfaces) CVC4::Record "java.lang.Iterable<Object[]>";
69
70 // the JavaIteratorAdapter should not be public, and implements Iterator
71 %typemap(javaclassmodifiers) CVC4::JavaIteratorAdapter<CVC4::Record> "class";
72 %typemap(javainterfaces) CVC4::JavaIteratorAdapter<CVC4::Record> "java.util.Iterator<Object[]>";
73 // add some functions to the Java side (do it here because there's no way to do these in C++)
74 %typemap(javacode) CVC4::JavaIteratorAdapter<CVC4::Record> "
75 public void remove() {
76 throw new java.lang.UnsupportedOperationException();
77 }
78
79 public Object[] next() {
80 if(hasNext()) {
81 return getNext();
82 } else {
83 throw new java.util.NoSuchElementException();
84 }
85 }
86 "
87 // getNext() just allows C++ iterator access from Java-side next(), make it private
88 %javamethodmodifiers CVC4::JavaIteratorAdapter<CVC4::Record>::getNext() "private";
89
90 // map the types appropriately. for records, the "payload" of the iterator is an Object[].
91 // These Object arrays are always of two elements, the first is a String and the second a
92 // Type. (On the C++ side, it is a std::pair<std::string, SExpr>.)
93 %typemap(jni) CVC4::Record::const_iterator::value_type = std::pair<std::string, CVC4::Type>;
94 %typemap(jtype) CVC4::Record::const_iterator::value_type = std::pair<std::string, CVC4::Type>;
95 %typemap(jstype) CVC4::Record::const_iterator::value_type = std::pair<std::string, CVC4::Type>;
96 %typemap(javaout) CVC4::Record::const_iterator::value_type = std::pair<std::string, CVC4::Type>;
97 %typemap(out) CVC4::Record::const_iterator::value_type = std::pair<std::string, CVC4::Type>;
98
99 #endif /* SWIGJAVA */
100
101 %include "util/record.h"
102
103 #ifdef SWIGJAVA
104
105 %include "bindings/java_iterator_adapter.h"
106 %include "bindings/java_stream_adapters.h"
107
108 %template(JavaIteratorAdapter_Record) CVC4::JavaIteratorAdapter<CVC4::Record>;
109
110 #endif /* SWIGJAVA */