stdx.allocator.internal

  • Declaration

    pure nothrow @safe T* emplace(T)(T* chunk);

    Given a pointer to uninitialized memory (but already typed as ), constructs an object of non- type at that address. If T is a class, initializes the class reference to null.

    Return Value

    A pointer to the newly constructed object (which is the same as ).

    Examples

    1. static struct S { int i = 42; } S[2] s2 = void; emplace(&s2); assert(s2[0].i == 42 && s2[1].i == 42);

    Examples

    1. interface I {} class K : I {} K k = void; emplace(&k); assert(k is null); I i = void; emplace(&i); assert(i is null);

  • Declaration

    T* emplace(T, Args...)(T* chunk, auto ref Args args) if (is(T == struct) || Args.length == 1);

    Given a pointer to uninitialized memory (but already typed as a non-class type ), constructs an object of type at that address from arguments . If T is a class, initializes the class reference to args[0].

    Discussion

    This function can be if the corresponding constructor of is .

    Return Value

    A pointer to the newly constructed object (which is the same as ).

    Examples

    1. int a; int b = 42; assert(*emplace!int(&a, b) == 42);

  • Declaration

    T emplace(T, Args...)(T chunk, auto ref Args args) if (is(T == class));

    Given a raw memory area chunk (but already typed as a class type T), constructs an object of class type T at that address. The constructor is passed the arguments Args.

    Discussion

    If T is an inner class whose outer field can be used to access an instance of the enclosing class, then Args must not be empty, and the first member of it must be a valid initializer for that outer field. Correct initialization of this field is essential to access members of the outer class inside T methods.

    Note: This function is @safe if the corresponding constructor of T is @safe.

    Return Value

    The newly constructed object.

    Examples

    1. () @safe { class SafeClass { int x; @safe this(int x) { this.x = x; } } auto buf = new void[__traits(classInstanceSize, SafeClass)]; auto support = (() @trusted => cast(SafeClass)(buf.ptr))(); auto safeClass = emplace!SafeClass(support, 5); assert(safeClass.x == 5); class UnsafeClass { int x; @system this(int x) { this.x = x; } } auto buf2 = new void[__traits(classInstanceSize, UnsafeClass)]; auto support2 = (() @trusted => cast(UnsafeClass)(buf2.ptr))(); static assert(!__traits(compiles, emplace!UnsafeClass(support2, 5))); static assert(!__traits(compiles, emplace!UnsafeClass(buf2, 5))); }();

  • Declaration

    T emplace(T, Args...)(void[] chunk, auto ref Args args) if (is(T == class));

    Given a raw memory area chunk, constructs an object of class type T at that address. The constructor is passed the arguments Args.

    Discussion

    If T is an inner class whose outer field can be used to access an instance of the enclosing class, then Args must not be empty, and the first member of it must be a valid initializer for that outer field. Correct initialization of this field is essential to access members of the outer class inside T methods.

    Preconditions: chunk must be at least as large as T needs and should have an alignment multiple of T's alignment. (The size of a class instance is obtained by using ).

    Note: This function can be @trusted if the corresponding constructor of T is @safe.

    Return Value

    The newly constructed object.

    Examples

    1. static class C { int i; this(int i){this.i = i;} } auto buf = new void[__traits(classInstanceSize, C)]; auto c = emplace!C(buf, 5); assert(c.i == 5);

  • Declaration

    T* emplace(T, Args...)(void[] chunk, auto ref Args args) if (!is(T == class));

    Given a raw memory area , constructs an object of non- type at that address. The constructor is passed the arguments , if any.

    Preconditions: must be at least as large as needs and should have an alignment multiple of 's alignment.

    Note: This function can be if the corresponding constructor of is .

    Return Value

    A pointer to the newly constructed object.

    Examples

    1. struct S { int a, b; } auto buf = new void[S.sizeof]; S s; s.a = 42; s.b = 43; auto s1 = emplace!S(buf, s); assert(s1.a == 42 && s1.b == 43);

  • Declaration

    pure nothrow @nogc @safe bool isPowerOf2(X)(const X x) if (isNumeric!X);

    Check whether a number is an integer power of two.

    Discussion

    Note that only positive numbers can be integer powers of two. This function always return false if x is negative or zero.

    Parameters

    X x

    the number to test

    Return Value

    true if x is an integer power of two.

    Examples

    1. import std.math : pow; assert( isPowerOf2(1.0L)); assert( isPowerOf2(2.0L)); assert( isPowerOf2(0.5L)); assert( isPowerOf2(pow(2.0L, 96))); assert( isPowerOf2(pow(2.0L, -77))); assert(!isPowerOf2(-2.0L)); assert(!isPowerOf2(-0.5L)); assert(!isPowerOf2(0.0L)); assert(!isPowerOf2(4.315)); assert(!isPowerOf2(1.0L / 3.0L)); assert(!isPowerOf2(real.nan)); assert(!isPowerOf2(real.infinity));

    Examples

    1. assert( isPowerOf2(1)); assert( isPowerOf2(2)); assert( isPowerOf2(1uL << 63)); assert(!isPowerOf2(-4)); assert(!isPowerOf2(0)); assert(!isPowerOf2(1337u));

  • Declaration

    template isInnerClass(T) if (is(T == class))

    Determines whether T is a class nested inside another class and that T.outer is the implicit reference to the outer class (i.e. outer has not been used as a field or method name)

    Parameters

    T

    type to test

    Return Value

    true if T is a class nested inside another, with the conditions described above; false otherwise

    Examples

    1. class C { int outer; } static assert(!isInnerClass!C); class Outer1 { class Inner1 { } class Inner2 { int outer; } } static assert(isInnerClass!(Outer1.Inner1)); static assert(!isInnerClass!(Outer1.Inner2)); static class Outer2 { static class Inner { int outer; } } static assert(!isInnerClass!(Outer2.Inner));