SharedPtr.make - multiple declarations
Function SharedPtr.make
Constructs an object of type ElementType
and wraps it in a SharedPtr
using args as the parameter list for the constructor of ElementType
.
auto make(AllocatorType, bool supportGC = platformSupportGC, Args...)
(
auto ref Args args
)
if (!isDynamicArray!ElementType);
The object is constructed as if by the expression emplace!ElementType(_payload, forward!args)
, where payload is an internal pointer to storage suitable to hold an object of type ElementType
.
The storage is typically larger than ElementType
in order to use one allocation for both the control block and the ElementType
object.
Examples
{
SharedPtr!long a = SharedPtr!long .make();
assert(a .get == 0);
SharedPtr!(const long) b = SharedPtr!long .make(2);
assert(b .get == 2);
}
{
static struct Struct{
int i = 7;
this(int i)pure nothrow @safe @nogc{
this .i = i;
}
}
SharedPtr!Struct s1 = SharedPtr!Struct .make();
assert(s1 .get .i == 7);
SharedPtr!Struct s2 = SharedPtr!Struct .make(123);
assert(s2 .get .i == 123);
}
{
static interface Interface{
}
static class Class : Interface{
int i;
this(int i)pure nothrow @safe @nogc{
this .i = i;
}
}
SharedPtr!Interface x = SharedPtr!Class .make(3);
//assert(x.dynTo!Class.get.i == 3);
}
Function SharedPtr.make
Constructs a SharedPtr
with element
as the pointer to the managed object.
auto make(AllocatorType, bool supportGC = platformSupportGC, DeleterType)
(
SharedPtr . ElementReferenceType element,
DeleterType deleter
)
if (isCallable!DeleterType);
Uses the specified deleter
as the deleter. The expression deleter(element)
must be well formed, have well-defined behavior and not throw any exceptions.
The construction of deleter
and of the stored deleter from d must not throw exceptions.
Examples
long deleted = -1;
auto x = SharedPtr!long .make(new long(123), (long* data){
deleted = *data;
});
assert(deleted == -1);
assert(*x == 123);
x = null;
assert(deleted == 123);
Function SharedPtr.make
Constructs an object of array type ElementType
including its array elements and wraps it in a SharedPtr
.
auto make(AllocatorType, bool supportGC = platformSupportGC, Args...)
(
const size_t n,
auto ref Args args
)
if (isDynamicArray!ElementType);
Parameters
n = Array length
args = parameters for constructor for each array element.
The array elements are constructed as if by the expression emplace!ElementType(_payload, args)
, where payload is an internal pointer to storage suitable to hold an object of type ElementType
.
The storage is typically larger than ElementType
in order to use one allocation for both the control block and the each array element.
Examples
auto arr = SharedPtr!(long[]) .make(6, -1);
assert(arr .length == 6);
assert(arr .get .length == 6);
import std .algorithm : all;
assert(arr .get .all!(x => x == -1));
for(int i = 0; i < 6; ++i)
arr .get[i] = i;
assert(arr .get == [0, 1, 2, 3, 4, 5]);