Best Builder to Use

eris jvburnes at gmail.com
Fri Mar 12 10:10:52 PST 2010


Andrei,

I don't see why you couldn't see through it, unless you weren't expanding the mixin.

The component factory pulls it blueprints from a directory path created by the
mixin.  This path is imported in the namespace of the factory, thus limiting the
factory to creating components from a specific set of blueprints.

Even though I haven't really cleaned up the code for an "embarrassment-free"
release, here is the specific class:

// creates a runtime component factory from "blueprint.all"
// creates a catalog hash of the module's classinfo also (better way of doing this?)

// mixin template
template ComponentFactory(char[] blueprint)
{
	const char[] ComponentFactory = `

		class `~blueprint~`Factory
		{
			import dendrite.component.` ~ blueprint ~ `.all;
			HashMap!(char[],ClassInfo) catalog;
			this(char[] blueprint="` ~ blueprint ~ `")
			{
				catalog = new HashMap!(char[],ClassInfo);
				auto minfo = new ModuleInfo();
				foreach (mod; minfo)
				{
					if (mod.name == blueprint) {
						debug (1) Stdout("creating component catalog:").newline;
						foreach (classInfo; mod.localClasses)
						{
							catalog[classInfo.name] = classInfo;
							debug (1) Stdout("  ")(classInfo.name).newline;
						}
					}
				}
			}

			Component createComponent(char[] name)
			{
				debug (2) Stdout("creating component: ")(name).newline;
				return cast(Component)catalog[name].create();
			}
		}
	`;
}

// in-line declaration

mixin(ComponentFactory!("Minimal"));

// usage

// create the factory

factory = new MinimalFactory();

// use it to create a component

auto intGenerator = factory.createComponent("IntStreamer");

// attach it to the simulation backplane

backplane.activate(intGenerator);



More information about the Digitalmars-d mailing list