FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
livecode/libfoundation/src/foundation-foreign.cpp at develop · livecode/livecode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
livecode
/
livecode
Public archive
Notifications
You must be signed in to change notification settings
Fork
223
Star
517
Code
Pull requests
189
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
livecode
/
libfoundation
/
src
/
foundation-foreign.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1559 lines (1346 loc) · 56.8 KB
Breadcrumbs
livecode
/
libfoundation
/
src
/
foundation-foreign.cpp
Copy path
File metadata and controls
1559 lines (1346 loc) · 56.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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 (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>.
*/
#
include
<
foundation.h
>
#
include
<
foundation-auto.h
>
#
include
"
foundation-private.h
"
#
include
"
foundation-hash.h
"
#
include
<
limits
>
#
include
<
type_traits
>
//
//////////////////////////////////////////////////////////////////////////////
/*
Machine Types
*
* These types are what may be considered 'independent of any language'.
*/
/*
The fundamental bool type - this is considered to be 1 byte in size.
*/
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCBoolTypeInfo
;
/*
The fundamental sized integer types
*/
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCUInt8TypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCSInt8TypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCUInt16TypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCSInt16TypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCUInt32TypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCSInt32TypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCUInt64TypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCSInt64TypeInfo
;
/*
The fundamental binary floating-point types.
*/
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCFloatTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCDoubleTypeInfo
;
/*
The size_t and ssize_t types which are architecture / platform dependent.
*/
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCUIntSizeTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCSIntSizeTypeInfo
;
/*
The uintptr_t and intptr_t types which are architecture / platform
* dependent.
*/
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCUIntPtrTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCSIntPtrTypeInfo
;
/*
The fundamental unmanaged pointer type (equivalent to void * in C). This
* uses the initialize and defined members of a foreign value description to
* provide an element of safety against nullptr vs non-nullptr.
*/
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCPointerTypeInfo
;
/*
The natural int and float types are 32-bit wide on 32-bit processors and
* 64-bit wide on 64-bit processors.
*/
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCNaturalUIntTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCNaturalSIntTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCNaturalFloatTypeInfo
;
/*
C Types
*
* These types represent the C types which are relative to the compiler,
* platform and architecture the engine is compiled against. We have a distinct
* type for CBool as (in theory) it could be any length, and not just a byte.
*/
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCBoolTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCCharTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCUCharTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCSCharTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCUShortTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCSShortTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCUIntTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCSIntTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCULongTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCSLongTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCULongLongTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCCSLongLongTypeInfo
;
//
The uinteger_t and integer_t types, which are engine dependent
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCUIntTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCSIntTypeInfo
;
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignBoolTypeInfo
() {
return
kMCBoolTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignUInt8TypeInfo
() {
return
kMCUInt8TypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignSInt8TypeInfo
() {
return
kMCSInt8TypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignUInt16TypeInfo
() {
return
kMCUInt16TypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignSInt16TypeInfo
() {
return
kMCSInt16TypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignUInt32TypeInfo
() {
return
kMCUInt32TypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignSInt32TypeInfo
() {
return
kMCSInt32TypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignUInt64TypeInfo
() {
return
kMCUInt64TypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignSInt64TypeInfo
() {
return
kMCSInt64TypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignFloatTypeInfo
() {
return
kMCFloatTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignDoubleTypeInfo
() {
return
kMCDoubleTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignPointerTypeInfo
() {
return
kMCPointerTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignUIntSizeTypeInfo
() {
return
kMCUIntSizeTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignSIntSizeTypeInfo
() {
return
kMCSIntSizeTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignUIntPtrTypeInfo
() {
return
kMCUIntPtrTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignSIntPtrTypeInfo
() {
return
kMCSIntPtrTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignNaturalUIntTypeInfo
() {
return
kMCNaturalUIntTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignNaturalSIntTypeInfo
() {
return
kMCNaturalSIntTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignNaturalFloatTypeInfo
() {
return
kMCNaturalFloatTypeInfo
; }
/*
*/
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCBoolTypeInfo
() {
return
kMCCBoolTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCCharTypeInfo
() {
return
kMCCSCharTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCUCharTypeInfo
() {
return
kMCCUCharTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCSCharTypeInfo
() {
return
kMCCSCharTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCUShortTypeInfo
() {
return
kMCCUShortTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCSShortTypeInfo
() {
return
kMCCSShortTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCUIntTypeInfo
() {
return
kMCCUIntTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCSIntTypeInfo
() {
return
kMCCSIntTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCULongTypeInfo
() {
return
kMCCULongTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCSLongTypeInfo
() {
return
kMCCSLongTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCULongLongTypeInfo
() {
return
kMCCULongLongTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignCSLongLongTypeInfo
() {
return
kMCCSLongLongTypeInfo
; }
/*
*/
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignUIntTypeInfo
() {
return
kMCUIntTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCForeignSIntTypeInfo
() {
return
kMCSIntTypeInfo
; }
//
//////////////////////////////////////////////////////////////////////////////
#
if
defined(__32_BIT__)
typedef
uint32_t
natural_uint_t
;
typedef
int32_t
natural_sint_t
;
typedef
float
natural_float_t
;
#
elif
defined(__64_BIT__)
typedef
uint64_t
natural_uint_t
;
typedef
int64_t
natural_sint_t
;
typedef
double
natural_float_t
;
#
else
#
error
Bitness of target not defined
#
endif
static_assert
(
sizeof
(
int
) ==
4
,
"
Assumption that int is 4 bytes in size not valid
"
);
static_assert
(
sizeof
(
bool
) ==
1
,
"
Assumption that bool is 1 byte in size not valid
"
);
static_assert
(
sizeof
(
uintptr_t
) ==
sizeof
(
natural_uint_t
),
"
Assumption that natural_uint_t is the same width as uintptr_t not valid
"
);
static_assert
(
sizeof
(
intptr_t
) ==
sizeof
(
natural_sint_t
),
"
Assumption that natural_uint_t is the same width as uintptr_t not valid
"
);
template
<
typename
CType,
typename
Enable =
void
>
struct
compute_primitive_type
{
static_assert
(
sizeof
(CType) ==
0
,
"
No mapping from CType to primitive type
"
);
};
template
<
typename
CType>
struct
compute_primitive_type
<
CType,
typename
std::enable_if<std::is_integral<CType>::value &&
std::is_signed<CType>::value>::type>
{
static_assert
(
sizeof
(CType) !=
1
||
sizeof
(CType) !=
2
||
sizeof
(CType) !=
4
||
sizeof
(CType) !=
8
,
"
Unsupported signed integer size
"
);
static
constexpr
auto
value =
sizeof
(CType) ==
1
?
kMCForeignPrimitiveTypeSInt8
:
sizeof
(CType) ==
2
?
kMCForeignPrimitiveTypeSInt16
:
sizeof
(CType) ==
4
?
kMCForeignPrimitiveTypeSInt32
:
sizeof
(CType) ==
8
?
kMCForeignPrimitiveTypeSInt64
:
kMCForeignPrimitiveTypeVoid
;
};
template
<
typename
CType>
struct
compute_primitive_type
<
CType,
typename
std::enable_if<std::is_integral<CType>::value &&
!std::is_signed<CType>::value>::type>
{
static_assert
(
sizeof
(CType) !=
1
||
sizeof
(CType) !=
2
||
sizeof
(CType) !=
4
||
sizeof
(CType) !=
8
,
"
Unsupported unsigned integer size
"
);
static
constexpr
auto
value =
sizeof
(CType) ==
1
?
kMCForeignPrimitiveTypeUInt8
:
sizeof
(CType) ==
2
?
kMCForeignPrimitiveTypeUInt16
:
sizeof
(CType) ==
4
?
kMCForeignPrimitiveTypeUInt32
:
sizeof
(CType) ==
8
?
kMCForeignPrimitiveTypeUInt64
:
kMCForeignPrimitiveTypeVoid
;
};
template
<>
struct
compute_primitive_type
<
float
>
{
static
constexpr
auto
value =
kMCForeignPrimitiveTypeFloat32
;
};
template
<>
struct
compute_primitive_type
<
double
>
{
static
constexpr
auto
value =
kMCForeignPrimitiveTypeFloat64
;
};
template
<
typename
CType>
struct
numeric_type_desc_t
{
using
c_type = CType;
static
constexpr
auto
primitive_type = compute_primitive_type<CType>::value;
static
constexpr
MCTypeInfoRef&
base_type_info
() {
return
kMCNullTypeInfo
; }
static
constexpr
auto
is_optional =
false
;
static
constexpr
auto
is_bridgable =
true
;
using
bridge_type = MCNumberRef;
static
constexpr
MCTypeInfoRef&
bridge_type_info
() {
return
kMCNumberTypeInfo
; }
};
template
<
typename
CType>
struct
integral_type_desc_t
:
public
numeric_type_desc_t
<CType> {
static
constexpr
auto
& hash_func = MCHashInt<CType>;
};
/*
*/
struct
bool_type_desc_t
{
using
c_type =
bool
;
static
constexpr
auto
primitive_type =
kMCForeignPrimitiveTypeBool
;
static
constexpr
MCTypeInfoRef&
base_type_info
() {
return
kMCNullTypeInfo
; }
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCBoolTypeInfo
; }
static
constexpr
auto
is_optional =
false
;
static
constexpr
auto
is_bridgable =
true
;
using
bridge_type = MCBooleanRef;
static
constexpr
MCTypeInfoRef&
bridge_type_info
() {
return
kMCBooleanTypeInfo
; }
static
constexpr
auto
& hash_func = MCHashBool;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCUInt32TypeInfo
; }
};
struct
uint8_type_desc_t
:
public
integral_type_desc_t
<
uint8_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCUInt8TypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign 8-bit unsigned integer %u>
"
;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCUInt32TypeInfo
; }
};
struct
sint8_type_desc_t
:
public
integral_type_desc_t
<
int8_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCSInt8TypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign 8-bit signed integer %d>
"
;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCSInt32TypeInfo
; }
};
struct
uint16_type_desc_t
:
public
integral_type_desc_t
<
uint16_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCUInt16TypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign 16-bit unsigned integer %u>
"
;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCUInt32TypeInfo
; }
};
struct
sint16_type_desc_t
:
public
integral_type_desc_t
<
int16_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCSInt16TypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign 16-bit signed integer %d>
"
;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCSInt32TypeInfo
; }
};
struct
uint32_type_desc_t
:
public
integral_type_desc_t
<
uint32_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCUInt32TypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign 32-bit unsigned integer %u>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
sint32_type_desc_t
:
public
integral_type_desc_t
<
int32_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCSInt32TypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign 32-bit signed integer %d>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
uint64_type_desc_t
:
public
integral_type_desc_t
<
uint64_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCUInt64TypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign 64-bit unsigned integer %llu>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
sint64_type_desc_t
:
public
integral_type_desc_t
<
int64_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCSInt64TypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign 64-bit signed integer %lld>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
float_type_desc_t
:
public
numeric_type_desc_t
<
float
> {
using
c_type =
float
;
static
constexpr
auto
primitive_type =
kMCForeignPrimitiveTypeFloat32
;
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCFloatTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign float %lg>
"
;
static
constexpr
auto
& hash_func = MCHashDouble;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCDoubleTypeInfo
; }
};
struct
double_type_desc_t
:
public
numeric_type_desc_t
<
double
> {
using
c_type =
double
;
static
constexpr
auto
primitive_type =
kMCForeignPrimitiveTypeFloat64
;
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCDoubleTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign double %lg>
"
;
static
constexpr
auto
& hash_func = MCHashDouble;
static
constexpr
auto
is_promotable =
false
;
};
struct
pointer_type_desc_t
{
using
c_type =
void
*;
static
constexpr
auto
primitive_type =
kMCForeignPrimitiveTypePointer
;
static
constexpr
auto
is_optional =
true
;
static
constexpr
auto
is_bridgable =
false
;
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCPointerTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign pointer %p>
"
;
static
constexpr
MCTypeInfoRef&
base_type_info
() {
return
kMCNullTypeInfo
; }
static
constexpr
auto
& hash_func = MCHashPointer;
static
constexpr
auto
is_promotable =
false
;
};
struct
uintsize_type_desc_t
:
public
integral_type_desc_t
<
size_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCUIntSizeTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign unsigned size %zu>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
sintsize_type_desc_t
:
public
integral_type_desc_t
<
ssize_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCSIntSizeTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign signed size %zd>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
uintptr_type_desc_t
:
public
integral_type_desc_t
<
uintptr_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCUIntPtrTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign unsigned intptr %zu>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
sintptr_type_desc_t
:
public
integral_type_desc_t
<
intptr_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCSIntPtrTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign signed intptr %zd>
"
;
static
constexpr
auto
is_promotable =
false
;
};
/*
*/
struct
naturaluint_type_desc_t
:
public
integral_type_desc_t
<
natural_uint_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCNaturalUIntTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign natural unsigned integer %zu>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
naturalsint_type_desc_t
:
public
integral_type_desc_t
<
natural_sint_t
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCNaturalSIntTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign natural signed integer %zd>
"
;
static
constexpr
auto
is_promotable =
false
;
};
#
if
defined(__32_BIT__)
struct
naturalfloat_type_desc_t
:
public
float_type_desc_t
{
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCNaturalFloatTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign natural float %lg>
"
;
};
#
elif
defined(__64_BIT__)
struct
naturalfloat_type_desc_t
:
public
double_type_desc_t
{
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCNaturalFloatTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign natural float %lg>
"
;
};
#
endif
/*
*/
struct
cbool_type_desc_t
:
public
bool_type_desc_t
{
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCBoolTypeInfo
; }
};
struct
cchar_type_desc_t
:
public
integral_type_desc_t
<
char
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCCharTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c char '%c'>
"
;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCSInt32TypeInfo
; }
};
struct
cuchar_type_desc_t
:
public
integral_type_desc_t
<
unsigned
char
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCUCharTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c unsigned char %u>
"
;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCUInt32TypeInfo
; }
};
struct
cschar_type_desc_t
:
public
integral_type_desc_t
<
signed
char
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCSCharTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c signed char %d>
"
;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCSInt32TypeInfo
; }
};
struct
cushort_type_desc_t
:
public
integral_type_desc_t
<
unsigned
short
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCUShortTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c unsigned short %u>
"
;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCUInt32TypeInfo
; }
};
struct
csshort_type_desc_t
:
public
integral_type_desc_t
<
signed
short
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCSShortTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c signed short %d>
"
;
static
constexpr
auto
is_promotable =
true
;
static
constexpr
MCTypeInfoRef&
promoted_type_info
() {
return
kMCSInt32TypeInfo
; }
};
struct
cuint_type_desc_t
:
public
integral_type_desc_t
<
unsigned
int
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCUIntTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c unsigned int %u>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
csint_type_desc_t
:
public
integral_type_desc_t
<
signed
int
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCSIntTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c signed int %d>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
culong_type_desc_t
:
public
integral_type_desc_t
<
unsigned
long
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCULongTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c unsigned long %lu>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
cslong_type_desc_t
:
public
integral_type_desc_t
<
long
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCSLongTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c signed long %ld>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
culonglong_type_desc_t
:
public
integral_type_desc_t
<
unsigned
long
long
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCULongLongTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c unsigned long long %llu>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
cslonglong_type_desc_t
:
public
integral_type_desc_t
<
long
long
> {
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCCSLongLongTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign c signed long long %lld>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
uint_type_desc_t
:
public
integral_type_desc_t
<
uinteger_t
> {
static_assert
(
UINTEGER_MAX
==
UINT32_MAX
,
"
Unsupported size for uinteger_t
"
);
static
constexpr
auto
primitive_type =
kMCForeignPrimitiveTypeUInt32
;
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCUIntTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign unsigned integer %u>
"
;
static
constexpr
auto
is_promotable =
false
;
};
struct
sint_type_desc_t
:
public
integral_type_desc_t
<
integer_t
> {
static_assert
(
INTEGER_MAX
==
INT32_MAX
,
"
Unsupported size for integer_t
"
);
static
constexpr
auto
primitive_type =
kMCForeignPrimitiveTypeSInt32
;
static
constexpr
MCTypeInfoRef&
type_info
() {
return
kMCSIntTypeInfo
; }
static
constexpr
auto
describe_format =
"
<foreign signed integer %d>
"
;
static
constexpr
auto
& hash_func = MCHashInteger;
static
constexpr
auto
is_promotable =
false
;
};
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCForeignImportErrorTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCForeignExportErrorTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCForeignAggregateExportErrorTypeInfo
;
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCForeignValueCreate
(MCTypeInfoRef p_typeinfo,
void
*p_contents, MCForeignValueRef& r_value)
{
MCAssert
(
MCTypeInfoIsNamed
(p_typeinfo));
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo =
__MCTypeInfoResolve
(p_typeinfo);
__MCForeignValue *t_value;
if
(!
__MCValueCreateExtended
(
kMCValueTypeCodeForeignValue
,
t_resolved_typeinfo -> foreign . descriptor . size,
t_value))
return
false
;
if
(!t_resolved_typeinfo->
foreign
.
descriptor
.
copy
(&t_resolved_typeinfo->
foreign
.
descriptor
, p_contents, t_value +
1
))
{
MCMemoryDelete
(t_value);
return
false
;
}
t_value -> typeinfo =
MCValueRetain
(p_typeinfo);
r_value = t_value;
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCForeignValueCreateAndRelease
(MCTypeInfoRef p_typeinfo,
void
*p_contents, MCForeignValueRef& r_value)
{
MCAssert
(
MCTypeInfoIsNamed
(p_typeinfo));
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo =
__MCTypeInfoResolve
(p_typeinfo);
__MCForeignValue *t_value;
if
(!
__MCValueCreateExtended
(
kMCValueTypeCodeForeignValue
,
t_resolved_typeinfo -> foreign . descriptor . size,
t_value))
return
false
;
if
(!t_resolved_typeinfo->
foreign
.
descriptor
.
move
(&t_resolved_typeinfo->
foreign
.
descriptor
, p_contents, t_value +
1
))
{
MCMemoryDelete
(t_value);
return
false
;
}
t_value -> typeinfo =
MCValueRetain
(p_typeinfo);
r_value = t_value;
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCForeignValueExport
(MCTypeInfoRef p_typeinfo, MCValueRef p_value, MCForeignValueRef& r_value)
{
MCAssert
(
MCTypeInfoIsNamed
(p_typeinfo));
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo =
__MCTypeInfoResolve
(p_typeinfo);
__MCForeignValue *t_value;
if
(!
__MCValueCreateExtended
(
kMCValueTypeCodeForeignValue
,
t_resolved_typeinfo -> foreign . descriptor . size,
t_value))
return
false
;
if
(t_resolved_typeinfo->
foreign
.
descriptor
.
doexport
== nil ||
!t_resolved_typeinfo->
foreign
.
descriptor
.
doexport
(&t_resolved_typeinfo->
foreign
.
descriptor
, p_value,
false
, t_value +
1
))
{
MCMemoryDelete
(t_value);
return
false
;
}
t_value -> typeinfo =
MCValueRetain
(p_typeinfo);
r_value = t_value;
return
true
;
}
MC_DLLEXPORT_DEF
void
*
MCForeignValueGetContentsPtr
(MCValueRef self)
{
return
((__MCForeignValue *)self) +
1
;
}
//
//////////////////////////////////////////////////////////////////////////////
void
__MCForeignValueDestroy
(__MCForeignValue *self)
{
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo =
__MCTypeInfoResolve
(self -> typeinfo);
t_resolved_typeinfo->
foreign
.
descriptor
.
finalize
(self +
1
);
MCValueRelease
(self -> typeinfo);
}
hash_t
__MCForeignValueHash
(__MCForeignValue *self)
{
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo =
__MCTypeInfoResolve
(self -> typeinfo);
hash_t
t_hash;
if
(!t_resolved_typeinfo->
foreign
.
descriptor
.
hash
(&t_resolved_typeinfo->
foreign
.
descriptor
, self +
1
, t_hash))
return
0
;
return
t_hash;
}
bool
__MCForeignValueIsEqualTo
(__MCForeignValue *self, __MCForeignValue *other_self)
{
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo =
__MCTypeInfoResolve
(self -> typeinfo);
bool
t_result;
if
(!t_resolved_typeinfo->
foreign
.
descriptor
.
equal
(&t_resolved_typeinfo->
foreign
.
descriptor
, self +
1
, other_self +
1
, t_result))
return
false
;
return
t_result;
}
bool
__MCForeignValueCopyDescription
(__MCForeignValue *self, MCStringRef& r_description)
{
MCTypeInfoRef t_resolved_typeinfo;
t_resolved_typeinfo =
__MCTypeInfoResolve
(self->
typeinfo
);
bool
(*t_describe_func)(
const
MCForeignTypeDescriptor*,
void
*, MCStringRef &);
t_describe_func = t_resolved_typeinfo->
foreign
.
descriptor
.
describe
;
if
(
NULL
!= t_describe_func)
return
t_describe_func
(&t_resolved_typeinfo->
foreign
.
descriptor
,
MCForeignValueGetContentsPtr
(self),
r_description);
else
return
MCStringFormat
(r_description,
"
<foreign: %p>
"
, self);
}
//
//////////////////////////////////////////////////////////////////////////////
template
<
typename
OverflowedType>
static
bool
__throw_numeric_overflow
(MCTypeInfoRef p_error)
{
return
MCErrorCreateAndThrow
(p_error,
"
type
"
,
OverflowedType::type_info
(),
"
reason
"
,
MCSTR
(
"
numeric overflow
"
),
nil);
}
/*
----------------------------------------------------------------
* Generic implementations
* ----------------------------------------------------------------
*/
namespace
/*
anonymous
*/
{
/*
We need to be able to partially specialize describe(), doimport()
* and doexport() as they have very type specific requirements.
* Unfortunately, you can't partially specialize a function template.
* The correct thing to do in this case is to have a single function
* template that hands off to a functor template -- because struct
* templates _can_ be partially specialized.
*
* See also "Why not specialize function templates?"
* http://gotw.ca/publications/mill17.htm
*/
template
<
typename
TypeDesc>
struct
Describe
{
static
bool
describe
(
typename
TypeDesc::c_type p_value, MCStringRef& r_description)
{
return
MCStringFormat
(r_description, TypeDesc::describe_format, p_value);
}
};
template
<
typename
TypeDesc,
typename
Enable =
void
>
struct
DoExport
{
static_assert
(
sizeof
(
typename
TypeDesc::c_type) ==
0
,
"
Missing export specialization
"
);
static
bool
doexport
(
typename
TypeDesc::bridge_type p_boxed_value,
typename
TypeDesc::c_type& r_value)
{
return
false
;
}
};
template
<
typename
TypeDesc,
typename
Enable =
void
>
struct
DoImport
{
static_assert
(
sizeof
(
typename
TypeDesc::c_type) ==
0
,
"
Missing import specialization
"
);
static
bool
doimport
(
typename
TypeDesc::c_type p_value,
typename
TypeDesc::bridge_type& r_boxed_value)
{
return
false
;
}
};
template
<
typename
TypeDesc,
typename
Enable =
void
>
struct
DoPromote
{
static_assert
(
sizeof
(
typename
TypeDesc::c_type) ==
0
,
"
Missing promote specialization
"
);
static
bool
promote
(
void
*contents)
{
return
false
;
}
};
template
<
typename
TypeDesc>
bool
initialize
(
void
*contents)
{
static_assert
(TypeDesc::is_optional,
"
This type is not optional
"
);
*
static_cast
<
typename
TypeDesc::c_type *>(contents) =
typename
TypeDesc::c_type
();
return
true
;
}
template
<
typename
TypeDesc>
void
finalize
(
void
*contents)
{
}
template
<
typename
TypeDesc>
bool
defined
(
void
*contents)
{
static_assert
(TypeDesc::is_optional,
"
This type is not optional
"
);
return
*
static_cast
<
typename
TypeDesc::c_type *>(contents) !=
typename
TypeDesc::c_type
();
}
template
<
typename
TypeDesc>
bool
equal
(
const
MCForeignTypeDescriptor* desc,
void
*left,
void
*right,
bool
& r_equal)
{
r_equal = *
static_cast
<
typename
TypeDesc::c_type *>(left) == *
static_cast
<
typename
TypeDesc::c_type *>(right);
return
true
;
}
template
<
typename
TypeDesc>
bool
copy
(
const
MCForeignTypeDescriptor* desc,
void
*from,
void
*to)
{
*
static_cast
<
typename
TypeDesc::c_type *>(to) = *
static_cast
<
typename
TypeDesc::c_type *>(from);
return
true
;
}
template
<
typename
TypeDesc>
bool
move
(
const
MCForeignTypeDescriptor* desc,
void
*from,
void
*to)
{
return
copy<TypeDesc>(desc, from, to);
}
template
<
typename
TypeDesc>
bool
hash
(
const
MCForeignTypeDescriptor* desc,
void
*value,
hash_t
& r_hash)
{
r_hash =
TypeDesc::hash_func
(*
static_cast
<
typename
TypeDesc::c_type *>(value));
return
true
;
}
template
<
typename
TypeDesc>
bool
describe
(
const
MCForeignTypeDescriptor* desc,
void
*contents, MCStringRef& r_string)
{
return
Describe<TypeDesc>::
describe
(*
static_cast
<
typename
TypeDesc::c_type *>(contents), r_string);
}
template
<
typename
TypeDesc>
bool
doexport
(
const
MCForeignTypeDescriptor* desc, MCValueRef p_value,
bool
p_release,
void
*contents)
{
static_assert
(TypeDesc::is_bridgable,
"
This type is not bridgable
"
);
if
(!DoExport<TypeDesc>::
doexport
(
static_cast
<
typename
TypeDesc::bridge_type>(p_value),
*
static_cast
<
typename
TypeDesc::c_type *>(contents)))
{
return
false
;
}
if
(p_release)
{
MCValueRelease
(p_value);
}
return
true
;
}
template
<
typename
TypeDesc>
bool
doimport
(
const
MCForeignTypeDescriptor* desc,
void
*contents,
bool
p_release, MCValueRef& r_value)
{
static_assert
(TypeDesc::is_bridgable,
"
This type is not bridgable
"
);
return
DoImport<TypeDesc>::
doimport
(*
static_cast
<
typename
TypeDesc::c_type *>(contents),
reinterpret_cast
<
typename
TypeDesc::bridge_type&>(r_value));
}
template
<
typename
TypeDesc>
void
promote
(
void
*contents)
{
static_assert
(TypeDesc::is_promotable,
"
This type is not promotable
"
);
return
DoPromote<TypeDesc>::
promote
(contents);
}
/*
---------- bool specializations
*/
/*
We need to specialize describe for bool because it returns fixed
* strings instead of using a format string.
*/
template
<>
struct
Describe
<
bool_type_desc_t
>
{
static
bool
describe
(
bool
p_value, MCStringRef& r_string)
{
return
MCStringFormat
(r_string, p_value ?
"
<foreign bool true>
"
:
"
<foreign bool false>
"
);
}
};
template
<>
struct
DoImport
<
bool_type_desc_t
>
{
static
bool
doimport
(
bool
p_value, MCBooleanRef& r_boxed_value)
{
r_boxed_value =
MCValueRetain
(p_value ?
kMCTrue
:
kMCFalse
);
return
true
;
}
};
template
<>
struct
DoExport
<
bool_type_desc_t
>
{
static
bool
doexport
(MCValueRef p_boxed_value,
bool
& r_value)
{
r_value = (p_boxed_value ==
kMCTrue
);
return
true
;
}
};
/*
We need to specialize describe for cbool because it returns fixed
* strings instead of using a format string.
*/
template
<>
struct
Describe
<
cbool_type_desc_t
>
{
static
bool
describe
(
bool
p_value, MCStringRef& r_string)
{
return
MCStringFormat
(r_string, p_value ?
"
<foreign c bool true>
"
:
"
<foreign c bool false>
"
);
}
};
template
<>
struct
DoImport
<
cbool_type_desc_t
>
{
static
bool
doimport
(
bool
p_value, MCBooleanRef& r_boxed_value)
{
r_boxed_value =
MCValueRetain
(p_value ?
kMCTrue
:
kMCFalse
);
return
true
;
}
};
template
<>
struct
DoExport
<
cbool_type_desc_t
>
{
static
bool
doexport
(MCValueRef p_boxed_value,
bool
& r_value)
{
r_value = (p_boxed_value ==
kMCTrue
);
return
true
;
}
};
/*
---------- pointer specializations
*/
/*
---------- floating-point numeric specializations
*/
template
<
typename
RealType>
struct
DoExport
<
RealType,
typename
std::enable_if<std::is_floating_point<
typename
RealType::c_type>::value>::type>
{
static
bool
doexport
(MCNumberRef p_boxed_value,
typename
RealType::c_type& r_value)
{
r_value =
typename
RealType::c_type
(
MCNumberFetchAsReal
(p_boxed_value));
return
true
;
}
};
template
<
typename
RealType>
struct
DoImport
<
RealType,
typename
std::enable_if<std::is_floating_point<
typename
RealType::c_type>::value>::type>
{
static
bool
doimport
(
typename
RealType::c_type& p_value, MCNumberRef& r_boxed_value)
{
return
MCNumberCreateWithReal
(p_value, r_boxed_value);
}
};
template
<
typename
RealType>
struct
DoPromote
<
RealType,
typename
std::enable_if<std::is_floating_point<
typename
RealType::c_type>::value>::type>
{
static
void
promote
(
void
*contents)
{
*(
double
*)contents = *(
typename
RealType::c_type *)contents;
}
};
/*
---------- integer numeric specializations
*/
template
<
typename
IntType>
struct
DoExport
<
IntType,
typename
std::enable_if<std::is_integral<
typename
IntType::c_type>::value>::type>
{
static
bool
doexport
(MCNumberRef p_boxed_value,
typename
IntType::c_type& r_value)
{
//
Fetch the number as a double
double
t_value =
MCNumberFetchAsReal
(p_boxed_value);
//
First check that the value is within the contiguous integer range
//
of doubles. If that succeeds, then check it fits within the target
//
integer type.
if
(t_value <
double
(
DBL_INT_MIN
) ||
t_value >
double
(
DBL_INT_MAX
) ||
t_value <
double
(std::numeric_limits<
typename
IntType::c_type>::
min
()) ||
t_value >
double
(std::numeric_limits<
typename
IntType::c_type>::
max
()))
{
return
__throw_numeric_overflow<IntType>(
kMCForeignExportErrorTypeInfo
);
}
r_value =
typename
IntType::c_type
(t_value);
return
true
;
}
};
template
<
typename
IntType>
struct
DoImport
<
IntType,
typename
std::enable_if<std::is_integral<
typename
IntType::c_type>::value &&
std::is_signed<
typename
IntType::c_type>::value>::type>
{
static
bool
doimport
(
typename
IntType::c_type p_value, MCNumberRef& r_boxed_value)
{
if
(p_value >=
INTEGER_MIN
&& p_value <=
INTEGER_MAX
)
{
return
MCNumberCreateWithInteger
(
integer_t
(p_value), r_boxed_value);
}
else
if
(p_value >=
DBL_INT_MIN
&&
p_value <= (
DBL_INT_MAX
))
{
return
MCNumberCreateWithReal
(
double
(p_value), r_boxed_value);
}
else
{
return
__throw_numeric_overflow<IntType>(
kMCForeignImportErrorTypeInfo
);
}
}
};
template
<
typename
UIntType>
struct
DoImport
<
UIntType,
typename
std::enable_if<std::is_integral<
typename
UIntType::c_type>::value &&
std::is_unsigned<
typename
UIntType::c_type>::value>::type>
{
static
bool
doimport
(
typename
UIntType::c_type p_value, MCNumberRef& r_boxed_value)
{
if
(p_value <=
UINTEGER_MAX
)
{
return
MCNumberCreateWithUnsignedInteger
(
uinteger_t
(p_value), r_boxed_value);
}
else
if
(p_value <= (
1ULL
<< std::numeric_limits<
double
>::digits))
{
return
MCNumberCreateWithReal
(
double
(p_value), r_boxed_value);
}
else
{
return
__throw_numeric_overflow<UIntType>(
kMCForeignImportErrorTypeInfo
);
}
}
};
template
<
typename
IntType>
struct
DoPromote
<
IntType,
typename
std::enable_if<std::is_integral<
typename
IntType::c_type>::value &&
std::is_signed<
typename
IntType::c_type>::value>::type>
{
static
void
promote
(
void
*contents)
{
*(
int
*)contents = *(
typename
IntType::c_type *)contents;
}
};
template
<
typename
UIntType>
struct
DoPromote
<
UIntType,
typename
std::enable_if<std::is_integral<
typename
UIntType::c_type>::value &&
std::is_unsigned<
typename
UIntType::c_type>::value>::type>
{
static
void
promote
(
void
*contents)
{
*(
unsigned
int
*)contents = *(
typename
UIntType::c_type *)contents;
}
};
template
<
typename
TypeDesc>
class
DescriptorBuilder
{
public:
static
bool
create
(
const
char
*p_name)
{
MCTypeInfoRef& destination =
TypeDesc::type_info
();
MCForeignPrimitiveType primitive =
TypeDesc::primitive_type;
/*
TODO[C++17] Some of the fields in here are left empty, and
* are filled in by calling setup_optional() and
* setup_bridge(). We could use constexpr if instead (see
* below).
*/
MCForeignTypeDescriptor d = {
sizeof
(
typename
TypeDesc::c_type),
TypeDesc::base_type_info
(),
nullptr
,
/*
bridgetype
*/
&primitive,
/*
layout
*/
1
,
/*
layout_size
*/
nullptr
,
/*
initialize
*/
finalize<TypeDesc>,
nullptr
,
/*
defined
*/
move<TypeDesc>,
copy<TypeDesc>,
equal<TypeDesc>,
hash<TypeDesc>,
nullptr
,
/*
doimport
*/
nullptr
,
/*
doexport
*/
describe<TypeDesc>,
nullptr
,
/*
promotedtype
*/
nullptr
,
/*
promote
*/
};
setup_optional<TypeDesc>(d);
setup_bridge<TypeDesc>(d);
setup_promote<TypeDesc>(d);
MCAutoStringRef t_name_string;
if
(!
MCStringCreateWithCString
(p_name, &t_name_string))
return
false
;
MCNewAutoNameRef t_name_name;
if
(!
MCNameCreate
(*t_name_string, &t_name_name))
return
false
;
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL