-
Notifications
You must be signed in to change notification settings - Fork 22
/
Cpp.d
586 lines (478 loc) · 16.5 KB
/
Cpp.d
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
/*******************************************************************************
Types currently missing from `core.stdcpp` and some additional utilities
Hopefully in the future we can reduce / remove this module.
In the meantime, this is the most pragmatic way to do C++ bindings,
as code in `core.stdcpp` needs to care about cross platform,
cross compiler, cross C++ versions compatibility, but we have a much smaller
target.
The first step in reducing / removing this module would be to import
exceptions / runtime binding for OSX to Druntime.
See_Also:
https://github.com/dlang-cpp-interop
Copyright:
Copyright (c) 2019-2021 BOSAGORA Foundation
All rights reserved.
License:
MIT License. See LICENSE for details.
*******************************************************************************/
module scpd.Cpp;
import agora.crypto.Hash;
import agora.serialization.Serializer;
//import core.stdcpp.exception;
import core.stdcpp.string;
import core.stdcpp.xutility;
import std.meta;
import std.traits;
import vibe.data.json;
public enum CppCtor { Use = 0 }
public alias std_string = basic_string!char;
extern(C++) {
nothrow @nogc pure:
void defaultCtorCPPObject(T) (inout(T)* ptr);
void dtorCPPObject(T) (inout(T)* ptr);
void opAssignCPPObject(T) (inout(T)* lhs, inout(T)* rhs);
void copyCtorCPPObject(T) (inout(T)* ptr, inout(T)* rhs);
int getCPPSizeof(T) () @safe;
std_string sliceToStdString (const(char)* ptr, size_t length);
}
private mixin template CPPBindingMixin (T, bool Copyable = true, bool DefaultConstructable = false)
{
extern(D)
{
import std.traits : Fields, FieldNameTuple;
static if (!DefaultConstructable)
@disable this ();
this (CppCtor use) inout @trusted nothrow @nogc pure
{
defaultCtorCPPObject!T(&this);
}
~this () inout @trusted nothrow @nogc pure
{
dtorCPPObject!T(&this);
}
// Workaround for https://issues.dlang.org/show_bug.cgi?id=22505
static if (Copyable)
this (this) @trusted nothrow @nogc pure
{
T temp = T(this);
copyCtorCPPObject!T(&this, &temp);
}
static if (Copyable)
this (ref return scope inout T rhs) inout @trusted nothrow @nogc pure
{
copyCtorCPPObject!T(&this, &rhs);
}
static if (Copyable)
void opAssign()(auto ref inout(T) rhs) inout @trusted nothrow @nogc pure
{
opAssignCPPObject!T(&this, &rhs);
}
}
}
/// Can't import `core.stdcpp.allocator` because it transitively imports
/// `core.stdcpp.exception`
/// In this case we just need to get the name right for `vector`
extern(C++, (StdNamespace)) extern(C++, class) struct allocator (T) {}
extern(C++, (StdNamespace)) extern(C++, class) struct less (T) {}
extern(C++, `stellar`) extern(C++, class) struct RandHasher (T, Hasher = hash!T) { }
extern(C++, (StdNamespace)) extern(C++, class) struct default_delete (T) {}
// simplistic std::pair bindings
public extern(C++, (StdNamespace)) struct pair (T1, T2)
{
T1 first;
T2 second;
}
// fake std::hash (for mangling)
public extern(C++, (StdNamespace)) struct hash (T) {}
// fake std::equal_to (for mangling)
public extern(C++, (StdNamespace)) struct equal_to (T = void) {}
extern(C++, (StdNamespace)) {
/// Simple binding to `std::shared_ptr`
extern(C++, class) struct shared_ptr (T)
{
static if (is(T == class) || is(T == interface))
private alias TPtr = T;
else
private alias TPtr = T*;
mixin CPPBindingMixin!(shared_ptr!T);
TPtr ptr;
void* _control_block;
alias ptr this;
}
/// Simple binding to `std::unique_ptr`
extern(C++, class) struct unique_ptr (T, Deleter = default_delete!T)
{
static if (is(T == class) || is(T == interface))
private alias TPtr = T;
else
private alias TPtr = T*;
mixin CPPBindingMixin!(unique_ptr, false);
TPtr ptr;
alias ptr this;
}
}
/// C++ support for foreach
extern(C++) private int cpp_set_foreach(T)(void* set, void* ctx, void* cb);
/// std::set.empty() support
nothrow pure @nogc extern(C++) private bool cpp_set_empty(T)(const(void)* set);
/// unordered map assignment support
private nothrow @nogc extern(C++) void cpp_unordered_map_assign (K, V)(
void* map, ref const(K) key, ref const(V) value);
/// unordered map length support
private pure nothrow @nogc @safe extern(C++) size_t cpp_unordered_map_length (K, V)(
const(void)* map);
/// Rudimentary bindings for std::unordered_map
extern(C++, (StdNamespace))
public struct unordered_map (Key, T, Hash = RandHasher!(Key), KeyEqual = equal_to!Key, Allocator = allocator!(pair!(const Key, T)))
{
version (CppRuntime_Clang)
private ulong[40 / ulong.sizeof] _data;
else
private ulong[56 / ulong.sizeof] _data;
mixin CPPBindingMixin!unordered_map;
void opIndexAssign (in T value, in Key key) @trusted @nogc nothrow
{
cpp_unordered_map_assign!(Key, T)(&this, key, value);
}
size_t length () pure nothrow @safe @nogc
{
return cpp_unordered_map_length!(Key, T)(&this);
}
}
unittest
{
auto map = unordered_map!(int, int)(CppCtor.Use);
assert(map.length() == 0);
map[1] = 1;
assert(map.length() == 1);
auto copy = map;
assert(copy.length() == map.length());
auto copy2 = unordered_map!(int, int)(CppCtor.Use);
copy2 = copy;
assert(copy2.length() == map.length());
}
extern(C++, `std`) {
class runtime_error : exception { }
class logic_error : exception { }
/// TODO: Move to druntime
class exception
{
this() nothrow {}
const(char)* what() const nothrow;
}
}
extern(C++, (StdNamespace)) {
/// Binding: Needs to be instantiated on C++ side
shared_ptr!T make_shared(T, Args...)(Args args);
/// Fake bindings for std::set
public extern(C++, class) struct set (Key, Compare = less!Key, Allocator = allocator!Key)
{
version (CppRuntime_Clang)
private ulong[24 / ulong.sizeof] _data;
else
private ulong[48 / ulong.sizeof] _data;
mixin CPPBindingMixin!set;
/// Foreach support
extern(D) public int opApply (scope int delegate(ref const(Key)) dg) const
{
extern(C++) static int wrapper (void* context, ref const(Key) value)
{
auto dg = *cast(typeof(dg)*)context;
return dg(value);
}
return cpp_set_foreach!Key(cast(void*)&this, cast(void*)&dg,
cast(void*)&wrapper);
}
/// Returns: true if the set is empty
extern(D) bool empty () const nothrow pure @nogc
{
return cpp_set_empty!Key(cast(const void*)&this);
}
}
/// Fake bindings for std::map
public extern(C++, class) struct map (Key, Value, Compare = less!Key, Allocator = allocator!(pair!(const Key, Value)))
{
version (CppRuntime_Clang)
private ulong[24 / ulong.sizeof] _data;
else
private ulong[48 / ulong.sizeof] _data;
mixin CPPBindingMixin!map;
}
// only used at compile-time on the C++ side, here for mangling
extern(C++, class) struct ratio (int _Num, int _Den = 1)
{
}
/// Simple bindings to std::chrono
extern(C++, `chrono`)
{
public extern(C++, class) struct duration (_Rep, _Period = ratio!1)
{
_Rep __r;
alias __r this;
}
alias milli = ratio!(1, 1000);
alias milliseconds = duration!(long, milli);
}
/// Simple wrapper around std::function
/// note: pragma(mangle) doesn't currently work on types
align(1) public struct CPPDelegate (Callback)
{
align(1):
shared_ptr!int __ptr_;
ubyte[24] _1;
ubyte[24] _2;
}
static assert(CPPDelegate!SCPCallback.sizeof == 64);
}
/// Type of SCP function callback called by a timer
public alias SCPCallback = extern(C++) void function();
// TODO : MSVC mangling issue w.r.t. the return type
version (Posix)
{
private extern(C++) set!uint* makeTestSet();
unittest
{
auto set = makeTestSet;
assert(!set.empty);
uint[] values;
foreach (val; *set)
values ~= val;
assert(values == [1, 2, 3, 4, 5]);
}
}
/*******************************************************************************
Simple bindings from `std::vector`
Note that this binding is incomplete and possibly incorrect.
There is a druntime version but it's likely buggy and much harder to
reason about because it supports all runtimes:
https://github.com/dlang/druntime/pull/2448
It's very easy to get the memory management wrong, so prefer passing this
by ref and do anything that modifies the memory on the C++ side
(e.g. push_back).
Extra items, like `ConstIterator` and `toString` / `fromString` are for
ease of use (e.g. `to/fromString` actually allows vibe.d to deserialize it)
*******************************************************************************/
extern(C++, (StdNamespace)) extern(C++, class) struct vector (T, Alloc = allocator!T)
{
T* _start;
T* _end;
T* _end_of_storage;
alias ElementType = T;
mixin CPPBindingMixin!(vector, true, true);
extern(D)
{
/// TODO: Separate from `vector` definition
private static struct ConstIterator
{
size_t index;
const(vector!T)* orig;
public ref const(T) front () const pure nothrow @nogc @trusted
{
return (*this.orig)[this.index];
}
public void popFront () pure nothrow @nogc @trusted
{
if (!this.empty)
this.index++;
}
public @property bool empty () const pure nothrow @safe @nogc
{
return !(this.index < this.orig.length);
}
}
public ref inout(T) opIndex(size_t idx) inout pure nothrow @nogc @trusted
{
if (idx >= this.length)
assert(0);
return this._start[idx];
}
public size_t length () const pure nothrow @nogc @safe
{
return this._end - this._start;
}
public size_t capacity () const @safe pure nothrow @nogc
{
return this._end_of_storage - this._start;
}
public ConstIterator constIterator () const pure nothrow @nogc @safe
{
return ConstIterator(0, &this);
}
public inout(T[]) opSlice () inout pure nothrow @nogc @safe
{
return this.opSlice(0, this.length());
}
public inout(T[]) opSlice (size_t start, size_t end) inout pure nothrow @nogc @trusted
{
if (end > this.length())
assert(0);
return this._start[start .. end];
}
public bool opEquals (in vector rhs) const pure nothrow @nogc @safe
{
static assert(__traits(isRef, rhs));
import std.range : zip;
if (this.length != rhs.length)
return false;
// note: cannot do 'return this.innerSets[] == rhs.innerSets[];'
// object.d(358,64): Error: `cast(const(vector))(cast(const(vector)*)r)[i]`
// is not an lvalue and cannot be modified
foreach (idx; 0 .. this.length)
{
if (this[idx] != rhs[idx])
return false;
}
return true;
}
alias opDollar = length;
static if (is(T : ubyte))
{
import vibe.data.serialization : Base64ArrayPolicy;
package alias SerPolicy = Base64ArrayPolicy;
}
else
{
package alias SerPolicy = DefaultPolicy;
}
string toString() const @trusted
{
import std.array : appender, Appender;
auto app = appender!string();
serializeWithPolicy!(JsonStringSerializer!(Appender!string), SerPolicy)(this[], app);
return app.data;
}
static typeof(this) fromString(string src) @safe
{
auto array = src.deserializeWithPolicy!(JsonStringSerializer!string, SerPolicy, T[]);
typeof(this) vec;
vec.reserve(array.length);
foreach (ref item; array)
vec.push_back(item);
return vec;
}
void computeHash (scope HashDg dg) const nothrow @safe @nogc
{
hashPart(this.opSlice(), dg);
}
public void serialize (scope SerializeDg dg) const @safe
{
serializePart(this.length, dg);
foreach (ref entry; this.constIterator())
serializePart(entry, dg);
}
static QT fromBinary (QT) (scope DeserializeDg data,
in DeserializerOptions opts) @safe
{
import scpd.types.Utils;
// Note: Unqual necessary because we can't construct an
// `immutable` vector yet
Unqual!(vector!(Unqual!(QT.ElementType))) ret;
immutable len = deserializeLength(data, opts.maxLength);
ret.reserve(len);
foreach (idx; 0 .. len)
{
auto entry = deserializeFull!(QT.ElementType)(data, opts);
ret.push_back(entry);
}
return () @trusted { return cast(QT) ret; }();
}
static if (isBasicType!T)
{
/// Overload for basic types to not require a `const ref`
public void push_back (T value) inout @trusted pure nothrow @nogc
{
import Utils = scpd.types.Utils;
Utils.push_back(this, value);
}
}
public void push_back (ref T value) inout @trusted pure nothrow @nogc
{
import Utils = scpd.types.Utils;
import scpd.types.XDRBase;
// Workaround for Dlang issue #20805
static if (is(T == xvector!ubyte))
{
version (Windows)
Utils.push_back_vec(&this, &value);
else
Utils.push_back(this, value);
}
else
{
Utils.push_back(this, value);
}
}
}
/// Set vector capacity
public void reserve (size_t new_cap) inout @trusted pure nothrow @nogc;
}
unittest
{
import std.algorithm : each;
import std.conv : to;
import std.range : iota;
vector!ubyte vec_ubyte;
vec_ubyte.reserve(1337);
assert(vec_ubyte.capacity() >= 1337);
iota(5).each!((num) {ubyte b = cast(ubyte)num; vec_ubyte.push_back(b);});
auto serialized = vec_ubyte.toString();
assert(serialized == `"AAECAwQ="`, "actual serialized: " ~ serialized);
vector!ubyte deserialized = vec_ubyte.fromString(serialized);
assert(deserialized[] == [0, 1, 2, 3, 4]);
vector!ubyte vec_copy = deserialized;
assert(vec_copy._start != deserialized._start);
}
unittest
{
import scpd.types.Utils;
vector!ubyte vec;
assert(vec.length == 0);
assert(vec[] == []);
ubyte x = 1;
vec.push_back(x);
x = 2;
vec.push_back(x);
x = 3;
vec.push_back(x);
assert(vec.length == 3);
assert(vec[] == [1, 2, 3]);
assert(vec[0 .. $] == [1, 2, 3]);
assert(vec[0..2] == [1, 2]);
assert(vec[1..3] == [2, 3]);
vector!ubyte vec2;
assert(vec2 != vec);
x = 1;
vec2.push_back(x);
x = 2;
vec2.push_back(x);
x = 3;
vec2.push_back(x);
assert(vec2 == vec);
vector!ubyte vec3;
vec3.push_back(x);
x = 2;
vec3.push_back(x);
x = 3;
vec3.push_back(x);
assert(vec3 != vec);
}
unittest
{
checkFromBinary!(vector!ubyte);
}
/// Invoke an std::function pointer (note: must be void* due to mangling issues)
extern(C++) void callCPPDelegate (void* cb);
public mixin template NonMovableOrCopyable ()
{
@disable this ();
@disable this (this);
@disable ref typeof (this) opAssign () (auto ref typeof(this) rhs);
}
unittest
{
assert(unordered_map!(int,int).sizeof == getCPPSizeof!(unordered_map!(int,int))());
assert(shared_ptr!int.sizeof == getCPPSizeof!(shared_ptr!int)());
assert(set!int.sizeof == getCPPSizeof!(set!int)());
assert(map!(int,int).sizeof == getCPPSizeof!(map!(int,int))());
assert(vector!ubyte.sizeof == getCPPSizeof!(vector!ubyte)());
}