Typelist with a list of class identifiers - c++

This is a discussion on Typelist with a list of class identifiers - c++ ; Hi All Here is the thing I want to do. There is a typelist (Modern C++ Design ... approach and Loki). I want this type list to return a list of class IDs for all classes which are part of ...

+ Reply to Thread
Results 1 to 8 of 8

Typelist with a list of class identifiers

  1. Default Typelist with a list of class identifiers

    Hi All

    Here is the thing I want to do. There is a typelist (Modern C++
    Design ...
    approach and Loki). I want this type list to return a list of class
    IDs for
    all classes which are part of the typelist. Let me give an example:

    class C1{

    public:

    enum{ class_id = 1 };

    }

    class C2{

    public:

    enum{ class_id = 2}

    }

    typedef TYPELIST_2( C1, C2 ) My_list;

    template < typename T_list >

    class Class_id_list;

    template < typename H, typename T >

    class Class_id_list{

    public:

    // ??? static const `hypothetical data structure` list = H ::
    class_id ??? Class_id_list< T > :: class_id;

    }


    // code for Nulltype - end of recursion
    template ...

    `hypothetical data structure` list = Class_id_list< My_list > :: list;
    ....

    Since it is not possible to make static const initialization for
    complex
    types in the class I do not know how to make ( if it is possible at
    all?)
    it. It looks like if you put this recursive definition outside of
    class then
    it is not used during template instantiation.

    The only way that comes to my mind is to use an old trick with bits
    and
    shifts:

    static unsigned long const list = H :: class_id | class_id_list< T
    > :: class_id << X; // x number of bits for class id


    But that is a silly implementation for some obvious reasons. Any ideas?

    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  2. Default Re: Typelist with a list of class identifiers

    In article
    <9c1ef349-70cc-46eb-9496-7cf3068e840b@t54g2000hsg.googlegroups.com>,
    <v_golikov@yahoo.com> wrote:

    > Hi All
    >
    > Here is the thing I want to do. There is a typelist (Modern C++
    > Design ...
    > approach and Loki). I want this type list to return a list of class
    > IDs for
    > all classes which are part of the typelist. Let me give an example:
    >
    > class C1{
    >
    > public:
    >
    > enum{ class_id = 1 };
    >
    > }
    >
    > class C2{
    >
    > public:
    >
    > enum{ class_id = 2}
    >
    > }
    >
    > typedef TYPELIST_2( C1, C2 ) My_list;
    >

    [snip]
    the following produces a typelist of Int2Type<class_id>'s in same order
    as given TypeList of classes:
    <quote>
    #include "typelist.h"
    #include "typemanip.h"


    template <class TL> struct get_ids;


    template <class T>
    struct get_class_id
    {
    typedef ::Loki::Int2Type<T::class_id> Result;
    };


    template <class H,class T>
    struct get_ids< ::Loki::Typelist<H,T> >
    {
    typedef typename
    ::Loki::Typelist
    <
    get_class_id<H>::Result,
    get_ids<T>::Result
    > Result;

    };

    template <>
    struct get_ids< ::Loki::NullType>
    {
    typedef ::Loki::NullType Result;
    };
    </quote>

    transforms a type list of said classes int a typelist of
    Int2Type<class_id>

    typedef TYPELIST_2(C1,C2) Types;
    typedef get_ids<Types>::Result Class_IDs;

    Class_IDs is
    Loki::Typelist
    <
    Loki::Int2Type<1>,
    Loki::Typelist
    <
    Loki::Int2Type<2>
    Loki::NullType
    >
    >;


    I ran my tests with
    <quote>
    #include "get_ids.hpp"

    template <int N>
    struct test
    {
    enum {class_id = N};
    };

    int main()
    {
    typedef TYPELIST_4(test<1>,test<2>,test<3>,test<4>) Types;
    typedef get_ids<Types>::Result Foo;
    Foo(30);
    }
    /* error indicates typeof Foo is:

    Loki::Typelist
    <
    Loki::Int2Type<1>,
    Loki::Typelist
    <
    Loki::Int2Type<2>,
    Loki::Typelist
    <
    Loki::Int2Type<3>,
    Loki::Typelist
    <
    Loki::Int2Type<4>,
    Loki::NullType
    >
    >
    >
    >


    which looks rignt
    */

    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  3. Default Re: Typelist with a list of class identifiers

    On Mar 12, 4:58 am, v_goli...@yahoo.com wrote:
    > Hi All
    >
    > Here is the thing I want to do. There is a typelist (Modern C++
    > Design ...
    > approach and Loki). I want this type list to return a list of class
    > IDs for
    > all classes which are part of the typelist. Let me give an example:
    >
    > class C1{
    >
    > public:
    >
    > enum{ class_id = 1 };
    >
    > }
    >
    > class C2{
    >
    > public:
    >
    > enum{ class_id = 2}
    >
    > }
    >
    > typedef TYPELIST_2( C1, C2 ) My_list;
    >
    > template < typename T_list >
    >
    > class Class_id_list;
    >
    > template < typename H, typename T >
    >
    > class Class_id_list{
    >
    > public:
    >
    > // ??? static const `hypothetical data structure` list = H ::
    > class_id ??? Class_id_list< T > :: class_id;
    >
    > }
    >
    > // code for Nulltype - end of recursion
    > template ...
    >
    > `hypothetical data structure` list = Class_id_list< My_list > :: list;
    > ...
    >
    > Since it is not possible to make static const initialization for
    > complex
    > types in the class I do not know how to make ( if it is possible at
    > all?)
    > it. It looks like if you put this recursive definition outside of
    > class then
    > it is not used during template instantiation.
    >
    > The only way that comes to my mind is to use an old trick with bits
    > and
    > shifts:
    >
    > static unsigned long const list = H :: class_id | class_id_list< T
    >
    > > :: class_id << X; // x number of bits for class id

    >
    > But that is a silly implementation for some obvious reasons. Any ideas?
    >


    It depends on what you want to do with your class_id_list. If you just
    want to do further template metaprogramming with it, then
    class_id_list *is* your typelist. To get a type's class_id from your
    typelist just use a get_class_id structure :

    template< class Typelist, class T >
    struct get_class_id {
    enum { value = get_class_id< typename Typelist::tail,
    T >::value };
    };

    template< class Typelist >
    struct get_class_id< Typelist, typename Typelist::head > {
    enum { value = Typelist::head::class_id };
    };

    ....

    get_class_id< My_list, C1 >::value ;


    Alexandre Courpron.


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  4. Default Re: Typelist with a list of class identifiers

    I was not very specific about what I want to do. I want to have an
    structure from which I can iterate through the list of class IDs.

    type_smothing list = Class_id_list< TYPELIST.... >

    for ( ... list ... ){

    do-action-for-each-class-id;

    }


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  5. Default Re: Typelist with a list of class identifiers

    > It depends on what you want to do with your class_id_list.

    I want to be able to iterate through this list. E.g. in the simplest
    case if I have just an array I want to be able to run a simple loop on
    this list.

    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  6. Default Re: Typelist with a list of class identifiers

    On 12 mar, 04:58, v_goli...@yahoo.com wrote:
    > Hi All
    >
    > Here is the thing I want to do. There is a typelist (Modern C++
    > Design ...
    > approach and Loki). I want this type list to return a list of class
    > IDs for
    > all classes which are part of the typelist.


    typedef mpl::map<
    mpl:air<C1, mpl::int_<1> >,
    mpl:air<C2, mpl::int_<2> >,
    ...
    mpl:air<Cn, mpl::int_<n> >
    > list;



    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  7. Default Re: Typelist with a list of class identifiers

    On Mar 13, 2:45 am, v_goli...@yahoo.com wrote:
    > > It depends on what you want to do with your class_id_list.

    >
    > I want to be able to iterate through this list. E.g. in the simplest
    > case if I have just an array I want to be able to run a simple loop on
    > this list.
    >


    You can define a for_each_class_id structure that takes as a parameter
    a functor to be applied to each class_id :

    // for_each_class_id definition

    template < class T >
    struct for_each_class_id {
    template < class U >
    static void run( U& functor )
    {
    functor( T::Head::class_id );
    for_each_class_id< T::Tail >::run( functor );
    }
    };

    template <>
    struct for_each_class_id< NullType > {
    template < class U >
    static void run( U& functor ) {}
    };

    // the functor that will be applied to each class_id

    struct process_class_id {
    void operator()( int class_id ) {
    // your code that will be applied to each class_id here
    // example : std::cout << class_id ;
    // or store class_id in a standard container for later use,
    etc.
    }
    };

    // now you can use the for_each_class_id :

    for_each_class_id< MyTypelist >::run( process_class_id() );


    Alexandre Courpron.


    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


  8. Default Re: Typelist with a list of class identifiers

    In article
    <60120a13-0398-465a-8ea6-375f5e4a6326@p25g2000hsf.googlegroups.com>,
    Mathias Gaunard <loufoque@gmail.com> wrote:

    > On 12 mar, 04:58, v_goli...@yahoo.com wrote:
    > > Hi All
    > >
    > > Here is the thing I want to do. There is a typelist (Modern C++
    > > Design ...
    > > approach and Loki). I want this type list to return a list of class
    > > IDs for
    > > all classes which are part of the typelist.

    >
    > typedef mpl::map<
    > mpl:air<C1, mpl::int_<1> >,
    > mpl:air<C2, mpl::int_<2> >,
    > ...
    > mpl:air<Cn, mpl::int_<n> >
    > > list;


    or perhaps reversing the contents of the map one needs a meta function
    class to convert a class_id to an int_. and then a struct inheriting
    from fold, could create the mpl map from a list of classes, reducing
    chances of typos...
    a mpl::map
    < mpl:air<mpl::int_<Class1::class_id>, Class1>,
    mpl:air<mpl::int_<Class2::class_id>, Class2>,
    ...
    > classMap;

    this provides easy look of class type from class_id, getting the
    id of a class type is easy. Now a simple macro since the pair's only
    differ in class_types makes this simpler.

    #define INSERT_MPL_MAP_OF_CLASSES(x)\
    boost::mpl_pair<boost::mpl::int_<x::class_id>,x>

    makes this easier if desired. It should be possible to build the map
    from an mpl sequence of classes, via fold. Result is to find type of
    a class with class_id = x in the map it is just boost::mpl::at<classMap,
    mpl::int_<x> >::type;

    template <class classMap,int N>
    struct get_class_from_id:boost::mpl::at
    <
    classMap,
    boost::mpl::int_<N>
    >{};


    // is there a class with given id?
    template<class classMap,int ID>
    struct class_exists:boost::mpl::has_key
    <
    classMap,
    boost::mpl::int_<ID>
    >{};

    etc.

    Even the boost preprocessor lib can automate more with BOOST_PP
    sequences and all the non-boost macros can be undef'ed after the
    typedef of the mpl map is created.

    A map can be iterated like a vector so if we write

    template <class P,class Base>
    struct do_sometning:do_something<typename boost::mpl::front<Base>::type,
    typename boost::mpl:op_front<Base> >
    {
    static void do_something(int n)
    {
    if(n == mpl::first<P>::type::value)
    {
    //processs an mpl::second<P>::type
    }
    else
    do_something< ...>::do_something(n);
    };
    }

    template <class

    --
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]
    [ comp.lang.c++.moderated. First time posters: Do this! ]


+ Reply to Thread