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 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
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
int a; int b = 42; assert(*emplace!int(&a, b) == 42);
-
Declaration
T
emplace
(T, Args...)(Tchunk
, auto ref Argsargs
) if (is(T == class));Given a raw memory area
(but already typed as a class typechunk
T
), constructs an object ofclass
typeT
at that address. The constructor is passed the argumentsArgs
.Discussion
If
T
is an inner class whoseouter
field can be used to access an instance of the enclosing class, thenArgs
must not be empty, and the first member of it must be a valid initializer for thatouter
field. Correct initialization of this field is essential to access members of the outer class insideT
methods.Note: This function is
@safe
if the corresponding constructor ofT
is@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
T
emplace
(T, Args...)(void[]chunk
, auto ref Argsargs
) if (is(T == class));Given a raw memory area
, constructs an object ofchunk
class
typeT
at that address. The constructor is passed the argumentsArgs
.Discussion
If
T
is an inner class whoseouter
field can be used to access an instance of the enclosing class, thenArgs
must not be empty, and the first member of it must be a valid initializer for thatouter
field. Correct initialization of this field is essential to access members of the outer class insideT
methods.Preconditions:
must be at least as large aschunk
T
needs and should have an alignment multiple ofT
's alignment. (The size of aclass
instance is obtained by using ).Note: This function can be
@trusted
if the corresponding constructor ofT
is@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 bool
isPowerOf2
(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
false
if
is negative or zero.x
Parameters
X
x
the number to test
Return Value
true
if
is an integer power of two.x
Examples
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
template
isInnerClass
(T) if (is(T == class))Determines whether
T
is a class nested inside another class and thatT.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
ifT
is a class nested inside another, with the conditions described above;false
otherwiseExamples
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));