summaryrefslogtreecommitdiffstats
path: root/debian/uncrustify-trinity/uncrustify-trinity-0.75.0/tests/input/oc/c-cpp-oc-wrapper.c
blob: 663a4cc89daceb8ffaeed030322088b8dc8f4f55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// C/C++/Obj-C/Obj-C++ wrapper that exports C functions
//
// As an example, distributed with .c suffix,
// but depending on extension needs, can be
// compiled as C, C++, Obj-C or Obj-C++

#include <stdio.h>
#include <unistd.h>
#include "TestClassNativeHelper.h"

#undef NDEBUG

#if defined(__ANDROID__)

#define TCH_LOGI(...) __android_log_print(ANDROID_LOG_INFO, "TestClassNativeHelper", __VA_ARGS__)
#define TCH_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "TestClassNativeHelper", __VA_ARGS__)

#ifndef NDEBUG
#define TCH_LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "TestClassNativeHelper", __VA_ARGS__)
#else
#define TCH_LOGV(...)
#endif

#define TCH_ELOG TCH_LOGV("%s", __FUNCTION__)

void TCH_onTestClassUploadDidProgress(C_JNIEnv *env, jobject thiz, jint videoId, jdouble progress) {
    TCH_ELOG;
}

#ifndef TCH_NELEM
#define TCH_NELEM(x) ((int)(sizeof(x) / sizeof((x)[0])))
#endif

static const char *kTCHTestClassClass = "com/testclass/TestClass/TestClass";
static const char *kTCHTestClassLoader = "com.testclass.TestClass.TestClass";

static const char *kTCHTestClassNativeListenerClass = "com/testclass/TestClass/communication/TestClassNativeListener";
static const char *kTCHTestClassNativeListenerLoader = "com.testclass.TestClass.communication.TestClassNativeListener";

static JavaVM *TCH_vm = NULL;

static JNINativeMethod TCH_listenerMethods[] = {
    { "onTestClassUploadDidProgress", "(ID)V", (void *)TCH_onTestClassUploadDidProgress },
};

static C_JNIEnv *TCH_getEnv() {
    C_JNIEnv *ret = NULL;

    if (TCH_vm == NULL) {
        TCH_LOGE("TCH_getEnv failed, no JVM");
        return NULL;
    }

#if defined(__cplusplus)
    JNIEnv *env = NULL;

    if (TCH_vm->GetEnv((void **)&env, JNI_VERSION_1_6) != JNI_OK) {
        JavaVMAttachArgs args;
        args.version = JNI_VERSION_1_6;
        args.name = NULL;
        args.group = NULL;

        int attachStatus;
        if ((attachStatus = TCH_vm->AttachCurrentThread(&env, &args)) < 0) {
            TCH_LOGE("TCH_getEnv failed");
        }
    }
    ret = (C_JNIEnv *)env;
#else
    JNIEnv *env = NULL;

    if ((*TCH_vm)->GetEnv(TCH_vm, (void **)&env, JNI_VERSION_1_6) != JNI_OK) {
        JavaVMAttachArgs args;
        args.version = JNI_VERSION_1_6;
        args.name = NULL;
        args.group = NULL;

        int attachStatus;
        if ((attachStatus = (*TCH_vm)->AttachCurrentThread(TCH_vm, &env, &args)) < 0) {
            TCH_LOGE("TCH_getEnv failed");
        }
    }
    ret = env;
#endif /* if defined(__cplusplus) */
    return ret;
}

static jclass TCH_loadClass(C_JNIEnv *env, jobject activity, const char *className) {
    jclass cls_Activity = (*env)->GetObjectClass((JNIEnv *)env, activity);
    jmethodID mid_getClassLoader = (*env)->GetMethodID((JNIEnv *)env, cls_Activity, "getClassLoader", "()Ljava/lang/ClassLoader;");
    jobject obj_classLoader = (*env)->CallObjectMethod((JNIEnv *)env, activity, mid_getClassLoader);

    jclass cls_classLoader = (*env)->GetObjectClass((JNIEnv *)env, obj_classLoader);
    jmethodID mid_loadClass = (*env)->GetMethodID((JNIEnv *)env, cls_classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");

    jstring _className = (*env)->NewStringUTF((JNIEnv *)env, className);
    jclass cls = (jclass)(*env)->CallObjectMethod((JNIEnv *)env, obj_classLoader, mid_loadClass, _className);

    (*env)->DeleteLocalRef((JNIEnv *)env, _className);

    if (!cls) {
        TCH_LOGE("Couldn't find class %s", className);
    }

    return cls;
}

#define TCH_str2(x)             # x
#define TCH_str(x)              TCH_str2(x)

#define TCH_CLASS(_class)       tchClass_ ## _class

#define TCH_LOCAL_CLASS(_class) local_tchClass_ ## _class

#define TCH_REGISTER_CLASS(_class) \
    jclass TCH_CLASS(_class) = NULL;

#define TCH_EXTERN_CLASS(_class) \
    jclass TCH_CLASS(_class);

#define TCH_METHOD(_method)     tchMethod_ ## _method

#define TCH_REGISTER_METHOD(_method) \
    jmethodID TCH_METHOD(_method) = NULL;

#define TCH_EXTERN_METHOD(_method) \
    jmethodID TCH_METHOD(_method);

#define TCH_REGISTER_NATIVES(_env, _class, methods) \
    if ((*_env)->RegisterNatives((JNIEnv *)_env, TCH_CLASS(_class), methods, TCH_NELEM(methods)) < 0) { \
        TCH_LOGE("RegisterNatives failed for %s\n", TCH_str(_class)); \
    }

#define TCH_FIND_CLASS(_env, _class, _className) \
    jclass TCH_LOCAL_CLASS(_class) = (*_env)->FindClass((JNIEnv *)_env, _className); \
    if (TCH_LOCAL_CLASS(_class) == NULL) { \
        TCH_LOGE("Unable to find class %s\n", _className); \
    } else { \
        TCH_CLASS(_class) = (jclass)(*_env)->NewGlobalRef((JNIEnv *)_env, TCH_LOCAL_CLASS(_class)); \
    }

#define TCH_FIND_STATIC_METHOD(_env, _class, _method, _prototype) \
    TCH_METHOD(_method) = (*_env)->GetStaticMethodID((JNIEnv *)_env, TCH_CLASS(_class), TCH_str(_method), _prototype); \
    if (TCH_METHOD(_method) == NULL) { \
        TCH_LOGE("Unable to find method %s", TCH_str(_method)); \
    }

#define TCH_FIND_STATIC_METHOD2(_env, _class, _method, _symbol, _prototype) \
    TCH_METHOD(_method) = (*_env)->GetStaticMethodID((JNIEnv *)_env, TCH_CLASS(_class), TCH_str(_symbol), _prototype); \
    if (TCH_METHOD(_method) == NULL) { \
        TCH_LOGE("Unable to find method %s", TCH_str(_method)); \
    }

#define TCH_CALL_STATIC_METHOD_ARGS(_env, _class, _method, ...) \
    (*_env)->CallStaticVoidMethod((JNIEnv *)_env, TCH_CLASS(_class), TCH_METHOD(_method), __VA_ARGS__);

#define TCH_CALL_STATIC_METHOD_BOOL(_env, _class, _method) \
    (*_env)->CallStaticBooleanMethod((JNIEnv *)_env, TCH_CLASS(_class), TCH_METHOD(_method));

TCH_REGISTER_CLASS(testclassClass);
TCH_REGISTER_METHOD(testclassConstructor);
static jobject TCH_testclassInstance = NULL;

TCH_REGISTER_CLASS(testclassNativeListenerClass);
TCH_REGISTER_METHOD(testclassNativeListenerConstructor);
static jobject TCH_testclassNativeListenerInstance = NULL;

TCH_REGISTER_METHOD(isSupported);
TCH_REGISTER_METHOD(initTestClass);

static void TCH_preload(C_JNIEnv *env) {
    TCH_ELOG;

    if (TCH_CLASS(testclassClass) == NULL) {
        TCH_FIND_CLASS(env, testclassClass, kTCHTestClassClass);
    }

    // Class really not found or not loaded, bail
    if (TCH_CLASS(testclassClass) == NULL) {
        return;
    }

    if (TCH_CLASS(testclassClass) != NULL && TCH_METHOD(isSupported) == NULL) {
        TCH_FIND_STATIC_METHOD(env, testclassClass, isSupported, "()Z");
        TCH_FIND_STATIC_METHOD(env, testclassClass, initTestClass, "(Lcom/testclass/TestClass/ITestClassListener;Landroid/app/Activity;)Z");

        if (TCH_CLASS(testclassClass) != NULL) {
            TCH_METHOD(testclassConstructor) = (*env)->GetMethodID((JNIEnv *)env, TCH_CLASS(testclassClass), "<init>", "()V");
            jobject constructor = (*env)->NewObject((JNIEnv *)env, TCH_CLASS(testclassClass), TCH_METHOD(testclassConstructor));
            TCH_testclassInstance = (*env)->NewGlobalRef((JNIEnv *)env, constructor);
        }

        if (TCH_CLASS(testclassNativeListenerClass) == NULL) {
            TCH_FIND_CLASS(env, testclassNativeListenerClass, kTCHTestClassNativeListenerClass);
        }

        if (TCH_CLASS(testclassNativeListenerClass) != NULL) {
            TCH_METHOD(testclassNativeListenerConstructor) = (*env)->GetMethodID((JNIEnv *)env, TCH_CLASS(testclassNativeListenerClass), "<init>", "()V");
            jobject listener = (*env)->NewObject((JNIEnv *)env, TCH_CLASS(testclassNativeListenerClass), TCH_METHOD(testclassNativeListenerConstructor));
            TCH_testclassNativeListenerInstance = (*env)->NewGlobalRef((JNIEnv *)env, listener);
            TCH_REGISTER_NATIVES(env, testclassNativeListenerClass, TCH_listenerMethods);
            TCH_LOGV("Initializing built-in listener");
        }
    }
}

#elif defined(__APPLE__)

#ifndef NDEBUG
#define TCH_LOGV(...)  NSLog(__VA_ARGS__)
#else
#define TCH_LOGV(...)
#endif

#define TCH_ELOG TCH_LOGV(@"%s", __FUNCTION__)

#if defined(__cplusplus)
#define TCH_NS(_class) ::_class
#else
#define TCH_NS(_class) _class
#endif

@interface TCH_delegateHandler : NSObject <TestClassDelegate>
@end

@implementation TCH_delegateHandler
- (void)testclassUploadDidProgress:(NSNumber *)videoId progress:(NSNumber *)progress {
    TCH_ELOG;
}

@end

static TCH_delegateHandler *TCH_builtInHandler = nil;

#endif /* if defined(__ANDROID__) */

#if defined(__ANDROID__)

void TCH_initTestClass(JNIEnv *env, jobject activity, jobject listener) {
    TCH_ELOG;

    C_JNIEnv *cenv = NULL;
#if defined(__cplusplus)
    cenv = (C_JNIEnv *)env;
#else
    cenv = env;
#endif

    if (TCH_vm == NULL) {
        int status = (*cenv)->GetJavaVM((JNIEnv *)cenv, &TCH_vm);
        if (status != 0) {
            TCH_LOGE("GetJavaVM failed");
            return;
        }
    }

    TCH_CLASS(testclassClass) = TCH_loadClass(cenv, activity, kTCHTestClassLoader);
    TCH_CLASS(testclassNativeListenerClass) = TCH_loadClass(cenv, activity, kTCHTestClassNativeListenerLoader);
    TCH_preload(cenv);

    jobject listenerRef = listener;
    if (listenerRef == NULL) {
        TCH_LOGV("Using built-in listener");
        listenerRef = TCH_testclassNativeListenerInstance;
    }

    TCH_CALL_STATIC_METHOD_ARGS(cenv, testclassClass, initTestClass, listenerRef, activity);
}

#elif defined(__APPLE__)

void TCH_initTestClass(UIViewController *viewController, id <TestClassDelegate>testclassDelegate) {
    TCH_ELOG;

    id <TestClassDelegate>testclassDelegateRef = testclassDelegate;
    if (testclassDelegateRef == nil) {
        TCH_builtInHandler = [[TCH_delegateHandler alloc] init];
        testclassDelegateRef = TCH_builtInHandler;
    }

    [TCH_NS(TestClass) initWithDelegate : testclassDelegateRef andParentViewController : viewController];
}

#endif /* if defined(__ANDROID__) */

bool TCH_isSupported(void) {
    TCH_ELOG;
    bool ret = false;

#if defined(__ANDROID__)
    C_JNIEnv *env = TCH_getEnv();
    if (env && TCH_CLASS(testclassClass)) {
        ret = (bool)TCH_CALL_STATIC_METHOD_BOOL(env, testclassClass, isSupported);
    }
#elif defined(__APPLE__)
    ret = (bool) [TCH_NS(TestClass) isSupported];
#endif

    return ret;
}