-
Notifications
You must be signed in to change notification settings - Fork 31
/
jni_bind_release.h
10121 lines (8246 loc) · 324 KB
/
jni_bind_release.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
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*******************************************************************************
* JNI Bind Version __JNI_BIND_VERSION__.
* Beta Public Release.
********************************************************************************
* This header is the single header version which you can use to quickly test or
* deploy in your own project without using Bazel. It is self contained.
*
* To use this header you must be compiling with clang and with C++17 support.
*
* It must also have jni.h in its build path. I.e. the following must compile:
* #include <jni.h>
*
* See GitHub for sample API usage.
* https://github.com/google/jni-bind
*
* 🚨 Are you enjoying JNI Bind? Please consider adding a ⭐️ on GitHub! 🚨
* JNI Bind is the culmination of a lot of hard work and your support is greatly
* appreciated.
*
* CODE BELOW IS AUTO GENERATED.
*******************************************************************************/
#ifndef JNI_BIND_RELEASE_
#define JNI_BIND_RELEASE_
#include <jni.h>
#include <tuple>
#include <type_traits>
namespace jni::metaprogramming {
// Helper struct for figuring out the contents of a tuple. Use Unwrap if you
// plan to use this as a metafunction (this breaks the paradigm as type is not
// templatized, but instead embedded in the type).
template <typename Func, typename TupleType>
struct TupleUnroller {
static_assert(std::is_same_v<TupleType, void>,
"You used a non tuple type when a tuple was expected.");
};
template <typename Func, typename... Ts>
struct TupleUnroller<Func, std::tuple<Ts...>> {
// Templated to avoid invalid instantiations.
template <typename = void>
struct TypeHelperStruct {
using type = typename Func::template type<Ts...>;
};
// Templated to avoid invalid instantiations.
template <typename = void>
struct ValueHelperStruct {
static constexpr auto value = Func::template value<Ts...>;
};
};
template <typename Func, typename TupleType>
using TupleUnroller_t =
typename TupleUnroller<Func,
TupleType>::template TypeHelperStruct<void>::type;
template <typename Func, typename TupleType>
static constexpr auto TupleUnroller_v =
TupleUnroller<Func, TupleType>::template ValueHelperStruct<void>::value;
// Takes a variadic pack and injects it inside another type.
// e.g. TupleToType<std::tuple<int, float>>::type<std::pair> =
// std::pair<int, float>.
template <typename T>
struct TupleToType {
static_assert(std::is_same_v<T, std::tuple<>>,
"TupleToType should be templated on a std::tuple type");
using type = std::tuple<>;
};
template <typename... Ts>
struct TupleToType<std::tuple<Ts...>> {
template <template <typename...> class Container>
using type = Container<Ts...>;
};
template <typename TupleType, template <typename...> class Container>
using TupleToType_t = typename TupleToType<TupleType>::template type<Container>;
// Takes a container and extracts its variadic arguments into a tuple.
template <typename T>
struct ExtractTupleFromType {
static_assert(
std::is_same_v<T, void>,
"ExtractTupleFromType should be used on a type with variadic params.");
};
template <template <typename...> class ContainerType, typename... Ts>
struct ExtractTupleFromType<ContainerType<Ts...>> {
using type = std::tuple<Ts...>;
};
template <typename ContainerType>
using ExtractTupleFromType_t =
typename ExtractTupleFromType<ContainerType>::type;
} // namespace jni::metaprogramming
namespace jni::metaprogramming {
// Metafunction to forward all args in a tuple on to a metafunction.
// It's useful if you want to "derefence" arguments from another meta function.
template <typename Func>
struct Unwrap {
template <typename TupType>
using type = TupleUnroller_t<Func, TupType>;
};
template <typename Func, typename... Ts>
using Unwrap_t = typename Unwrap<Func>::template type<Ts...>;
template <typename Func, typename... Ts>
static constexpr auto Unwrap_v = Unwrap_t<Func, Ts...>::value;
} // namespace jni::metaprogramming
#include <cstddef>
namespace jni::metaprogramming {
template <std::size_t N>
struct TypeOfNthElement {
template <std::size_t idx, typename... T>
struct PackUnroller;
template <typename T0, typename... T>
struct PackUnroller<0, T0, T...> {
using type = T0;
};
template <std::size_t idx, typename T0, typename... T>
struct PackUnroller<idx, T0, T...> {
using type = typename PackUnroller<idx - 1, T...>::type;
};
template <typename... Ts>
using type = typename PackUnroller<N, Ts...>::type;
};
template <std::size_t N, typename... Ts>
using TypeOfNthElement_t = typename TypeOfNthElement<N>::template type<Ts...>;
template <std::size_t N, typename TupType>
using TypeOfNthTupleElement_t = Unwrap_t<TypeOfNthElement<N>, TupType>;
} // namespace jni::metaprogramming
#include <cstddef>
#include <tuple>
#include <utility>
namespace jni::metaprogramming {
// Metafunction that takes variadic arguments and applies a bound metafunction
// to each element. Type exposed will always be a tuple. This is intended for
// use with a Monoid that you might want to apply across a set of every
// argument. Otherwise, you would use the argument directly.
template <typename BoundType>
struct PerElement {
template <typename Args, typename Is>
struct Sequence {};
template <typename... Args, std::size_t... Is>
struct Sequence<std::tuple<Args...>, std::index_sequence<Is...>> {
using type = std::tuple<
typename BoundType::template type<TypeOfNthElement_t<Is, Args...>>...>;
};
template <typename... Ts>
using type = typename Sequence<std::tuple<Ts...>,
std::make_index_sequence<sizeof...(Ts)>>::type;
};
template <typename Func, typename... Ts>
using PerElement_t = typename PerElement<Func>::template type<Ts...>;
} // namespace jni::metaprogramming
#include <tuple>
namespace jni::metaprogramming {
// Takes two tuples and collapses them into a single tuple.
struct Combine {
template <typename T1, typename T2>
struct CombineHelper {
static_assert(std::is_same_v<T1, void> && std::is_same_v<T2, void>,
"Combine requires tuples.");
};
template <typename... T1s, typename... T2s>
struct CombineHelper<std::tuple<T1s...>, std::tuple<T2s...>> {
using type = std::tuple<T1s..., T2s...>;
};
template <typename Tup1, typename Tup2>
using type = typename CombineHelper<Tup1, Tup2>::type;
};
template <typename Tup1, typename Tup2>
using Combine_t = typename Combine::template type<Tup1, Tup2>;
} // namespace jni::metaprogramming
#include <tuple>
#include <type_traits>
namespace jni::metaprogramming {
// Metafunction to take a sequence of values and emit std::true or std::false
// per element based on their type equality.
template <typename... Ts>
struct Same {
template <typename... Us>
using type =
typename std::is_same<std::tuple<Ts...>, std::tuple<Us...>>::type;
template <typename... Us>
constexpr static bool value = type<Us...>::value;
};
} // namespace jni::metaprogramming
namespace jni::metaprogramming {
// Metafunction for a left fold reduction of a sequence of types with an
// operation. Single element sequences return the input type with no use of
// Operation.
template <class Operation>
struct Reduce {
template <typename... Ts>
struct SequenceGenerator {};
template <typename T1, typename T2>
using Operation_t = typename Operation::template type<T1, T2>;
template <typename... Ts>
using SequenceGenerator_t = typename SequenceGenerator<Ts...>::type;
// Used for single element sequence.
template <typename T1, typename... Ts>
struct SequenceGenerator<T1, Ts...> {
using type = T1;
};
// Terminal element in sequence.
template <typename T1, typename T2>
struct SequenceGenerator<T1, T2> {
using type = Operation_t<T1, T2>;
};
template <typename T1, typename T2, typename... Ts>
struct SequenceGenerator<T1, T2, Ts...> {
using type = SequenceGenerator_t<Operation_t<T1, T2>, Ts...>;
};
template <typename... Args>
using type = typename SequenceGenerator<Args...>::type;
};
template <typename Operation, typename... Ts>
using Reduce_t = typename Reduce<Operation>::template type<Ts...>;
template <typename Operation, typename TupleOfTs>
using ReduceAsPack_t = TupleUnroller_t<Reduce<Operation>, TupleOfTs>;
template <typename Operation, typename TupleOfTs>
static constexpr auto ReduceAsPack_v =
TupleUnroller_t<Reduce<Operation>, TupleOfTs>::val;
} // namespace jni::metaprogramming
#include <tuple>
namespace jni::metaprogramming {
// "Invokes" a metafunction (i.e. a type alias for Func::type).
// Handles both variadic or non-variadic args for a consistent syntax.
template <typename Func>
struct Invoke {
template <typename, typename Enable = void>
struct InvokeHelper {};
// Variadic forwarding.
template <typename... Ts>
struct InvokeHelper<std::tuple<Ts...>> {
using type = typename Func::template type<Ts...>;
};
// Non-variadic forwarding.
template <typename Ts>
struct InvokeHelper<std::tuple<Ts>> {
using type = typename Func::template type<Ts>;
};
template <typename... Args>
using type = typename InvokeHelper<std::tuple<Args...>>::type;
};
// Helper alias to invoke a metafunction with args as a variadic pack.
template <typename Func, typename... Args>
using Invoke_t = typename Invoke<Func>::template type<Args...>;
// Helper alias to take a pack from a tuple and use it as args to invocation.
template <typename Func, typename TupleOfArgs>
using InvokeAsPack_t = TupleUnroller_t<Invoke<Func>, TupleOfArgs>;
// Helper alias to invoke a metafunction with args contained in a tuple. Each
// tuple element will itself be an arg.
template <typename Func, typename TupleOfArgs>
using InvokePerTupArg_t = TupleUnroller_t<PerElement<Func>, TupleOfArgs>;
// Helper alias to invoke a metafunction with args contained in each element of
// a tuple. Each tuple element will itself be a tuple of arguments.
template <typename Func, typename TupleOfTupleOfArgs>
using InvokePerTupArgAsPack_t =
InvokePerTupArg_t<Unwrap<Func>, TupleOfTupleOfArgs>;
} // namespace jni::metaprogramming
#include <type_traits>
namespace jni::metaprogramming {
template <typename Func>
struct Any {
template <typename... Ts>
using type =
typename std::disjunction<typename Func::template type<Ts>...>::type;
};
template <typename Func, typename... Ts>
using Any_t = typename Any<Func>::template type<Ts...>;
template <typename Func, typename... Ts>
static constexpr bool Any_v = Any_t<Func, Ts...>::value;
template <typename Func, typename Ts>
using Any_Tup = TupleUnroller_t<Any<Func>, Ts>;
template <typename Func, typename Ts>
static constexpr bool Any_Tup_v = TupleUnroller_t<Any<Func>, Ts>::value;
} // namespace jni::metaprogramming
// IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h"
namespace jni {
// Single type that be used as a value when expressing void.
struct Void {
using Raw = void;
};
template <typename T>
struct VoidIfVoid {
using type = T;
};
template <>
struct VoidIfVoid<Void> {
using type = void;
};
template <typename T>
using VoidIfVoid_t = typename VoidIfVoid<T>::type;
} // namespace jni
// IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h"
namespace jni {
struct Object {
const char* name_;
constexpr explicit Object(const char* name) : name_(name) {}
};
} // namespace jni
namespace jni::metaprogramming {
template <typename SoughtType, typename... Ts>
using Contains_t = Invoke_t<Any<Same<SoughtType>>, Ts...>;
template <typename SoughtType, typename... Tup>
static constexpr bool Contains_v = Contains_t<SoughtType, Tup...>::value;
template <typename SoughtType, typename Tup>
using TupContains_t = TupleUnroller_t<Any<Same<SoughtType>>, Tup>;
template <typename SoughtType, typename Tup>
static constexpr bool TupContains_v = TupContains_t<SoughtType, Tup>::value;
// Constexpr value implementation.
template <typename SoughtType, typename... Ts>
constexpr bool ContainsValue(const SoughtType& sought_value, Ts&&... ts) {
return ((sought_value == ts) || ...);
}
} // namespace jni::metaprogramming
#include <tuple>
namespace jni::metaprogramming {
// Metafunction to generate a concatenation of variadic args.
struct Concatenate {
template <typename... Ts>
using type = std::tuple<Ts...>;
};
template <typename... Ts>
using Concatenate_t = typename Concatenate::template type<Ts...>;
// Metafunction to concatenate tuples into a single tuple.
template <typename... Ts>
struct ConcatenateTup {
static_assert(sizeof...(Ts) != 0, "ConcatenateTup must only take tuples.");
};
template <typename... T1s>
struct ConcatenateTup<std::tuple<T1s...>> {
using type = std::tuple<T1s...>;
};
template <typename... Tups>
using ConcatenateTup_t = Reduce_t<Combine, Tups...>;
} // namespace jni::metaprogramming
// IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h"
namespace jni {
struct ReturnBase {};
template <typename Raw_>
struct Return : ReturnBase {
const Raw_ raw_ = {};
using Raw = Raw_;
constexpr Return() = default;
template <typename Raw>
constexpr explicit Return(Raw raw) : raw_(raw) {}
};
template <>
struct Return<void> : ReturnBase {
using Raw = void;
const Void raw_{};
constexpr Return() = default;
};
Return() -> Return<void>;
template <typename Raw>
Return(Raw) -> Return<Raw>;
template <typename T>
using Raw_t = typename T::Raw;
} // namespace jni
// IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h"
#include <cstddef>
#include <tuple>
#include <type_traits>
namespace jni {
struct ParamsBase {};
// Represents a sequence of parameters for a method in a compile time
// definition.
//
// These may be in their native type (e.g. int, float, etc) or they may have
// some minimal type wrapping such as Object<Name> (see JniTProxy).
template <typename... ValuesRaw_>
struct Params : ParamsBase {
public:
using ParamsRawTup = std::tuple<ValuesRaw_...>;
const std::tuple<ValuesRaw_...> values_ = {};
// Zero args constructor.
template <std::size_t N = sizeof...(ValuesRaw_),
typename std::enable_if<(N == 0), int>::type = 0>
constexpr explicit Params() {}
// CTAD constructor, fully defined args (e.g. Params{class_value}).
template <
std::size_t N = sizeof...(ValuesRaw_), typename... Args,
typename std::enable_if<(N > 0 && sizeof...(Args) == N), int>::type = 0>
constexpr explicit Params(Args... args) : values_(args...) {}
// Constructor for explicit templatizing, no args (e.g. Params<jint>{}).
template <
std::size_t N = sizeof...(ValuesRaw_), typename... Args,
typename std::enable_if<(N > 0 && sizeof...(Args) == 0), int>::type = 0>
constexpr explicit Params() {}
};
template <typename... Ts>
Params(Ts...) -> Params<Ts...>;
template <typename... LhsValuesRaw, typename... RhsValuesRaw>
constexpr bool operator==(const Params<LhsValuesRaw...>& lhs,
const Params<RhsValuesRaw...>& rhs) {
return lhs.values_ == rhs.values_;
}
//==============================================================================
template <typename T>
using ParamsRawTup_t = typename T::ParamsRawTup;
} // namespace jni
#include <cstddef>
#include <string_view>
#include <type_traits>
#include <utility>
#define STR(x) []() { return x; }
namespace jni::metaprogramming {
template <typename Identifier>
using identifier_type = decltype(std::declval<Identifier>()());
constexpr std::size_t ConstexprStrlen(const char* str) {
return str[0] == 0 ? 0 : ConstexprStrlen(str + 1) + 1;
}
struct StringAsTypeBase {};
// Represents a string by embedding a sequence of characters in a type.
template <char... chars>
struct StringAsType : StringAsTypeBase {
static constexpr char static_chars[] = {chars..., 0};
static constexpr std::string_view chars_as_sv = {static_chars,
sizeof...(chars)};
};
template <typename Identifier, std::size_t... I>
constexpr auto LambdaToStr(Identifier id, std::index_sequence<I...>) {
return StringAsType<id()[I]...>{};
}
template <
typename Identifier,
std::enable_if_t<std::is_same_v<identifier_type<Identifier>, const char*>,
int> = 0>
constexpr auto LambdaToStr(Identifier id) {
return LambdaToStr(id, std::make_index_sequence<ConstexprStrlen(id())>{});
}
template <typename NameLambda>
using LambdaStringToType = decltype(LambdaToStr(std::declval<NameLambda>()));
} // namespace jni::metaprogramming
#include <string>
#include <string_view>
namespace jni::metaprogramming {
struct Color {
static constexpr std::string_view kNone{"\033[0m"};
// --- Text Colors ---
static constexpr std::string_view kBlack = "\033[0;30m";
static constexpr std::string_view kBlackBold = "\033[1;30m";
static constexpr std::string_view kRed = "\033[0;31m";
static constexpr std::string_view kRedBold = "\033[1;31m";
static constexpr std::string_view kGreen = "\033[0;32m";
static constexpr std::string_view kGreenBold = "\033[1;32m";
static constexpr std::string_view kYellow = "\033[0;33m";
static constexpr std::string_view kYellowBold = "\033[1;33m";
static constexpr std::string_view kBlue = "\033[0;34m";
static constexpr std::string_view kBlueBold = "\033[1;34m";
static constexpr std::string_view kPurple = "\033[0;35m";
static constexpr std::string_view kPurpleBold = "\033[1;35m";
static constexpr std::string_view kCyan = "\033[0;36m";
static constexpr std::string_view kCyanBold = "\033[1;36m";
static constexpr std::string_view kWhite = "\033[0;37m";
static constexpr std::string_view kWhiteBold = "\033[1;37m";
// --- Background Colors ---
static constexpr std::string_view kBgBlack = "\033[40m";
static constexpr std::string_view kBgBlackBold = "\033[100m";
static constexpr std::string_view kBgRed = "\033[41m";
static constexpr std::string_view kBgRedBold = "\033[101m";
static constexpr std::string_view kBgGreen = "\033[42m";
static constexpr std::string_view kBgGreenBold = "\033[102m";
static constexpr std::string_view kBgYellow = "\033[43m";
static constexpr std::string_view kBgYellowBold = "\033[103m";
static constexpr std::string_view kBgBlue = "\033[44m";
static constexpr std::string_view kBgBlueBold = "\033[104m";
static constexpr std::string_view kBgPurple = "\033[45m";
static constexpr std::string_view kBgPurpleBold = "\033[105m";
static constexpr std::string_view kBgCyan = "\033[46m";
static constexpr std::string_view kBgCyanBold = "\033[106m";
static constexpr std::string_view kBgWhite = "\033[47m";
static constexpr std::string_view kBgWhiteBold = "\033[107m";
};
// Colorize function for runtime manipulation of text. This is suitable when
// the string is unknowable until runtime.
inline std::string Colorize(std::string_view colour, std::string_view str,
bool colourize = true) {
return !colourize ? std::string{str}
: std::string{colour} + std::string{str} +
std::string{Color::kNone};
}
} // namespace jni::metaprogramming
#include <tuple>
#include <type_traits>
namespace jni::metaprogramming {
// Returns all elements derived from DesiredBase.
//
// Note, if no values found, this will be std::tuple<>. If you'd like a default
// value use BaseFilterWithDefault.
template <typename DesiredBase, typename Enable = void,
typename T = std::tuple<>>
struct BaseFilter {
static_assert(std::is_same_v<T, std::tuple<>>);
using type = std::tuple<>;
};
template <typename DesiredBase, typename... Ts>
using BaseFilter_t =
typename BaseFilter<DesiredBase, void, std::tuple<Ts...>>::type;
template <typename DesiredBase, typename T, typename... Ts>
struct BaseFilter<DesiredBase,
std::enable_if_t<std::is_base_of_v<DesiredBase, T>, void>,
std::tuple<T, Ts...>> {
using type =
ConcatenateTup_t<std::tuple<T>, BaseFilter_t<DesiredBase, Ts...>>;
};
template <typename DesiredBase, typename T, typename... Ts>
struct BaseFilter<DesiredBase,
std::enable_if_t<!std::is_base_of_v<DesiredBase, T>, void>,
std::tuple<T, Ts...>> {
using type = BaseFilter_t<DesiredBase, Ts...>;
};
//==============================================================================
template <typename PostFilterValue, typename DefaultValue>
using DefaultIfEmpty_Tup =
std::conditional_t<std::is_same_v<PostFilterValue, std::tuple<>>,
DefaultValue, PostFilterValue>;
template <typename DesiredBase, typename DefaultValue, typename... Ts>
using BaseFilterWithDefault_t =
DefaultIfEmpty_Tup<BaseFilter_t<DesiredBase, Ts...>,
std::tuple<DefaultValue>>;
} // namespace jni::metaprogramming
#include <type_traits>
namespace jni::metaprogramming {
template <typename...>
struct AllUnique {
static constexpr bool value = true;
using type = std::bool_constant<value>;
};
template <typename T, typename... Ts>
struct AllUnique<T, Ts...> {
static constexpr bool value =
!Contains_v<T, Ts...> && AllUnique<Ts...>::value;
using type = std::bool_constant<value>;
};
template <typename... Ts>
static constexpr bool AllUnique_v = AllUnique<Ts...>::value;
// Constexpr value implementation.
constexpr bool AllUniqueValues(...) { return true; }
template <typename T1, typename... Ts>
constexpr bool AllUniqueValues(const T1&& t1, const Ts&&... ts) {
return (!ContainsValue(t1, ts...)) && AllUniqueValues(ts...);
}
} // namespace jni::metaprogramming
namespace jni {
// Convenience struct for returning results from pinning array.
template <typename SpanType>
struct GetArrayElementsResult {
SpanType* ptr_;
jboolean is_copy;
};
} // namespace jni
#include <sstream>
#include <string>
namespace jni {
// Converts a single argument into a std::string (useful for debugging).
template <typename T>
struct ArgStringify {
static std::string Str(const T& val) {
std::stringstream ss;
ss << val;
return ss.str();
}
};
template <typename T>
struct ArgStringify<T*> {
static std::string Str(const T* val) {
if (val == nullptr) {
return "nullptr";
}
std::stringstream ss;
ss << val;
return ss.str();
}
};
// Helper function to select correct ArgString partial specialization.
template <typename T>
std::string ArgString(const T& val) {
return ArgStringify<T>::Str(val);
}
////////////////////////////////////////////////////////////////////////////////
// Void.
////////////////////////////////////////////////////////////////////////////////
template <>
struct ArgStringify<void> {
static std::string Str() { return ""; }
};
inline std::string ArgString() { return ArgStringify<void>::Str(); }
////////////////////////////////////////////////////////////////////////////////
// Bool.
////////////////////////////////////////////////////////////////////////////////
template <>
struct ArgStringify<bool> {
static std::string Str(const bool val) { return val ? "TRUE" : "FALSE"; }
};
} // namespace jni
// IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h"
#include <string_view>
#include <tuple>
#include <type_traits>
namespace jni {
struct OverloadBase {};
struct MethodBase {};
// Represents a single overload of a given method.
template <typename ReturnT_, typename Params_>
struct Overload : OverloadBase {
const ReturnT_ return_;
const Params_ params_;
// `Return`, no `Params`.
constexpr Overload(ReturnT_ return_type)
: return_(return_type), params_(Params{}) {}
// `Return` and `Params`.
constexpr Overload(ReturnT_ return_type, Params_ params)
: return_(return_type), params_(params) {}
};
template <typename ReturnT_>
Overload(ReturnT_) -> Overload<ReturnT_, Params<>>;
template <typename ReturnT_, typename Params_>
Overload(ReturnT_, Params_) -> Overload<ReturnT_, Params_>;
template <typename Returns_, typename Params_>
struct Method {};
template <typename... Returns, typename... Params_>
struct Method<std::tuple<Returns...>, std::tuple<Params_...>>
: public MethodBase {
public:
const char* name_;
const std::tuple<Overload<Returns, Params_>...> invocations_;
// `Return`, no `Params`.
template <typename ReturnT_,
std::enable_if_t<std::is_base_of_v<ReturnBase, ReturnT_>, int> = 0>
constexpr Method(const char* name, ReturnT_ return_type)
: name_(name), invocations_(Overload{return_type}) {}
// `Return` and `Params`.
template <typename ReturnT_, typename ParamsT_,
std::enable_if_t<std::is_base_of_v<ReturnBase, ReturnT_>, int> = 0>
constexpr Method(const char* name, ReturnT_ return_type, ParamsT_ params)
: name_(name), invocations_(Overload{return_type, params}) {}
// `Overload` Set.
constexpr Method(const char* name, Overload<Returns, Params_>... invocations)
: name_(name), invocations_(invocations...) {}
};
// CTAD for Non-overloaded form, no Params.
template <typename ReturnT, typename = std::enable_if_t<
!std::is_base_of_v<OverloadBase, ReturnT>>>
Method(const char*, ReturnT)
-> Method<std::tuple<ReturnT>, std::tuple<Params<>>>;
// CTAD for Non-overloaded form.
template <
typename ReturnT, typename ParamsT,
typename = std::enable_if_t<!std::is_base_of_v<OverloadBase, ReturnT> &&
!std::is_base_of_v<OverloadBase, ParamsT>>>
Method(const char*, ReturnT, ParamsT)
-> Method<std::tuple<ReturnT>, std::tuple<ParamsT>>;
// CTAD for Overloaded form.
template <typename... Returns, typename... Params>
Method(const char*, Overload<Returns, Params>...)
-> Method<std::tuple<Returns...>, std::tuple<Params...>>;
template <typename ReturnT1, typename ParamsT1, typename ReturnT2,
typename ParamsT2>
constexpr bool operator==(const Method<ReturnT1, ParamsT1>& lhs,
const Method<ReturnT2, ParamsT2>& rhs) {
return std::string_view(lhs.name_) == std::string_view(rhs.name_);
}
} // namespace jni
// IWYU pragma: private, include "third_party/jni_wrapper/jni_bind.h"
#include <string>
namespace jni {
struct FieldBase {};
template <typename Raw_>
struct Field : public FieldBase {
public:
using Raw = Raw_;
const char* name_;
const Raw_ raw_ = {};
constexpr Field(const char* name) : name_(name) {}
constexpr Field(const char* name, Raw_ value_raw)
: name_(name), raw_(value_raw) {}
};
template <typename Raw_>
Field(const char*, Raw_) -> Field<Raw_>;
template <typename T>
using Raw_t = typename T::Raw;
} // namespace jni
using jclass = jclass;
using jthrowable = jthrowable;
using jstring = jstring;
using jarray = jarray;
using jbooleanArray = jbooleanArray;
using jbyteArray = jbyteArray;
using jcharArray = jcharArray;
using jshortArray = jshortArray;
using jintArray = jintArray;
using jlongArray = jlongArray;
using jfloatArray = jfloatArray;
using jdoubleArray = jdoubleArray;
using jobjectArray = jobjectArray;
using jweak = jweak;
using jvalue = jvalue;
using jfieldID = jfieldID;
using jmethodID = jmethodID;
using JavaVM = JavaVM;
using JNIEnv = JNIEnv;
// IWYU pragma: end_exports
#include <cstddef>
namespace jni::metaprogramming {
// Metafunction that binds a metafunction (e.g. Same), a query (e.g. "A"), a
// variadic pack as arguments, and returns the idx of the query. All members
// of the variadic pack must be unique.
//
// |Comparator| must express a static bool data member "value".
template <typename Comparator>
struct FindIdxOfVal {
template <typename... Ts>
struct FindIdxOfValHelper {
static_assert(sizeof...(Ts) == -1, "The sought element is not in the set");
};
template <typename T, typename... Ts>
struct FindIdxOfValHelper<T, Ts...> {
template <std::size_t idx>
static constexpr std::size_t GetIdxHelper() {
if constexpr (Comparator::template value<T>) {
return idx;
} else {
return FindIdxOfValHelper<Ts...>::template GetIdxHelper<idx - 1>();
}
}
static constexpr std::size_t GetIdx() {
return sizeof...(Ts) - GetIdxHelper<sizeof...(Ts)>();
}
};
// Done once in a seperate struct for improved compilation time.
template <typename... Ts>
struct StaticAssertWrapper {
static_assert(AllUnique_v<Ts...>,
"FindIdxOfVal only operates on unique sets.");
static constexpr std::size_t value = FindIdxOfValHelper<Ts...>::GetIdx();
};
template <typename... Ts>
static constexpr std::size_t value = StaticAssertWrapper<Ts...>::value;
};
template <typename Comparator, typename... Ts>
static constexpr std::size_t FindIdxOfValWithComparator_idx =
FindIdxOfVal<Comparator>::template value<Ts...>;
// Convenience alias with default of std::is_same Comparator (typical use case).
template <typename Query, typename... Ts>
static constexpr std::size_t FindIdxOfVal_idx =
FindIdxOfValWithComparator_idx<Same<Query>, Ts...>;
template <typename Comparator, typename TupType>
static constexpr std::size_t FindIdxOfValInTupWithComparator_idx =
TupleUnroller_v<FindIdxOfVal<Comparator>, TupType>;
// Convenience alias with default of std::is_same Comparator (typical use case).
template <typename Query, typename TupType>
static constexpr std::size_t FindIdxOfValInTup_idx =
FindIdxOfValInTupWithComparator_idx<Same<Query>, TupType>;
} // namespace jni::metaprogramming
#include <tuple>
namespace jni::metaprogramming {
// Metafunction to return only even elements.
struct Even {