Casts and @trusted
Anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Jun 14 03:45:21 PDT 2014
That really is it. The other methods are just other gets to the
buffer, like this:
T[] get_dup(TS strat=TS.cyclic)(size_t n) const {
static if (strat==TS.once) size_t numreads = fixNToFill(n);
else size_t numreads = n;
auto ret = new T[](numreads);
size_t read = index;
foreach(i;0..numreads) {
ret[i] = _buffer[read].dup;
if (read == 0) read = fill-1;
else --read;
}
return ret;
}
But ah, the .ptr property is not supposed to work for array
element? In any case it still works as cast(T*) &(_buffer[read]).
Here's the unittest I slapped together:
unittest {
char[5] hello = "hello";
char[5] world = "world";
char[5] forty = "forty";
char[5] three = "three";
char[5] gdbye = "gdbye";
alias chars_t = char[5];
chars_t[] foo = [hello,world,forty,three];
chars_t[] oob = foo.dup.reverse;
StaticRingBuffer!(chars_t,4) bar;
bar.put(foo);
assert(bar.index==3);
const(chars_t)*[4] ptrs;
bar.get_ref(4,ptrs);
assert(bar.get_dup(4) == oob);
foreach(i,ptr; ptrs) assert(*ptr == oob[i]);
assert(bar.get_dup(7) ==
[three,forty,world,hello,three,forty,world]);
bar.put("gdbye");
assert(bar.index==0);
assert(bar.get_dup(4) == [gdbye,three,forty,world]);
assert(bar.get_dup(7) ==
[gdbye,three,forty,world,gdbye,three,forty]);
}
But now I see my problem is that a simple const(T*)[N] already
initializes its elements to null, so nothing past that should
modify them. And without the casts, the type on &(_buffer[read])
is a const(T*) instead of something else which finally makes
sense. Somehow I thought .ptr was a property of everything.
And now I realize that I can just put the ptr array within the
get scope, and return it, to initialize a lhs const(T*)[] by
value. I was making things hard on myself by trying to modify a
passed-in buffer.
More information about the Digitalmars-d-learn
mailing list