/* Copyright (c) 2015,2016 Sequence Logic Inc. All rights reserved * * SWIG interface for the cppcore, wraps the interfaces * Uses SWIG "directors", to allow C++ to call back into Java for the observer and * network service classes. * * Be aware, ordering of SWIG directives is ORDER DEPENDENT. */ %module(directors="1", assumeoverride=1) cppcore %pragma(java) jniclassimports=%{ import java.util.Date; %} %include "stl.i" %include "sequencelogic-collaborator.i" %include "sequencelogic-json.i" %include "sequencelogic-cgurn.i" %include "sequencelogic-misc.i" %include "sequencelogic-file.i" %typemap(out) std::shared_ptr > { // This may look strange, but SWIG insists on wrapping the return value in their // SwigValueWrapper template class. Unfortunately, adding the line: // %feature("novaluewrapper") std::shared_ptr >; // Doesn't seem to work. I'm probably being too cute with my code. std::shared_ptr > pRes = *(&result); if (pRes == NULL) { SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "NULL std::shared_ptr >"); return $null; } size_t nLen = pRes->size(); const jclass jStringClass = jenv->FindClass("java/lang/String"); jresult = jenv->NewObjectArray(static_cast(nLen), jStringClass, NULL); for (unsigned int i = 0; i < nLen; ++i) { jstring tmpStr = jenv->NewStringUTF(pRes->at(i).c_str()); jenv->SetObjectArrayElement(jresult, i, tmpStr); jenv->DeleteLocalRef(tmpStr); } } %typemap(jni) std::shared_ptr > "jobjectArray" %typemap(jtype) std::shared_ptr > "String []" %typemap(jstype) std::shared_ptr > "String []" %typemap(directorin, descriptor="[Ljava/lang/String;") std::shared_ptr > "$input = (jobjectArray) $1;" %typemap(javadirectorout) std::shared_ptr > "$javacall" %typemap(directorout) std::shared_ptr > { // This may look strange, but SWIG insists on wrapping the return value in their // SwigValueWrapper template class. Unfortunately, adding the line: // %feature("novaluewrapper") std::shared_ptr >; // Doesn't seem to work. I'm probably being too cute with my code. std::shared_ptr > *pRes = (&c_result); pRes->reset(new std::vector()); unsigned int nLen = jenv->GetArrayLength(jresult); for (unsigned int i = 0; i < nLen; ++i) { jstring tmpStr = static_cast(jenv->GetObjectArrayElement(jresult, i)); const char *pStr = jenv->GetStringUTFChars(tmpStr, 0); (*pRes)->push_back(pStr); jenv->ReleaseStringUTFChars(tmpStr, pStr); jenv->DeleteLocalRef(tmpStr); } } %typemap(javain) std::shared_ptr > "$javainput" %typemap(javaout) std::shared_ptr > { return $jnicall; } %header %{ /** #include "client/syncandmessagedisp.h" **/ #include "client/vanaccess.h" #include "component/storagenodedbaccess_java.h" using namespace sequencelogic; %} /* It seems that SWIG allows you to add code to the generated CPP/Java files but **not** the generated header files. Bummer. These lines need to be added to the generated ionu-samdjni.h file. */ /* update cppcorejni.h: { #include "client/peerobserver.h" #include "client/websvcs.h" } */ /** %feature("director") PeerObserver; %feature("director") WebSvcs; **/ /** %include "client/peerobserver.h" %include "client/websvcs.h" %include "client/syncandmessagedisp.h" */ %include "client/vanaccess.h" %include "component/storagenodedbaccess_java.h" %pragma(java) jniclassimports=%{ import java.util.Date; %} %pragma(java) jniclasscode=%{ // bootstrap JNI/Lib static { System.out.println("[JAVA] " + new Date() + " cppcore static init"); Exception here = new Exception("YOU ARE HERE: " + cppcore.class.getClassLoader()); here.fillInStackTrace(); System.out.println("LD_LIBRARY_PATH (Linux): " + System.getenv("LD_LIBRARY_PATH")); System.out.println("DYLD_LIBRARY_PATH (OSX): " + System.getenv("DYLD_LIBRARY_PATH")); try { System.out.println("[JAVA] " + new Date() + " Loading cppcore... " + System.getProperty("java.library.path")); System.loadLibrary("cppcore"); System.out.println("[JAVA] " + new Date() + " cppcore Loaded native libraries!"); } catch (Throwable ex){ System.err.println("[JAVA] " + new Date() + " FATAL: cppcore Unable to load shared library: " + ex.getMessage()); ex.printStackTrace(); } } %}