Шаблон посетителя с использованием Boost :: Bind и перегруженных функций - PullRequest
4 голосов
/ 14 мая 2011

Я пытаюсь добавить шаблон Visitor в свой код и хочу сохранить его как можно более общим. Точнее, я бы не хотел жестко кодировать функцию обратного вызова в моей функции accept. Итак, в качестве параметра функции accept я даю объект boost::function, который затем вызывается посещаемым объектом.

Однако моя проблема в том, что я не могу привязать перегруженные функции (потому что boost :: bind не знает, к какой именно функции привязать) и не могу привести перегруженную функцию к правильной, потому что Я не знаю точный тип посещаемого класса (это важно).

Есть ли способ создать то, что я хочу? Я искал SO, но нашел только вопросы о том, как исправить проблему с привязкой (то есть путем приведения, что я не могу сделать).

Ниже приведен код, который не компилирует , но показывает, что я хотел бы заархивировать:

#include <string>
#include <vector>
#include <boost/bind.hpp>
#include <boost/function.hpp>

struct A
{
    virtual void acceptVisitor (boost::function<void (A const &)> callbackFnc)
    {
        callbackFnc(*this);
    }
};

struct B : virtual public A {};

std::string printMe (A const & a) { return "A"; }
std::string printMe(B const & a) { return "B"; }


int main()
{
    std::vector<std::string> stringVector;

    boost::function<void (A const &)> bindedFnc = boost::bind(&std::vector<std::string>::push_back, 
        &stringVector, boost::bind(&printMe, _1));

    A A1;
    B A2;

    A1.acceptVisitor(bindedFnc);
    A2.acceptVisitor(bindedFnc);
}

[Edit] Исправлен пример кода, потому что предыдущая версия (как заметил ildjarn) фактически не вызывала функцию accept.

Ответы [ 2 ]

2 голосов
/ 15 мая 2011

Это должно привести вас на полпути. Он компилируется с Visual C ++ 2010 и g ++ 4.5.1 с использованием Boost 1.46.0. Он не компилируется с реализацией Visual C ++ 2010 C ++ 0x <functional>; Я еще не уверен, почему.

Настройка:

#include <iostream>
#include <iterator>
#include <string>
#include <vector>

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

// This helper allows you to do a push_back in a bind; you can't bind
// directly to std::vector::push_back because the type of a Standard
// Library member function is unspecified.
struct do_push_back
{
    typedef void result_type;

    template <typename TSequence, typename TElement>
    void operator()(TSequence* sequence, const TElement& element) const
    {
        sequence->push_back(element);
    }
};

Демонстрация:

// Class hierarchy for demonstration:
struct B { };
struct D : B { };

// Instead of using overlodaed nonmember functions, you can overload
// operator() in a function object.  This allows you to bind to an 
// instance of this function object, not directly to one of the overloads.
struct make_string
{
    typedef std::string result_type;
    std::string operator()(const B&) const { return "B"; }
    std::string operator()(const D&) const { return "D"; }
};

int main()
{
    std::vector<std::string> strings;

    // Note that we do not use a boost::function here:
    auto f = boost::bind(do_push_back(), 
                         &strings, 
                         boost::bind(make_string(), _1));

    // Call our 'f' with B and D objects:
    f(B());
    f(D());

    std::copy(strings.begin(), strings.end(),
              std::ostream_iterator<std::string>(std::cout));
}

Результат:

BD

Вот почему это только половина решения: вы не можете сохранить результат вызова на boost::bind в boost::function. Проблема в том, что когда вы используете boost::function<void(const B&)> для хранения объекта связанной функции, он всегда будет передавать const A& в качестве аргумента связанной функции.

Даже если вы вызываете объект boost::function с аргументом D, он преобразуется в const B&. Большая часть информации о типах теряется при использовании boost::function; эта потеря информации о типе необходима, чтобы сделать boost::function пригодным для использования в качестве универсального контейнера вызываемого объекта.

Это не значит, что вы не можете передать объект связанной функции; вам просто нужно использовать шаблоны, чтобы предотвратить потерю информации о типе:

template <typename TFunction>
void test(std::vector<std::string>& strings, TFunction f)
{
    f(B());
    f(D());
}

// In main():
test(strings, f);

// Or, if you don't have C++0x's "auto", you can pass the bound 
// function object directly:
test(strings, boost::bind(do_push_back(), 
                          &strings, 
                          boost::bind(make_string(), _1)));

К сожалению, чтобы не потерять информацию о типе, вы должны передать связанный функциональный объект в шаблон функции. Это означает, что ваша идея сделать acceptVisitor виртуальной функцией-членом не будет работать с этим решением (невозможно иметь шаблон виртуальной функции).

В любом случае, надеюсь, это поможет вам.

1 голос
/ 17 августа 2012

Возможно, вы найдете общий шаблон Visitor из библиотеки Loki полезным

См. http://loki -lib.sourceforge.net

#include <iostream>
#include <Loki/Visitor.h>

struct Animal : public Loki::BaseVisitable<void, Loki::DefaultCatchAll, false>
{
};

struct Cat : public Animal
{
    LOKI_DEFINE_VISITABLE();
};

struct Dog : public Animal
{
    LOKI_DEFINE_VISITABLE();
};

struct Flower : public Loki::BaseVisitable<void, Loki::DefaultCatchAll, false>
{
};

struct Tulip : public Flower
{
    LOKI_DEFINE_VISITABLE();
};

struct AnimalAndFlowerVisitor 
    : public Loki::BaseVisitor
    , public Loki::Visitor<Cat, void, false>
    , public Loki::Visitor<Dog, void, false>
    , public Loki::Visitor<Tulip, void, false>
{
    void Visit(Dog & dog)
    {
        std::cout << "Do something with the dog\n";
    }

    void Visit(Cat & cat)
    {
        std::cout << "Do something with the cat\n";
    }

    void Visit(Tulip & tulip)
    {
        std::cout << "Do something with the tulip\n";
    }
};

int main(int argc, char* argv[])
{
    Dog dog;
    Cat cat;
    Tulip tulip;

    Animal & animalDog = dog;
    Flower & tulipFlower = tulip;

    AnimalAndFlowerVisitor visitor;
    animalDog.Accept(visitor);    // will print "Do something with the dog"
    tulipFlower.Accept(visitor);  // will print "Do something with the tulip"

    return 0;
}
...