add core.factory (#14888)

This commit is contained in:
Walter Bright 2023-02-22 01:13:32 -08:00 committed by GitHub
parent 96d62e6f79
commit 481e169269
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 0 deletions

View file

@ -15,6 +15,7 @@ COPY=\
$(IMPDIR)\core\cpuid.d \
$(IMPDIR)\core\demangle.d \
$(IMPDIR)\core\exception.d \
$(IMPDIR)\core\factory.d \
$(IMPDIR)\core\int128.d \
$(IMPDIR)\core\lifetime.d \
$(IMPDIR)\core\math.d \

View file

@ -12,6 +12,7 @@ DOCS=\
$(DOCDIR)\core_attribute.html \
$(DOCDIR)\core_cpuid.html \
$(DOCDIR)\core_demangle.html \
$(DOCDIR)\core_factory.html \
$(DOCDIR)\core_lifetime.html \
$(DOCDIR)\core_memory.html \
$(DOCDIR)\core_runtime.html \

View file

@ -9,6 +9,7 @@ SRCS=\
src\core\cpuid.d \
src\core\demangle.d \
src\core\exception.d \
src\core\factory.d \
src\core\int128.d \
src\core\lifetime.d \
src\core\math.d \

View file

@ -0,0 +1,68 @@
/* Create classes from their modules and names.
*
* Copyright: Copyright (C) D Language Foundation 2023
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Authors: Walter Bright, Steven Schveighoffer
* Source: $(DRUNTIMESRC core/_factory.d)
*/
module core.factory;
/**
* Create instance of class specified by the module symbol and a string
* representing the name of the class.
* The class must either have no constructors or have
* a default constructor.
* Params:
* mod = symbol representing the module that the class is in
* classname = string representing the name of the class
* Returns:
* null if failed
* Example:
* ---
* module foo.bar;
*
* class C
* {
* this() { x = 10; }
* int x;
* }
*
* void main()
* {
* auto c = cast(C)factory!(foo.bar)("C");
* assert(c !is null && c.x == 10);
* }
* ---
*/
Object factory(alias mod)(string classname)
{
foreach(cl; _getModuleClasses!mod)
{
if (cl.stringof == classname)
return cl.classinfo.create();
}
return null;
}
@system unittest
{
Object valid_obj = factory!object("Object");
Object invalid_obj = factory!object("__this_class_doesnt_exist__");
assert(valid_obj !is null);
assert(invalid_obj is null);
}
/**************************************
* Retrieve as a tuple all the types of the top level classes in the module mod.
*/
private template _getModuleClasses(alias mod) {
alias result = _AliasSeq!();
static foreach(m; __traits(allMembers, mod))
static if(is(__traits(getMember, mod, m) == class))
result = _AliasSeq!(result, __traits(getMember, mod, m));
alias _getModuleClasses = result;
}
private template _AliasSeq(TList...) { alias _AliasSeq = TList; }