Boost logo

Boost :

From: Sebastian Faust (sfaust_at_[hidden])
Date: 2003-11-15 17:15:44


Hi,

some days ago I asked how it is possible to build a chain of
responsibility up from a given typelist. You told me to use the
following, which works very well:
  namespace mpl = boost::mpl;
    using namespace mpl::placeholders;

    struct pass_through_make
    {
        static vehicle* make( vehicle* ptr )
        {
            return ptr;
        }
    };

    template< typename T, typename Base > struct make_vehicle
    {
        static vehicle* make( vehicle* ptr )
        {
            return new T( Base::make( ptr ) );
        }
    };

    typedef mpl::fold<
          mpl::vector<car,bicycle,train>
        , pass_through_make
        , make_vehicle<_2,_1>
>::type factory;

    int main()
    {
        factory::make( 0 );
        return 0;
    }

What I now wanna, and I sadly don't know how to solve such, is the
follwing:

Given again the classes which I wanna create with some kind of special
factory:

class vehicle
{
     public:
         vehicle(vehicle* successor) : m_successor(successor) {}
         virtual vehicle* create(vehicle* successor) = 0;
         vehicle* m_successor;

 };

 class car : public vehicle
 {
     public:
            static const int _key = 1;
          virtual vehicle* create(vehicle* successor)
          {
             return new car(successor);
          }
};

class bicycle : public vehicle
{
     public:
            static const int _key = 2;
         virtual vehicle* create(vehicle* successor)
         {
             return new bicycle (successor);
         }
};

Now the main-Function should look the following:

void main()
{
        std::map<int, boost::shared_ptr<vehicle> > myMap;
        factory::make(myMap);
}

After this the map should contain one object from type car with the key
1, and one object from type bicycle with the key 2.

How can I solve such with mpl?

I found a way to do it with vectors and lists. Code how it worked
following below. But somehow with maps this code has problems:
                
template <typename TypeContainer>
            struct container_factory
{
        template <typename Container>
        struct container_holder
        {
                public:
                        template<typename T>
                        void operator()(T*)
                        {
                                typename Container::value_type _elem(
new T);
                                                m_c.push_back(_elem);
                        }
                        container_holder(Container& v) : m_c(v) {}
                private:
                        Container& m_c;
        };

        template<typename T>
        static container_holder<T> make_container(T& t)
        {
                return container_holder<T>(t);
        }

        template<typename T>
        static void create(T& t)
        {
                boost::mpl::for_each<TypeContainer,
                        boost::add_pointer<boost::mpl::_1>
>(make_container(t));
        }

};

I know to use maps as containers, I have to use in the operator()(T*)
function instead of push_back some kind of insert, but if I change that,
I still get a compiler error.

Thanks a lot in advance for any help.
Sebastian


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