//
// Created by Ht on 2021/1/5.
//

#ifndef LIBDEVCPP_QJNIUTIL_H
#define LIBDEVCPP_QJNIUTIL_H

#ifdef ANDROID

#include <jni.h>
#include <qlibc/QData.hpp>

namespace qlibc {


    static const char *empty_str = "";

#define GET_JNI_STR(var, env, str) \
    const char* var = (str==nullptr)?nullptr:env-> GetStringUTFChars(str, JNI_FALSE)

#define GET_JSON_FROM_JNI_STR(var, env, str) \
    GET_JNI_STR(var##_str_, jenv, str); \
    QData var; \
    if(var##_str_!=nullptr) var.initWithJsonStr(var##_str_)

#define MAKE_JNI_STR(env, str) \
    env->NewStringUTF((const char*)str.c_str())

#define RET_STR_TO_JNI(env,str) \
    return MAKE_JNI_STR(env,str)

#define Jni_Sig_QData_Cass_  ("Lcom/changhong/qlib/QData;")
#define Jni_Sig_QIData_Cass_  ("Lcom/changhong/qlib/intf/QIData;")

#define implJni(package, rtype, method) \
    extern "C" JNIEXPORT rtype JNICALL Java_##package##_##method

#define AttacheJEnvThread(env,vm) \
    JNIEnv *env; vm->AttachCurrentThread(reinterpret_cast<void **>(&env), nullptr)

#define DetacheJEnvThread(env,vm) \
        vm->DetachCurrentThread()

    class QJNIUtil {
    public:
        static jobject CppQDataToJavaQData(JNIEnv *env, const QData &message);
        static void CppQDataFromJavaQData(JNIEnv *env, QData &message, jobject jmsg);
    };
}
#endif

#endif //LIBDEVCPP_QJNIUTIL_H
