2012/02/12

Typelist exercise - forEach operation toward types



C++に関係する書籍で、中級者・上級者プログラマーに大きな影響を与えたものとしては
やはり Modern C++ Design ではないでしょうか?私がこの本に出会ったのは非常に
遅かったですが、その内容には目から鱗の連続だったと記憶しています。

その中でも最もインパクトがあったのはやはり Typelistでしょう。templateの特殊化などを
駆使して型の操作を行う様は、それまでに全く想像しなかったものでした。

ここではこのTypelist操作の練習とし、型に対するfor_eachを実現してみます。Typelistの
型に対する操作を走査します。

ここでファンクタは、Typelistの各要素型に対する操作を行う訳ですから、templateの operator()となる訳です。

for_eachを体現するテンプレートクラス forEach_tを書いてみます。

template <class TL, class Functor>
struct forEach_t
{
    forEach_t(Functor& functor)
    {
        typedef typename TL::Head THead;
        functor.template operator()<THead>();
        forEach_t<typename TL::Tail, Functor> nested(functor);
        (void) nested;
        return;
    }
};

template <class T, class Functor>
struct forEach_t< Typelist<T, NullType>, Functor>
{
    forEach_t(Functor& functor)
    {
        functor.template operator()<T>();
        return;
    }
};

ここで、Functorクラスは与えた引数で推論できるようにテンプレート関数を導入します。

template <class TL, class Functor>
inline void forEach(const Functor& functor)
{
    Functor func(functor);
    forEach_t<TL, Functor> temp(func);
    return;
}

では、実際に使ってみましょう。ファンクタはあまり凝らずに typeid()で操作対象の型情報を 出力するだけにしています。

#include <iostream>
#include <typeinfo>
#include "typelist.h" // forEach, foeEach_t is also defined here

class TestFunctor
{
public:
    TestFunctor()
    {
    }

    template <typename T>
    void operator()()
    {
        std::cout << "Test Functor executed - typeid [" << typeid(T).name() << " ] " << std::endl;
    }
};

struct TestClass1 {};
struct TestClass2 {};
struct TestClass3 {};
struct TestClass4 {};

typedef MakeTypelist<TestClass1, TestClass2, TestClass3, TestClass4>::Result TL;

int main(int argc, char* argv[])
{
    (void) argc; (void) argv;

    forEach<TL>(TestFunctor());
}

実行結果は下記のような感じになりました。

Test Functor executed - typeid [10TestClass1 ] 
Test Functor executed - typeid [10TestClass2 ] 
Test Functor executed - typeid [10TestClass3 ] 
Test Functor executed - typeid [10TestClass4 ] 

きちんと for_each操作されているようですね。




Among various books on C++, I wonder that Modern C++ Design is one of the books
that impose pretty big impact on intermediate / advanced C++ programmer.
Although the time I met this book was quite late, I surely remember that various
things covered in this book were all quite impressed me very much. If I'm supposed
to raise the most impressive technique, it must be 'Typelist' as you may relate to.
Making use of template specialization etc., a sort of type manipulation using Typelist
was something I could not imagine until then, indeed...

Here, I will show you some exercise on how to do for_each operation towards
various types in Typelist

Since functor being used in this for_each operation will be handling Type, operator()
member function shall be template member function.

Please take a look at the following forEach_t class which has the very for_each
operation towards Types.

template <class TL, class Functor>
struct forEach_t
{
    forEach_t(Functor& functor)
    {
        typedef typename TL::Head THead;
        functor.template operator()<THead>();
        forEach_t<typename TL::Tail, Functor> nested(functor);
        (void) nested;
        return;
    }
};

template <class T, class Functor>
struct forEach_t< Typelist<T, NullType>, Functor>
{
    forEach_t(Functor& functor)
    {
        functor.template operator()<T>();
        return;
    }
};

here, we're going to introduce inline template function to deduce Functor class type.

template <class TL, class Functor>
inline void forEach(const Functor& functor)
{
    Functor func(functor);
    forEach_t<TL, Functor> temp(func);
    return;
}

See how it works by writing some simple client code. Here, I've used typeid() to do some operation towards various types.

#include <iostream>
#include <typeinfo>
#include "typelist.h" // forEach, foeEach_t is also defined here

class TestFunctor
{
public:
    TestFunctor()
    {
    }

    template <typename T>
    void operator()()
    {
        std::cout << "Test Functor executed - typeid [" << typeid(T).name() << " ] " << std::endl;
    }
};

struct TestClass1 {};
struct TestClass2 {};
struct TestClass3 {};
struct TestClass4 {};

typedef MakeTypelist<TestClass1, TestClass2, TestClass3, TestClass4>::Result TL;

int main(int argc, char* argv[])
{
    (void) argc; (void) argv;

    forEach<TL>(TestFunctor());
}

We can get the following result. Now we can see that we could make it.

Test Functor executed - typeid [10TestClass1 ] 
Test Functor executed - typeid [10TestClass2 ] 
Test Functor executed - typeid [10TestClass3 ] 
Test Functor executed - typeid [10TestClass4 ] 


0 件のコメント:

コメントを投稿