RcPtr.alloc - multiple declarations

Function RcPtr.alloc

Constructs an object of type ElementType and wraps it in a RcPtr using args as the parameter list for the constructor of ElementType.

auto alloc(bool supportGC = platformSupportGC, AllocatorType, Args...) (
  AllocatorType a,
  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.sizeof in order to use one allocation for both the control block and the ElementType object.

Examples

auto a = allocatorObject(Mallocator.instance);
{
    auto a = RcPtr!long.alloc(a);
    assert(a.get == 0);

    auto b = RcPtr!(const long).alloc(a, 2);
    assert(b.get == 2);
}

{
    static struct Struct{
        int i = 7;

        this(int i)pure nothrow @safe @nogc{
            this.i = i;
        }
    }

    auto s1 = RcPtr!Struct.alloc(a);
    assert(s1.get.i == 7);

    auto s2 = RcPtr!Struct.alloc(a, 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;
        }
    }

    RcPtr!Interface x = RcPtr!Class.alloc(a, 3);
    //assert(x.dynTo!Class.get.i == 3);
}

Function RcPtr.alloc

Constructs an object of array type ElementType including its array elements and wraps it in a RcPtr.

auto alloc(bool supportGC = platformSupportGC, AllocatorType, Args...) (
  AllocatorType a,
  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.sizeof * n in order to use one allocation for both the control block and the each array element.

Examples

auto a = allocatorObject(Mallocator.instance);
auto arr = RcPtr!(long[], DestructorType!(typeof(a))).alloc(a, 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]);