最近本当につくづく思うのですが、プログラミングにおいて Compositeパターンだとか、再帰的な 構造というのが、物事をシンプルにするのにすごく役立つということを感じます。
教科書的な Compositeパターンとしては、Compositeクラスなるものが あるインターフェースを継承した オブジェクトをコンテナに保有していて、そのアクションをコンテナオブジェクトに委譲するというものだったり しますが、このアイデアを「型」すなわちクラスまで持っていくと、より強力な構築概念になるのではないかと思います。
TypelistはこのCompositeパターンを構成するのに非常に役立つ道具と言えると思います。ここで文字列チェーンクラスを考えてみましょう。
template <typename T, typename TL>
struct StringChain;
// Chain starter implementation
template <NullType, NullType>
struct StringChain
{
template <typename T>
StringChain< T, Typelist<NullType, NullType> > operator()(const T& obj) const
{
return StringChain<T, Typelist<NullType, NullType> >(obj, *this);
}
...
};
いきなり特殊化されたクラスを書き始めましたが、まずこの StringChainクラスはテンプレートの第一引数にある型、第二引数には、Typelistでエンコードされた型を取ります。そしてこの特殊化されたクラスは、Chainスタータと言って、文字列チェーンを作成開始するための特別なクラスとなります。 operator() 関数を使って、このクラスを成長させて行く訳です。template <typename T, typename TL>
struct StringChain
{
StringChain(const T& t, const StringChain& tl)
: head(t), tail(tl)
{
}
template < typename X >
StringChain< X, Typelist< T, TL > > operator()(const T& obj)
{
return StringChain< X, Typelist< T, TL > > (obj, *this);
}
std::string str() const
{
std::string ret_str;
ret_str.resize(getSize());
size_t offset = 0;
tail_.appendString(&ret_str[0], offset);
appendString(&ret_str[offset], offset);
return ret_str;
}
size_t getSize() const
{
return tail.getSize() + doGetSize(head);
}
void appendString(char* str_ptr, size_t& offset) const
{
tail.appendString(str_ptr, offset);
offset += doAppendString(str_ptr + offset, head);
return;
}
};
ここで doAppendString, doGetSize は StringChainの head要素に格納されているオブジェクトに対して 文字列を書き込む関数と文字列長を返却する関数です。 StringChainクラスにて扱う型に対してあらかじめ これらの関数を準備しておきます。
下記にサンプル実装をポストしました。
http://ideone.com/Ubmtd
It's quite long time since I posted C++ programming matter last time. This is again and again C++ Typelist.
Recently, I often come to think that so-called Composite pattern or some sort of recursive pattern / structure should be the key to simplify the structure in the programming.
According to some authoritative book or textbook kind of stuffs, what is explained as Composite pattern will be typically as follows. So-called Composite class which accommodates inherited class objects in its container or something like that, and then it delegates the very action to those objects. I would rather say that if we could successfully take this idea on to the level of type i.e. user-defined type (class), we can get more powerful construct.
And also I can say that Typelist can help to construct this pattern when it comes to applying Composite pattern into the type. As an example, let's consider the String chain class below.
template <typename T, typename TL>
struct StringChain;
// Chain starter implementation
template <NullType, NullType>
struct StringChain
{
template <typename T>
StringChain< T, Typelist<NullType, NullType> > operator()(const T& obj) const
{
return StringChain<T, Typelist<NullType, NullType> >(obj, *this);
}
...
};
Although this is the very specialized class template implementation, StringChain class here will take 2 template paramters which takes first argument as conventional type and second argument as typelist encoded type. And this specialized class template is called as 'Chain starter', which will trigger the string chain object creation. By making use of operator() function, we are going to make the string chain grow.
template <typename T, typename TL>
struct StringChain
{
StringChain(const T& t, const StringChain& tl)
: head(t), tail(tl)
{
}
template < typename X >
StringChain< X, Typelist< T, TL > > operator()(const T& obj)
{
return StringChain< X, Typelist< T, TL > > (obj, *this);
}
std::string str() const
{
std::string ret_str;
ret_str.resize(getSize());
size_t offset = 0;
tail_.appendString(&ret_str[0], offset);
appendString(&ret_str[offset], offset);
return ret_str;
}
size_t getSize() const
{
return tail.getSize() + doGetSize(head);
}
void appendString(char* str_ptr, size_t& offset) const
{
tail.appendString(str_ptr, offset);
offset += doAppendString(str_ptr + offset, head);
return;
}
};
Here, doAppendString and doGestSize function is the function to write head element to string buffer and size retrieval. We are supposed to prepare for appropriate overload functions towards types we are going to use with StringChain class in advance.
As for some sort of concrete code snippet, please take a look at the following. http://ideone.com/Ubmtd
Reference
Advanced C++ Metaprogramming - by Davide Di Gennaro