-
Notifications
You must be signed in to change notification settings - Fork 0
/
jni_tl.h
1074 lines (898 loc) · 41 KB
/
jni_tl.h
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef JNI_TL_H
#define JNI_TL_H
#include "athor/Andriy.Bulka"
#include "license/BSD"
#include <jni.h>
#include <string>
#include <algorithm>
namespace JNI {
struct Env {
Env(JNIEnv *env) : env(env) {}
JNIEnv *operator -> () const { return *this; }
operator JNIEnv * () const { return env; }
private: JNIEnv *env;
};
struct String : Env {
String(const Env &env, jstring string)
: Env(env), string(string) {}
String(const Env &env, jchar *string, jsize length)
: Env(env), string(env->NewString(string, length)) {}
String(const Env &env, const char *string)
: Env(env), string(env->NewStringUTF(string)) {}
String(const Env &env, const std::string &string)
: Env(env), string(env->NewStringUTF(string.c_str())) {}
template <typename Char> unsigned length() const;
operator std::string () const {
const char * cstr = (*this)->GetStringUTFChars(string, 0);
std::string result = cstr;
(*this)->ReleaseStringUTFChars(string, cstr);
return result;
}
operator jstring () const { return string; }
private: jstring string;
};
template <> unsigned String::length<char>() const;
template <> unsigned String::length<jchar>() const;
struct Region {
Region(jsize start, jsize length = 1) : start(start), length(length) {}
jsize start, length;
};
template <typename T>
struct Array : Env {
template <typename C, typename U = void> struct Cast {};
template <typename C,
typename U> struct Cast<C[], U> { typedef jobjectArray Type; };
template <typename U> struct Cast<jboolean, U> { typedef jbooleanArray Type; };
template <typename U> struct Cast<jbyte, U> { typedef jbyteArray Type; };
template <typename U> struct Cast<jchar, U> { typedef jcharArray Type; };
template <typename U> struct Cast<jshort, U> { typedef jshortArray Type; };
template <typename U> struct Cast<jint, U> { typedef jintArray Type; };
template <typename U> struct Cast<jlong, U> { typedef jlongArray Type; };
template <typename U> struct Cast<jfloat, U> { typedef jfloatArray Type; };
template <typename U> struct Cast<jdouble, U> { typedef jdoubleArray Type; };
template <typename U> struct Cast<jobject, U> { typedef jobjectArray Type; };
template <typename U> struct Cast<jstring, U> { typedef jobjectArray Type; };
typedef typename Cast<T>::Type Type;
template <typename C, typename U = void> struct CastInput { typedef T Type;};
template <typename C, typename U> struct CastInput<C[], U> {
typedef typename Array<T>::template Cast<C>::Type Type;
};
template <typename C, typename U = void> struct CastOutput { typedef T Type; };
template <typename C, typename U> struct CastOutput<C[], U> { typedef Array<C> Type; };
typedef typename CastInput <T>::Type Input;
typedef typename CastOutput<T>::Type Output;
struct Elements;
struct Critical { struct Elements; };
struct Region { struct Elements; };
struct Object { struct Element; };
jsize length() const { return (*this)->GetArrayLength(array); }
Array(const Env &env, Type array) : Env(env), array(array) {}
Array(const Env &env, jsize count);
Array(const Env &env, jsize count, jclass clazz, jobject object = 0);
Array(const Env &env, jsize count, jstring string);
Elements elements(bool copyBack = false) {
return Elements(*this, copyBack);
}
const Elements elements(bool copyBack = false) const {
return Elements(*this, copyBack);
}
typename Critical::Elements critical(bool copyBack = false) {
return typename Critical::Elements(*this, copyBack);
}
const typename Critical::Elements critical(bool copyBack = false) const {
return typename Critical::Elements(*this, copyBack);
}
typename Region::Elements region(const JNI::Region ®ion, bool copyBack = false) {
return typename Region::Elements(*this, region, copyBack);
}
const typename Region::Elements region(const JNI::Region ®ion, bool copyBack = false) const {
return typename Region::Elements(*this, region, copyBack);
}
typename Object::Element operator[] (int index) {
return typename Object::Element(*this, index);
}
const typename Object::Element operator[] (int index) const {
return typename Object::Element(*this, index);
}
operator Type () const { return array; }
private: Type array;
};
template <typename T>
struct Array<T>::Object::Element : Array<T> {
operator typename Array<T>::Output () const;
Element &operator = (const typename Array<T>::Input &value);
Element(const Array<T> &array, int index) : Array<T>(array), index(index) {}
private: int index;
};
template <typename T>
struct Array<T>::Region::Elements : private Array<T>, private JNI::Region {
typedef typename Array<T>::Input Type;
struct Element;
Element operator [] (jsize index) { return Element(*this, index); }
const Element operator [] (jsize index) const { return Element(*this, index); }
~Elements() {
if (copyBack) { commit(); }
if (region != 0) { delete [] region; }
}
Elements(const Array<T> &array, const JNI::Region ®ion, bool copyBack)
: Array<T>(array), JNI::Region(region), copyBack(copyBack), region(0) {}
Elements(const Elements &elements)
: Array<T>(static_cast<const Array<T> &>(elements)),
JNI::Region(static_cast<const JNI::Region &>(elements)) {
if (region != 0) { delete [] region; }
region = elements.region; const_cast<Elements &>(elements).region = 0;
}
Elements &operator = (const Elements &elements) {
if (this != &elements) {
static_cast<Array<T> &>(*this) = static_cast<const Array<T> &>(elements);
static_cast<JNI::Region &>(*this) = static_cast<const JNI::Region &>(elements);
if (region != 0) { delete [] region; }
region = elements.region; const_cast<Elements &>(elements).region = 0;
} return *this;
}
Elements &operator = (const Type *values);
void commit() { *this = (Type *)(*this); }
private: operator Type * () {
return region != 0 ? region :
(region = new Type[JNI::Region::length], get(), region);
}
private: void get();
private: bool copyBack;
private: Type *region;
};
template <typename T>
struct Array<T>::Region::Elements::Element {
Element(const Elements &elements, jsize index)
: elements(const_cast<Elements &>(elements)), index(index) {}
operator typename Array<T>::Output () const {
return ((typename Elements::Type *)elements)[index];
}
Element &operator = (const typename Array<T>::Input &value) {
return ((typename Elements::Type *)elements)[index] = value, *this;
}
private: Elements &elements;
private: jsize index;
};
template <typename T>
struct Array<T>::Critical::Elements : private Array<T> {
typedef typename Array<T>::Input Type;
struct Element;
Element operator [] (jsize index) { return Element(*this, index); }
const Element operator [] (jsize index) const { return Element(*this, index); }
Elements(const Array<T> &array, bool copyBack)
: Array<T>(array),
array((Type *)(*this)->GetPrimitiveArrayCritical(*this, &isCopy)),
copyBack(copyBack) {}
~Elements() {
(*this)->ReleasePrimitiveArrayCritical(*this, array, copyBack ? 0 : JNI_ABORT);
}
Elements(const Elements &elements)
: Array<T>(static_cast<const Array<T> &>(elements)),
array(elements.array), copyBack(elements.copyBack), isCopy(elements.isCopy) {
const_cast<Elements &>(elements).array = 0;
}
Elements &operator = (const Elements &elements) {
if (this != &elements) {
static_cast<Array<T> &>(*this) = static_cast<const Array<T> &>(elements);
array = elements.array; const_cast<Elements &>(elements).array = 0;
copyBack = elements.copyBack;
isCopy = elements.isCopy;
} return *this;
}
void commit() {
(*this)->ReleasePrimitiveArrayCritical(*this, array, JNI_COMMIT);
}
private: operator Type * () { return array; }
private: Type *array;
private: bool copyBack;
private: jboolean isCopy;
};
template <typename T>
struct Array<T>::Critical::Elements::Element {
Element(const Elements &elements, jsize index)
: elements(const_cast<Elements &>(elements)), index(index) {}
operator typename Array<T>::Output () const {
return ((typename Elements::Type *)elements)[index];
}
Element &operator = (const typename Array<T>::Input &value) {
return ((typename Elements::Type *)elements)[index] = value, *this;
}
private: Elements &elements;
private: jsize index;
};
template <typename T>
struct Array<T>::Elements : private Array<T> {
typedef typename Array<T>::Input Type;
struct Element;
Element operator [] (jsize index) { return Element(*this, index); }
const Element operator [] (jsize index) const { return Element(*this, index); }
Elements(const Array<T> &array, bool copyBack)
: Array<T>(array), array(0), copyBack(copyBack) { init(); }
~Elements() { release(); }
Elements(const Elements &elements)
: Array<T>(static_cast<const Array<T> &>(elements)),
array(elements.array), copyBack(elements.copyBack), isCopy(elements.isCopy) {
const_cast<Elements &>(elements).array = 0;
}
Elements &operator = (const Elements &elements) {
if (this != &elements) {
static_cast<Array<T> &>(*this) = static_cast<const Array<T> &>(elements);
array = elements.array; const_cast<Elements &>(elements).array = 0;
copyBack = elements.copyBack;
isCopy = elements.isCopy;
} return *this;
}
void commit();
private: void init();
private: void release();
private: operator Type * () { return array; }
private: Type *array;
private: bool copyBack;
private: jboolean isCopy;
};
template <typename T>
struct Array<T>::Elements::Element {
Element(const Elements &elements, jsize index)
: elements(const_cast<Elements &>(elements)), index(index) {}
operator typename Array<T>::Output () const {
return ((typename Elements::Type *)elements)[index];
}
Element &operator = (const typename Array<T>::Input &value) {
return ((typename Elements::Type *)elements)[index] = value, *this;
}
private: Elements &elements;
private: jsize index;
};
struct Object;
struct Class : Env {
struct Static {
template <typename Type> struct Field;
template <typename ReturnType> struct Method;
}; template <typename ReturnType> struct Method; struct Constructor;
static std::string signature(std::string name) {
return std::replace(name.begin(), name.end(), '.', '/'), name;
}
Class(const Env &env, jclass clazz)
: Env(env), clazz(clazz) {}
Class(const Env &env, const char *clazz)
: Env(env), clazz(env->FindClass(signature(clazz).c_str())) {}
Class(const Env &env, const std::string &clazz)
: Env(env), clazz(env->FindClass(signature(clazz).c_str())) {}
Class(const Env &env, const char *clazz,
jobject loader, const jbyte *buffer, jsize bufferLength)
: Env(env), clazz(env->DefineClass(clazz,
loader, buffer, bufferLength)) {}
Class(const Env &env, const std::string &clazz,
jobject loader, const jbyte *buffer, jsize bufferLength)
: Env(env), clazz(env->DefineClass(clazz.c_str(),
loader, buffer, bufferLength)) {}
String string(const char *s) { return String(*this, s); }
String string(const std::string &s) { return String(*this, s); }
String string(const jstring &s) { return String(*this, s); }
Class super() const { return Class(*this, (*this)->GetSuperclass(clazz)); }
bool cast(jclass clazz) const {
return (*this)->IsAssignableFrom(*this, clazz);
}
jfieldID fieldIDStatic(const char *name, const char *sig) const {
return (*this)->GetStaticFieldID(*this, name, sig);
}
jfieldID fieldID(const char *name, const char *sig) const {
return (*this)->GetFieldID(*this, name, sig);
}
jmethodID methodIDStatic(const char *name, const char *sig) const {
return (*this)->GetStaticMethodID(*this, name, sig);
}
jmethodID methodID(const char *name, const char *sig) const {
return (*this)->GetMethodID(*this, name, sig);
}
template <typename Description>
typename Description::ID operator() (const Description &desc) const {
return typename Description::ID(*this, desc);
}
template <typename Constructor>
typename Constructor::Result newObject(const Constructor &constructor, ...);
template <typename ID>
typename ID::Reference
operator [] (const ID &id) { return at(id); }
template <typename ID>
const typename ID::Reference
operator [] (const ID &id) const { return at(id); }
template <typename Type>
typename Static::Field<Type>::Reference
operator [] (const Static::Field<Type> &field) {
return typename Static::Field<Type>::Reference
(*this, typename Static::Field<Type>::ID(*this, field));
}
template <typename Type>
const typename Static::Field<Type>::Reference
operator [] (const Static::Field<Type> &field) const {
return typename Static::Field<Type>::Reference
(*this, typename Static::Field<Type>::ID(*this, field));
}
template <typename ReturnType>
typename Static::Method<ReturnType>::Reference
operator [] (const Static::Method<ReturnType> &method) {
return typename Static::Method<ReturnType>::Reference
(*this, typename Static::Method<ReturnType>::ID(*this, method));
}
template <typename ReturnType>
const typename Static::Method<ReturnType>::Reference
operator [] (const Static::Method<ReturnType> &method) const {
return typename Static::Method<ReturnType>::Reference
(*this, typename Static::Method<ReturnType>::ID(*this, method));
}
operator jclass () const { return clazz; }
private: jclass clazz;
// SFINAE workaround due to poor C++ compiler function template specialization logic
private: template <typename ID>
typename Static::Field<typename ID::Type>::Reference
at(const ID &id, const typename ID::IsStaticFieldID & = typename ID::IsStaticFieldID()) {
return typename Static::Field<typename ID::Type>::Reference(*this, id);
}
private: template <typename ID>
const typename Static::Field<typename ID::Type>::Reference
at(const ID &id, const typename ID::IsStaticFieldID & = typename ID::IsStaticFieldID()) const {
return typename Static::Field<typename ID::Type>::Reference(*this, id);
}
private: template <typename ID>
typename Static::Method<typename ID::ReturnType>::Reference
at(const ID &id, const typename ID::IsStaticMethodID & = typename ID::IsStaticMethodID()) {
return typename Static::Method<typename ID::ReturnType>::Reference(*this, id);
}
private: template <typename ID>
const typename Static::Method<typename ID::ReturnType>::Reference
at(const ID &id, const typename ID::IsStaticMethodID & = typename ID::IsStaticMethodID()) const {
return typename Static::Method<typename ID::ReturnType>::Reference(*this, id);
}
};
template <typename Type>
struct JType {
struct Signature;
};
template <typename Type>
struct JType<Type>::Signature {
operator const char * () const { return value; }
private: static const char *value;
};
template <>
struct JType<jobject>::Signature {
Signature(const char *clazz) : clazz(std::string("L") + Class::signature(clazz) + ";") {}
operator const char * () const { return clazz.c_str(); }
private: std::string clazz;
};
template <typename Type>
struct JType<Type[]> { struct Signature {
operator const char * () const { return value; }
private: static const char *value;
}; };
template <>
struct JType<jobject[]>::Signature {
Signature(const char *clazz)
: clazz(std::string("[") + (const char *)JType<jobject>::Signature(clazz)) {}
operator const char * () const { return clazz.c_str(); }
private: std::string clazz;
};
struct Nop { Nop() {} };
template <typename T0 = Nop, typename T1 = Nop, typename T2 = Nop,
typename T3 = Nop, typename T4 = Nop, typename T5 = Nop,
typename T6 = Nop, typename T7 = Nop, typename T8 = Nop,
typename T9 = Nop > // extendable ...
struct Args {
Args(const char *prepared = 0, ...) {
va_list va_list;
va_start(va_list, prepared);
VaList vl(prepared, va_list);
va_end(va_list);
args += Arg<T0>(vl); args += Arg<T1>(vl); args += Arg<T2>(vl);
args += Arg<T3>(vl); args += Arg<T4>(vl); args += Arg<T5>(vl);
args += Arg<T6>(vl); args += Arg<T7>(vl); args += Arg<T8>(vl);
args += Arg<T9>(vl); // extendable ...
}
operator const char * () const { return args.c_str(); }
operator const std::string & () const { return args; }
private: std::string args;
struct VaList {
VaList(const char *prepared, va_list vl)
: preparedIsInvalid(false), prepared(prepared) { va_copy(this->vl, vl); }
~VaList() { va_end(vl); }
const char * next() { return preparedIsInvalid ?
(va_arg(vl, const char *)) :
(preparedIsInvalid = true, prepared);
}
bool preparedIsInvalid; const char *prepared; va_list vl;
};
template <typename T, typename U = void> struct Arg;
template <typename T, typename U>
struct Arg {
operator const char * () const {
return typename JType<T>::Signature();
} Arg(VaList &vl) : vl(vl) {} private: VaList &vl;
};
template <typename U>
struct Arg<jobject, U> {
operator const char * () const {
return typename JType<jobject>::Signature(vl.next());
} Arg(VaList &vl) : vl(vl) {} private: VaList &vl;
};
template <typename U>
struct Arg<jobject[], U> {
operator const char * () const {
return typename JType<jobject[]>::Signature(vl.next());
} Arg(VaList &vl) : vl(vl) {} private: VaList &vl;
};
};
template <typename T>
struct Class::Static::Field {
template <typename C> struct Cast { typedef C Type; };
template <typename C> struct Cast<C[]> { typedef Array<C> Type; };
typedef typename Cast<T>::Type Accessor;
typedef typename JType<T>::Signature Signature;
struct Reference;
struct ID;
Field(const char *name, const Signature &signature = Signature())
: name(name), signature(signature) {}
operator const char * () const { return name; }
const char *sig() const { return signature; }
private: const char *name;
private: Signature signature;
};
template <typename T>
struct Class::Static::Field<T>::ID {
ID(const Class &clazz, const Field<T> &field) : id(clazz.fieldIDStatic(field, field.sig())) {}
ID(jfieldID id) : id(id) {}
operator jfieldID () const { return id; }
private: jfieldID id;
// Stuff for SFINAE workaround
public: typedef T Type; struct IsStaticFieldID {};
public: typedef typename Field<T>::Reference Reference;
};
template <typename T>
struct Class::Static::Field<T>::Reference : private Class, private ID {
typedef typename Field<T>::Accessor Type;
Reference(const Class &clazz, const ID &id) : Class(clazz), ID(id) {}
operator Type () const;
Reference &operator = (const Type &value);
};
template <typename Type>
struct Class::Static::Method {
typedef typename Class::Static::Field<Type>::Signature Signature;
struct Reference;
struct ID;
Method(const char *name, const Signature &signature = Signature())
: name(name), signature(std::string("()") + (const char *)signature) {}
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6, typename T7,
typename T8, typename T9 > // extendable ...
Method(const char *name,
const Args<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> &args,
const Signature &signature = Signature())
: name(name), signature(std::string("(") +
(const std::string &)args + ")" +
(const char *)signature) {}
operator const char * () const { return name; }
const char *sig() const { return signature.c_str(); }
private: const char *name;
private: std::string signature;
};
template <typename T>
struct Class::Static::Method<T>::ID {
ID(const Class &clazz, const Method<T> &method) : id(clazz.methodIDStatic(method, method.sig())) {}
ID(jmethodID id) : id(id) {}
operator jmethodID () const { return id; }
private: jmethodID id;
// Stuff for SFINAE workaround
public: typedef T ReturnType; struct IsStaticMethodID {};
public: typedef typename Method<T>::Reference Reference;
};
template <typename T>
struct Class::Static::Method<T>::Reference : private Class, private ID {
typedef typename Class::Static::Field<T>::Accessor Type;
Reference(const Class &clazz, const ID &id) : Class(clazz), ID(id) {}
Type call(va_list) const;
Type call(const Nop & nop, ...) const {
va_list vl; va_start(vl, nop); Type ret = call(vl); va_end(vl);
return ret;
}
Type operator () () const
{ return call(Nop()); }
template <typename T0>
Type operator () (const T0 &v0) const
{ return call(Nop(), v0); }
template <typename T0, typename T1>
Type operator () (const T0 &v0, const T1 &v1) const
{ return call(Nop(), v0, v1); }
template <typename T0, typename T1, typename T2>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2) const
{ return call(Nop(), v0, v1, v2); }
template <typename T0, typename T1, typename T2, typename T3>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3) const
{ return call(Nop(), v0, v1, v2, v3); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4) const
{ return call(Nop(), v0, v1, v2, v3, v4); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4, const T5 &v5) const
{ return call(Nop(), v0, v1, v2, v3, v4, v5); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4, const T5 &v5,
const T6 &v6) const
{ return call(Nop(), v0, v1, v2, v3, v4, v5, v6); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6, typename T7>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4, const T5 &v5,
const T6 &v6, const T7 &v7) const
{ return call(Nop(), v0, v1, v2, v3, v4, v5, v6, v7); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6, typename T7,
typename T8>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4, const T5 &v5,
const T6 &v6, const T7 &v7, const T8 &v8) const
{ return call(Nop(), v0, v1, v2, v3, v4, v5, v6, v7, v8); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6, typename T7,
typename T8, typename T9>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4, const T5 &v5,
const T6 &v6, const T7 &v7, const T8 &v8,
const T9 &v9) const
{ return call(Nop(), v0, v1, v2, v3, v4, v5, v6, v7, v8, v9); }
};
struct Object : Class {
struct Monitor;
template <typename Type> struct Field;
template <typename ReturnType> struct Method;
Object(const Env &env, jobject object)
: Class(env, env->GetObjectClass(object)), object(object) {}
Object(const Class &clazz, jobject object)
: Class(clazz), object(object) {}
bool isInstanceOf(jclass clazz) const { return (*this)->IsInstanceOf(*this, clazz); }
bool operator == (jobject object) { return (*this)->IsSameObject(*this, object); }
jobjectRefType refType() const { return (*this)->GetObjectRefType(*this); }
template <typename ID>
typename ID::Reference
operator [] (const ID &id) { return at(id); }
template <typename ID>
const typename ID::Reference
operator [] (const ID &id) const { return at(id); }
template <typename Type>
typename Class::Static::Field<Type>::Reference
operator [] (const Class::Static::Field<Type> &field) {
return Class::operator [] (field);
}
template <typename Type>
const typename Class::Static::Field<Type>::Reference
operator [] (const Class::Static::Field<Type> &field) const {
return Class::operator [] (field);
}
template <typename ReturnType>
typename Class::Static::Method<ReturnType>::Reference
operator [] (const Class::Static::Method<ReturnType> &method) {
return Class::operator [] (method);
}
template <typename ReturnType>
const typename Class::Static::Method<ReturnType>::Reference
operator [] (const Class::Static::Method<ReturnType> &method) const {
return Class::operator [] (method);
}
template <typename ReturnType>
typename Class::Method<ReturnType>::Reference
operator [] (const Class::Method<ReturnType> &method) {
return typename Class::Method<ReturnType>::Reference(*this, typename Class::Method<ReturnType>::ID(*this, method));
}
template <typename ReturnType>
const typename Class::Method<ReturnType>::Reference
operator [] (const Class::Method<ReturnType> &method) const {
return typename Class::Method<ReturnType>::Reference(*this, Class::Method<ReturnType>::ID(*this, method));
}
template <typename Type>
typename Field<Type>::Reference
operator [] (const Field<Type> &field) {
return typename Field<Type>::Reference(*this, typename Field<Type>::ID(*this, field));
}
template <typename Type>
const typename Field<Type>::Reference
operator [] (const Field<Type> &field) const {
return typename Field<Type>::Reference(*this, typename Field<Type>::ID(*this, field));
}
template <typename ReturnType>
typename Method<ReturnType>::Reference
operator [] (const Method<ReturnType> &method) {
return typename Method<ReturnType>::Reference(*this, typename Method<ReturnType>::ID(*this, method));
}
template <typename ReturnType>
const typename Method<ReturnType>::Reference
operator [] (const Method<ReturnType> &method) const {
return typename Method<ReturnType>::Reference(*this, Method<ReturnType>::ID(*this, method));
}
operator jobject () const { return object; }
private: jobject object;
// SFINAE workaround due to poor C++ compiler function template specialization logic
private: template <typename ID>
typename Class::Static::Field<typename ID::Type>::Reference
at(const ID &id, const typename ID::IsStaticFieldID & = typename ID::IsStaticFieldID()) {
return Class::operator [] (id);
}
private: template <typename ID>
const typename Class::Static::Field<typename ID::Type>::Reference
at(const ID &id, const typename ID::IsStaticFieldID & = typename ID::IsStaticFieldID()) const {
return Class::operator [] (id);
}
private: template <typename ID>
typename Class::Static::Method<typename ID::ReturnType>::Reference
at(const ID &id, const typename ID::IsStaticMethodID & = typename ID::IsStaticMethodID()) {
return Class::operator [] (id);
}
private: template <typename ID>
const typename Class::Static::Method<typename ID::ReturnType>::Reference
at(const ID &id, const typename ID::IsStaticMethodID & = typename ID::IsStaticMethodID()) const {
return Class::operator [] (id);
}
private: template <typename ID>
typename Field<typename ID::Type>::Reference
at(const ID &id, const typename ID::IsObjectFieldID & = typename ID::IsObjectFieldID()) {
return typename Field<typename ID::Type>::Reference(*this, id);
}
private: template <typename ID>
const typename Field<typename ID::Type>::Reference
at(const ID &id, const typename ID::IsObjectFieldID & = typename ID::IsObjectFieldID()) const {
return typename Field<typename ID::Type>::Reference(*this, id);
}
private: template <typename ID>
typename Class::Method<typename ID::ReturnType>::Reference
at(const ID &id, const typename ID::IsClassMethodID & = typename ID::IsClassMethodID()) {
return typename Class::Method<typename ID::ReturnType>::Reference(*this, id);
}
private: template <typename ID>
const typename Class::Method<typename ID::ReturnType>::Reference
at(const ID &id, const typename ID::IsClassMethodID & = typename ID::IsClassMethodID()) const {
return typename Class::Method<typename ID::ReturnType>::Reference(*this, id);
}
private: template <typename ID>
typename Method<typename ID::ReturnType>::Reference
at(const ID &id, const typename ID::IsObjectMethodID & = typename ID::IsObjectMethodID()) {
return typename Method<typename ID::ReturnType>::Reference(*this, id);
}
private: template <typename ID>
const typename Method<typename ID::ReturnType>::Reference
at(const ID &id, const typename ID::IsObjectMethodID & = typename ID::IsObjectMethodID()) const {
return typename Method<typename ID::ReturnType>::Reference(*this, id);
}
};
template <typename Type>
struct Object::Field : private Class::Static::Field<Type> {
typedef typename Class::Static::Field<Type>::Signature Signature;
struct Reference;
struct ID;
operator const char * () const { return Class::Static::Field<Type>::operator const char * (); }
const char *sig() const { return Class::Static::Field<Type>::sig(); }
Field(const char *name, const Signature &signature = Signature())
: Class::Static::Field<Type>(name, signature) {}
};
template <typename T>
struct Object::Field<T>::ID {
ID(const Class &clazz, const Field<T> &field) : id(clazz.fieldID(field, field.sig())) {}
ID(jfieldID id) : id(id) {}
operator jfieldID () const { return id; }
private: jfieldID id;
// Stuff for SFINAE workaround
public: typedef typename Class::Static::Field<T>::ID::Type Type;
public: struct IsObjectFieldID {}; typedef typename Field<T>::Reference Reference;
};
template <typename T>
struct Object::Field<T>::Reference : private Object, private ID {
typedef typename Field<T>::Accessor Type;
Reference(const Object &object, const ID &id) : Object(object), ID(id) {}
operator Type () const;
Reference &operator = (const Type &value);
};
template <typename Type>
struct Class::Method : private Class::Static::Method<Type> {
typedef typename Class::Static::Method<Type>::Signature Signature;
struct Reference;
struct ID;
Method(const char *name, const Signature &signature = Signature())
: Class::Static::Method<Type>(name, signature) {}
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6, typename T7,
typename T8, typename T9 > // extendable ...
Method(const char *name,
const Args<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> &args,
const Signature &signature = Signature())
: Class::Static::Method<Type>(name, args, signature) {}
};
template <typename T>
struct Class::Method<T>::ID {
ID(const Class &clazz, const Method<T> &method) : id(clazz.methodID(method, method.sig())) {}
ID(jmethodID id) : id(id) {}
operator jmethodID () const { return id; }
private: jmethodID id;
// Stuff for SFINAE workaround
public: typedef typename Class::Static::Method<T>::ID::ReturnType ReturnType;
public: struct IsClassMethodID{}; typedef typename Method<T>::Reference Reference;
};
template <typename T>
struct Class::Method<T>::Reference : private Object, private ID {
typedef typename Class::Static::Method<T>::Reference::Type Type;
Reference(const Object &object, const ID &id) : Object(object), ID(id) {}
Type call(va_list) const;
Type call(const Nop & nop, ...) const {
va_list vl; va_start(vl, nop); Type ret = call(vl); va_end(vl);
return ret;
}
Type operator () () const
{ return call(Nop()); }
template <typename T0>
Type operator () (const T0 &v0) const
{ return call(Nop(), v0); }
template <typename T0, typename T1>
Type operator () (const T0 &v0, const T1 &v1) const
{ return call(Nop(), v0, v1); }
template <typename T0, typename T1, typename T2>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2) const
{ return call(Nop(), v0, v1, v2); }
template <typename T0, typename T1, typename T2, typename T3>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3) const
{ return call(Nop(), v0, v1, v2, v3); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4) const
{ return call(Nop(), v0, v1, v2, v3, v4); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4, const T5 &v5) const
{ return call(Nop(), v0, v1, v2, v3, v4, v5); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4, const T5 &v5,
const T6 &v6) const
{ return call(Nop(), v0, v1, v2, v3, v4, v5, v6); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6, typename T7>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4, const T5 &v5,
const T6 &v6, const T7 &v7) const
{ return call(Nop(), v0, v1, v2, v3, v4, v5, v6, v7); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6, typename T7,
typename T8>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4, const T5 &v5,
const T6 &v6, const T7 &v7, const T8 &v8) const
{ return call(Nop(), v0, v1, v2, v3, v4, v5, v6, v7, v8); }
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6, typename T7,
typename T8, typename T9>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3, const T4 &v4, const T5 &v5,
const T6 &v6, const T7 &v7, const T8 &v8,
const T9 &v9) const
{ return call(Nop(), v0, v1, v2, v3, v4, v5, v6, v7, v8, v9); }
// extendable ...
};
template <typename Type>
struct Object::Method : private Class::Static::Method<Type> {
typedef typename Class::Static::Method<Type>::Signature Signature;
struct Reference;
struct ID;
Method(const char *name, const Signature &signature = Signature())
: Class::Static::Method<Type>(name, signature) {}
template <typename T0, typename T1, typename T2, typename T3,
typename T4, typename T5, typename T6, typename T7,
typename T8, typename T9 > // extendable ...
Method(const char *name,
const Args<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> &args,
const Signature &signature = Signature())
: Class::Static::Method<Type>(name, args, signature) {}
};
template <typename T>
struct Object::Method<T>::ID {
ID(const Class &clazz, const Method<T> &method) : id(clazz.methodID(method, method.sig())) {}
ID(jmethodID id) : id(id) {}
operator jmethodID () const { return id; }
private: jmethodID id;
// Stuff for SFINAE workaround
public: typedef typename Class::Static::Method<T>::ID::ReturnType ReturnType;
public: struct IsObjectMethodID{}; typedef typename Method<T>::Reference Reference;
};
template <typename T>
struct Object::Method<T>::Reference : private Object, private ID {
typedef typename Class::Static::Method<T>::Reference::Type Type;
Reference(const Object &object, const ID &id) : Object(object), ID(id) {}
Type call(va_list) const;
Type call(const Nop & nop, ...) const {
va_list vl; va_start(vl, nop); Type ret = call(vl); va_end(vl);
return ret;
}
Type operator () () const
{ return call(Nop()); }
template <typename T0>
Type operator () (const T0 &v0) const
{ return call(Nop(), v0); }
template <typename T0, typename T1>
Type operator () (const T0 &v0, const T1 &v1) const
{ return call(Nop(), v0, v1); }
template <typename T0, typename T1, typename T2>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2) const
{ return call(Nop(), v0, v1, v2); }
template <typename T0, typename T1, typename T2, typename T3>
Type operator () (const T0 &v0, const T1 &v1, const T2 &v2,
const T3 &v3) const