Unstandard

Functions for user-defined lifetime implementation.

License
Boost License 1.0.
Authors
Denis Shelomovskij

void  move(T)(ref T source, ref T target);
T  move(T)(ref T source);

Moves source into target.

Specifically:
See also std.exception.doesPointTo.
Preconditions:
&source == &target || !doesPointTo(source, source)

template  forward(args...)

Forwards function arguments with saving ref-ness.

Example:
int foo(int n) { return 1; }
int foo(ref int n) { return 2; }
int bar()(auto ref int x) { return foo(forward!x); }

assert(bar(1) == 1);
int i;
assert(bar(i) == 2);


void foo(int n, ref string s) { s = null; foreach (i; 0..n) s ~= "Hello"; }

// forwards all arguments which are bound to parameter tuple
void bar(Args...)(auto ref Args args) { return foo(forward!args); }

// forwards all arguments with swapping order
void baz(Args...)(auto ref Args args) { return foo(forward!args[$/2..$], forward!args[0..$/2]); }

string s;
bar(1, s);
assert(s == "Hello");
baz(s, 2);
assert(s == "HelloHello");
Note:
This is just a copy of std.algorithm.forward implementation except it uses fixed move.

void  constructFrom(T, Arg)(ref T chunk, auto ref Arg arg);

Constructs an object of type T in given chunk of uninitialized memory just like T t = arg;.


void  constructFromLiteral(S, Args...)(ref S chunk, auto ref Args args) if (is(S == struct));

Constructs an object of struct type S in given chunk of uninitialized memory just like auto s = S(args);.


void  initializeClassInstance(C, Args...)(C chunk, auto ref Args args) if (is(C == class));

Constructs an object of class type C at given reference to uninitialized memory just like auto c = new C(args); except given memory is used instead of allocating.


void  finalizeClassInstance(T)(T t, bool resetMemory = true);

Destroys the given class instance and puts it in an invalid state. It's used to destroy an object so that any cleanup which its destructor or finalizer does is done. It does not initiate a GC cycle or free any GC memory. It always zero class instance __vptr. If resetMemory is true it will also set class instance memory to its initial state so that it no longer references any other objects.


bool  isFinalized(T)(in T t);
bool  finalized(T)(in T t);

Determines whether class instance t is finalized.

Also returns true if t's memory is zero-filled.

T must be either class or interface.


void  destruct(T)(ref T t, bool resetInitialState = true);

Destructs t exactly the same way a compiler does in a case it goes out of scope. Also puts destructed object in its init state if resetInitialState is true, otherwise object state will be undefined (i.e. possibly invalid).


void  setToInitialState(T)(ref T t);

Sets the passed object to its `init` state.

Use this function instead of dealing with tricky typeid(T).init().


void  setElementsToInitialState(T)(T[] arr);

Sets all elements of the passed dynamic array to its `init` state.

Use this function for better performance instead of calling setToInitialState on each element.


void  callPostblits(T)(ref T t);

Calls the postblit of the given object, if any.

Faster and convenient replacement for typeid(T).postblit(&t).


void  callDestructors(T)(ref T t);

Calls the destructor of the given object, if any.

Faster and convenient replacement for typeid(T).destroy(&t).