Skip to content

Instantly share code, notes, and snippets.

@rikkimax
Created October 3, 2015 08:48
Show Gist options
  • Save rikkimax/986595dde4f57c539469 to your computer and use it in GitHub Desktop.
Save rikkimax/986595dde4f57c539469 to your computer and use it in GitHub Desktop.
module dnetdev.webserver.common.classfinder;
Interface findAndCreateClass(Interface)(string name) if (is(Interface == class) || is(Interface == interface)) {
import std.experimental.allocator;
auto alloc = theAllocator();
auto classinfo = TypeInfo_Class.find(name);
if (classinfo is null)
return null;
size_t issize = classinfo.init.length;
void[] dataallocated = alloc.allocate(issize);
Object obj;
dataallocated[] = cast(void[])classinfo.init[];
if (dataallocated is null)
return null;
obj = cast(Object)dataallocated.ptr;
if (obj !is null) {
if (classinfo.defaultConstructor !is null)
(cast(void function(Object))classinfo.defaultConstructor)(obj);
if (Interface obj2 = cast(Interface)obj) {
return obj2;
} else {
alloc.dispose(obj);
return null;
}
} else {
alloc.dispose(dataallocated);
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment