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
Tis a class, initializes the class reference tonull.Return Value
A pointer to the newly constructed object (which is the same as ).
Examples
static struct S { int i = 42; } S[2] s2 = void; emplace(&s2); assert(s2[0].i == 42 && s2[1].i == 42);
Examples
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 Argsargs) 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
Tis 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
int a; int b = 42; assert(*emplace!int(&a, b) == 42);
-
Declaration
Templace(T, Args...)(Tchunk, auto ref Argsargs) if (is(T == class));Given a raw memory area
(but already typed as a class typechunkT), constructs an object ofclasstypeTat that address. The constructor is passed the argumentsArgs.Discussion
If
Tis an inner class whoseouterfield can be used to access an instance of the enclosing class, thenArgsmust not be empty, and the first member of it must be a valid initializer for thatouterfield. Correct initialization of this field is essential to access members of the outer class insideTmethods.Note: This function is
@safeif the corresponding constructor ofTis@safe.Return Value
The newly constructed object.
Examples
() @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
Templace(T, Args...)(void[]chunk, auto ref Argsargs) if (is(T == class));Given a raw memory area
, constructs an object ofchunkclasstypeTat that address. The constructor is passed the argumentsArgs.Discussion
If
Tis an inner class whoseouterfield can be used to access an instance of the enclosing class, thenArgsmust not be empty, and the first member of it must be a valid initializer for thatouterfield. Correct initialization of this field is essential to access members of the outer class insideTmethods.Preconditions:
must be at least as large aschunkTneeds and should have an alignment multiple ofT's alignment. (The size of aclassinstance is obtained by using ).Note: This function can be
@trustedif the corresponding constructor ofTis@safe.Return Value
The newly constructed object.
Examples
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 Argsargs) 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
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 boolisPowerOf2(X)(const Xx) 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
falseifis negative or zero.xParameters
Xxthe number to test
Return Value
trueifis an integer power of two.xExamples
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
assert( isPowerOf2(1)); assert( isPowerOf2(2)); assert( isPowerOf2(1uL << 63)); assert(!isPowerOf2(-4)); assert(!isPowerOf2(0)); assert(!isPowerOf2(1337u));
-
Declaration
templateisInnerClass(T) if (is(T == class))Determines whether
Tis a class nested inside another class and thatT.outeris the implicit reference to the outer class (i.e.outerhas not been used as a field or method name)Parameters
Ttype to test
Return Value
trueifTis a class nested inside another, with the conditions described above;falseotherwiseExamples
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));