-
Notifications
You must be signed in to change notification settings - Fork 56
/
Buffer.d
217 lines (156 loc) · 5.8 KB
/
Buffer.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
/*******************************************************************************
Utility for manipulation of reusable mutable array buffers.
Notes:
* Because of a DMD1 bug, declaring Buffer!(Buffer!(T)) is not possible and
will result in a compile-time error complaining about recursive template
instantiation.
* Buffers of void of any dimension greater than one are disallowed (i.e.
Buffer!(void) is allowed; Buffer!(void[]), Buffer!(void[][]), etc are
not). For > 1d arrays, you should use buffers of ubyte instead. The reason
for this limitation is that any array type can be implicitly cast to
void[], leading to internal ambiguities in the code of Buffer. (A fix for
this problem may possible, but would add a lot of complexity to the code.)
Copyright:
Copyright (c) 2016 dunnhumby Germany GmbH.
All rights reserved.
License:
Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
Alternatively, this file may be distributed under the terms of the Tango
3-Clause BSD License (see LICENSE_BSD.txt for details).
*******************************************************************************/
module ocean.core.Buffer;
import ocean.meta.types.Qualifiers;
import ocean.meta.traits.Basic;
import ocean.meta.types.Arrays;
import ocean.core.buffer.Void;
import ocean.core.buffer.WithIndirections;
import ocean.core.buffer.NoIndirections;
Buffer!(Unqual!(T)) createBuffer ( T ) ( T[] initial... )
{
return Buffer!(Unqual!(T))(initial.dup);
}
struct Buffer ( T )
{
static assert (
!isBasicArrayType!(T) || !is(StripAllArrays!(T) == void),
"Buffer doesn't work as void[][] replacement, try use ubyte[][] instead"
);
/***************************************************************************
Disable postblit constructor in D2 to prevent copying
***************************************************************************/
@disable this(this);
@disable void opAssign (Buffer!(T) other);
/***************************************************************************
Mixin appropriate API based on kind of element type requested
***************************************************************************/
static if (is(T == void))
{
alias ubyte ElementType;
mixin VoidBufferImpl Impl;
}
else static if (is(typeof({ T x; const(T) y; x = y; })))
{
alias T ElementType;
mixin NoIndirectionsBufferImpl Impl;
}
else
{
alias T ElementType;
mixin WithIndirectionsBufferImpl Impl;
}
/***************************************************************************
Add opAssign from currently used implementation to main overload set
so that it won't get shadowed by opAssign automatically generated
from postblit.
***************************************************************************/
alias Impl.opAssign opAssign;
/***************************************************************************
More readable alias for resetting buffer length to 0 while preserving
capacity.
***************************************************************************/
void reset ( )
{
this.length = 0;
}
/***************************************************************************
Returns:
stored data length
***************************************************************************/
size_t length ( ) const
{
return this.data.length;
}
/***************************************************************************
Resizes buffer and allows memory stomping
Params:
new_length = length to resize to
***************************************************************************/
void length ( size_t new_length )
{
assumeSafeAppend(this.data);
this.data.length = new_length;
assumeSafeAppend(this.data);
}
/***************************************************************************
Ensures buffer has enough capacity to hold specified length but does
not modify effective length.
Params:
new_length = length to extend capacity to
***************************************************************************/
void reserve ( size_t new_length )
{
assumeSafeAppend(this.data);
auto old_length = this.data.length;
this.data.length = new_length;
this.data.length = old_length;
assumeSafeAppend(this.data);
}
/***************************************************************************
Exposes owned data as an array slice
Params:
begin = start index, inclusive
end = end index, exclusive
Returns:
requested slice fo stored data
***************************************************************************/
inout(T[]) opSlice ( size_t begin, size_t end ) inout
{
return this.data[begin .. end];
}
/***************************************************************************
Exposes owned data as an array slice
Returns:
requested slice fo stored data
***************************************************************************/
inout(T[]) opSlice ( ) inout
{
return this.data[];
}
}
unittest
{
// test instantiation with various types
{
Buffer!(void) buffer;
}
{
Buffer!(char) buffer;
}
{
static struct S
{
}
Buffer!(S) buffer;
}
{
Buffer!(istring) buffer1;
Buffer!(cstring) buffer2;
Buffer!(mstring) buffer3;
}
{
static class C
{
}
Buffer!(C) buffer;
}
}