[Issue 2484] Templated classes have no moduleinfo

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jul 1 11:31:26 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2484





--- Comment #3 from Justin <mrjnewt at gmail.com>  2009-07-01 11:31:25 PDT ---
Ok, with a bit of work, I thing I've improved on this technique a bit.
Here's factory.d:

module factory;

import std.stdio;
import container;

void main() {    
    auto foo = new Container!(int)();
    foo.thingy = 3;
    assert(foo.thingy == 3);
    writefln("foo Classinfo.name : ", foo.classinfo.name);

    auto bar = Object.factory(foo.classinfo.name);
    assert(bar !is null);
    writefln("bar Classinfo.name : ", bar.classinfo.name);
}

And container.d uses a template to insert the patching work:
module container;

import std.moduleinit;

const char[] CURRENT_MODULE = "container";

class Container(T) { 
    T thingy; 

    mixin registerTemplatedClass;
}

public template registerTemplatedClass() {
    static this() {
        // Patch ModuleInfo
        foreach (mod; ModuleInfo.modules)
        {
            if (mod.name == CURRENT_MODULE)
            {
                mod.localClasses ~= [this.classinfo];
                break;
            }
        }
    }
}

This could be improved if there's a way of getting the current module; I
couldn't find anything, so I created the constant CURRENT_MODULE which will
have to be placed in each module which uses the template.

Output is the expected:
foo Classinfo.name : container.Container!(int).Container
bar Classinfo.name : container.Container!(int).Container

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list