0
\$\begingroup\$

I'm trying to create some tool for easier dependency injection in c++14, here is one of my concepts using std::tuple.

template <class... Types>
class injector
{
public:
    injector(Types... i) { injected_ = std::tuple<Types...>(i...); }

protected:
    template <class T>
    T *injected()
    {
        return std::get<T *>(injected_);
    }

private:
    std::tuple<Types...> injected_;
};


using component_injector = injector<scene *, entity *, foo *>;
class component : component_injector
{
public:
    component(component_injector injected) : component_injector(injected) {};
    virtual ~component(){};

    void method()
    {
        injected<scene>()->method();
        injected<foo>()->method();
    }
};


int main(int argc, char *argv[])
{
    scene s;
    entity e;
    foo f;

    component c({&s, &e, &f});
    c.method();

    return 0;
}

Is it good idea to create dependence this way? It has some limitations, for example I cannot have two dependencies of the same type etc. but code of useful part (component class) seems to be much clearer. The injected types should be replaced by interfaces but the real types are used directly here for simplicity.

\$\endgroup\$
  • \$\begingroup\$ Easier? Why is that easier? \$\endgroup\$ – Martin York Jul 25 '17 at 14:50
2
\$\begingroup\$
  1. I don't think that makes it easier (or cleaner)
  2. You are injecting pointers (which begs to ask ownership)
  3. You should be preferring encapsulation over inheritance.

If I was doing it normally:

class component
{
    Scene&   scene;
    Entity&  entity;
    Foo&     foo;
    public:
    component(Scene& s, Entity& e, Foo& f)
        : scene(s)
        , entity(e)
        , foo(f)
    {}
    virtual ~component(){};

    void method()
    {
        scene->method();
        foo->method();
    }
};

int main()
{
    scene s;
    entity e;
    foo f;
    component c(s, e, f});

    c.method();
}

This seems much cleaner than your method (where you introduce another level of inheritance).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.