private CustomFunctionWrapperImpl ReturnsVectorWrapper(System.Func <string, bool> matchFun)
        {
            CustomFunctionWrapperImpl cw = new CustomFunctionWrapperImpl()
            {
                StatementSep              = this.StatementSep,
                IsMatchFunc               = matchFun,
                ApiArgToWrappingLang      = ApiArgToRfunctionArgument,
                ApiCallArgument           = this.ApiCallArgument,
                TransientArgsCreation     = this.TransientArgsCreation,
                FunctionNamePostfix       = this.FunctionNamePostfix,
                CalledFunctionNamePostfix = this.ApiCallPostfix,
                ApiSignatureToDocString   = this.ApiSignatureToBasicRoxygenString,
                Template = @"
%WRAPFUNCTIONDOCSTRING%
%WRAPFUNCTION% <- function(%WRAPARGS%)
{
    %TRANSARGS%
    result <- %FUNCTION%(%WRAPARGS%);
    return(" + CreateXptrObjRefFunction + @"(result,'dummytype'))
}
"
            };

            return(cw);
        }
        private CustomFunctionWrapperImpl ReturnsVectorWrapper(System.Func <string, bool> matchFun, string apitype,
                                                               string rcpptype, string convertingFunc)
        {
            CustomFunctionWrapperImpl cw = new CustomFunctionWrapperImpl()
            {
                IsMatchFunc           = matchFun,
                ApiArgToWrappingLang  = ApiArgToRcpp,
                ApiCallArgument       = ApiCallArgument,
                TransientArgsCreation = TransientArgsCreation,
                TransientArgsCleanup  = TransientArgsCleanup,
                FunctionNamePostfix   = this.FunctionNamePostfix,

                Template = @"
// [[Rcpp::export]]
" + rcpptype + @" %WRAPFUNCTION%(%WRAPARGS%)
{
	int size; 
    %TRANSARGS%    " + apitype + @" values = %FUNCTION%(%ARGS% &size);
    %CLEANTRANSARGS%    return " + convertingFunc + @"(values, size, true);
}
"
            };

            return(cw);
        }
예제 #3
0
        public CustomFunctionWrapperImpl ReturnsCharPtrPtrWrapper()
        {
            CustomFunctionWrapperImpl cw = new CustomFunctionWrapperImpl()
            {
                IsMatchFunc           = StringHelper.ReturnsCharPP,
                ApiArgToWrappingLang  = ApiArgToCsharp,
                ApiCallArgument       = ApiCallArgument,
                TransientArgsCreation = TransientArgsCreation,
                TransientArgsCleanup  = TransientArgsCleanup,
                FunctionNamePostfix   = this.FunctionNamePostfix,

                Template = @"string[] %FUNCTION%_cs(%WRAPARGS%)
{
    IntPtr size = InteropHelper.AllocHGlobal<int>();
    %TRANSARGS%    IntPtr result = " + ClassName + @".NativeSwiftLib.GetFunction<%FUNCTION%_csdelegate>(" +
                           QuotedString("%FUNCTION%") +
                           @")(%ARGS%size);
    %CLEANTRANSARGS%    int n = InteropHelper.Read<int>(size, true);
    return InteropHelper.GlobalAnsiToArrayString(result, n, true);
}
"
            };

            return(cw);
        }
        public RXptrWrapperGenerator()
        {
            AssignmentSymbol          = "<-";
            ReturnedValueVarname      = "result";
            Indentation               = "  ";
            RoxygenStartMarker        = "#'";
            RoxyExportFunctions       = true;
            RoxygenParameterTag       = "@param";
            RoxygenExportTag          = "@export";
            StatementSep              = "";
            CreateXptrObjRefFunction  = "cinterop::mkExternalObjRef";
            GetXptrFromObjRefFunction = "cinterop::getExternalXptr";

            ClearCustomWrappers();
            CustomFunctionWrapperImpl cw = ReturnsCharPtrPtrWrapper();

            AddCustomWrapper(cw);

            GenerateRoxygenDoc  = true;
            RoxygenDocPostamble = string.Empty;

            SetTransientArgConversion(".*", "",
                                      "C_ARGNAME <- " + GetXptrFromObjRefFunction + @"(RCPP_ARGNAME)" + StatementSep, //    x <- getSwiftXptr(x);
                                      "");                                                                            // no cleanup
        }
예제 #5
0
        public CustomFunctionWrapperImpl ReturnsCharPtrPtrWrapper()
        {
            CustomFunctionWrapperImpl cw = new CustomFunctionWrapperImpl()
            {
                IsMatchFunc          = StringHelper.ReturnsCharPP,
                ApiArgToWrappingLang = ApiArgToCsharp,
                ApiCallArgument      = ApiCallArgument,
                FunctionNamePostfix  = this.FunctionNamePostfix,

                Template = UniformIndentation + @"private delegate IntPtr %FUNCTION%_csdelegate(%WRAPARGS%, IntPtr size)"
            };

            return(cw);
        }
예제 #6
0
        public PythonCffiWrapperGenerator()
        {
            ApiCallPrefix = "mynativelib.";

            AssignmentSymbol           = "=";
            ReturnedValueVarname       = "result";
            Indentation                = "    ";
            FunctionBodyOpenDelimiter  = "";
            FunctionBodyCloseDelimiter = "";
            PyDocstringStartMarker     = "";
            PyDocExportFunctions       = true;

            // See https://pypi.org/project/refcount/
            CreateXptrObjRefFunction  = "custom_wrap_cffi_native_handle"; // because needs a custom callback del function.
            GetXptrFromObjRefFunction = "unwrap_cffi_native_handle";
            PyDocstringParameterTag   = "";
            PyDocstringExportTag      = "";

            StatementSep              = "";
            CreateXptrObjRefFunction  = "cinterop.mkExternalObjRef";
            GetXptrFromObjRefFunction = "cinterop.getExternalXptr";

            ClearCustomWrappers();
            CustomFunctionWrapperImpl cw = ReturnsCharPtrPtrWrapper();

            AddCustomWrapper(cw);

            GeneratePyDocstringDoc  = true;
            PyDocstringDocPostamble = string.Empty;

            //SetTransientArgConversion(".*", "",
            //    "C_ARGNAME = " + GetXptrFromObjRefFunction + @"(RCPP_ARGNAME)" + StatementSep, //    x <- getSwiftXptr(x);
            //    ""); // no cleanup

            // termination_elems_c = [FFI_.new("char[]", as_bytes(x)) for x in termination_elements]
            // which I want as:
            // termination_elems_c = to_c_char_ptrptr(termination_elements)
            SetTransientArgConversion(
                "char**",
                "_charpp",
                // "IntPtr elementIds_charpp = InteropHelper.ArrayStringToHGlobalAnsi(elementIds);"
                // "IntPtr C_ARGNAME = InteropHelper.ArrayStringToHGlobalAnsi(RCPP_ARGNAME);"
                "C_ARGNAME = to_c_char_ptrptr(RCPP_ARGNAME)",
                ""); // no cleanup?
        }
예제 #7
0
        public CustomFunctionWrapperImpl ReturnsCharPtrPtrWrapper()
        {
            CustomFunctionWrapperImpl cw = new CustomFunctionWrapperImpl()
            {
                IsMatchFunc               = StringHelper.ReturnsCharPP,
                StatementSep              = this.StatementSep,
                ApiArgToWrappingLang      = ApiArgToMatlabfunctionArgument,
                ApiCallArgument           = this.ApiCallArgument,
                TransientArgsCreation     = this.TransientArgsCreation,
                FunctionNamePostfix       = this.FunctionNamePostfix,
                CalledFunctionNamePostfix = this.ApiCallPostfix,
                ApiSignatureToDocString   = this.ApiSignatureToBasicRoxygenString,
                Template = @"
function f = %WRAPFUNCTION%(%WRAPARGS%)
%WRAPFUNCTIONDOCSTRING%
    pSize = libpointer('int32Ptr', 0);

    result = calllib('" + NativeLibraryNameNoext + @"', '%FUNCTION%', %ARGS% pSize);

    len = pSize.Value;

    resCell = cell(1,len);
    for i=1:len,
        nP = result + (i - 1);
        resCell(i) = nP.Value;
    end

    f = resCell;

    clear pSize;
    clear result;
end
"
            };

            return(cw);
        }
        public RcppGlueWrapperGenerator()
        {
            AssignmentSymbol                = "=";
            ReturnedValueVarname            = "result";
            FunctionNamePostfix             = "_R";
            OpaquePointers                  = false;
            DeclarationOnly                 = false;
            AddRcppExport                   = true;
            NewLineString                   = "\n";
            ReturnedValueDeclarationKeyword = "auto";

            SetTypeMap("void", "void");
            SetTypeMap("int", "IntegerVector");
            SetTypeMap("int*", "IntegerVector");
            SetTypeMap("char**", "CharacterVector");
            SetTypeMap("char*", "CharacterVector");
            SetTypeMap("char", "CharacterVector");
            SetTypeMap("double", "NumericVector");
            SetTypeMap("double*", "NumericVector");
            SetTypeMap("double**", "NumericMatrix");
            SetTypeMap("bool", "LogicalVector");
            SetTypeMap("const char", "CharacterVector");
            SetTypeMap("const int", "IntegerVector");
            SetTypeMap("const double", "NumericVector");
            SetTypeMap("const char*", "CharacterVector");
            SetTypeMap("const int*", "IntegerVector");
            SetTypeMap("const double*", "NumericVector");

            OpaquePointerClassName = "OpaquePointer";
            PrependOutputFile      = "// This file was GENERATED\n//Do NOT modify it manually, as you are very likely to lose work\n\n";

            ClearCustomWrappers();
            CustomFunctionWrapperImpl cw = ReturnsCharPtrPtrWrapper();

            AddCustomWrapper(cw);
        }
예제 #9
0
        public CustomFunctionWrapperImpl ReturnsDoublePtrWrapper()
        {
            CustomFunctionWrapperImpl cw = new CustomFunctionWrapperImpl()
            {
                StatementSep              = this.StatementSep,
                IsMatchFunc               = StringHelper.ReturnsDoublePtr,
                ApiCallArgument           = this.ApiCallArgument,
                TransientArgsCreation     = this.TransientArgsCreation,
                FunctionNamePostfix       = this.FunctionNamePostfix,
                CalledFunctionNamePrefix  = this.ApiCallPrefix,
                CalledFunctionNamePostfix = this.ApiCallPostfix,
                ApiArgToWrappingLang      = ApiArgToRcpp,
                Template = @"
std::vector<std::vector<std::double>> %WRAPFUNCTION%(%WRAPARGS%)
{
	int size; 
	double** values = %FUNCTION%(%ARGS% &size);
	return cinterop::utils::to_cpp_numeric_vector(values, size);
}
"
            };

            return(cw);
        }
 public void AddCustomWrapper(CustomFunctionWrapperImpl cw)
 {
     customWrappers.Add(cw);
 }
예제 #11
0
        public MatlabApiWrapperGenerator()
        {
            ApiCallOpenParenthesis = false; // a kludge switch to cater for matlab's calllib
            FunctionOutputName     = "f";
            FunctionNamePostfix    = "";
            NativeLibraryNameNoext = "mylibname";

            UniformIndentationCount = 0;
            Indentation             = "    ";

            AssignmentSymbol           = "=";
            ReturnedValueVarname       = "result";
            Indentation                = "  ";
            FunctionBodyOpenDelimiter  = "";
            FunctionBodyCloseDelimiter = "end";
            MatlabCommentMarker        = "% ";
            //RoxyExportFunctions = true;
            MatlabInputParameterTag = "INPUT";
            //RoxygenExportTag = "@export";
            StatementSep              = ";";
            CreateXptrObjRefFunction  = "mkExternalObjRef";
            GetXptrFromObjRefFunction = "getExternalPtr";

            ClearCustomWrappers();
            CustomFunctionWrapperImpl cw = ReturnsCharPtrPtrWrapper();

            AddCustomWrapper(cw);

            GenerateFunctionDoc = false;
            //RoxygenDocPostamble = string.Empty;

            SetTransientArgConversion(
                "char**",
                "_charpp",
                // pNodeIds = libpointer('stringPtrPtr', nodeIds);
                "C_ARGNAME = libpointer('stringPtrPtr', RCPP_ARGNAME);",
                // clear pNodeIds;
                "clear(C_ARGNAME);");

            // // All structs are marshalled in a similar manner:
            // string createPtr =
            //     // IntPtr geom_struct = InteropHelper.StructureToPtr(geom);
            //     // IntPtr C_ARGNAME = InteropHelper.StructureToPtr(RCPP_ARGNAME);
            //     "IntPtr C_ARGNAME = InteropHelper.StructureToPtr(RCPP_ARGNAME);";
            // string freePtr =
            //     // InteropHelper.FreeNativeStruct(geom_struct, ref geom, true);
            //     // InteropHelper.FreeNativeStruct(C_ARGNAME, ref RCPP_ARGNAME, true);
            //     "InteropHelper.FreeNativeStruct(C_ARGNAME, ref RCPP_ARGNAME, true);";

            //SetTypeMap("TS_GEOMETRY_PTR", "ref MarshaledTimeSeriesGeometry");
            SetTransientArgConversion(
                "TS_GEOMETRY_PTR",
                "_struct",
                // e.g. matlab\native\estimateDualPassParameters.m
                //tsGeo = createTsGeometry(dts, timeStepInSeconds, lenData);
                //pTsGeo = libstruct('MarshaledTsGeometry', tsGeo);
                "C_ARGNAME = createTsGeometry(RCPP_ARGNAME);",
                ""); // No cleanup? really?

            SetTypeMap("DATE_TIME_INFO_PTR", "ref MarshaledDateTime");
            SetTransientArgConversion(
                "DATE_TIME_INFO_PTR",
                "_struct",
                // estimationEndDt = createDateTimeFrom(estimationEnd);
                "C_ARGNAME = createDateTimeFrom(RCPP_ARGNAME);",
                ""); // No cleanup? really?

            string createArrayStructPtr =
                "IntPtr C_ARGNAME = InteropHelper.ArrayOfStructureToPtr(RCPP_ARGNAME);";
            string freeArrayStructPtr =
                "InteropHelper.FreeNativeArrayOfStruct(C_ARGNAME, ref RCPP_ARGNAME, false);";

            //#define NODE_INFO_PTR  NodeInfoTxt*
            SetTransientArgConversion(
                "NODE_INFO_PTR",
                "_struct", createArrayStructPtr, freeArrayStructPtr);

            //#define LINK_INFO_PTR  LinkInfoTxt*
            SetTransientArgConversion(
                "LINK_INFO_PTR",
                "_struct", createArrayStructPtr, freeArrayStructPtr);

            SetTransientArgConversion("double**", "_doublepp",
                                      "IntPtr C_ARGNAME = InteropHelper.BiArrayDoubleToNative(RCPP_ARGNAME);",
                                      "InteropHelper.FreeBiArrayDouble(C_ARGNAME, RCPP_ARGNAME.Length);");

            SetTransientArgConversion("double*", "_doublep",
                                      // pAreasKm2 = libpointer('doublePtr', areasKm2);
                                      "C_ARGNAME = libpointer('doublePtr', RCPP_ARGNAME);",
                                      "clear(C_ARGNAME);");

            //SWIFT_API COMPOSITE_PARAMETERIZER_PTR AggregateParameterizers(const char* strategy, ARRAY_OF_PARAMETERIZERS_PTR parameterizers, int numParameterizers);
            //         INativeParameterizer AggregateParameterizers_cs(string strategy, INativeParameterizer[] parameterizers, int numParameterizers)
            SetTransientArgConversion(
                "ARRAY_OF_PARAMETERIZERS_PTR",
                "_array_ptr",
                //IntPtr parameterizers_array_ptr = InteropHelper.CreateNativeArray(Array.ConvertAll(parameterizers, p => p.GetHandle()));
                "IntPtr C_ARGNAME = InteropHelper.CreateNativeArray(Array.ConvertAll(RCPP_ARGNAME, p => p.GetHandle()));",
                "InteropHelper.DeleteNativeArray(C_ARGNAME);");

            SetTransientArgConversion(".*_PTR", "",
                                      "C_ARGNAME = " + GetXptrFromObjRefFunction + @"(RCPP_ARGNAME)" + StatementSep, //    x_ptr = getSwiftXptr(x);
                                      "");                                                                           // no cleanup

            // typenames followed by at least one *
            SetTransientArgConversion(".*\\*+", "",
                                      "C_ARGNAME = " + GetXptrFromObjRefFunction + @"(RCPP_ARGNAME)" + StatementSep, //    x_ptr = getSwiftXptr(x);
                                      "");                                                                           // no cleanup
        }