Compiler as dll

Daniel Keep daniel.keep.lists at gmail.com
Fri Jan 30 21:52:35 PST 2009



Bill Baxter wrote:
> [snip]

This is really interesting, since you've almost described the solution I
moved to after Variants (with rather ironically still uses Variants, but
that's just an implementation detail now :P).

The problem for me was that I wanted to be able to expose various
internal bits of state to the console by string name.  I also wanted to
KNOW when something had been changed.  The console had to be able to
get, set and copy those values as both strings and their native types.

Basically, random parts of the engine and game code could register a
block of callback functions with the central cvar registry; the struct
looks like this:

struct ConfigVarCallbacks
{
    Variant delegate()          getValue;
    void delegate(Variant)      setValue;
    Variant delegate(char[])    fromRepr;
    char[] delegate(Variant)    toRepr;
}

The above was automatically generated by templates.  Incidentally, THAT
is was spurred me to write Tango's to!(T) template.  :D

It was used like this (mostly; I think I lost the implementation of
cvars, but the templates and CTFE underlying it are still there):

class Window
{
    mixin(cvars(
    `
        uint width : window.width = 800;
        uint height : window.height = 600;
    `
    ));

    this()
    {
        _cvars_register;
    }
}

This would generate the storage for the cvar as a member of Window,
generate the callback functions, and then do all the registration
boilerplate.

With that, you could talk to the config object to play with those values.

{
    config.setVarString("window.width", "1024");
    config.setVar("window.height", 768u);

    writefln("Window size: %sx%s",
        config.getVar("window.width").get!(uint),
        config.getVarString("window.height"));
}

And of course, the console can now get/set values from raw strings, or
copy values between cvars in their native type via Variants.

The one thing that this system never accounted for was user-created
cvars which could potentially be... Variants.  :P

  -- Daniel



More information about the Digitalmars-d mailing list