implicitcontext

Contextual parameter system for D.

Members

Aliases

print_fun_t
alias print_fun_t = void function(const(char)* message)

This functions prints a format-less, ZERO-TERMINATED string. Hence, the @system interface. Also, cannot fail. \n must flush.

realloc_fun_t
alias realloc_fun_t = void* function(void* p, size_t size)

This is not _exactly_ the same as C's realloc function!

Functions

allocator
ContextAllocator allocator(ImplicitContext ctx)

UFCS Getter for context allocator.

allocator
void allocator(ImplicitContext ctx, ContextAllocator allocator)

UFCS Setter for context allocator.

context
ImplicitContext context()

Return current context object. An ImplicitContext is safely copyable, but only the top-most context per-thread can be modified (like in _the_ stack). There is no reason to store it though?

logger
void logger(ImplicitContext ctx, ContextLogger allocator)

UFCS Setter for context allocator.

logger
ContextLogger logger(ImplicitContext ctx)

UFCS Getter for context allocator.

popContext
void popContext()

Restore formerly pushed context from thread-local context stack.

pushContext
ImplicitContext pushContext()

Saves context on the thread-local context stack. The current context() becomes a copy of that. Needs to be paired with pop.

userPointer
void* userPointer(ImplicitContext ctx)

UFCS Getter for user pointer.

userPointer
void userPointer(ImplicitContext ctx, void* userData)

UFCS Setter for user pointer.

Structs

ContextAllocator
struct ContextAllocator
Undocumented in source.
ContextLogger
struct ContextLogger
Undocumented in source.
ImplicitContext
struct ImplicitContext

A "context" implements per-thread scoped globals. It is a frame in the secundary stack.

Examples

- allocators - loggers - the UI "context" in UIs ...

Note: internal stack is handled with malloc/realloc/free from the C stdlib. Performance of internal stack relies on a malloc implementation that is itself lockfree, else the realloc could stall other threads from time to time. The context are also not designed to be very large.

Important difference: context.push() and context.pop() are explicit here. Leaving a D scope {} doesn't restore parent context (there is no language support), you need to call push/pop manually. On the plus side, no ABI change are needed and context stack is not meant to be touched very frequently.

context.set!int("userStuff", 4); assert(context.get!int("userStuff") == 4);

void subProc() { context.push;

assert(context.get!int("userStuff") == 4); // follows chain of contexts

context.set!int("userStuff", 3); assert(context.get!int("userStuff") == 3); // stack-like organization of contexts

context.pop;

assert(context.get!int("userStuff") == 4); // stack-like organization of contexts, with hierarchic namespacing } subProc();

Meta

License

Boost License 1.0

This is an implicit context system like in Odin, itself probably inspired by Scala "implicit parameters". In other words, a system to have scoped globals.

"In each scope, there is an implicit value named context. This CONTEXT VARIABLE is local to each scope and is implicitly passed by pointer to any procedure call in that scope (if the procedure has the Odin calling convention)."

Without language support, we don't have the ABI and the scope-specific context, however with TLS and manual scopes we can emulate that to pass parameters in an implicit way (scoped globals, in a way).

Note: this module uses TLS, and C runtime.