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! ]