Boost logo

Boost Users :

From: Paul Mensonides (pmenso57_at_[hidden])
Date: 2004-09-01 21:02:36


By "typelist", do you mean the template parameters (e.g. class T0, class T1,
etc., in your code below)? If so, the following implements the member function:
 
#include <boost/preprocessor/iteration/local.hpp>
#include <boost/preprocessor/repetition/enum_params.hpp>
 
#ifndef TYPELIST_MAX_SIZE
#define TYPELIST_MAX_SIZE 7
#endif
 
template <BOOST_PP_ENUM_PARAMS(TYPELIST_MAX_SIZE, class T)>
IObject const * ObjectImpl::getInterface(InterfaceID const & iid)
{
    IObject const * obj = 0;
    switch (iid)
    {
        case IObject::IID :
        {
            this->acquire();
            obj = static_cast<IObject const *>(this);
            break;
        }
        #define BOOST_PP_LOCAL_LIMITS (0, TYPELIST_MAX_SIZE - 1)
        #define BOOST_PP_LOCAL_MACRO(n) \
            case T ## n::IID: \
            { \
                this->acquire(); \
                obj = static_cast<T const *>(this); \
                break; \
            } \
            /**/
        #include BOOST_PP_LOCAL_ITERATE()
        default :
        {
            throw InterfaceUnsupportedException(iid)
            break
        }
    }
    return obj;
}

Regards,
Paul Mensonides

  _____

From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Allen Gooch
Sent: Wednesday, September 01, 2004 11:33 AM
To: boost-users_at_[hidden]
Subject: [Boost-users] Preprocessor lib question wrt to vertical repetition

Given the following

 

#include <boost/preprocessor/repetition/enum_params.hpp>

 

#ifndef TYPELIST_MAX_SIZE

# define TYPELIST_MAX_SIZE 7

#endif

 

template <BOOST_PP_ENUM_PARAMS(TYPELIST_MAX_SIZE, class T)>

class ObjectImpl : BOOST_PP_ENUM_PARAMS(TYPELIST_MAX_SIZE, public T), public
ObjectBase<REFCNTR>

{

public:

 

    virtual Refcount acquire() const

    {

        return Base::acquire();

    }

 

    virtual Refcount release() const

    {

        return Base::release();

    }

 

    virtual Refcount getRefcount() const

    {

        return Base::getRefcount();

    }

 

    virtual bool hasInterface(InterfaceID const & iid) const

    {

        return Base::hasInterface(iid);

    }

 

    virtual IObject * getInterface(InterfaceID const & iid)

    {

        IObject const * const_this = (IObject const *) this;

        return (IObject *) const_this->getInterface(iid);

    }

 

    virtual IObject const * getInterface(InterfaceID const & iid)

    {

        IObject const * obj = 0;

        switch (iid)

        {

            case IObject::IID :

            {

                this->acquire();

                obj = static_cast<IObject const *>(this);

                break;

            }

            // Desired: for each type T in typelist generate the following case
block

            // case T::IID :

            // {

            // this->acquire();

            // obj = static_cast<T const *>(this);

            // break;

            // }

            default :

            {

                throw InterfaceUnsupportedException(iid)

                break

            }

        }

        return obj;

    }

 

protected:

 

    ObjectImpl()

    {

    }

 

    virtual ~ObjectImpl()

    {

    }

 

    typedef ObjectBase<REFCNTR> Base;

    

};

 

 

What is the best way to generate a case block for each type in the typelist?
I've been looking at the Preprocessor appendix from the "C++ Template
Metaprogramming" book (can't wait!) and see three different approaches to
vertical repetition: local, file and self; however, I'm not clean on how to
express what I want using these approaches. Can someone shed some light, or
point me to an example in the Boost PP docs which solves a similar problem?

 

Many thanks in advance.

 

-allen

 



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net