Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 10c9bfb

Browse files
committed
string, vector, array working on MSVC.
1 parent b1d7ab4 commit 10c9bfb

File tree

8 files changed

+476
-147
lines changed

8 files changed

+476
-147
lines changed

src/core/stdcpp/allocator.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
module core.stdcpp.allocator;
1414

15+
alias allocator = std.allocator;
16+
1517
extern(C++, std):
1618

1719
/**

src/core/stdcpp/array.d

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* D header file for interaction with C++ std::array.
3+
*
4+
* Copyright: Manu Evans 2014 - 2018.
5+
* License: Distributed under the
6+
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
7+
* (See accompanying file LICENSE)
8+
* Authors: Manu Evans
9+
* Source: $(DRUNTIMESRC core/stdcpp/array.d)
10+
*/
11+
12+
module core.stdcpp.array;
13+
14+
///////////////////////////////////////////////////////////////////////////////
15+
// std::array declaration.
16+
//
17+
// - no iterators
18+
///////////////////////////////////////////////////////////////////////////////
19+
20+
import stdcpp.allocator;
21+
22+
alias array = std.array;
23+
24+
extern(C++, std):
25+
26+
extern(C++, class) struct array(T, size_t N)
27+
{
28+
alias size_type = size_t;
29+
alias difference_type = ptrdiff_t;
30+
alias value_type = T;
31+
alias reference = ref T;
32+
alias const_reference = ref const(T);
33+
alias pointer = T*;
34+
alias const_pointer = const(T)*;
35+
// alias iterator = pointer;
36+
// alias const_iterator = const_pointer;
37+
// alias reverse_iterator
38+
// alias const_reverse_iterator
39+
40+
// Iterators
41+
// iterator begin() @trusted @nogc;
42+
// const_iterator begin() const @trusted @nogc;
43+
// const_iterator cbegin() const @trusted @nogc;
44+
// iterator end() @trusted @nogc;
45+
// const_iterator end() const @trusted @nogc;
46+
// const_iterator cend() const @trusted @nogc;
47+
48+
// no reverse iterator for now.
49+
50+
alias as_array this;
51+
52+
extern(D) size_type size() const nothrow @safe @nogc { return N; }
53+
extern(D) size_type max_size() const nothrow @safe @nogc { return N; }
54+
extern(D) bool empty() const nothrow @safe @nogc { return N > 0; }
55+
56+
// Element access
57+
extern(D) reference front() @safe @nogc { return as_array()[0]; }
58+
extern(D) const_reference front() const @safe @nogc { return as_array()[0]; }
59+
extern(D) reference back() @safe @nogc { return as_array()[N == 0 ? 0 : N-1]; }
60+
extern(D) const_reference back() const @safe @nogc { return as_array()[N == 0 ? 0 : N-1]; }
61+
62+
extern(D) void fill(ref const(T) value) @safe @nogc { foreach (ref T v; as_array()) v = value; }
63+
64+
// D helpers
65+
extern(D) T[] opSlice() nothrow @safe @nogc { return as_array(); }
66+
extern(D) const(T)[] opSlice() const nothrow @safe @nogc { return as_array(); }
67+
extern(D) T[] opSlice(size_type start, size_type end) @safe { assert(start <= end && end <= N, "Index out of bounds"); return as_array()[start .. end]; }
68+
extern(D) const(T)[] opSlice(size_type start, size_type end) const @safe { assert(start <= end && end <= N, "Index out of bounds"); return as_array()[start .. end]; }
69+
extern(D) size_type opDollar(size_t pos)() const nothrow @safe @nogc { static assert(pos == 0, "std::vector is one-dimensional"); return N; }
70+
71+
// support all the assignment variants
72+
extern(D) void opSliceAssign(T value) { opSlice()[] = value; }
73+
extern(D) void opSliceAssign(T value, size_type i, size_type j) { opSlice(i, j)[] = value; }
74+
extern(D) void opSliceUnary(string op)() if (op == "++" || op == "--") { mixin(op ~ "opSlice()[];"); }
75+
extern(D) void opSliceUnary(string op)(size_type i, size_type j) if (op == "++" || op == "--") { mixin(op ~ "opSlice(i, j)[];"); }
76+
extern(D) void opSliceOpAssign(string op)(T value) { mixin("opSlice()[] " ~ op ~ "= value;"); }
77+
extern(D) void opSliceOpAssign(string op)(T value, size_type i, size_type j) { mixin("opSlice(i, j)[] " ~ op ~ "= value;"); }
78+
79+
private:
80+
version(CRuntime_Microsoft)
81+
{
82+
import core.stdcpp.utility : _Xout_of_range;
83+
84+
T[N ? N : 1] _Elems;
85+
86+
void _Xran() const @trusted @nogc { _Xout_of_range("invalid array<T, N> subscript"); }
87+
88+
public:
89+
// perf will be greatly improved by inlining the primitive access functions
90+
extern(D) T* data() nothrow @safe @nogc { return &_Elems[0]; }
91+
extern(D) const(T)* data() const nothrow @safe @nogc { return &_Elems[0]; }
92+
93+
extern(D) ref T opIndex(size_type i) nothrow @safe @nogc { return _Elems[0 .. N][i]; }
94+
extern(D) ref const(T) opIndex(size_type i) const nothrow @safe @nogc { return _Elems[0 .. N][i]; }
95+
extern(D) ref T at(size_type i) nothrow @trusted @nogc { static if (N > 0) { if (N <= i) _Xran(); return _Elems.ptr[i]; } else { _Xran(); } }
96+
extern(D) ref const(T) at(size_type i) const nothrow @trusted @nogc { static if (N > 0) { if (N <= i) _Xran(); return _Elems.ptr[i]; } else { _Xran(); } }
97+
98+
extern(D) T[] as_array() nothrow @safe @nogc { return _Elems[0 .. N]; }
99+
extern(D) const(T)[] as_array() const nothrow @safe @nogc { return _Elems[0 .. N]; }
100+
}
101+
else version(CRuntime_Glibc)
102+
{
103+
static if (N > 0)
104+
{
105+
T[N] _M_elems;
106+
}
107+
else
108+
{
109+
struct _Placeholder {}
110+
_Placeholder _M_placeholder;
111+
}
112+
113+
public:
114+
import core.exception : RangeError;
115+
116+
// perf will be greatly improved by inlining the primitive access functions
117+
extern(D) T* data() nothrow @safe @nogc { static if (N > 0) { return &_M_elems[0]; } else { return null; } }
118+
extern(D) const(T)* data() const nothrow @safe @nogc { static if (N > 0) { return &_M_elems[0]; } else { return null; } }
119+
120+
extern(D) ref T opIndex(size_type i) nothrow @nogc { static if (N > 0) { return _M_elems[i]; } else { return (cast(T[])null)[i]; } }
121+
extern(D) ref const(T) opIndex(size_type i) const nothrow @nogc { static if (N > 0) { return _M_elems[i]; } else { return (cast(T[])null)[i]; } }
122+
extern(D) ref T at(size_type i) @trusted { if (i >= N) throw new RangeError("Index out of range"); return _M_elems.ptr[i]; }
123+
extern(D) ref const(T) at(size_type i) const @trusted { if (i >= N) throw new RangeError("Index out of range"); return _M_elems.ptr[i]; }
124+
125+
alias as_array this;
126+
extern(D) T[] as_array() nothrow @safe @nogc { static if (N > 0) { return _M_elems[]; } else { return null; } }
127+
extern(D) const(T)[] as_array() const nothrow @safe @nogc { static if (N > 0) { return _M_elems[]; } else { return null; } }
128+
}
129+
else
130+
{
131+
static assert(false, "C++ runtime not supported");
132+
}
133+
}

src/core/stdcpp/exception.d

Lines changed: 76 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -11,95 +11,96 @@
1111

1212
module core.stdcpp.exception;
1313

14+
alias exception = std.exception;
15+
alias bad_exception = std.bad_exception;
16+
alias bad_alloc = std.bad_alloc;
17+
18+
extern (C++, std):
19+
1420
version (CRuntime_DigitalMars)
1521
{
1622
import core.stdcpp.typeinfo;
1723

18-
extern (C++, std)
24+
alias void function() unexpected_handler;
25+
unexpected_handler set_unexpected(unexpected_handler f) nothrow;
26+
void unexpected();
27+
28+
alias void function() terminate_handler;
29+
terminate_handler set_terminate(terminate_handler f) nothrow;
30+
void terminate();
31+
32+
bool uncaught_exception();
33+
34+
class exception
1935
{
20-
alias void function() unexpected_handler;
21-
unexpected_handler set_unexpected(unexpected_handler f) nothrow;
22-
void unexpected();
23-
24-
alias void function() terminate_handler;
25-
terminate_handler set_terminate(terminate_handler f) nothrow;
26-
void terminate();
27-
28-
bool uncaught_exception();
29-
30-
class exception
31-
{
32-
this() nothrow { }
33-
this(const exception) nothrow { }
34-
//exception operator=(const exception) nothrow { return this; }
35-
//virtual ~this() nothrow;
36-
void dtor() { }
37-
const(char)* what() const nothrow;
38-
}
39-
40-
class bad_exception : exception
41-
{
42-
this() nothrow { }
43-
this(const bad_exception) nothrow { }
44-
//bad_exception operator=(const bad_exception) nothrow { return this; }
45-
//virtual ~this() nothrow;
46-
override const(char)* what() const nothrow;
47-
}
36+
this() nothrow { }
37+
this(const exception) nothrow { }
38+
//exception operator=(const exception) nothrow { return this; }
39+
//virtual ~this() nothrow;
40+
void dtor() { }
41+
const(char)* what() const nothrow;
42+
}
43+
44+
class bad_exception : exception
45+
{
46+
this() nothrow { }
47+
this(const bad_exception) nothrow { }
48+
//bad_exception operator=(const bad_exception) nothrow { return this; }
49+
//virtual ~this() nothrow;
50+
override const(char)* what() const nothrow;
4851
}
4952
}
5053
else version (CRuntime_Glibc)
5154
{
52-
extern (C++, std)
55+
alias void function() unexpected_handler;
56+
unexpected_handler set_unexpected(unexpected_handler f) nothrow;
57+
void unexpected();
58+
59+
alias void function() terminate_handler;
60+
terminate_handler set_terminate(terminate_handler f) nothrow;
61+
void terminate();
62+
63+
pure bool uncaught_exception();
64+
65+
class exception
5366
{
54-
alias void function() unexpected_handler;
55-
unexpected_handler set_unexpected(unexpected_handler f) nothrow;
56-
void unexpected();
57-
58-
alias void function() terminate_handler;
59-
terminate_handler set_terminate(terminate_handler f) nothrow;
60-
void terminate();
61-
62-
pure bool uncaught_exception();
63-
64-
class exception
65-
{
66-
this();
67-
//virtual ~this();
68-
void dtor1();
69-
void dtor2();
70-
const(char)* what() const;
71-
}
72-
73-
class bad_exception : exception
74-
{
75-
this();
76-
//virtual ~this();
77-
override const(char)* what() const;
78-
}
67+
this();
68+
//virtual ~this();
69+
void dtor1();
70+
void dtor2();
71+
const(char)* what() const;
72+
}
73+
74+
class bad_exception : exception
75+
{
76+
this();
77+
//virtual ~this();
78+
override const(char)* what() const;
7979
}
8080
}
8181
else version (CRuntime_Microsoft)
8282
{
83-
extern (C++, std)
83+
class exception
84+
{
85+
this(const(char)* _Message = "unknown", int x = 1) { _Ptr = _Message; }
86+
this(ref const(exception) _Right) { _Ptr = _Right._Ptr; }
87+
~this() {}
88+
89+
const(char)* what() const { return _Ptr ? _Ptr : "unknown exception"; }
90+
91+
private:
92+
const(char)* _Ptr;
93+
}
94+
95+
class bad_exception : exception
96+
{
97+
this(const(char)* _Message = "bad exception") { super(_Message); }
98+
~this() {}
99+
}
100+
101+
class bad_alloc : exception
84102
{
85-
class exception
86-
{
87-
this();
88-
this(const exception);
89-
//exception operator=(const exception) { return this; }
90-
//virtual ~this();
91-
void dtor() { }
92-
const(char)* what() const;
93-
94-
private:
95-
const(char)* mywhat;
96-
bool dofree;
97-
}
98-
99-
class bad_exception : exception
100-
{
101-
this(const(char)* msg = "bad exception");
102-
//virtual ~this();
103-
}
103+
this() { super("bad allocation", 1); }
104+
~this() {}
104105
}
105106
}

0 commit comments

Comments
 (0)