【问题标题】:C++ generic factory with multiple constructor signatures?具有多个构造函数签名的 C++ 通用工厂?
【发布时间】:2018-02-16 04:00:19
【问题描述】:

有没有人将 Andrei Alexandrescu 的经典仿制药工厂(Modern C++ Design 中的 Chapter 8 第 208 页)与 Boost.TypeErasure 的“多功能”功能结合起来?也就是说,具有多个创建者函数签名的灵活性,这些签名在参数的数量和类型方面有所不同(但仍然具有相同的返回类型并且在编译时已知)。

也就是说,如何结合这个稍微简化的泛型Factory:

#include <map>
#include <utility>
#include <stdexcept>

template <class AbstractProduct, typename IdentifierType, typename ProductCreator>
class Factory
{
public:
    bool Register(const IdentifierType& id, ProductCreator creator) {
        return associations_.emplace(id, creator).second;
    }

    bool Unregister(const IdentifierType& id) {
        return associations_.erase(id) == 1;
    }

    template <typename... Arguments>
    AbstractProduct CreateObject(const IdentifierType& id, Arguments&& ... args) {
        auto i = associations_.find(id);
        if (i != associations_.end()) {
            return (i->second)(std::forward<Arguments>(args)...);
        }
        throw std::runtime_error("Creator not found.");
    }

private:
    std::map<IdentifierType, ProductCreator> associations_;
};

使用这个(不完整的)函数类型擦除“模式”:

#include <boost/type_erasure/any.hpp>
#include <boost/type_erasure/builtin.hpp>
#include <boost/type_erasure/callable.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/variant.hpp>    

template<class... Sig>
using multifunction = any< mpl::vector< copy_constructible<>, typeid_<>, relaxed, callable<Sig>... > >;
using variant_type = boost::make_recursive_variant< void, double, ... >::type;
using function_type = multifunction<AbstractProduct(void), AbstractProduct(double), AbstractProduct(double, double)>;

class variant_handler
{
public:
    void handle(const variant_type& arg) {
        boost::apply_visitor(impl, arg);
    }
    void set_handler(function_type f) {
        impl.f = f;
    }
private:
    struct dispatcher : boost::static_visitor<void>
    {
        template<class T>
        void operator()(const T& t) { f(t); }
        // For a vector, we recursively operate on the elements
        void operator()(const vector_type& v)
        {
            boost::for_each(v, boost::apply_visitor(*this));
        }
        function_type f;
    };
    dispatcher impl;
};

所以最终人们可以像这样使用它:

Factory<Arity*, int, ???> factory;
factory.Register(0, boost::bind( boost::factory<Nullary *>() ));
factory.Register(1, boost::bind( boost::factory<Unary *>(), _1 ));
auto x = factory.CreateObject(0);
auto y = factory.CreateObject(1, 0.5);

我还没有在野外找到现有的实现,我目前正坚持自己的尝试。我的第一次尝试错误地将boost::bind() 的结果存储在function_type 中,这导致this SO 问题出现同样的错误。我怀疑答案需要将ProductCreator 模板参数移动到Register 函数并在那里做一些事情。

所以我想我最终是在寻找一个通用多功能工厂的完整、有效的实现,它可能已经存在,但我只是忽略了它。但是,我们将不胜感激任何有关将其组合在一起的帮助。

我更喜欢 C++11 解决方案,但显然 C++14 总比没有好,等等。

在此先感谢您的帮助!

【问题讨论】:

  • IIUC 你的MultiCtors 无法存储签名为Base*() OR Base*(double) 但带有Base*() AND 的函数对象b> Base*(double).
  • “当我调用 register 时编译失败”。在过去,register 是一个关键字(存储类说明符),尽管现在它已经没有任何意义了。
  • 根据 cppreference.com register 在 C++11 中仍然存在,甚至自 C++17 起保留(尽管未使用)。
  • 对不起,我打错了,这个函数叫做Register。
  • @llonesmiz 所说的,加上boost::bind(或std::bind)创建的函子可以take in and ignore extra arguments,这就是nullary case 起作用的原因。

标签: c++ c++11 factory boost-bind boost-type-erasure


【解决方案1】:

好的,我有一个不使用 Boost.TypeErasure 的稍微难看的解决方案,它是 C++14,但它确实提供了基本相同的功能。它是多层的,因此 id 编号是每个工厂的(但您也可以唯一编号)。 我很快就会写更多,但我现在真的要睡觉了......

#include <boost/functional/factory.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>

#include <cassert>
#include <map>
#include <tuple>
#include <type_traits>
#include <utility>


template <class AbstractProduct, typename IdentifierType, typename... ProductCreators>
class Factory
{
    using AssociativeContainers = std::tuple<std::map<IdentifierType, boost::function<ProductCreators>>...>;
public:
    template <typename Product, typename... Arguments>
    bool Register(const IdentifierType& id, boost::function<Product(Arguments...)> creator) {
        auto &foo = std::get<std::map<IdentifierType, boost::function<AbstractProduct(const Arguments&...)>>>(associations_);
        return foo.emplace(id, creator).second;
    }

    // This function left as an exercise to the reader...
    bool Unregister(const IdentifierType& id) {
        return associations_.erase(id) == 1;
    }

    template <typename... Arguments>
    AbstractProduct CreateObject(const IdentifierType& id, Arguments&& ... args) const {
        auto const &foo = std::get<std::map<IdentifierType, boost::function<AbstractProduct(const Arguments&...)>>>(associations_);
        auto const i = foo.find(id);
        if (i != foo.end()) {
            return (i->second)(std::forward<Arguments...>(args)...);
        }
        throw std::runtime_error("Creator not found.");
    }

private:
    AssociativeContainers associations_;
};


struct Arity {
    virtual ~Arity() = default;
};

struct Nullary : Arity {};

struct Unary : Arity {
    Unary() {}
    Unary(double x) : x(x) {}

    double x;
};


int main(void)
{
    Factory<Arity*, int, Arity*(), Arity*(const double&)> factory;
    factory.Register(0, boost::function<Arity*()>{boost::factory<Nullary*>()} );
    factory.Register(1, boost::function<Arity*(const double&)>{boost::bind(boost::factory<Unary*>(), _1)});
    auto x = factory.CreateObject(1, 2.0);
    assert(typeid(*x) == typeid(Unary));
    x = factory.CreateObject(0);
    assert(typeid(*x) == typeid(Nullary));
}

【讨论】:

    【解决方案2】:

    哈利路亚,我找到了一个使用 Boost.Variant 但没有类型擦除的解决方案。我认为这比我之前的答案要好得多,因为:

    • 创作者 ID 是唯一的。
    • CreateObject 支持将参数隐式转换为构造函数。

    存在构造函数必须采用const&amp; 参数的相同限制。

    我在一定程度上简化了整体设计,以专注于基本行为。缺少的是错误处理和可配置关联容器类型的策略,它们应该是附加的类模板参数。我还留下了一些最小的调试代码,以便您在测试时亲眼看到它的工作原理。

    #include <boost/functional/factory.hpp>
    #include <boost/function.hpp>
    #include <boost/variant.hpp>
    
    #include <map>
    #include <stdexcept>
    #include <tuple>
    #include <type_traits>
    #include <utility>
    // Just for debugging.
    #include <cassert>
    #include <iostream>
    #include <typeinfo>
    #include <cxxabi.h>
    
    // Tuple manipulation.
    
    template <typename Signature>
    struct signature_impl;
    
    template <typename ReturnType, typename... Args>
    struct signature_impl<ReturnType(Args...)>
    {
        using return_type = ReturnType;
        using param_types = std::tuple<Args...>;
    };
    
    template <typename T>
    using signature_t = signature_impl<T>;
    
    
    template <std::size_t... Ints>
    struct indices {};
    
    template <std::size_t N, std::size_t... Ints>
    struct build_indices : build_indices<N-1, N-1, Ints...> {};
    
    template <std::size_t... Ints>
    struct build_indices<0, Ints...> : indices<Ints...> {};
    
    template <typename Tuple>
    using make_tuple_indices = build_indices<std::tuple_size<typename std::remove_reference<Tuple>::type>::value>;
    
    // The multiple-signature factory.
    template <class AbstractProduct, typename IdentifierType, typename... ProductCreators>
    class multifactory
    {
        using functions = boost::variant<boost::function<ProductCreators>...>;
    
        std::map<IdentifierType, functions> associations_;
    
        template <typename Signature>
        struct dispatch_foo
        {
            template <typename CreateArgs, std::size_t... Indices>
            typename std::enable_if<std::is_convertible<CreateArgs, typename signature_t<Signature>::param_types>::value, AbstractProduct>::type
            static apply(boost::function<Signature> const &f, CreateArgs && t, indices<Indices...>)
            {
                return f(std::get<Indices>(std::forward<CreateArgs>(t))...);
            }
    
            template <typename CreateArgs, std::size_t... Indices>
            typename std::enable_if<!std::is_convertible<CreateArgs, typename signature_t<Signature>::param_types>::value, AbstractProduct>::type
            static apply(boost::function<Signature> const &, CreateArgs &&, indices<Indices...>)
            {
                return nullptr;
            }
        };
    
        template <typename... CreateArguments>
        struct dispatcher : boost::static_visitor<AbstractProduct>
        {
            std::tuple<CreateArguments...> args;
    
            dispatcher(CreateArguments const&... args) : args{std::forward_as_tuple(args...)} {}
    
            template <typename Signature>
            AbstractProduct operator()(boost::function<Signature> const &f) const
            {
                int status;
                std::cout << "visitor: " << abi::__cxa_demangle(typeid(Signature).name(), nullptr, 0, &status) << "\n";
                return dispatch_foo<Signature>::apply(f, args, make_tuple_indices<std::tuple<CreateArguments...>>{});
            }
        };
    
    public:
        template <typename ProductCreator>
        bool Register(IdentifierType id, ProductCreator &&creator) {
            return associations_.emplace(id, std::forward<ProductCreator>(creator)).second;
        }
    
        bool Unregister(const IdentifierType& id) {
            return associations_.erase(id) == 1;
        }
    
        template <typename... Arguments>
        AbstractProduct CreateObject(const IdentifierType& id, Arguments const& ... args) {
            auto i = associations_.find(id);
            if (i != associations_.end()) {
                dispatcher<Arguments...> impl(args...);
                return boost::apply_visitor(impl, i->second);
            }
            throw std::runtime_error("Creator not found.");
        }
    };
    
    
    struct Arity {
        virtual ~Arity() = default;
    };
    
    struct Nullary : Arity {};
    
    struct Unary : Arity {
        Unary() {} // Also has nullary ctor.
        Unary(int) {}
    };
    
    
    int main(void)
    {
        multifactory<Arity*, int, Arity*(), Arity*(const int&)> factory;
        factory.Register(0, boost::function<Arity*()>( boost::factory<Nullary*>() ));
        factory.Register(1, boost::function<Arity*(const int&)>(boost::factory<Unary*>()) );
        auto a = factory.CreateObject(0);
        assert(a);
        assert(typeid(*a) == typeid(Nullary));
        auto b = factory.CreateObject(1, 2);
        assert(b);
        assert(typeid(*b) == typeid(Unary));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 2017-03-19
      • 2011-01-24
      • 2022-11-12
      相关资源
      最近更新 更多