samedi 16 juin 2018

Variadic templates Factory using std::bind and std::placeholders

I'm trying to implement a Factory pattern that can instantiate plug-ins into plug-in instances. The goal is to have a single Factory class, templated by the plug-in type, and the arguments to the create function of the plug-in, e.g:

APluginInstance and A2PluginInstance all derive ABase. They should each have a different Plugin in the factory, but be mapped in the factory against a name.

class ABase {};

class APluginInstance : public ABase {

public:

    static std::shared_ptr<ABase> create(double a, double b)
    {
        return std::shared_ptr<ABase>(new APluginInstance(a,b));
    }

    APluginInstance(double a, double b)
    : ret(a + b)
    {
        std::cout << ret << std::endl;
    }

    double ret;
};


class A2PluginInstance : public ABase {

public:

    static std::shared_ptr<ABase> create(double a, double b)
    {
        return std::shared_ptr<ABase>(new APluginInstance(a,b));
    }

    A2PluginInstance(double a, double b)
    : ret(a - b)
    {
        std::cout << ret << std::endl;
    }

    double ret;
};

Similarly, we have another type of plug-in somewhere else:

class BBase {};

class BPluginInstance : public BBase {

public:

    static std::shared_ptr<BBase> create(int a)
    {
        return std::shared_ptr<BBase>(new BPluginInstance(a));
    }

    BPluginInstance(int a)
    {
        std::cout  << a << std::endl;
    }
};

class B2PluginInstance : public BBase {

public:

    static std::shared_ptr<BBase> create(int a)
    {
        return std::shared_ptr<BBase>(new B2PluginInstance(a));
    }

    B2PluginInstance(int a)
    {
        std::cout  << -a << std::endl;
    }
};

Now here is the generic Plugin class that takes in template parameters the base instance type (the one returned from the create function) and the arguments to that function:

template <typename PluginInstanceBaseType, typename ...Args>
class Plugin
{
public:

    using PluginInstanceBase_t = PluginInstanceBaseType;


    void setFunc(const std::function<PluginInstanceBaseType(Args...)>& func)
    {
        _createFunc = func;
    }

    PluginInstanceBaseType create(const Args&... args)
    {
        return _createFunc(args...);
    }

    std::function<PluginInstanceBaseType(Args...)> _createFunc;

};

So we have our 2 plug-in types:

using APlugin = Plugin<std::shared_ptr<ABase>, double, double>;
using BPlugin = Plugin<std::shared_ptr<BBase>, int>;

Now here is the factory class, that takes the plug-in type in parameter (either APlugin or BPlugin)

template <typename PluginType>
class Factory
{

public:

    using PluginInstanceBase_t = typename PluginType::PluginInstanceBase_t;


    template <typename T, typename...Args>
    void addPlugin(const std::string& name, const std::function<T(Args...)>& f)
    {
        std::shared_ptr<PluginType> plugin(new PluginType());
        plugin->setFunc(f);
        _plugins[name] = plugin;
    }

    template <typename... Args>
    PluginInstanceBase_t create(const std::string& name, const Args&... args)
    {
        auto found = _plugins.find(name);
        if (found == _plugins.end()) {
            return PluginInstanceBase_t();
        }
        return found->second->create(args...);
    }

private:


    std::map<std::string, std::shared_ptr<PluginType> > _plugins;
};

Our 2 factories:

using AFactory = Factory<APlugin>;
using BFactory = Factory<BPlugin>;

Finally, here is an example usage:

int main()
{

     {
        // Works
        APlugin tmpa;
        tmpa.setFunc(APluginInstance::create);
    }

    AFactory af;
    BFactory bf;

    // Doesn't work
    //af.addPlugin("A1", APluginInstance::create);
    //af.addPlugin("A2", A2PluginInstance::create);
    //bf.addPlugin("B1", BPluginInstance::create);
    //bf.addPlugin("B2", B2PluginInstance::create);

    std::shared_ptr<ABase> a = af.create("A1",2., 4.);
    std::shared_ptr<ABase> a2 = af.create("A2",2., 4.);
    std::shared_ptr<BBase> b = bf.create("B1", 1);
    std::shared_ptr<BBase> b2 = bf.create("B2", 1);

    return 0;
}

Creating the plug-in and calling setFunc with the instance create function works directly. However when using the function addPlugin from the factory, it doesn't work because it cannot match the create function to the std::function type.

I'm trying to find a fix for it so that I can use the addPlugin function

Here is a link on Coliru for a live example:

http://coliru.stacked-crooked.com/a/cba8fe84b4d6bd85

Aucun commentaire:

Enregistrer un commentaire