Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2007-10-21 21:38:43


AMDG

Corrado Zoccolo <czoccolo <at> gmail.com> writes:

> BTW, the idea of the hand made vtable born in the discussion with gpd. My
> first implementation used just one function ptr to encapsulate all the
> behaviour (with a signature to fit all the methods), and was substantially
> less efficient than the current solution.

You might find this useful if you want to generalize.
http://tinyurl.com/2jap3m
(I wrote it when I was trying to solve this same
problem a year ago)

Basic usage:

#include <boost/vtable/make.hpp>
#include <boost/vtable/pointer.hpp>

struct print_function {
    typedef void (*function_pointer_type)(const void*);
    template<class T>
    stuct apply {
        static void value(const void* x) {
            std::cout << *static_cast<const T*>(x);
        }
    }
};

boost::vtable::pointer<
    boost::vtable::make<boost::mpl::vector<print_function> >::type
> table_type;

table_type table;

table.set<double>();

double x = 2.5;
void* vp = &x;

table.find<function1>()(vp); // prints 2.5

In Christ,
Steven Watanabe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk