SWIG pickling improvements (and other cleanup) (#6133)

* wip

* - avoid leaking memory after instantiating UChar_Vect
- fix some indentation
- make it easier to read/write pickles as native byte arrays from Java and C#
- add tests

---------

Co-authored-by: Tosco, Paolo <paolo.tosco@novartis.com>
This commit is contained in:
Paolo Tosco
2023-03-01 05:00:56 +01:00
committed by GitHub
parent ec5d922eee
commit ab980bca44
6 changed files with 223 additions and 37 deletions

View File

@@ -61,37 +61,44 @@
#endif
%typemap(javacode) ExplicitBitVect %{
public static ExplicitBitVect fromByteArray(byte[] fp) {
UChar_Vect vec = new UChar_Vect();
vec.reserve(fp.length);
for (int size=0;size<fp.length;++size) {
vec.add((short)fp[size]);
}
return new ExplicitBitVect(vec);
}
public static ExplicitBitVect fromByteArray(byte[] fp) {
UChar_Vect vec = null;
try {
vec = new UChar_Vect();
vec.reserve(fp.length);
for (int size=0;size<fp.length;++size) {
vec.add((short)fp[size]);
}
return new ExplicitBitVect(vec);
} finally {
if (vec != null) {
vec.delete();
}
}
}
%}
%include <DataStructs/ExplicitBitVect.h>
%newobject ExplicitBitVect::getOnBits;
%extend ExplicitBitVect {
IntVect *getOnBits() {
IntVect* bits = new IntVect;
($self)->getOnBits(*bits);
return bits;
}
IntVect *getOnBits() {
IntVect* bits = new IntVect;
($self)->getOnBits(*bits);
return bits;
}
}
#ifdef SWIGJAVA
%extend ExplicitBitVect {
const std::string toByteArray() {
return ($self)->toString();
}
const std::string toByteArray() {
return ($self)->toString();
}
ExplicitBitVect(const std::vector<unsigned char> & data ) {
std::string str(data.begin(), data.end());
return new ExplicitBitVect(str);
}
ExplicitBitVect(const std::vector<unsigned char> & data ) {
std::string str(data.begin(), data.end());
return new ExplicitBitVect(str);
}
}
#endif