parent
64ceb0330c
commit
1e46109e50
@ -561,3 +561,49 @@ if (isInputRange!Range && hasLvalueElements!Range)
|
||||
NonCopyable[] nonCopyable;
|
||||
initializeAll(nonCopyable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys all elements in the $(D_PARAM range).
|
||||
*
|
||||
* This function has effect only if the element type of $(D_PARAM Range) has
|
||||
* an elaborate destructor, i.e. it is a $(D_PSYMBOL struct) with an explicit
|
||||
* or generated by the compiler destructor.
|
||||
*
|
||||
* Params:
|
||||
* Range = Input range type.
|
||||
* range = Input range.
|
||||
*/
|
||||
void destroyAll(Range)(Range range)
|
||||
if (isInputRange!Range && hasLvalueElements!Range)
|
||||
{
|
||||
static if (hasElaborateDestructor!(ElementType!Range))
|
||||
{
|
||||
foreach (ref e; range)
|
||||
{
|
||||
destroy(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @trusted unittest
|
||||
{
|
||||
static struct WithDtor
|
||||
{
|
||||
private size_t* counter;
|
||||
~this() @nogc nothrow pure
|
||||
{
|
||||
if (this.counter !is null)
|
||||
{
|
||||
++(*this.counter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t counter;
|
||||
WithDtor[2] withDtor = [WithDtor(&counter), WithDtor(&counter)];
|
||||
|
||||
destroyAll(withDtor[]);
|
||||
|
||||
assert(counter == 2);
|
||||
}
|
||||
|
@ -414,28 +414,20 @@ struct Array(T)
|
||||
*/
|
||||
@property void length(size_t len) @trusted
|
||||
{
|
||||
if (len == length)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if (len > length)
|
||||
if (len > length)
|
||||
{
|
||||
reserve(len);
|
||||
initializeAll(this.data[length_ .. len]);
|
||||
}
|
||||
else
|
||||
{
|
||||
static if (hasElaborateDestructor!T)
|
||||
destroyAll(this.data[len .. this.length_]);
|
||||
}
|
||||
if (len != length)
|
||||
{
|
||||
const T* end = this.data + length_ - 1;
|
||||
for (T* e = this.data + len; e != end; ++e)
|
||||
{
|
||||
destroy(*e);
|
||||
}
|
||||
}
|
||||
}
|
||||
length_ = len;
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
@nogc nothrow pure @safe unittest
|
||||
|
@ -341,13 +341,7 @@ if (isPolymorphicType!T)
|
||||
|
||||
package(tanya) void[] finalize(T)(ref T[] p)
|
||||
{
|
||||
static if (hasElaborateDestructor!(typeof(p[0])))
|
||||
{
|
||||
foreach (ref e; p)
|
||||
{
|
||||
destroy(e);
|
||||
}
|
||||
}
|
||||
destroyAll(p);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user