Created
October 3, 2015 08:48
-
-
Save rikkimax/986595dde4f57c539469 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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