English 中文(简体)
是否有可能利用宏观方法确定内容?
原标题:Is it possible using macros to process defines contents?

我目前正在与JNI(Java原住民接口)合作,在 Java和C++之间发送数据。 在执行部分法典后,每个方法的代码总是相似的。 例如:

JNIEXPORT void JNICALL Java_com_trial_jni_Receiver_setData__II(JNIEnv * env, jobject thiz, jint nativeObject, jint value)  
{  
    reinterpret_cast<Receiver *>(nativeObject)->setData(value);  
}  

JNIEXPORT void JNICALL Java_com_trial_jni_Receiver_setData__ILjava_lang_String_2(JNIEnv *env, jobject thiz, jint nativeObject, jstring value)  
{  
    reinterpret_cast<Receiver *>(nativeObject)->setData(value);  
}

由于所有法典都有类似的结构,我决定产生一套宏观办法,以自动产生所有这一守则。 感谢格雷戈·帕科斯兹在这一链接中的回答: 可否对理论论调? 现在,我能够利用预处理器核实我在宏观和每段处理中采用多少参数。

但是,从前一个例子来看,我无法回避我想要的东西。 附录一在称为JNI_的宏观范围内有这种方法。 METHOD。 我愿这样做:

#define JNI_METHOD(package,clazz,method,...)   
    JNIEXPORT void JNICALL Java_ ##package## _ ##clazz## _ ##method##__II(JNIEnv * env, jobject thiz, jint nativeObject, SET_DECLARATION_PARAMS(__VA_ARGS__ ))   
    {   
        reinterpret_cast<clazz *>(nativeObject)->method(SET_DECLARED_PARAMS(__VA_ARGS__));   
    }  

JNI_METHOD(com_trial_jni,Receiver,setData,jint);  
JNI_METHOD(com_trial_jni,Receiver,setData,jstring);  

In order to avoid having this question too long I didn t paste the declaration of SET_DECLARATION_PARAMS and SET_DECLARED_PARAMS but the first one will result in something like jint arg1 and the second in arg1 without the type.

问题在于:有没有办法为 j或Ljava_lang_String_2 for jstring创造宏观回报。 请注意,无法使用扼杀装置,需要将ILjava_lang_String_2而不是II列入第二个生成的方法名称。

感谢!

最佳回答

你提供的联系在很大程度上给你必要的解决办法。 考虑:

#define CONCATENATE(arg1, arg2)   CONCATENATE1(arg1, arg2)
#define CONCATENATE1(arg1, arg2)  CONCATENATE2(arg1, arg2)
#define CONCATENATE2(arg1, arg2)  arg1##arg2

#define JNI_TRANSLATE_TYPE_jint I
#define JNI_TRANSLATE_TYPE_jstring Ljava_lang_String_2

#define JNI_TRANSLATE_TYPE(T) CONCATENATE(JNI_TRANSLATE_TYPE_, T)

在VS2010年测试:

#define STRINGIZE(arg)  STRINGIZE1(arg)
#define STRINGIZE1(arg) STRINGIZE2(arg)
#define STRINGIZE2(arg) #arg

#pragma message("jint: " STRINGIZE(JNI_TRANSLATE_TYPE(jint)))
#pragma message("jstring: " STRINGIZE(JNI_TRANSLATE_TYPE(jstring)))

产出:

1>  jint: I
1>  jstring: Ljava_lang_String_2
问题回答

You might also consider using SWIG to generate the JNI wrappers for you. It can handle and hide all the nasty code for wrapping objects with JNI, as well as many other language mappings.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签