有没有人将 Andrei Alexandrescu 的经典通用工厂(Chapter 8 的第 208 页,Modern C++ Design )与 Boost.TypeErasure 的“多功能”功能相结合? ?也就是说,具有多个创建者函数签名的灵 active ,这些签名在参数的数量和类型方面有所不同(但仍然具有相同的返回类型并且在编译时已知)。
也就是说,如何组合这个稍微简化的泛型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 相同的错误。所以问题。我怀疑答案需要将 ProductCreator
模板参数移动到 Register
函数并在那里做一些事情。
所以我想我最终是在寻找通用多功能工厂的完整、有效的实现,它可能已经存在,但我只是忽略了它。但是,我们将不胜感激将其整合在一起的任何帮助。
我更喜欢 C++11 解决方案,但显然 C++14 总比没有好,等等。
在此先感谢您的帮助!
最佳答案
好的,我有一个不使用 Boost.TypeErasure 的略显丑陋的解决方案,它是 C++14,但它确实提供了基本相同的功能。它是多层的,因此 ID 编号是按工厂编号的(但您也可以唯一编号)。 我会尽快写更多,但我现在真的要 sleep 了......
#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));
}
关于具有多个构造函数签名的 C++ 通用工厂?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48819868/