diff options
author | Stefan Radomski <github@mintwerk.de> | 2016-05-26 10:36:49 (GMT) |
---|---|---|
committer | Stefan Radomski <github@mintwerk.de> | 2016-05-26 10:36:49 (GMT) |
commit | 6e13c7b6e0888323223afd5d2e36e86243df57af (patch) | |
tree | f558fd45fa499c8bc95041554ecad6be1bf788c1 /src/bindings/swig/csharp | |
parent | f6714b1484b641ea61053350b7d156d2da760b8b (diff) | |
download | uscxml-6e13c7b6e0888323223afd5d2e36e86243df57af.zip uscxml-6e13c7b6e0888323223afd5d2e36e86243df57af.tar.gz uscxml-6e13c7b6e0888323223afd5d2e36e86243df57af.tar.bz2 |
Minor polishing for Java bindings and first draft of JEXL datamodel
Diffstat (limited to 'src/bindings/swig/csharp')
-rw-r--r-- | src/bindings/swig/csharp/CMakeLists.txt | 88 | ||||
-rw-r--r-- | src/bindings/swig/csharp/org/uscxml/InterpreterException.cs | 7 | ||||
-rw-r--r-- | src/bindings/swig/csharp/uscxml.i | 429 |
3 files changed, 524 insertions, 0 deletions
diff --git a/src/bindings/swig/csharp/CMakeLists.txt b/src/bindings/swig/csharp/CMakeLists.txt new file mode 100644 index 0000000..384507b --- /dev/null +++ b/src/bindings/swig/csharp/CMakeLists.txt @@ -0,0 +1,88 @@ +# generate JNI library and create a jar +# Make from within Eclipse fails miserably with the whole thing + +if (WIN32) + LIST(APPEND CMAKE_PROGRAM_PATH "C:/Windows/Microsoft.NET/Framework/v3.5") # CSharp compiler + LIST(APPEND CMAKE_PROGRAM_PATH "C:/Windows/Microsoft.NET/Framework/v4.0.30319") # CSharp compiler +endif() + +FIND_PROGRAM(DMCS_EXECUTABLE dmcs PATHS $ENV{DMCS_HOME} ENV PATH ) # Mono compiler +FIND_PROGRAM(CSC_EXECUTABLE csc PATHS $ENV{CSC_HOME} ENV PATH ) # CSharp compiler + +if (DMCS_EXECUTABLE OR CSC_EXECUTABLE) + + # unset all library suffixes as swig will hardcode a library without + SET(LIB_POSTFIX ${CMAKE_LIBRARY_POSTFIX}) + + SET(CMAKE_DEBUG_POSTFIX "") + SET(CMAKE_RELEASE_POSTFIX "") + SET(CMAKE_RELWITHDEBINFO_POSTFIX "") + SET(CMAKE_MINSIZEREL_POSTFIX "") + SET(CMAKE_LIBRARY_POSTFIX "") + + if (MSVC) + # MSVC does not include inttypes.h but SWIG needs it + include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../msvc) + endif() + + include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + + SET(CMAKE_SWIG_FLAGS "") + SET(USCXML_CSHARP_NAMESPACE "org.uscxml") + SET(USCXML_CSHARP_DIR "org/uscxml") + + if (CMAKE_BUILD_TYPE STREQUAL "Debug") + list(APPEND CMAKE_SWIG_FLAGS "-DDEBUG") + endif() + + + # we need ; to produce a space with the package .. weird + SET_SOURCE_FILES_PROPERTIES(uscxml.i PROPERTIES SWIG_FLAGS "-w401;-namespace;${USCXML_CSHARP_NAMESPACE}") + SET_SOURCE_FILES_PROPERTIES(uscxml.i PROPERTIES CPLUSPLUS ON) + SET(CMAKE_SWIG_OUTDIR "${CMAKE_CURRENT_BINARY_DIR}/${USCXML_CSHARP_DIR}") + + set(USCXML_LANGUAGE_BINDINGS "csharp ${USCXML_LANGUAGE_BINDINGS}") + + SWIG_ADD_MODULE(uscxmlNativeCSharp csharp uscxml.i) + set_target_properties(uscxmlNativeCSharp PROPERTIES FOLDER "Bindings") + set_target_properties(uscxmlNativeCSharp + PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/csharp${LIB_POSTFIX}" + LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/csharp${LIB_POSTFIX}" + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/csharp${LIB_POSTFIX}" + ) + + set_target_properties(uscxmlNativeCSharp PROPERTIES COMPILE_FLAGS "-DSWIG") + swig_link_libraries(uscxmlNativeCSharp uscxml) + + # build managed code part + if (CSC_EXECUTABLE) + ADD_CUSTOM_TARGET(csharp + COMMAND ${CMAKE_COMMAND} -E + copy ${CMAKE_CURRENT_SOURCE_DIR}/org/uscxml/InterpreterException.cs ${PROJECT_BINARY_DIR}/src/bindings/swig/csharp/org/uscxml + COMMAND ${CSC_EXECUTABLE} + /target:library + /out:${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/uscxmlCSharp.dll + *.cs + WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/src/bindings/swig/csharp/org/uscxml + COMMENT "Creating umundoCSharp.dll for C# ...") + else() + ADD_CUSTOM_TARGET(csharp + COMMAND ${CMAKE_COMMAND} -E + copy ${CMAKE_CURRENT_SOURCE_DIR}/org/uscxml/InterpreterException.cs ${PROJECT_BINARY_DIR}/src/bindings/swig/csharp/org/uscxml + COMMAND ${DMCS_EXECUTABLE} + -target:library + /out:${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/uscxmlCSharp.dll + *.cs + WORKING_DIRECTORY ${PROJECT_BINARY_DIR}/src/bindings/swig/csharp/org/uscxml + COMMENT "Creating umundoCSharp.dll for Mono ...") + endif() + + add_dependencies(csharp uscxmlNativeCSharp) + if (BUILD_TESTS) + add_dependencies(ALL_TESTS csharp) + endif() + set_target_properties(csharp PROPERTIES FOLDER "Bindings") + + set(USCXML_LANGUAGE_BINDINGS ${USCXML_LANGUAGE_BINDINGS} PARENT_SCOPE) +endif()
\ No newline at end of file diff --git a/src/bindings/swig/csharp/org/uscxml/InterpreterException.cs b/src/bindings/swig/csharp/org/uscxml/InterpreterException.cs new file mode 100644 index 0000000..38430d8 --- /dev/null +++ b/src/bindings/swig/csharp/org/uscxml/InterpreterException.cs @@ -0,0 +1,7 @@ +namespace org.uscxml { + public class InterpreterException : System.ApplicationException { + public InterpreterException(string message) + : base(message) { + } + } +}
\ No newline at end of file diff --git a/src/bindings/swig/csharp/uscxml.i b/src/bindings/swig/csharp/uscxml.i new file mode 100644 index 0000000..fbc5f20 --- /dev/null +++ b/src/bindings/swig/csharp/uscxml.i @@ -0,0 +1,429 @@ +%module(directors="1", allprotected="1") uscxmlNativeCSharp + +// provide a macro for the header files +#define SWIGIMPORTED 1 + +%include <stl.i> +%include <std_map.i> +%include <std_string.i> +%include <inttypes.i> +%include "../stl_set.i" +%include "../stl_list.i" + +%include <boost_shared_ptr.i> + +// these are needed at least for the templates to work +typedef uscxml::Blob Blob; +typedef uscxml::Data Data; +typedef uscxml::Event Event; +typedef uscxml::InvokeRequest InvokeRequest; +typedef uscxml::SendRequest SendRequest; +typedef uscxml::Invoker Invoker; +typedef uscxml::IOProcessor IOProcessor; +typedef uscxml::DataModel DataModel; +typedef uscxml::DataModelExtension DataModelExtension; +typedef uscxml::ExecutableContent ExecutableContent; +typedef uscxml::InvokerImpl InvokerImpl; +typedef uscxml::IOProcessorImpl IOProcessorImpl; +typedef uscxml::DataModelImpl DataModelImpl; +typedef uscxml::ExecutableContentImpl ExecutableContentImpl; +typedef uscxml::InterpreterIssue InterpreterIssue; + +%feature("director") uscxml::WrappedInvoker; +%feature("director") uscxml::WrappedDataModel; +%feature("director") uscxml::WrappedDataModelExtension; +%feature("director") uscxml::WrappedIOProcessor; +%feature("director") uscxml::WrappedExecutableContent; +%feature("director") uscxml::WrappedInterpreterMonitor; + +// disable warnings + +// unknown base class +#pragma SWIG nowarn=401 +// override symbols via extend +#pragma SWIG nowarn=302 +// ignoring overrided method +#pragma SWIG nowarn=516 +// pointer from director +#pragma SWIG nowarn=473 +// renamed params -> _params +#pragma SWIG nowarn=314 + +%csconst(1); + +%rename(equals) operator==; +%rename(isValid) operator bool; + + +//************************************************** +// This ends up in the generated wrapper code +//************************************************** + +%{ + +#include "../../../uscxml/Message.h" +#include "../../../uscxml/Factory.h" +#include "../../../uscxml/Interpreter.h" +#include "../../../uscxml/concurrency/BlockingQueue.h" +#include "../../../uscxml/server/HTTPServer.h" +//#include "../../../uscxml/debug/DebuggerServlet.h" + +#include "../wrapped/WrappedInvoker.h" +#include "../wrapped/WrappedDataModel.h" +#include "../wrapped/WrappedExecutableContent.h" +#include "../wrapped/WrappedIOProcessor.h" +#include "../wrapped/WrappedInterpreterMonitor.h" + +using namespace uscxml; +using namespace Arabica::DOM; + +// the wrapped* C++ classes get rid of DOM nodes and provide more easily wrapped base classes +#include "../wrapped/WrappedInvoker.cpp" +#include "../wrapped/WrappedDataModel.cpp" +#include "../wrapped/WrappedExecutableContent.cpp" +#include "../wrapped/WrappedIOProcessor.cpp" +#include "../wrapped/WrappedInterpreterMonitor.cpp" + +%} + +// see http://binf.gmu.edu/software/SWIG/CSharp.html#csharp_exceptions +%insert(runtime) %{ + // Code to handle throwing of C# CustomApplicationException from C/C++ code. + // The equivalent delegate to the callback, CSharpExceptionCallback_t, is CustomExceptionDelegate + // and the equivalent customExceptionCallback instance is customDelegate + typedef void (SWIGSTDCALL* CSharpExceptionCallback_t)(const char *); + CSharpExceptionCallback_t customExceptionCallback = NULL; + + extern "C" SWIGEXPORT + void SWIGSTDCALL CustomExceptionRegisterCallback(CSharpExceptionCallback_t customCallback) { + customExceptionCallback = customCallback; + } + + // Note that SWIG detects any method calls named starting with + // SWIG_CSharpSetPendingException for warning 845 + static void SWIG_CSharpSetPendingExceptionCustom(const char *msg) { + customExceptionCallback(msg); + } +%} + +%pragma(csharp) imclasscode=%{ + class CustomExceptionHelper { + // C# delegate for the C/C++ customExceptionCallback + public delegate void CustomExceptionDelegate(string message); + static CustomExceptionDelegate customDelegate = + new CustomExceptionDelegate(SetPendingCustomException); + + [System.Runtime.InteropServices.DllImport("$dllimport", EntryPoint="CustomExceptionRegisterCallback")] + public static extern + void CustomExceptionRegisterCallback(CustomExceptionDelegate customCallback); + + static void SetPendingCustomException(string message) { + SWIGPendingException.Set(new org.uscxml.InterpreterException(message)); + } + + static CustomExceptionHelper() { + CustomExceptionRegisterCallback(customDelegate); + } + } + static CustomExceptionHelper exceptionHelper = new CustomExceptionHelper(); +%} + + +%define WRAP_THROW_EXCEPTION( MATCH ) +%exception MATCH %{ +try { + $action +} catch (uscxml::Event& e) { + std::stringstream ss; + ss << std::endl << e; + SWIG_CSharpSetPendingExceptionCustom(ss.str().c_str()); +} +%} +%enddef + +WRAP_THROW_EXCEPTION(uscxml::Interpreter::fromXML); +WRAP_THROW_EXCEPTION(uscxml::Interpreter::fromURI); +WRAP_THROW_EXCEPTION(uscxml::Interpreter::step); +WRAP_THROW_EXCEPTION(uscxml::Interpreter::interpret); + + +%define WRAP_TO_STRING( CLASSNAME ) +%csmethodmodifiers CLASSNAME::ToString() "public override"; +%extend CLASSNAME { + virtual std::string ToString() { + std::stringstream ss; + ss << *self; + return ss.str(); + } +}; +%enddef + +WRAP_TO_STRING(uscxml::Event); +WRAP_TO_STRING(uscxml::Data); +WRAP_TO_STRING(uscxml::SendRequest); +WRAP_TO_STRING(uscxml::InvokeRequest); +WRAP_TO_STRING(uscxml::InterpreterIssue); + +%include "../uscxml_ignores.i" + +// InterpreterMonitor -> StateTransitionMonitor +%ignore uscxml::StateTransitionMonitor; + +//*********************************************** +// Beautify important classes +//*********************************************** + + +// byte[] signature for Blob get/setData +// see http://permalink.gmane.org/gmane.comp.programming.swig/5804 + +%csmethodmodifiers uscxml::Blob::setData(const char* data, size_t length) "private"; +%csmethodmodifiers uscxml::Blob::setMimeType(const std::string& mimeType) "private"; +%csmethodmodifiers uscxml::Blob::Blob(const char* data, size_t size, const std::string& mimeType) "private"; +%csmethodmodifiers uscxml::Blob::Blob(const char* data, size_t size) "private"; + +%typemap(cscode) uscxml::Blob %{ + public Blob(byte[] data, string mimeType) : this(uscxmlNativeCSharpPINVOKE.new_Blob__SWIG_2(data, (uint)data.Length, mimeType), true) { + if (uscxmlNativeCSharpPINVOKE.SWIGPendingException.Pending) throw uscxmlNativeCSharpPINVOKE.SWIGPendingException.Retrieve(); + } + +%} + +%typemap(imtype, out="System.IntPtr") const char *data "byte[]" +%typemap(cstype) const char *data "byte[]" +%typemap(in) const char *data %{ $1 = ($1_ltype)$input; %} +%typemap(csin) const char *data "$csinput" + +%typemap(imtype, out="System.IntPtr") char* getData "byte[]" +%typemap(cstype) char* getData "byte[]" + +%typemap(csout) char* getData %{ + { + byte[] ret = new byte[this.getSize()]; + System.IntPtr data = $imcall; + System.Runtime.InteropServices.Marshal.Copy(data, ret, 0, (int)this.getSize()); + return ret; + } +%} + +// make sure we do not get the default with SWIG_csharp_string_callback +%typemap(out) char* getData { + $result = (char *)result; +} + + + +%csmethodmodifiers uscxml::Event::getParamMap() "private"; +%csmethodmodifiers uscxml::Event::getParamMapKeys() "private"; +%csmethodmodifiers uscxml::Event::setParamMap(const std::map<std::string, std::list<uscxml::Data> >&) "private"; +%csmethodmodifiers uscxml::Event::getNameListKeys() "private"; +%csmethodmodifiers uscxml::Interpreter::getIOProcessorKeys() "private"; +%csmethodmodifiers uscxml::Interpreter::getInvokerKeys() "private"; +%csmethodmodifiers uscxml::Interpreter::getInvokers() "private"; +%csmethodmodifiers uscxml::Interpreter::getIOProcessors() "private"; +%csmethodmodifiers uscxml::Data::getCompoundKeys() "private"; + +%include "../uscxml_beautify.i" + +%typemap(csimports) uscxml::Interpreter %{ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +%} + +%typemap(cscode) uscxml::Interpreter %{ + public Dictionary<string, NativeIOProcessor> getIOProcessors() { + Dictionary<string, NativeIOProcessor> ioProcs = new Dictionary<string, NativeIOProcessor>(); + StringVector keys = getIOProcessorKeys(); + IOProcMap ioProcMap = getIOProcessorsNative(); + for (size_t i = 0; i < keys.Count; i++) { + ioProcs[keys[i]] = ioProcMap[keys[i]]; + } + return ioProcs; + } + + public Dictionary<string, NativeInvoker> getInvokers() { + Dictionary<string, NativeInvoker> invokers = new Dictionary<string, NativeInvoker>(); + StringVector keys = getInvokerKeys(); + InvokerMap invokerMap = getInvokersNative(); + for (size_t i = 0; i < keys.Count; i++) { + invokers[keys[i]] = invokerMap[keys[i]]; + } + return invokers; + } + +%} + + +%rename(getCompoundNative) uscxml::Data::getCompound(); +%rename(getArrayNative) uscxml::Data::getArray(); +%rename(setCompoundNative) uscxml::Data::setCompound(const std::map<std::string, Data>&); +%rename(setArrayNative) uscxml::Data::setArray(const std::list<Data>&); +%csmethodmodifiers uscxml::Data::getCompound() "private"; +%csmethodmodifiers uscxml::Data::getArray() "private"; +%csmethodmodifiers uscxml::Data::setCompound(const std::map<std::string, Data>&) "private"; +%csmethodmodifiers uscxml::Data::setArray(const std::list<Data>&) "private"; +%csmethodmodifiers uscxml::Data::getCompoundKeys() "private"; + +%typemap(csimports) uscxml::Data %{ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; +%} + +%typemap(cscode) uscxml::Data %{ + public Data(byte[] data, String mimeType) : this() { + setBinary(new Blob(data, mimeType)); + } + + public Data(List<Data> arr) : this() { + setArray(arr); + } + + public Data(Dictionary<string, Data> compound) : this() { + setCompound(compound); + } + + public Dictionary<string, Data> getCompound() { + Dictionary<string, Data> compound = new Dictionary<string, Data>(); + DataMap dataMap = getCompoundNative(); + StringVector dataMapKeys = getCompoundKeys(); + for (size_t i = 0; i < dataMapKeys.Count; i++) { + compound[dataMapKeys[i]] = dataMap[dataMapKeys[i]]; + } + return compound; + } + + public void setCompound(Dictionary<string, Data> compound) { + DataMap dataMap = new DataMap(); + foreach(KeyValuePair<string, Data> entry in compound) { + dataMap.Add(entry); + } + setCompoundNative(dataMap); + } + + public List<Data> getArray() { + List<Data> arr = new List<Data>(); + DataList dataList = getArrayNative(); + for (size_t i = 0; i < dataList.size(); i++) { + arr.Add(dataList.get(i)); + } + return arr; + } + + public void setArray(List<Data> arr) { + DataList dataList = new DataList(); + foreach (Data data in arr) { + dataList.add(data); + } + setArrayNative(dataList); + } + +%} + +%rename(getNameListNative) uscxml::Event::getNameList(); +%rename(getParamsNative) uscxml::Event::getParams(); +%rename(setNameListNative) uscxml::Event::setNameList(const std::map<std::string, Data>&); +%rename(setParamsNative) uscxml::Event::setParams(const std::multimap<std::string, Data>&); +%csmethodmodifiers uscxml::Event::getNameList() "private"; +%csmethodmodifiers uscxml::Event::getNameListKeys() "private"; +%csmethodmodifiers uscxml::Event::getParams() "private"; +%csmethodmodifiers uscxml::Event::setNameList(const std::map<std::string, Data>&) "private"; +%csmethodmodifiers uscxml::Event::setParams(const std::multimap<std::string, Data>&) "private"; + +%typemap(csimports) uscxml::Event %{ + using System; + using System.Collections.Generic; + using System.Runtime.InteropServices; +%} + +%typemap(cscode) uscxml::Event %{ + public Dictionary<string, List<Data> > getParams() { + Dictionary<string, List<Data>> parameters = new Dictionary<string, List<Data>>(); + ParamMap paramMap = getParamMap(); + + foreach (KeyValuePair<string, DataList> entry in paramMap) { + DataList dataList = entry.Value; + List<Data> paramList = new List<Data>(); + for (size_t i = 0; i < dataList.size(); i++) { + Data data = dataList.get(i); + paramList.Add(data); + } + parameters.Add(entry.Key, paramList); + } + return parameters; + } + + public void setParams(Dictionary<string, List<Data>> parameters) { + ParamMap paramMap = new ParamMap(); + foreach(KeyValuePair<string, List<Data>> entry in parameters) { + DataList dataList = new DataList(); + foreach (Data data in entry.Value) { + dataList.add(data); + } + paramMap.Add(entry.Key, dataList); + } + setParamMap(paramMap); + } + + public Dictionary<string, Data> getNameList() { + Dictionary<string, Data> nameList = new Dictionary<string, Data>(); + DataMap nameListMap = getNameListNative(); + StringVector nameListMapKeys = getNameListKeys(); + for (size_t i = 0; i < nameListMapKeys.Count; i++) { + nameList[nameListMapKeys[i]] = nameListMap[nameListMapKeys[i]]; + } + return nameList; + } + + public void setNameList(Dictionary<string, Data> nameList) { + DataMap dataMap = new DataMap(); + foreach (KeyValuePair<string, Data> entry in nameList) { + dataMap.Add(entry); + } + setNameListNative(dataMap); + } +%} + +//*********************************************** +// Parse the header file to generate wrappers +//*********************************************** + +%include "../../../uscxml/Common.h" +%include "../../../uscxml/Factory.h" +%include "../../../uscxml/Message.h" +%include "../../../uscxml/Interpreter.h" +%include "../../../uscxml/concurrency/BlockingQueue.h" +%include "../../../uscxml/server/HTTPServer.h" +//%include "../../../uscxml/debug/DebuggerServlet.h" +%include "../../../uscxml/debug/InterpreterIssue.h" + +%include "../../../uscxml/messages/Blob.h" +%include "../../../uscxml/messages/Data.h" +%include "../../../uscxml/messages/Event.h" +%include "../../../uscxml/messages/InvokeRequest.h" +%include "../../../uscxml/messages/SendRequest.h" + +%include "../../../uscxml/plugins/DataModel.h" +%include "../../../uscxml/plugins/EventHandler.h" +%include "../../../uscxml/plugins/ExecutableContent.h" +%include "../../../uscxml/plugins/Invoker.h" +%include "../../../uscxml/plugins/IOProcessor.h" + +%include "../wrapped/WrappedInvoker.h" +%include "../wrapped/WrappedDataModel.h" +%include "../wrapped/WrappedExecutableContent.h" +%include "../wrapped/WrappedIOProcessor.h" +%include "../wrapped/WrappedInterpreterMonitor.h" + + +%template(IssueList) std::list<uscxml::InterpreterIssue>; +%template(DataList) std::list<uscxml::Data>; +%template(DataMap) std::map<std::string, uscxml::Data>; +%template(StringSet) std::set<std::string>; +%template(StringVector) std::vector<std::string>; +%template(StringList) std::list<std::string>; +%template(ParamMap) std::map<std::string, std::list<uscxml::Data> >; +%template(IOProcMap) std::map<std::string, IOProcessor>; +%template(InvokerMap) std::map<std::string, Invoker>; +%template(ParentQueue) uscxml::concurrency::BlockingQueue<uscxml::SendRequest>; |