intel / compile-time-init-build

C++ library for composing modular firmware at compile-time.
https://intel.github.io/compile-time-init-build/
Boost Software License 1.0
425 stars 44 forks source link

Dependency injection support #71

Open WojciechMigda opened 2 years ago

WojciechMigda commented 2 years ago

Hi,

it occured to me yesterday that it would be a nice feature to have stateful nexus, which, in spirit of DI, would then be able to inject extra arguments to handlers registered through features.

What I have in mind is something that Kris Jusiak's library SML already has. https://github.com/boost-ext/sml Specifically, in Kris' library state machine instance can be instantiated with extra dependencies. Then, through 'smart' resolution it is able to pass these dependencies to actions which are part of state machine's transitions. Such basic example is presented in SML's documentation, specifically, Section 5: Create a state machine.

In case of CIB, it would allow to pass user's dependencies to created nexus:

cib::nexus<nexus_project> nexus{Type1: dependency1, Type2: dependency2, ...};

Then, CIB when it matches handlers registered against service signature, it would also allow handler's signatures which arguments are superset of those specified in service's definition, extended with types which can be matched against dependencies passed to instantiated nexus. For example:

struct service : public cib::callback_meta<Payload const &>{};`

struct feature
{
    static auto constexpr config = cib::config(
        cib::extend<service>(
            [](double x, Payload const & p)
            {
                // do sth with injected x and passed p
            }
        )
    );
};

cib::nexus<nexus_project> nexus{87.0};

In the above service is declared to accept const reference to Payload, but then we'd attempt to declare feature which is attached to a handler which accepts Payload and a double. Since nexus is instantiated with a double as one of dependencies passed by a user it would allow such feature and pass both arguments - one originating from the point of invocation of the service through the nexus, and the other from the pool of dependencies which are part of nexus' state.

I believe this would be huge undertaking to introduce such enhancement, but given that Kris' SML library already has it, maybe it could be used as a guidance?

Thanks, Wojciech

lukevalenty commented 2 years ago

This is a fantastic feature suggestion! I've looked at @krzysztof-jusiak's DI library and found that it didn't work in a constexpr context for myself. Maybe there are some changes that could be made.

I would want to be able to inject subclasses of a base interface (virtual function overriding.)

I would also want to inject arbitrary types into template functions.

Thanks for this suggestion @WojciechMigda.

krzysztof-jusiak commented 2 years ago

I think that would be nice. I'm happy to experiment and see what can be done.