2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JNI C/C++の長い関数名を短くする

Posted at

JNIの関数名は長い

普段プログラミングするときは識別子が長くなりすぎないよう気を遣っている。
だが JNI(Java Native Interface) では、System.loadLibrary でC/C++関数を使えるようにするためにも長い関数名にせざるを得ないのだ。

example
JNIEXPORT void JNICALL Java_com_example_araki_app_jniBridge_nativeOnSurfaceChanged(JNIEnv* env, jobject obj, jint height, jint width);

エディタを横に分割すると宣言が収まり切れなくなることがある。
関数宣言が途中で改行されると、ただでさえ読みづらいのに見栄えも悪くなる。

解決策

C言語のマクロを使う。

macro
# define JniFuncVoid(return_type, func_name)          \
JNIEXPORT return_type JNICALL                        \
Java_com_example_araki_app_jniBridge_##func_name     \
    (JNIEnv* env, jobject obj)

# define JniFuncArg(return_type, func_name, ...)      \
JNIEXPORT return_type JNICALL                        \
Java_com_example_araki_app_jniBridge_##func_name     \
    (JNIEnv* env, jobject obj, __VA_ARGS__)

JniFuncArg(void, nativeOnSurfaceChanged, jint height, jint width);

return_type 関数の戻り値の型
func_name 関数名の共通していない部分
... 関数の共通していない引数

JniFuncVoidは引数なし
JniFuncArgは引数あり

C++20で追加される __VA_OPT__ が使えればマクロを一つにまとめられる。

2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?