c ++ 11 — универсальная фабрика C ++ с несколькими сигнатурами конструктора?

Кто-нибудь когда-нибудь комбинировал классическую фабрику по производству дженериков Андрея Александреску? Глава 8 в Современный дизайн C ++) с «многофункциональными» возможностями Boost.TypeErasure? То есть гибкость, позволяющая иметь несколько сигнатур функций-создателей, которые различаются по количеству и типу параметров (но при этом имеют одинаковый тип возвращаемого значения и известны во время компиляции).

Другими словами, как объединить эту слегка упрощенную универсальную Фабрику:

#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_;
};

с помощью этого (неполного) типа функции стираем ‘pattern’:

#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, что привело к той же ошибке этот ТАК вопрос. Я подозреваю, что ответ потребует перемещения ProductCreator параметр шаблона в Register функционировать и делать что-то там.

Так что я думаю, что в конечном итоге я ищу полную, работающую реализацию универсальной многофункциональной фабрики, которая, возможно, уже существует, и я просто упустил это из виду. Но любая помощь в сборе будет очень признательна.

Я бы предпочел решение C ++ 11, но, очевидно, C ++ 14 лучше, чем ничего и т. Д.

Заранее спасибо за любую помощь с этим!

4

Решение

Хорошо, у меня есть немного уродливое решение, которое не использует Boost.TypeErasure, это C ++ 14, но оно обеспечивает практически ту же функциональность. Он многоуровневый, поэтому нумерация идентификаторов индивидуальна для каждого завода (но вы также можете указать уникальную нумерацию).
Я скоро напишу, но мне действительно нужно идти спать прямо сейчас …

#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));
}
0

Другие решения

Аллилуйя, я нашел решение, используя Boost.Variant, но без стирания типа. Я думаю, что это намного лучше, чем мой предыдущий ответ, так как:

  • Идентификатор создателя уникален.
  • CreateObject поддерживает неявное преобразование параметров в конструктор.

То же ограничение, которое должны принимать конструкторы const& параметры существуют.

Я несколько упростил общий дизайн, чтобы сосредоточиться на основном поведении. Чего не хватает, так это политики обработки ошибок и настраиваемого ассоциативного типа контейнера, который должен быть дополнительными параметрами шаблона класса. Я также оставил минимальный отладочный код, чтобы вы могли убедиться, что он работает, когда вы его тестируете.

#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));
}
0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector