Boost logo

Boost :

Subject: [boost] [TypeErasure] Similar simple class.
From: Arman Hunanyan (arman.hunanyan_at_[hidden])
Date: 2012-12-23 11:39:51


Hi All,

I have implemented xany class
https://sourceforge.net/projects/extendableany/?source=directory.
Then I noticed that there is a Boost.TypeErasure library which allows to do
same
as xany does and something more. Implementation of class xany has 1005
lines and
it is implemented and tested in a week. Of course xany isn't comparable with
Boost.TypeErasure, but it may be interesting to look. Below is a definition
of
same method for Boost.TypeErasure and for xany.

// definition of push_back for Boost.TypeErasure

template<class C, class T>
struct push_back
{
    static void apply(C& cont, const T& arg) { cont.push_back(arg); }
};

namespace boost {
namespace type_erasure {
    template<class C, class T, class Base>
    struct concept_interface< ::push_back<C, T>, Base, C> : Base
    {
        void push_back(typename rebind_any<Base, const T&>::type arg)
        {
            call(::push_back<C, T>(), *this, arg);
        }
    };
}
}

any<push_back<_self, int>, _self&> c;

// definition of push_back for xany

template <typename Xany, typename T>
struct push_back_method
{
    typedef void (Xany::* signature) (const T&);

    template <typename B>
    struct wrapper
        : public B
    {
        void push_back(const T& e)
        {
            this->call(push_back_method(), e);
        }
    };

    struct implementation
    {
        template <typename Container>
        void operator() (Container& c, const T& e)
        {
            c.push_back(e);
        }
    };
};

xany<boost::mpl::vector<push_back_method<boost::mpl::_1, int> > > c;

xany requires additional signature type to be specified. This allows to use
dispatching in implementation.

    struct implementation
    {
        void operator() (OtherCodingStyleContainer& c, const T& e)
        {
            c.PushBack(e);
        }

        template <typename Container>
        void operator() (Container& c, const T& e)
        {
            c.push_back(e);
        }
    };

Documentation contains more examples.


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