exasim-project / NeoFOAM

WIP Prototype of a modern CFD core
19 stars 1 forks source link

compile class discovery #80

Closed HenningScheufler closed 3 weeks ago

HenningScheufler commented 4 weeks ago

The provided code provides a template for compile class discovery in C++.

All derived class are automatically registered inside:

static inline std::unordered_map<std::string, createFunction> classMap;

To sucessfully register the class the derived class needs to defined the following function: static create, static name and call registerClass inside the constructor.

    TestDerivedClass(std::string name, double test)
        : TestBaseClass(), testString_(name), testValue_(test)
    {
        registerClass<TestDerivedClass>();
    }

    static std::unique_ptr<TestBaseClass> create(std::string name, double test)
    {
        return std::make_unique<TestDerivedClass>(name, test);
    }

    static std::string name() { return "TestDerivedClass"; }

The derived class can now the instantiated by calling the static create function

    std::unique_ptr<TestBaseClass> testDerived =
        TestBaseClass::regTestBaseClass::create("TestDerivedClass", "FirstDerived", 1.0);

more details see test_RegisterClass:

TODO:

HenningScheufler commented 4 weeks ago

@greole @bevanwsjones @MarcelKoch What are you thoughts?

greole commented 4 weeks ago

So this is comparable to the constructorPtr table in OF?

greole commented 3 weeks ago

Just a quick comment, lets squash all the "Update ..." into one. Makes it easier to find meaningful comments later on.