www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Proposal: generic extensible storage qualifiers


recently I ran into a situation where I wanted to keep a freelist of freed
textures for every OpenGL context (there can be multiple).

I started with static { int[] freelist; } then realized this wouldn't work
because there'd be no way to map texture IDs to contexts.

To simplify situations like these, I propose two new keywords: __define_mode__
(or similar) and mode().

__define_mode(name, function) defines a mode based on a "differentiating
function" of the type int().

Whenever a variable (type T) with that mode is accessed, the function is called
- the result value is then used as the index into a T[] (that's dynamically

So for instance, to implement the "shared" qualifier would look like this:

int shared_id() { return 0; } // same area for all
__define_mode__ (shared, shared_id);

The "threadlocal" qualifier would look like this:

int tls_id() { return getThreadID(); }
__define_mode__ (threadlocal, tls_id);

The "glContext" qualifier would look like this:

mode(threadlocal) int bound_context;
int context_id() { return bound_context; }
__define_mode__ (glContext, context_id);

[later on]

/* static is orthogonal to mode - this translates into "static int[][][]" */
static mode(shared) mode(glContext) int[] freelist;

Dec 26 2009