简体   繁体   中英

SWIG C++ to Java - Wrapping Enum pointer (pass by reference)

I have an ENUM variable like this.

enum FIO
{
   FIO_Error = 0,
   FIO_Error_1,
   FIO_Error_2
};

Also a function that changes the error code as such:

void changeFIO (FIO_Error_Enum *errorCode)
{
    *errorCode = FIO_Error_2;
}

So my question is this. I know that SWIG is a pain when it comes to wrapping variables passed into a function by reference. Especially now that I am dealing with a variable that is an enumeration type.

I know that I have to set it up in the interface to code in order to do this properly but I'm having real trouble with it.

%include <typemaps.i>

%define ENUM_OUTPUT_TYPEMAPS(TYPE, TYPENAME) 
%typemap(in) int *OUTPUT($*1_ltype temp), int &OUTPUT($*1_ltype temp) { 
   if (!$input) { 
     SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "array null"); 
     return $null; 
   } 
   if (JCALL1(GetArrayLength, jenv, $input) == 0) { 
     SWIG_JavaThrowException(jenv, SWIG_JavaIndexOutOfBoundsException, "Array must contain at least 1 element"); 
     return $null; 
   } 
   $1 = (TYPE *)&temp; 
} 
%typemap(argout) int *OUTPUT, int &OUTPUT { 
   jint jvalue = (jint)temp$argnum; 
   JCALL4(SetIntArrayRegion, jenv, $input, 0, 1, &jvalue); 
} 

%typemap(javain, 
          pre= "    int[] temp$javainput = new int[1];", 
          post="      $javainput[0] = TYPE.swigToEnum(temp$javainput[0]);" 
         ) int *OUTPUT, int &OUTPUT "temp$javainput" 
%typemap(jni) int *OUTPUT, int &OUTPUT "jintArray" 
%typemap(jtype) int *OUTPUT, int &OUTPUT "int[]" 
%typemap(jstype) int *OUTPUT, int &OUTPUT "$*javaclassname[]" 

%apply int* OUTPUT {TYPE *TYPENAME, TYPE &TYPENAME}; 
%enddef 

ENUM_OUTPUT_TYPEMAPS(FIO_Error_Enum, errorCode);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM