5

I found this https://gist.github.com/2945472 but I need an implementation that does not depend on c++11. I tried my hand at converting it to use only boost, but I'm having some trouble.

Here is what I came up with:

#include <boost/any.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/unordered_map.hpp>

struct type_info_hash {
    std::size_t operator()(std::type_info const & t) const {
        return t.hash_code();
    }
};

struct equal_ref {
    template <typename T> bool operator()(boost::reference_wrapper<T> a,boost::reference_wrapper<T> b) const {
        return a.get() == b.get();
    }
};
struct any_visitor {
    boost::unordered_map<boost::reference_wrapper<std::type_info const>, boost::function<void(boost::any&)>, type_info_hash, equal_ref> fs;

    template <typename T> void insert_visitor(boost::function<void(T)> f) {
        try {
            fs.insert(std::make_pair(boost::ref(typeid(T)), boost::bind(f, boost::any_cast<T>(boost::lambda::_1))));
        } catch (boost::bad_any_cast& e) {
            std::cout << e.what() << std::endl;
        }
    }

    bool operator()(boost::any & x) {
        boost::unordered_map<boost::reference_wrapper<std::type_info const>, boost::function<void(boost::any&)>, type_info_hash, equal_ref>::iterator it = fs.find(boost::ref(x.type()));
        if (it != fs.end()) {
            it->second(x);
            return true;
        } else {
            return false;
        }
    }
};

struct abc {};

void fa(int i) { std::cout << "fa(" << i << ")" << std::endl; }
void fb(abc) { std::cout << "fb(abc())" << std::endl; }

int main() {
    any_visitor f;
    f.insert_visitor<int>(fa);
    f.insert_visitor<abc>(fb);

    std::vector<boost::any> xs;
    xs.push_back(1);
    xs.push_back(abc());
    xs.push_back(1.5);

    for (auto & x : xs) {
        if (!f(x)) std::cout << "no visitor registered" << std::endl;
    }
}

I'm getting a bad_any_cast when inserting into the map. Shouldn't any_cast only be called by it->second(x) ? What am I doing wrong?

Keith
  • 131
  • 3
  • 8
  • Have you considered using `boost::variant` for which visitors are supported out of the box? The use of `any` presumes that the types can be *anything*, that is *all* types in the type system. `variant` assumes that there is a subset of the types that you might want to use in the object. A visitor is closer to a `variant` as the different functions must be defined. – David Rodríguez - dribeas Nov 15 '12 at 14:11
  • My intention is to use this to write out config files from boost::program_options, which uses boost::any. – Keith Nov 15 '12 at 14:30

2 Answers2

3

You can't cast _1 to T (at the time of the bind expression).

You need a lazy cast. Perhaps define a function and use a nested bind expression, or use Boost Phoenix with a custom any_cast actor.

Here's the nested bind approach:

#include <boost/any.hpp>
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/unordered_map.hpp>

struct type_info_hash {
    std::size_t operator()(std::type_info const & t) const {
        return 42; // t.hash_code();
    }
};

struct equal_ref {
    template <typename T> bool operator()(boost::reference_wrapper<T> a,boost::reference_wrapper<T> b) const {
        return a.get() == b.get();
    }
};
struct any_visitor {
    boost::unordered_map<boost::reference_wrapper<std::type_info const>, boost::function<void(boost::any&)>, type_info_hash, equal_ref> fs;

    template <typename T> static T any_cast_f(boost::any& any) { return boost::any_cast<T>(any); }

    template <typename T> void insert_visitor(boost::function<void(T)> f) {
        try {
            fs.insert(std::make_pair(boost::ref(typeid(T)), boost::bind(f, boost::bind(any_cast_f<T>, boost::lambda::_1))));
        } catch (boost::bad_any_cast& e) {
            std::cout << e.what() << std::endl;
        }
    }

    bool operator()(boost::any & x) {
        boost::unordered_map<boost::reference_wrapper<std::type_info const>, boost::function<void(boost::any&)>, type_info_hash, equal_ref>::iterator it = fs.find(boost::ref(x.type()));
        if (it != fs.end()) {
            it->second(x);
            return true;
        } else {
            return false;
        }
    }
};

struct abc {};

void fa(int i) { std::cout << "fa(" << i << ")" << std::endl; }
void fb(abc) { std::cout << "fb(abc())" << std::endl; }

int main() {
    any_visitor f;
    f.insert_visitor<int>(fa);
    f.insert_visitor<abc>(fb);

    std::vector<boost::any> xs;
    xs.push_back(1);
    xs.push_back(abc());
    xs.push_back(1.5);

    for (auto it=xs.begin(); it!=xs.end(); ++it)
        if (!f(*it)) std::cout << "no visitor registered" << std::endl;
}

Prints output:

fa(1)
fb(abc())
no visitor registered
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Note I replaced the type_info_hash functor body to let it compile on my system. Nested bind expressions rock, though :) – sehe Nov 15 '12 at 14:30
  • Looks like I forgot to remove the -std=c++11 flag when I was working on this. I'll need to write my own type_info hash_code method. I also missed removing the 'auto' in the loop in main. – Keith Nov 15 '12 at 14:33
  • I noticed those too; Not as hard to fix as the lambda -> nested bind though :) (Shot in the dark: Perhaps you'd like to know how Boost Extension library tried to do portable typeinfo comparisons, I remember reading about for this: http://stackoverflow.com/a/5838527/85371) – sehe Nov 15 '12 at 14:55
1

Try to use extendable any https://sourceforge.net/projects/extendableany/?source=directory.

struct f_method
{
    typedef void (boost::mpl::_1::* signature) () const;

    template <typename T>
    struct wrapper
        : public T
    {
        void f() const
        {
            return this->call(f_method());
        }
    };

    struct implementation
    {
        void operator() (int i) const
        {
           std::cout << "fa(" << i << ")" << std::endl;
        }

        void operator() (abc) const
        {
           std::cout << "fb(abc())" << std::endl;
        }

        template <typename T>
        void operator() (const T& t) const
        {
            std::cout << "Errr" << std::endl;
        }
    };
};

typedef xany<boost::mpl::list<f_method> > any;

int main() {
    std::vector<any> xs;
    xs.push_back(1);
    xs.push_back(abc());
    xs.push_back(1.5);

    for (auto it=xs.begin(); it!=xs.end(); ++it)
        (*it).f();
}
ArmanHunanyan
  • 905
  • 3
  • 11
  • 24