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! ]
|
|
0
|
|
|
|
Reply
|
v_golikov
|
3/11/2008 9:58:00 PM |
|
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! ]
|
|
0
|
|
|
|
Reply
|
Carl
|
3/12/2008 2:12:38 PM
|
|
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! ]
|
|
0
|
|
|
|
Reply
|
courpron
|
3/12/2008 2:16:05 PM
|
|
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! ]
|
|
0
|
|
|
|
Reply
|
v_golikov
|
3/12/2008 7:44:38 PM
|
|
> 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! ]
|
|
0
|
|
|
|
Reply
|
v_golikov
|
3/12/2008 7:45:00 PM
|
|
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::pair<C1, mpl::int_<1> >,
mpl::pair<C2, mpl::int_<2> >,
...
mpl::pair<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! ]
|
|
0
|
|
|
|
Reply
|
Mathias
|
3/12/2008 7:45:11 PM
|
|
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! ]
|
|
0
|
|
|
|
Reply
|
courpron
|
3/13/2008 1:12:26 PM
|
|
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::pair<C1, mpl::int_<1> >,
> mpl::pair<C2, mpl::int_<2> >,
> ...
> mpl::pair<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::pair<mpl::int_<Class1::class_id>, Class1>,
mpl::pair<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::pop_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! ]
|
|
0
|
|
|
|
Reply
|
Carl
|
3/13/2008 1:12:39 PM
|
|
|
7 Replies
173 Views
(page loaded in 0.101 seconds)
|