Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly chain boost::mpl::inherit_linearly and boost::mpl::inherit so placeholders can be resolved?

Say I have these types:

template
<
    class T,
    template <class> class Storage
>
struct AbstractFactoryUnit
{
    virtual ~AbstractFactoryUnit() {}
    virtual typename Storage< T >::StoredType doCreate(Storage< T >) = 0;
};

and

template
<
    class TypeSequence,
    template <class> class ProductStorage,
    template <class, template <class> class> class Unit = AbstractFactoryUnit
>
struct AbstractFactory
    : boost::mpl::inherit_linearly
        <
            TypeSequence,
            boost::mpl::inherit
            <
                boost::mpl::_1,
                Unit< boost::mpl::_2, ProductStorage >
            >
        >::type
{
    typedef TypeSequence Products;

    template <class T>
    auto create() -> typename ProductStorage< T >::StoredType
    {
        Unit< T, ProductStorage >& unit = *this;
        unit.doCreate(ProductStorage< T >());
    }
};

Now I want to implement le AbstractFactory...

Some lol types:

struct Foo {};
struct Bar {};
struct Baz {};

A lol storage:

template <class T>
struct RawPointerStorage
{
    typedef T* StoredType;
};

and finally the implementation:

struct FooBarBaz
    : AbstractFactory< boost::mpl::set< Foo, Bar, Baz >, RawPointerStorage >
{
    A* doCreate(RawPointerStorage< Foo >) override
    {
         return new A;
    }

    B* doCreate(RawPointerStorage< Bar >) override
    {
         return new B;
    }

    C* doCreate(RawPointerStorage< Baz >) override
    {
         return new C;
    }
};

Unfortunately, the compiler complains:

1>C:\Libs\boost\boost_1_51_0\boost/mpl/aux_/preprocessed/plain/inherit.hpp(20): error C2500: 'boost::mpl::inherit2<T1,T2>' : 'AbstractFactoryUnit<T,ProductStorage>' is already a direct base class
1>          with
1>          [
1>              T1=AbstractFactoryUnit<boost::mpl::_2,RawPointerStorage>,
1>              T2=AbstractFactoryUnit<boost::mpl::_2,RawPointerStorage>
1>          ]
1>          and
1>          [
1>              T=boost::mpl::_2,
1>              ProductStorage=RawPointerStorage
1>          ]

I'm a little bit confused since it compiles just fine when AbstractFactoryUnit accepts only one template parameter. My guess is the compiler cannot "resolve" the second placeholder, but I should admit I don't know why -- since I don't know well how boost invokes apply on placeholders.

I use VS2012 with either vc100 or vc110.

Any idea? (yes, I was playing with the AbstractFactory described in modern C++ design)

EDIT: I finally decided to provide my whole AbstractFactory code without disguises in both my question and my answer.

like image 916
mister why Avatar asked Jan 17 '13 20:01

mister why


1 Answers

I don't know exactly why --in this context-- the second placeholder cannot be "expanded" but I found out that wrapping the expression boost::mpl::inherit solved my problem.

So here you are, this AbstractFactory in a nutshell:

We encapsulate the implementation in a namespace Impl:

namespace Impl
{

    template
    <
        class TypeSequence,
        template <class> class ProductStorage,
        template <class, template <class> class> class Unit
    >
    struct AbstractFactory
    {
    private:
        template <class T, class U>
        struct Inherit : boost::mpl::inherit< T, Unit< U, ProductStorage > >
        {};

    public:
        typedef typename boost::mpl::inherit_linearly
                            <
                                TypeSequence,
                                // the trick is on the following line
                                Inherit< boost::mpl::_1, boost::mpl::_2 >
                            >
                            ::type Type;
    };

} // namespace Impl

and we derive from it like so:

template
<
    class TypeSequence,
    template <class> class ProductStorage = RawPointerStorage,
    template <class, template <class> class> class Unit = AbstractFactoryUnit
>
struct AbstractFactory
    : Impl::AbstractFactory< TypeSequence, ProductStorage, Unit >::Type
{
    typedef TypeSequence Products;

    template <class T>
    auto create() -> typename ProductStorage< T >::StoredType
    {
        Unit< T, ProductStorage >& unit = *this;
        return unit.doCreate(ProductStorage< T >());
    }
};
like image 51
mister why Avatar answered Nov 16 '22 21:11

mister why