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

Commit a47e916

Browse files
committed
Tweaked the stuff some more...
1 parent 61e7729 commit a47e916

File tree

2 files changed

+80
-50
lines changed

2 files changed

+80
-50
lines changed

src/core/stdcpp/string.d

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,49 +63,49 @@ extern(C++, class) struct basic_string(T, Traits = char_traits!T, Alloc = alloca
6363
this(const(T)* ptr, ref const(allocator_type) al = defaultAlloc);
6464
extern(D) this(const(T)[] dstr) { this(dstr.ptr, dstr.length); }
6565
extern(D) this(const(T)[] dstr, ref const(allocator_type) al = defaultAlloc) { this(dstr.ptr, dstr.length); }
66-
~this();
66+
~this() nothrow;
6767

6868
ref basic_string opAssign(ref const(basic_string) s);
6969

7070
// Iterators
71-
iterator begin() nothrow;
72-
const_iterator begin() const nothrow;
73-
const_iterator cbegin() const nothrow;
71+
iterator begin() nothrow @trusted @nogc;
72+
const_iterator begin() const nothrow @trusted @nogc;
73+
const_iterator cbegin() const nothrow @trusted @nogc;
7474

75-
iterator end() nothrow;
76-
const_iterator end() const nothrow;
77-
const_iterator cend() const nothrow;
75+
iterator end() nothrow @trusted @nogc;
76+
const_iterator end() const nothrow @trusted @nogc;
77+
const_iterator cend() const nothrow @trusted @nogc;
7878

7979
// no reverse iterator for now.
8080

8181
// Capacity
82-
size_type size() const nothrow;
83-
size_type length() const nothrow;
84-
size_type max_size() const nothrow;
85-
size_type capacity() const nothrow;
82+
size_type size() const nothrow @trusted @nogc;
83+
size_type length() const nothrow @trusted @nogc;
84+
size_type max_size() const nothrow @trusted @nogc;
85+
size_type capacity() const nothrow @trusted @nogc;
8686

87-
bool empty() const nothrow;
87+
bool empty() const nothrow @trusted @nogc;
8888

8989
void clear() nothrow;
9090
void resize(size_type n);
9191
void resize(size_type n, T c);
92-
void reserve(size_type n = 0);
92+
void reserve(size_type n = 0) @trusted @nogc;
9393
void shrink_to_fit();
9494

9595
// Element access
96-
ref T opIndex(size_type i);
97-
ref const(T) opIndex(size_type i) const;
98-
ref T at(size_type i);
99-
ref const(T) at(size_type i) const;
96+
ref T opIndex(size_type i) @trusted @nogc;
97+
ref const(T) opIndex(size_type i) const @trusted @nogc;
98+
ref T at(size_type i) @trusted @nogc;
99+
ref const(T) at(size_type i) const @trusted @nogc;
100100

101-
ref T back();
102-
ref const(T) back() const;
103-
ref T front();
104-
ref const(T) front() const;
101+
ref T back() @trusted @nogc;
102+
ref const(T) back() const @trusted @nogc;
103+
ref T front() @trusted @nogc;
104+
ref const(T) front() const @trusted @nogc;
105105

106-
const(T)* c_str() const nothrow;
107-
T* data() nothrow;
108-
const(T)* data() const nothrow;
106+
const(T)* c_str() const nothrow @trusted @nogc;
107+
T* data() nothrow @trusted @nogc;
108+
const(T)* data() const nothrow @trusted @nogc;
109109

110110
// Modifiers
111111
ref basic_string opOpAssign(string op : "+")(ref const(basic_string) s);
@@ -189,8 +189,22 @@ extern(C++, class) struct basic_string(T, Traits = char_traits!T, Alloc = alloca
189189

190190
// D helpers
191191
alias as_array this;
192-
extern(D) T[] as_array() { return data()[0 .. size()]; }
193-
extern(D) const(T)[] as_array() const { return data()[0 .. size()]; }
192+
extern(D) T[] as_array() nothrow @safe @nogc { return this[]; }
193+
extern(D) const(T)[] as_array() const nothrow @safe @nogc { return this[]; }
194+
195+
extern(D) T[] opSlice() nothrow @safe @nogc { return data()[0 .. size()]; }
196+
extern(D) const(T)[] opSlice() const nothrow @safe @nogc { return data()[0 .. size()]; }
197+
extern(D) T[] opSlice(size_type start, size_type end) @safe { assert(start <= end && end <= size(), "Index out of bounds"); return data()[start .. end]; }
198+
extern(D) const(T)[] opSlice(size_type start, size_type end) const @safe { assert(start <= end && end <= size(), "Index out of bounds"); return data()[start .. end]; }
199+
extern(D) size_type opDollar(size_t pos)() const nothrow @safe @nogc { static assert(pos == 0, "std::vector is one-dimensional"); return size(); }
200+
201+
// support all the assignment variants
202+
extern(D) void opSliceAssign(T value) { opSlice()[] = value; }
203+
extern(D) void opSliceAssign(T value, size_type i, size_type j) { opSlice(i, j)[] = value; }
204+
extern(D) void opSliceUnary(string op)() if (op == "++" || op == "--") { mixin(op ~ "opSlice()[];"); }
205+
extern(D) void opSliceUnary(string op)(size_type i, size_type j) if (op == "++" || op == "--") { mixin(op ~ "opSlice(i, j)[];"); }
206+
extern(D) void opSliceOpAssign(string op)(T value) { mixin("opSlice()[] " ~ op ~ "= value;"); }
207+
extern(D) void opSliceOpAssign(string op)(T value, size_type i, size_type j) { mixin("opSlice(i, j)[] " ~ op ~ "= value;"); }
194208

195209
private:
196210
void[8] _ = void; // to match sizeof(std::string) and pad the object correctly.

src/core/stdcpp/vector.d

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ module core.stdcpp.vector;
2121
// - missing noexcept
2222
// - iterators are implemented as pointers
2323
// - no reverse_iterator nor rbegin/rend
24+
// - nothrow @trusted @nogc for most functions depend on knowledge
25+
// of T's construction/destruction/assignment semantics
2426
///////////////////////////////////////////////////////////////////////////////
2527

2628
import core.stdcpp.allocator;
@@ -60,41 +62,41 @@ extern(C++, class) struct vector(T, Alloc = allocator!T)
6062
ref vector opAssign(ref const(vector) s);
6163

6264
// Iterators
63-
iterator begin();
64-
const_iterator begin() const;
65-
const_iterator cbegin() const;
66-
iterator end();
67-
const_iterator end() const;
68-
const_iterator cend() const;
65+
iterator begin() @trusted @nogc;
66+
const_iterator begin() const @trusted @nogc;
67+
const_iterator cbegin() const @trusted @nogc;
68+
iterator end() @trusted @nogc;
69+
const_iterator end() const @trusted @nogc;
70+
const_iterator cend() const @trusted @nogc;
6971

7072
// no reverse iterator for now.
7173

7274
// Capacity
73-
size_type size() const;
74-
size_type max_size() const;
75-
size_type capacity() const;
75+
size_type size() const nothrow @trusted @nogc;
76+
size_type max_size() const nothrow @trusted @nogc;
77+
size_type capacity() const nothrow @trusted @nogc;
7678

77-
bool empty() const;
79+
bool empty() const nothrow @trusted @nogc;
7880

79-
void clear();
81+
void clear() nothrow;
8082
void resize(size_type n);
8183
void resize(size_type n, T c);
82-
void reserve(size_type n = 0);
84+
void reserve(size_type n = 0) @trusted @nogc;
8385
void shrink_to_fit();
8486

8587
// Element access
86-
T* data() nothrow;
87-
const(T)* data() const nothrow;
88+
T* data() nothrow @trusted @nogc;
89+
const(T)* data() const nothrow @trusted @nogc;
8890

89-
ref T opIndex(size_type i);
90-
ref const(T) opIndex(size_type i) const;
91-
ref T at(size_type i);
92-
ref const(T) at(size_type i) const;
91+
ref T opIndex(size_type i) @trusted @nogc;
92+
ref const(T) opIndex(size_type i) const @trusted @nogc;
93+
ref T at(size_type i) @trusted @nogc;
94+
ref const(T) at(size_type i) const @trusted @nogc;
9395

94-
ref T back();
95-
ref const(T) back() const;
96-
ref T front();
97-
ref const(T) front() const;
96+
ref T back() @trusted @nogc;
97+
ref const(T) back() const @trusted @nogc;
98+
ref T front() @trusted @nogc;
99+
ref const(T) front() const @trusted @nogc;
98100

99101
// Modifiers
100102
void push_back(ref const(T) _);
@@ -104,8 +106,22 @@ extern(C++, class) struct vector(T, Alloc = allocator!T)
104106

105107
// D helpers
106108
alias as_array this;
107-
extern(D) T[] as_array() { return data()[0 .. size()]; }
108-
extern(D) const(T)[] as_array() const { return data()[0 .. size()]; }
109+
extern(D) T[] as_array() nothrow @safe @nogc { return this[]; }
110+
extern(D) const(T)[] as_array() const nothrow @safe @nogc { return this[]; }
111+
112+
extern(D) T[] opSlice() nothrow @safe @nogc { return data()[0 .. size()]; }
113+
extern(D) const(T)[] opSlice() const nothrow @safe @nogc { return data()[0 .. size()]; }
114+
extern(D) T[] opSlice(size_type start, size_type end) @safe { assert(start <= end && end <= size(), "Index out of bounds"); return data()[start .. end]; }
115+
extern(D) const(T)[] opSlice(size_type start, size_type end) const @safe { assert(start <= end && end <= size(), "Index out of bounds"); return data()[start .. end]; }
116+
extern(D) size_type opDollar(size_t pos)() const nothrow @safe @nogc { static assert(pos == 0, "std::vector is one-dimensional"); return size(); }
117+
118+
// support all the assignment variants
119+
extern(D) void opSliceAssign(T value) { opSlice()[] = value; }
120+
extern(D) void opSliceAssign(T value, size_type i, size_type j) { opSlice(i, j)[] = value; }
121+
extern(D) void opSliceUnary(string op)() if (op == "++" || op == "--") { mixin(op ~ "opSlice()[];"); }
122+
extern(D) void opSliceUnary(string op)(size_type i, size_type j) if (op == "++" || op == "--") { mixin(op ~ "opSlice(i, j)[];"); }
123+
extern(D) void opSliceOpAssign(string op)(T value) { mixin("opSlice()[] " ~ op ~ "= value;"); }
124+
extern(D) void opSliceOpAssign(string op)(T value, size_type i, size_type j) { mixin("opSlice(i, j)[] " ~ op ~ "= value;"); }
109125

110126
private:
111127
void[24] _ = void; // to match sizeof(std::vector) and pad the object correctly.

0 commit comments

Comments
 (0)