summaryrefslogtreecommitdiffstats
path: root/android-utils/utils_wrap.cxx
blob: 9a43bdae75ffddf8ecf319ee6d01d9b74c4778a8 (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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 2.0.4
 *
 * This file is not intended to be easily readable and contains a number of
 * coding conventions designed to improve portability and efficiency. Do not make
 * changes to this file unless you know what you are doing--modify the SWIG
 * interface file instead.
 * ----------------------------------------------------------------------------- */

#define SWIGJAVA
#define SWIG_DIRECTORS


#ifdef __cplusplus
/* SwigValueWrapper is described in swig.swg */
template<typename T> class SwigValueWrapper
{
    struct SwigMovePointer {
        T *ptr;
        SwigMovePointer(T *p) : ptr(p) { }
        ~SwigMovePointer() {
            delete ptr;
        }
        SwigMovePointer& operator=(SwigMovePointer& rhs) {
            T* oldptr = ptr;
            ptr = 0;
            delete oldptr;
            ptr = rhs.ptr;
            rhs.ptr = 0;
            return *this;
        }
    } pointer;
    SwigValueWrapper& operator=(const SwigValueWrapper<T>& rhs);
    SwigValueWrapper(const SwigValueWrapper<T>& rhs);
public:
    SwigValueWrapper() : pointer(0) { }
    SwigValueWrapper& operator=(const T& t) {
        SwigMovePointer tmp(new T(t));
        pointer = tmp;
        return *this;
    }
    operator T&() const {
        return *pointer.ptr;
    }
    T *operator&() {
        return pointer.ptr;
    }
};

template <typename T> T SwigValueInit()
{
    return T();
}
#endif

/* -----------------------------------------------------------------------------
 *  This section contains generic SWIG labels for method/variable
 *  declarations/attributes, and other compiler dependent labels.
 * ----------------------------------------------------------------------------- */

/* template workaround for compilers that cannot correctly implement the C++ standard */
#ifndef SWIGTEMPLATEDISAMBIGUATOR
# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560)
#  define SWIGTEMPLATEDISAMBIGUATOR template
# elif defined(__HP_aCC)
/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */
/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */
#  define SWIGTEMPLATEDISAMBIGUATOR template
# else
#  define SWIGTEMPLATEDISAMBIGUATOR
# endif
#endif

/* inline attribute */
#ifndef SWIGINLINE
# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__))
#   define SWIGINLINE inline
# else
#   define SWIGINLINE
# endif
#endif

/* attribute recognised by some compilers to avoid 'unused' warnings */
#ifndef SWIGUNUSED
# if defined(__GNUC__)
#   if !(defined(__cplusplus)) || (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
#     define SWIGUNUSED __attribute__ ((__unused__))
#   else
#     define SWIGUNUSED
#   endif
# elif defined(__ICC)
#   define SWIGUNUSED __attribute__ ((__unused__))
# else
#   define SWIGUNUSED
# endif
#endif

#ifndef SWIG_MSC_UNSUPPRESS_4505
# if defined(_MSC_VER)
#   pragma warning(disable : 4505) /* unreferenced local function has been removed */
# endif
#endif

#ifndef SWIGUNUSEDPARM
# ifdef __cplusplus
#   define SWIGUNUSEDPARM(p)
# else
#   define SWIGUNUSEDPARM(p) p SWIGUNUSED
# endif
#endif

/* internal SWIG method */
#ifndef SWIGINTERN
# define SWIGINTERN static SWIGUNUSED
#endif

/* internal inline SWIG method */
#ifndef SWIGINTERNINLINE
# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE
#endif

/* exporting methods */
#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
#  ifndef GCC_HASCLASSVISIBILITY
#    define GCC_HASCLASSVISIBILITY
#  endif
#endif

#ifndef SWIGEXPORT
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
#   if defined(STATIC_LINKED)
#     define SWIGEXPORT
#   else
#     define SWIGEXPORT __declspec(dllexport)
#   endif
# else
#   if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
#     define SWIGEXPORT __attribute__ ((visibility("default")))
#   else
#     define SWIGEXPORT
#   endif
# endif
#endif

/* calling conventions for Windows */
#ifndef SWIGSTDCALL
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
#   define SWIGSTDCALL __stdcall
# else
#   define SWIGSTDCALL
# endif
#endif

/* Deal with Microsoft's attempt at deprecating C standard runtime functions */
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
# define _CRT_SECURE_NO_DEPRECATE
#endif

/* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
# define _SCL_SECURE_NO_DEPRECATE
#endif



/* Fix for jlong on some versions of gcc on Windows */
#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
typedef long long __int64;
#endif

/* Fix for jlong on 64-bit x86 Solaris */
#if defined(__x86_64)
# ifdef _LP64
#   undef _LP64
# endif
#endif

#include <jni.h>
#include <stdlib.h>
#include <string.h>


/* Support for throwing Java exceptions */
typedef enum {
    SWIG_JavaOutOfMemoryError = 1,
    SWIG_JavaIOException,
    SWIG_JavaRuntimeException,
    SWIG_JavaIndexOutOfBoundsException,
    SWIG_JavaArithmeticException,
    SWIG_JavaIllegalArgumentException,
    SWIG_JavaNullPointerException,
    SWIG_JavaDirectorPureVirtual,
    SWIG_JavaUnknownError
} SWIG_JavaExceptionCodes;

typedef struct {
    SWIG_JavaExceptionCodes code;
    const char *java_exception;
} SWIG_JavaExceptions_t;


static void SWIGUNUSED SWIG_JavaThrowException(JNIEnv *jenv, SWIG_JavaExceptionCodes code, const char *msg)
{
    jclass excep;
    static const SWIG_JavaExceptions_t java_exceptions[] = {
        { SWIG_JavaOutOfMemoryError, "java/lang/OutOfMemoryError" },
        { SWIG_JavaIOException, "java/io/IOException" },
        { SWIG_JavaRuntimeException, "java/lang/RuntimeException" },
        { SWIG_JavaIndexOutOfBoundsException, "java/lang/IndexOutOfBoundsException" },
        { SWIG_JavaArithmeticException, "java/lang/ArithmeticException" },
        { SWIG_JavaIllegalArgumentException, "java/lang/IllegalArgumentException" },
        { SWIG_JavaNullPointerException, "java/lang/NullPointerException" },
        { SWIG_JavaDirectorPureVirtual, "java/lang/RuntimeException" },
        { SWIG_JavaUnknownError,  "java/lang/UnknownError" },
        { (SWIG_JavaExceptionCodes)0,  "java/lang/UnknownError" }
    };
    const SWIG_JavaExceptions_t *except_ptr = java_exceptions;

    while (except_ptr->code != code && except_ptr->code) {
        except_ptr++;
    }

    jenv->ExceptionClear();
    excep = jenv->FindClass(except_ptr->java_exception);
    if (excep) {
        jenv->ThrowNew(excep, msg);
    }
}


/* Contract support */

#define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, msg); return nullreturn; } else

/* -----------------------------------------------------------------------------
 * director.swg
 *
 * This file contains support for director classes that proxy
 * method calls from C++ to Java extensions.
 * ----------------------------------------------------------------------------- */

#ifdef __cplusplus

#if defined(DEBUG_DIRECTOR_OWNED)
#include <iostream>
#endif

namespace Swig
{
/* Java object wrapper */
class JObjectWrapper
{
public:
    JObjectWrapper() : jthis_(NULL), weak_global_(true) {
    }

    ~JObjectWrapper() {
        jthis_ = NULL;
        weak_global_ = true;
    }

    bool set(JNIEnv *jenv, jobject jobj, bool mem_own, bool weak_global) {
        if (!jthis_) {
            weak_global_ = weak_global;
            if (jobj) {
                jthis_ = ((weak_global_ || !mem_own) ? jenv->NewWeakGlobalRef(jobj) : jenv->NewGlobalRef(jobj));
            }
#if defined(DEBUG_DIRECTOR_OWNED)
            std::cout << "JObjectWrapper::set(" << jobj << ", " << (weak_global ? "weak_global" : "global_ref") << ") -> " << jthis_ << std::endl;
#endif
            return true;
        }
        else {
#if defined(DEBUG_DIRECTOR_OWNED)
            std::cout << "JObjectWrapper::set(" << jobj << ", " << (weak_global ? "weak_global" : "global_ref") << ") -> already set" << std::endl;
#endif
            return false;
        }
    }

    jobject get(JNIEnv *jenv) const {
#if defined(DEBUG_DIRECTOR_OWNED)
        std::cout << "JObjectWrapper::get(";
        if (jthis_) {
            std::cout << jthis_;
        }
        else {
            std::cout << "null";
        }
        std::cout << ") -> return new local ref" << std::endl;
#endif
        return (jthis_ ? jenv->NewLocalRef(jthis_) : jthis_);
    }

    void release(JNIEnv *jenv) {
#if defined(DEBUG_DIRECTOR_OWNED)
        std::cout << "JObjectWrapper::release(" << jthis_ << "): " << (weak_global_ ? "weak global ref" : "global ref") << std::endl;
#endif
        if (jthis_) {
            if (weak_global_) {
                if (jenv->IsSameObject(jthis_, NULL) == JNI_FALSE) {
                    jenv->DeleteWeakGlobalRef((jweak)jthis_);
                }
            }
            else {
                jenv->DeleteGlobalRef(jthis_);
            }
        }

        jthis_ = NULL;
        weak_global_ = true;
    }

    jobject peek() {
        return jthis_;
    }

    /* Java proxy releases ownership of C++ object, C++ object is now
       responsible for destruction (creates NewGlobalRef to pin Java
       proxy) */
    void java_change_ownership(JNIEnv *jenv, jobject jself, bool take_or_release) {
        if (take_or_release) {  /* Java takes ownership of C++ object's lifetime. */
            if (!weak_global_) {
                jenv->DeleteGlobalRef(jthis_);
                jthis_ = jenv->NewWeakGlobalRef(jself);
                weak_global_ = true;
            }
        }
        else {   /* Java releases ownership of C++ object's lifetime */
            if (weak_global_) {
                jenv->DeleteWeakGlobalRef((jweak)jthis_);
                jthis_ = jenv->NewGlobalRef(jself);
                weak_global_ = false;
            }
        }
    }

private:
    /* pointer to Java object */
    jobject jthis_;
    /* Local or global reference flag */
    bool weak_global_;
};

/* director base class */
class Director
{
    /* pointer to Java virtual machine */
    JavaVM *swig_jvm_;

protected:
#if defined (_MSC_VER) && (_MSC_VER<1300)
    class JNIEnvWrapper;
    friend class JNIEnvWrapper;
#endif
    /* Utility class for managing the JNI environment */
    class JNIEnvWrapper
    {
        const Director *director_;
        JNIEnv *jenv_;
    public:
        JNIEnvWrapper(const Director *director) : director_(director), jenv_(0) {
#if defined(SWIG_JAVA_ATTACH_CURRENT_THREAD_AS_DAEMON)
            // Attach a daemon thread to the JVM. Useful when the JVM should not wait for
            // the thread to exit upon shutdown. Only for jdk-1.4 and later.
            director_->swig_jvm_->AttachCurrentThreadAsDaemon((JNIEnv **) &jenv_, NULL);
#else
            director_->swig_jvm_->AttachCurrentThread((JNIEnv **) &jenv_, NULL);
#endif
        }
        ~JNIEnvWrapper() {
#if !defined(SWIG_JAVA_NO_DETACH_CURRENT_THREAD)
            // Some JVMs, eg jdk-1.4.2 and lower on Solaris have a bug and crash with the DetachCurrentThread call.
            // However, without this call, the JVM hangs on exit when the thread was not created by the JVM and creates a memory leak.
            director_->swig_jvm_->DetachCurrentThread();
#endif
        }
        JNIEnv *getJNIEnv() const {
            return jenv_;
        }
    };

    /* Java object wrapper */
    JObjectWrapper swig_self_;

    /* Disconnect director from Java object */
    void swig_disconnect_director_self(const char *disconn_method) {
        JNIEnvWrapper jnienv(this) ;
        JNIEnv *jenv = jnienv.getJNIEnv() ;
        jobject jobj = swig_self_.peek();
#if defined(DEBUG_DIRECTOR_OWNED)
        std::cout << "Swig::Director::disconnect_director_self(" << jobj << ")" << std::endl;
#endif
        if (jobj && jenv->IsSameObject(jobj, NULL) == JNI_FALSE) {
            jmethodID disconn_meth = jenv->GetMethodID(jenv->GetObjectClass(jobj), disconn_method, "()V");
            if (disconn_meth) {
#if defined(DEBUG_DIRECTOR_OWNED)
                std::cout << "Swig::Director::disconnect_director_self upcall to " << disconn_method << std::endl;
#endif
                jenv->CallVoidMethod(jobj, disconn_meth);
            }
        }
    }

public:
    Director(JNIEnv *jenv) : swig_jvm_((JavaVM *) NULL), swig_self_() {
        /* Acquire the Java VM pointer */
        jenv->GetJavaVM(&swig_jvm_);
    }

    virtual ~Director() {
        JNIEnvWrapper jnienv(this) ;
        JNIEnv *jenv = jnienv.getJNIEnv() ;
        swig_self_.release(jenv);
    }

    bool swig_set_self(JNIEnv *jenv, jobject jself, bool mem_own, bool weak_global) {
        return swig_self_.set(jenv, jself, mem_own, weak_global);
    }

    jobject swig_get_self(JNIEnv *jenv) const {
        return swig_self_.get(jenv);
    }

    // Change C++ object's ownership, relative to Java
    void swig_java_change_ownership(JNIEnv *jenv, jobject jself, bool take_or_release) {
        swig_self_.java_change_ownership(jenv, jself, take_or_release);
    }
};
}

#endif /* __cplusplus */



#include <stdint.h>		// Use the C99 official header


#include "api.h"



/* ---------------------------------------------------
 * C++ director class methods
 * --------------------------------------------------- */

#include "utils_wrap.h"


#ifdef __cplusplus
extern "C" {
#endif

SWIGEXPORT jlong JNICALL Java_org_doubango_utils_utilsJNI_new_1AndroidUtils(JNIEnv *jenv, jclass jcls)
{
    jlong jresult = 0 ;
    AndroidUtils *result = 0 ;

    (void)jenv;
    (void)jcls;
    result = (AndroidUtils *)new AndroidUtils();
    *(AndroidUtils **)&jresult = result;
    return jresult;
}


SWIGEXPORT void JNICALL Java_org_doubango_utils_utilsJNI_delete_1AndroidUtils(JNIEnv *jenv, jclass jcls, jlong jarg1)
{
    AndroidUtils *arg1 = (AndroidUtils *) 0 ;

    (void)jenv;
    (void)jcls;
    arg1 = *(AndroidUtils **)&jarg1;
    delete arg1;
}


SWIGEXPORT jobject JNICALL Java_org_doubango_utils_utilsJNI_AndroidUtils_1getCpuFamily(JNIEnv *jenv, jclass jcls)
{
    jobject jresult = 0 ;
    uint64_t result;

    (void)jenv;
    (void)jcls;
    result = (uint64_t)AndroidUtils::getCpuFamily();
    {
        jbyteArray ba = jenv->NewByteArray(9);
        jbyte* bae = jenv->GetByteArrayElements(ba, 0);
        jclass clazz = jenv->FindClass("java/math/BigInteger");
        jmethodID mid = jenv->GetMethodID(clazz, "<init>", "([B)V");
        jobject bigint;
        int i;

        bae[0] = 0;
        for(i=1; i<9; i++ ) {
            bae[i] = (jbyte)(result>>8*(8-i));
        }

        jenv->ReleaseByteArrayElements(ba, bae, 0);
        bigint = jenv->NewObject(clazz, mid, ba);
        jresult = bigint;
    }
    return jresult;
}


SWIGEXPORT jobject JNICALL Java_org_doubango_utils_utilsJNI_AndroidUtils_1getCpuFeatures(JNIEnv *jenv, jclass jcls)
{
    jobject jresult = 0 ;
    uint64_t result;

    (void)jenv;
    (void)jcls;
    result = (uint64_t)AndroidUtils::getCpuFeatures();
    {
        jbyteArray ba = jenv->NewByteArray(9);
        jbyte* bae = jenv->GetByteArrayElements(ba, 0);
        jclass clazz = jenv->FindClass("java/math/BigInteger");
        jmethodID mid = jenv->GetMethodID(clazz, "<init>", "([B)V");
        jobject bigint;
        int i;

        bae[0] = 0;
        for(i=1; i<9; i++ ) {
            bae[i] = (jbyte)(result>>8*(8-i));
        }

        jenv->ReleaseByteArrayElements(ba, bae, 0);
        bigint = jenv->NewObject(clazz, mid, ba);
        jresult = bigint;
    }
    return jresult;
}


#ifdef __cplusplus
}
#endif

OpenPOWER on IntegriCloud