-
Notifications
You must be signed in to change notification settings - Fork 0
/
omm.h
1186 lines (906 loc) · 42.9 KB
/
omm.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 OMM_H_INCLUDED
#define OMM_H_INCLUDED
#include <typeinfo>
#include <type_traits>
#include <algorithm>
/**
This library allows the programmer to use open multi-methods.
I was inspired by Jean-Louis Leroy's library named yomm2.
In the first lines are defined some metatypes like int_constant, collection and tlist.
I use them to make easier the metaprogramming. After all, using templates is functional
programming, but tedious and annoying.
The goal in this library is to create the omm table, an array containing function pointers
that points to the method implementations. In order to achieve this, several metafunctions
which operates with differents metatypes are needed. The diagram below shows the way the
omm table is created.
How to create the omm table:
vbsign_to_bsign -------------------------------> BS = tlist<R,BArgs> ---------------------
| |
| *create_table_omm* |
| make_function_cell |
ftype_to_sign | v
ftype ---------------> VBS *---------------------------------->* DSCOMB *----------------->* omm_table <----- F
| *
| *vbsign_to_dsign_combinations* ^
get_base_core_types | vbsign_to_dsign |
| |
v *
BCL -----> DCOMB
| / *
| *make_derived_combinations* / ^
| -----------------------/ |
create_type_id | / indices_to_types |
v / *
DCL ------> TID ----------------------------------> Indices
*make_indices*
Where:
- ftype: It is the function type template. In it, the virtual types are specified.
* Example: void(Virtual<Base1*>,int,SomeClass,Virtual<const Base2&>)
+ Actually, Virtual is turned into virtual_type.
- DCL (Derived Core List): It is a list containing the classes which participate in the multiple dispatch.
* Example: tlist<Derived1,Derived2,Derived3>
- F: It is an struct containing all the multi-method implementations. They have to be static function
named implementation.
- VBS (Virtual Base Signature): It is a tlist containing the type from ftype.
* Example: tlist<void,virtual_type<Base1*>,int,SomeClass,virtual_type<const Base2&>>
- BCL (Base Core List): It is a list containing all the virtual classes from VBS, in their
core form, i.e. without cv qualifiers, references or pointers.
* Example: tlist<Base1,Base2>
- BS (Base Signature): It is a list containing the types from VBS without the virtual_type wrappers.
* Example: tlist<void,Base1*,int,SomeClass,const Base2&>
- TID: It is a list of lists. Each list has as first element a base core type, and the rest of the elements
are their derived classes that appears in DCL.
* Example: tlist<tlist<Base1,Derived1>,tlist<Base2,Derived2,Derived3>>
- Indices: It is a list containing all the possible tuples of indices of the omm table. Each tuple has the same length
as TID, and contains numbers from 0 to the amount, minus 1, of derived types are in each list from TID.
* Example: tlist<tlist<0,0>,tlist<0,1>>
+ Actually, each number is a int_contant type.
- DCOMB: It is a list containing all the combinations of derived types that can be formed according
to their position in TID and to the list of Indices.
* Example: tlist<tlist<Derived1,Derived2>,tlist<Derived1,Derived3>>
- DSCOMB (Derived Signature Combinations): It is a list containing all the possible signatures that can be formed accordingly
to the Combination Derived Type list. Actually, the VBS is used to turn the virtual_types into a derived type.
* Example: tlist<tlist<void,Derived1*,int,SomeClass,const Derived2&>,tlist<void,Derived1*,int,SomeClass,const Derived3&>>
- table_omm: It is an array of pointers to the implementations.
*/
//---------------------------------------------------------------------------------
/**
* Some helper aliases
*/
namespace std{
template<typename T>
using is_reference_t = typename is_reference<T>::type;
template<typename T>
using is_pointer_t = typename is_pointer<T>::type;
template<typename T>
using is_const_t = typename is_const<T>::type;
template<typename T>
using is_volatile_t = typename is_volatile<T>::type;
template<typename T>
using is_lvalue_reference_t = typename is_lvalue_reference<T>::type;
template<typename T>
using is_rvalue_reference_t = typename is_rvalue_reference<T>::type;
template<typename T>
using is_polymorphic_t = typename is_polymorphic<T>::type;
template<typename B, typename D>
using is_base_of_t = typename is_base_of<B,D>::type;
template<typename B>
struct is_base_of_c{
template<typename D>
using type = std::is_base_of_t<B,D>;
};
}
//---------------------------------------------------------------------------------
//-------------------------------- INT_CONSTANT -----------------------------------
//---------------------------------------------------------------------------------
/**
* Represents an integer.
*/
template<int k>
using int_constant = std::integral_constant<int,k>;
//---------------------------------------------------------------------------------
/**
* The numbers 0 and 1.
*/
using zero = int_constant<0>;
using one = int_constant<1>;
//---------------------------------------------------------------------------------
/**
* Performs the sum of several numbers.
* - TS... : Each number is an int_constant type.
*/
template<typename... TS>
struct add : zero{};
template<typename T, typename... TS>
struct add<T,TS...> : int_constant<T::value+add<TS...>::value>{};
template<typename... TS>
using add_t = typename add<TS...>::type;
template<typename... TS>
static constexpr int add_v = add<TS...>::value;
//---------------------------------------------------------------------------------
/**
* Add 1 to an int_constant.
* - T : The int_constant to add 1.
*/
template<typename T>
using add1 = add<one,T>;
template<typename T>
using add1_t = typename add1<T>::type;
template<typename T>
static constexpr int add1_v = add1<T>::value;
//---------------------------------------------------------------------------------
/**
* Subtract 1 to an int_constant.
* - T : The int_constant to subtract 1.
*/
template<typename T>
using sub1 = add<int_constant<-1>,T>;
template<typename T>
using sub1_t = typename sub1<T>::type;
template<typename T>
static constexpr int sub1_v = sub1<T>::value;
//---------------------------------------------------------------------------------
//--------------------------------- COLLECTION ------------------------------------
//---------------------------------------------------------------------------------
/**
* Represents a collection of types. It is used to perform variadic template unpacking.
*/
template<typename... S>
struct collection{
using type = collection<S...>;
};
//---------------------------------------------------------------------------------
/**
* Applies a metafunction receiving all the types contained in a collection
* - P : The metafunction to be applied.
* - C : The collection containint the arguments of P.
*/
template<template<typename...> typename P, typename C>
struct apply_collection{};
template<template<typename...> typename P, typename... S>
struct apply_collection<P,collection<S...>> : P<S...>{};
template<template<typename...> typename P, typename C>
using apply_collection_t = typename apply_collection<P,C>::type;
//---------------------------------------------------------------------------------
//------------------------------------ TLIST --------------------------------------
//---------------------------------------------------------------------------------
/**
* Represents an empty list.
*/
struct nil{
using type = nil;
};
//---------------------------------------------------------------------------------
/**
* Represents a cons cell. A tlist is defined as a cons cell that its rigth cell
* is another list (empty or not).
* - T : The left cell.
* - S : The right cell.
*/
template<typename T, typename S>
struct cons{
using type = cons<T,S>;
};
template<typename T>
struct cons_c{
template<typename S>
using type = cons<T,S>;
};
//---------------------------------------------------------------------------------
/**
* Helper function to create tlist.
* - TS... : The types to be in the created list.
*/
template<typename... TS>
struct tlist : nil{};
template<typename T, typename... TS>
struct tlist<T,TS...> : cons<T,typename tlist<TS...>::type>{};
template<typename... TS>
using tlist_t = typename tlist<TS...>::type;
//---------------------------------------------------------------------------------
/**
* Transforms a tlist into a collection.
* - T : The list to turn into a collection.
*/
template<typename L, typename C>
struct tlist_to_collection_aux{};
template<typename C>
struct tlist_to_collection_aux<nil,C> : C{};
template<typename T, typename R, typename... S>
struct tlist_to_collection_aux<cons<T,R>,collection<S...>> : tlist_to_collection_aux<R,collection<S...,T>>{};
template<typename T>
struct tlist_to_collection : tlist_to_collection_aux<T,collection<>>{};
template<typename T>
using tlist_to_collection_t = typename tlist_to_collection<T>::type;
//---------------------------------------------------------------------------------
/**
* Returns the left cell of a cons type.
* - L : The cons cell to retrive its left cell.
*/
template<typename L>
struct car{};
template<>
struct car<nil> : nil{};
template<typename T, typename S>
struct car<cons<T,S>>{
using type = T;
};
template<typename L>
using car_t = typename car<L>::type;
//---------------------------------------------------------------------------------
/**
* Returns the right cell of a cons cell.
* - L : The cons cell to retrieve its right cell.
*/
template<typename L>
struct cdr{};
template<>
struct cdr<nil> : nil{};
template<typename T, typename S>
struct cdr<cons<T,S>>{
using type = S;
};
template<typename L>
using cdr_t = typename cdr<L>::type;
//---------------------------------------------------------------------------------
/**
* Concatenates several lists.
* - S... : The lists to be concatenated.
*/
template<typename... S>
struct append : nil{};
template<typename... S>
struct append<nil,S...> : append<S...>{};
template<typename T, typename R, typename... S>
struct append<cons<T,R>,S...> : cons<T,typename append<R,S...>::type>{};
template<typename... S>
using append_t = typename append<S...>::type;
//---------------------------------------------------------------------------------
/**
* Returns the length of a list, i.e. the number of its elements.
* - L : The list to retrieve its length.
*/
template<typename L>
struct length{};
template<>
struct length<nil> : zero{};
template<typename T, typename S>
struct length<cons<T,S>> : add1<typename length<S>::type>{};
template<typename L>
using length_t = typename length<L>::type;
template<typename L>
static constexpr int length_v = length<L>::value;
//---------------------------------------------------------------------------------
/**
* Returns the nth-element of a list.
* - L : The list to retrieve the element at the nth position.
* - K : The position where the element to retrieve is.
*/
template<typename L, typename K>
struct nth_aux : nth_aux<cdr_t<L>,sub1_t<K>>{};
template<typename L>
struct nth_aux<L,zero> : car<L>{};
template<typename L, typename K>
struct nth : nth_aux<L,K>{};
template<typename L, typename K>
using nth_t = typename nth<L,K>::type;
//---------------------------------------------------------------------------------
/**
* Applies a metafunction over all the elements of a list and retrurns the list with the results.
* - P : The metafunction to be applied to every element.
* - L : The list whose elements are passed P.
*/
template<template<typename> typename P, typename L>
struct mapcar{};
template<template<typename> typename P>
struct mapcar<P,nil> : nil{};
template<template<typename> typename P, typename T, typename S>
struct mapcar<P,cons<T,S>> : cons<typename P<T>::type,typename mapcar<P,S>::type>{};
template<template<typename> typename P, typename L>
using mapcar_t = typename mapcar<P,L>::type;
//---------------------------------------------------------------------------------
/**
* Performs a fold action from the right to the left of a list.
* - P : The metafunction used in the fold.
* - I : The initial value passed to P.
* - L : The list to be folded.
*/
template<template<typename,typename> typename P, typename I, typename L>
struct reduce_from_end : P<typename reduce_from_end<P,I,cdr_t<L>>::type,car_t<L>>{};
template<template<typename,typename> typename P, typename I, typename T>
struct reduce_from_end<P,I,cons<T,nil>> : P<I,T>{};
template<template<typename,typename> typename P, typename I, typename L>
using reduce_from_end_t = typename reduce_from_end<P,I,L>::type;
//---------------------------------------------------------------------------------
/**
* Performs a cons creation if B is true_type.
* - B : A bool_constant that indicates whether to perform the cons or not.
* - T : The type to be left consed with L.
* - L : The type to be right consed with T.
*/
template<typename B, typename T, typename L>
struct cons_if : cons<T,L>{};
template<typename T, typename L>
struct cons_if<std::false_type,T,L> : L{};
template<typename B, typename T, typename L>
using cons_if_t = typename cons_if<B,T,L>::type;
//---------------------------------------------------------------------------------
/**
* Removes the elements of a list that not verifies a condition.
* - P : P is evaluated once per element. If the result is false_type, the element is removed from the list.
* - L : The list whose elements could be removed.
*/
template<template<typename> typename P, typename L>
struct remove_if_not{};
template<template<typename> typename P>
struct remove_if_not<P,nil> : nil{};
template<template<typename> typename P, typename T, typename S>
struct remove_if_not<P,cons<T,S>> : cons_if<typename P<T>::type,T,typename remove_if_not<P,S>::type>{};
template<template<typename> typename P, typename L>
using remove_if_not_t = typename remove_if_not<P,L>::type;
//---------------------------------------------------------------------------------
/**
* Performs a metafunction using as arguments the types that are passed to apply, including all
* the elements contained in the list that should be in last place.
* - P : The metafunction to be performed.
* - S... : The arguments of P. The last arguments to be passed to P should be at a list being last argument of apply.
*/
template<typename... T>
struct apply_aux : nil{};
template<typename T, typename... TS>
struct apply_aux<T,TS...> : T{};
template<typename T, typename S, typename... TS>
struct apply_aux<T,S,TS...> : cons<T,typename apply_aux<S,TS...>::type>{};
template<template<typename...> typename P, typename... S>
struct apply : apply_collection<P,tlist_to_collection_t<typename apply_aux<S...>::type>>{};
template<template<typename...> typename P, typename... S>
using apply_t = typename apply<P,S...>::type;
template<template<typename...> typename P>
struct apply_c{
template<typename... S>
using type = apply<P,S...>;
};
//---------------------------------------------------------------------------------
/**
* Returns true_type if some element is true_type. In other case, it returns false_type.
* - TS... : A bunch of types being true_type or false_type.
*/
template<typename... TS>
struct or_type : std::false_type{};
template<typename... TS>
struct or_type<std::true_type,TS...> : std::true_type{};
template<typename... TS>
struct or_type<std::false_type,TS...> : or_type<TS...>{};
template<typename... TS>
using or_type_t = typename or_type<TS...>::type;
//---------------------------------------------------------------------------------
/**
* Returns a list where each element is a list that contains two elements from the initial lists
* as long as both elements has the same index.
* - L : A tlist to be zipped.
* - S : A tlist to be zipped.
*/
template<typename L, typename S>
struct zip : nil{};
template<typename P, typename Q, typename R, typename S>
struct zip<cons<P,Q>,cons<R,S>> : cons<tlist_t<P,R>,typename zip<Q,S>::type>{};
template<typename L, typename S>
using zip_t = typename zip<L,S>::type;
//---------------------------------------------------------------------------------
/**
* Reverses the order of the arguments a metafunction receives.
* - P : The metafunction whose arguments are going to be flipped.
*/
template<template<typename,typename> typename P>
struct flip{
template<typename T, typename S>
using type = typename P<S,T>::type;
};
//---------------------------------------------------------------------------------
//-------------------------------- virtual_type -----------------------------------
//---------------------------------------------------------------------------------
/**
* Wraps a type. It is used to know which type participate in the multiple dispatch.
* - T : The type that will be wrapped.
*/
template<typename T>
struct virtual_type{
using type = virtual_type<T>;
};
//---------------------------------------------------------------------------------
/**
* Checks whether a type is a pointer or a reference to a polymorphic type.
* - T : The type to check.
*/
template<typename T>
struct is_polymorphic_pr : or_type<std::is_polymorphic_t<std::remove_pointer_t<T>>,std::is_polymorphic_t<std::remove_reference_t<T>>>{};
template<typename T>
using is_polymorphic_pr_t = typename is_polymorphic_pr<T>::type;
template<typename T>
static constexpr bool is_polymorphic_pr_v = is_polymorphic_pr<T>::value;
//---------------------------------------------------------------------------------
//-------------------------- Type related metafunctions ---------------------------
//---------------------------------------------------------------------------------
/**
* Removes references, pointers and cv qualifiers from a type.
* - T : The type to be turned into a core form.
*/
template<typename IsRef, typename IsPtr, typename T>
struct core_type_aux{
using type = std::remove_cv_t<T>;
};
template<typename IsPtr, typename T>
struct core_type_aux<std::true_type,IsPtr,T> : core_type_aux<std::is_reference_t<std::remove_cv_t<std::remove_reference_t<T>>>,
std::is_pointer_t<std::remove_cv_t<std::remove_reference_t<T>>>,
std::remove_cv_t<std::remove_reference_t<T>>>{};
template<typename T>
struct core_type_aux<std::false_type,std::true_type,T> : core_type_aux<std::is_reference_t<std::remove_pointer_t<T>>,
std::is_pointer_t<std::remove_pointer_t<T>>,
std::remove_pointer_t<T>>{};
template<typename T>
struct core_type : core_type_aux<std::is_reference_t<T>,std::is_pointer_t<T>,T>{};
template<typename T>
using core_type_t = typename core_type<T>::type;
//---------------------------------------------------------------------------------
/**
* The reference, pointers and cv qualifiers are traslated from a type to another one.
* - N : The type with reference, pointers and cv qualifiers that will be traslated to S.
* - S : The type that will receive the reference, pointers and cv qualifiers from T.
*/
template<typename IsConst, typename IsVol, typename IsLval, typename IsRval, typename IsPtr, typename N, typename S>
struct slice_type_aux{
using type = S;
};
template<typename IsVol, typename IsLval, typename IsRval, typename IsPtr, typename N, typename S>
struct slice_type_aux<std::true_type,IsVol,IsLval,IsRval,IsPtr,N,S>{
using newN = std::remove_const_t<N>;
using type = std::add_const_t<typename slice_type_aux<std::is_const_t<newN>,std::is_volatile_t<newN>,std::is_lvalue_reference_t<newN>,
std::is_rvalue_reference_t<newN>,std::is_pointer_t<newN>,newN,S>::type>;
};
template<typename IsLval, typename IsRval, typename IsPtr, typename N, typename S>
struct slice_type_aux<std::false_type,std::true_type,IsLval,IsRval,IsPtr,N,S>{
using newN = std::remove_volatile_t<N>;
using type = std::add_volatile_t<typename slice_type_aux<std::is_const_t<newN>,std::is_volatile_t<newN>,std::is_lvalue_reference_t<newN>,
std::is_rvalue_reference_t<newN>,std::is_pointer_t<newN>,newN,S>::type>;
};
template<typename IsRval, typename IsPtr, typename N, typename S>
struct slice_type_aux<std::false_type,std::false_type,std::true_type,IsRval,IsPtr,N,S>{
using newN = std::remove_reference_t<N>;
using type = std::add_lvalue_reference_t<typename slice_type_aux<std::is_const_t<newN>,std::is_volatile_t<newN>,std::is_lvalue_reference_t<newN>,
std::is_rvalue_reference_t<newN>,std::is_pointer_t<newN>,newN,S>::type>;
};
template<typename IsPtr, typename N, typename S>
struct slice_type_aux<std::false_type,std::false_type,std::false_type,std::true_type,IsPtr,N,S>{
using newN = std::remove_reference_t<N>;
using type = std::add_rvalue_reference_t<typename slice_type_aux<std::is_const_t<newN>,std::is_volatile_t<newN>,std::is_lvalue_reference_t<newN>,
std::is_rvalue_reference_t<newN>,std::is_pointer_t<newN>,newN,S>::type>;
};
template<typename N, typename S>
struct slice_type_aux<std::false_type,std::false_type,std::false_type,std::false_type,std::true_type,N,S>{
using newN = std::remove_pointer_t<N>;
using type = std::add_pointer_t<typename slice_type_aux<std::is_const_t<newN>,std::is_volatile_t<newN>,std::is_lvalue_reference_t<newN>,
std::is_rvalue_reference_t<newN>,std::is_pointer_t<newN>,newN,S>::type>;
};
template<typename N, typename S>
struct slice_type{
using type = typename slice_type_aux<std::is_const_t<N>,std::is_volatile_t<N>,std::is_lvalue_reference_t<N>,
std::is_rvalue_reference_t<N>,std::is_pointer_t<N>,N,S>::type;
};
template<typename N, typename S>
using slice_type_t = typename slice_type<N,S>::type;
//---------------------------------------------------------------------------------
//------------------------- VBS (Virtual Base Signature) --------------------------
//---------------------------------------------------------------------------------
/**
* Creates the VBS from the ftype.
*/
template<typename T>
struct ftype_to_sign{};
template<typename R, typename... AS>
struct ftype_to_sign<R(AS...)> : tlist<R,AS...>{};
template<typename T>
using ftype_to_sign_t = typename ftype_to_sign<T>::type;
//---------------------------------------------------------------------------------
//----------------------------- BCL (Base Core List) ------------------------------
//---------------------------------------------------------------------------------
/**
* Retrieves from the VBS a list with the virtual base types in their core form.
* - VBS : The VBS type.
*/
template<typename VBS>
struct get_base_core_types{};
template<>
struct get_base_core_types<nil> : nil{};
template<typename B, typename BS>
struct get_base_core_types<cons<B,BS>> : get_base_core_types<BS>{};
template<typename B, typename BS>
struct get_base_core_types<cons<virtual_type<B>,BS>> : cons<core_type_t<B>,typename get_base_core_types<BS>::type>{};
template<typename VBS>
using get_base_core_types_t = typename get_base_core_types<VBS>::type;
//---------------------------------------------------------------------------------
//----------------------------- BS (Base Signature) -------------------------------
//---------------------------------------------------------------------------------
/**
* Creates a BS from a VBS.
* - VBS : The VBS type.
*/
template<typename VBS>
struct vbsign_to_bsign{};
template<>
struct vbsign_to_bsign<nil> : nil{};
template<typename B, typename BS>
struct vbsign_to_bsign<cons<virtual_type<B>,BS>> : cons<B,typename vbsign_to_bsign<BS>::type>{};
template<typename B, typename BS>
struct vbsign_to_bsign<cons<B,BS>> : cons<B,typename vbsign_to_bsign<BS>::type>{};
template<typename VBS>
using vbsign_to_bsign_t = typename vbsign_to_bsign<VBS>::type;
//---------------------------------------------------------------------------------
//------------------------------------- TID ---------------------------------------
//---------------------------------------------------------------------------------
/**
* Creates a list whose first element is a base type and the rest of elements are derived types from the DCL.
* - B : The base type that will be the first element of the list.
* - DCL : The DCL type.
*/
template<typename B, typename DCL>
struct create_base_of_many : cons<B,remove_if_not_t<std::is_base_of_c<B>::template type,DCL>>{};
template<typename B, typename DCL>
using create_base_of_many_t = typename create_base_of_many<B,DCL>::type;
template<typename DCL>
struct create_base_of_many_c_inv{
template<typename B>
using type = create_base_of_many_t<B,DCL>;
};
//---------------------------------------------------------------------------------
/**
* Create a TID from the BCL and the DCL.
* - BCL : The BCL type.
* - DCL : The DCL type.
*/
template<typename BCL, typename DCL>
struct create_type_id : mapcar<create_base_of_many_c_inv<DCL>::template type,BCL>{};
template<typename BCL, typename DCL>
using create_type_id_t = typename create_type_id<BCL,DCL>::type;
//---------------------------------------------------------------------------------
//----------------------------------- Indices -------------------------------------
//---------------------------------------------------------------------------------
/**
* Inserts an element in every list contained in the LL list.
* - T : The element that will be inserted in every list contained in the LL list.
* - LL : A list that contains a bunch of lists where the T element will be inserted in.
*/
template<typename T, typename LL>
using cons_all = mapcar<cons_c<T>::template type,LL>;
template<typename T, typename LL>
using cons_all_t = typename cons_all<T,LL>::type;
template<typename LL>
struct cons_all_c_inv{
template<typename T>
using type = cons_all_t<T,LL>;
};
//---------------------------------------------------------------------------------
/**
* Returns the cartesian product of several lists.
* - LS... : The lists used for creating the cartesian product.
*/
template<typename L, typename M>
struct cartesian_product_aux : apply<append,mapcar_t<cons_all_c_inv<M>::template type,L>>{};
template<typename... LS>
struct cartesian_product : reduce_from_end<flip<cartesian_product_aux>::template type,tlist_t<nil>,tlist_t<LS...>>{};
template<typename... LS>
using cartesian_product_t = typename cartesian_product<LS...>::type;
//---------------------------------------------------------------------------------
/**
* Returns a list with the numbers from zero until a given one.
* - M : Indicates that the maximum number to generate will be M.
*/
template<typename K, typename M>
struct less_numbers_aux : cons<K,typename less_numbers_aux<add1_t<K>,M>::type>{};
template<typename M>
struct less_numbers_aux<M,M> : nil{};
template<typename M>
using less_numbers = less_numbers_aux<zero,M>;
template<typename M>
using less_numbers_t = typename less_numbers<M>::type;
//---------------------------------------------------------------------------------
/**
* Creates a list containing every possible combinantion of indices in the omm table.
* - TID : The TID type.
*/
template<typename TID>
struct make_indices : apply<cartesian_product,mapcar_t<less_numbers,mapcar_t<length,TID>>>{};
template<typename TID>
using make_indices_t = typename make_indices<TID>::type;
//---------------------------------------------------------------------------------
//------------------------------------- DCOMB -------------------------------------
//---------------------------------------------------------------------------------
/**
* Returns a list of derived types associated with the indices in KS. For each number in
* the list KS, a type is retrieved from TID whose position is that number.
* - TID : The TID type.
* - KS : A list containing the indices.
*/
template<typename TID, typename KS>
struct indices_to_types : mapcar<apply_c<nth>::template type,zip_t<TID,KS>>{};
template<typename TID, typename KS>
using indices_to_types_t = typename indices_to_types<TID,KS>::type;
template<typename TID>
struct indices_to_types_c{
template<typename KS>
using type = indices_to_types_t<TID,KS>;
};
//---------------------------------------------------------------------------------
/**
* Returns a list containing all the combinations of derived types that the signatures of the
* implementations can have.
* - TID : The TID type.
* - IND : The list containing all the possible indices of the omm table.
*/
template<typename TID, typename IND>
struct make_derived_combinations : mapcar<indices_to_types_c<TID>::template type,IND>{};
template<typename TID, typename IND>
using make_derived_combinations_t = typename make_derived_combinations<TID,IND>::type;
//---------------------------------------------------------------------------------
//------------------------------------ DSCOMB -------------------------------------
//---------------------------------------------------------------------------------
/**
* Turns the VBS into a DS, i.e. a signature where the virtual types have been replaced
* by a derived type from the DL list.
* - VBS : The VBS type.
* - DL : A list containing derived types that will replace the virtual types from the VBS.
*/
template<typename VBS, typename DL>
struct vbsign_to_dsign{};
template<typename VBS>
struct vbsign_to_dsign<VBS,nil> : VBS{};
template<typename T, typename S, typename DL>
struct vbsign_to_dsign<cons<virtual_type<T>,S>,DL> : cons<slice_type_t<T,car_t<DL>>,typename vbsign_to_dsign<S,cdr_t<DL>>::type>{};
template<typename T, typename S, typename DL>
struct vbsign_to_dsign<cons<T,S>,DL> : cons<T,typename vbsign_to_dsign<S,DL>::type>{};
template<typename VBS, typename DL>
using vbsign_to_dsign_t = typename vbsign_to_dsign<VBS,DL>::type;
template<typename VBS>
struct vbsign_to_dsign_c{
template<typename DL>
using type = vbsign_to_dsign_t<VBS,DL>;
};
//---------------------------------------------------------------------------------
/**
* Returns a list containing every possible derived signature from the derived types in DCOMB
* that will replace the virtual base types from the VBS.
* - VBS : The VBS type.
* - DCOMB : The DCOMB type.
*/
template<typename VBS, typename DCOMB>
struct vbsign_to_dsign_combinations : mapcar<vbsign_to_dsign_c<VBS>::template type,DCOMB>{};
template<typename VBS, typename DCOMB>
using vbsign_to_dsign_combinations_t = typename vbsign_to_dsign_combinations<VBS,DCOMB>::type;
//---------------------------------------------------------------------------------
//---------------------------------- Omm table ------------------------------------
//---------------------------------------------------------------------------------
/**
* Returns a function type from a collection of types.
* - C : The collection with the types that will be used to form the function type.
*/
template<typename C>
struct collection_to_function_type{};
template<typename R, typename... AS>
struct collection_to_function_type<collection<R,AS...>>{
using type = R(AS...);
};
template<typename C>
using collection_to_function_type_t = typename collection_to_function_type<C>::type;
//---------------------------------------------------------------------------------
/**
* Returns a function type from a signature, i.e. a list of types.
* - L : The list with the types that will be used to form the function type.
*/
template<typename L>
struct signature_to_function_type : collection_to_function_type<tlist_to_collection_t<L>>{};
template<typename L>
using signature_to_function_type_t = typename signature_to_function_type<L>::type;
//---------------------------------------------------------------------------------
/**
* Taken from https://stackoverflow.com/questions/28309164/checking-for-existence-of-an-overloaded-member-function
* Checks whether an implementation exists inside the struct with all the implementations.
* - F: The struct containing all the implementations.
* - CD: A collection representing the signature of the specific implementation method.
*/
template <typename T, typename... Dargs>
class has_implementation_aux{
template <typename C, typename = decltype(C::implementation(std::declval<Dargs>()...))>
static std::true_type test(int);
template <typename C>
static std::false_type test(...);
public:
using type = decltype(test<T>(0));
static constexpr bool value = decltype(test<T>(0))::value;
};
template<typename F, typename CD>
struct has_implementation{};
template<typename F, typename R, typename... Dargs>
struct has_implementation<F,collection<R,Dargs...>> : has_implementation_aux<F,Dargs...>{};
template<typename F, typename CD>
using has_implementation_t = typename has_implementation<F,CD>::type;
template<typename F, typename CD>
static constexpr bool has_implementation_v = has_implementation<F,CD>::value;
//---------------------------------------------------------------------------------
/**
* Generates a function pointer that calls a specific implementation method after doing a cast.
* - F: The struct containing all the implementations.
* - BS: A tlist containing the signature of the returned function pointer.
* - DS: A tlist containing the signature of the specific implementation method.
*/
template<typename HasImplementation, typename F, typename BC, typename DC>
struct make_function_cell_aux{};
template<typename F, typename R, typename... Bargs, typename... Dargs>
struct make_function_cell_aux<std::false_type,F,collection<R,Bargs...>,collection<R,Dargs...>>{
static constexpr std::nullptr_t value = nullptr;
};
template<typename F, typename R, typename... Bargs, typename... Dargs>
struct make_function_cell_aux<std::true_type,F,collection<R,Bargs...>,collection<R,Dargs...>>{
static R value(Bargs... args){
return F::implementation(static_cast<Dargs>(args)...);
}
};
template<typename F, typename BS, typename DS>
struct make_function_cell : make_function_cell_aux<has_implementation_t<F,tlist_to_collection_t<DS>>,
F,tlist_to_collection_t<BS>,tlist_to_collection_t<DS>>{};
//---------------------------------------------------------------------------------
/**
* Creates the omm table.
* - F : The struct containing all the implementations.
* - BS : The BS type.
* - DSCOMB : The DSCOMB type.