开发者

Typelists visitor pattern example

I'm interested in Typelists . At this URLhttp://drdobbs.com/1844开发者_开发技巧03813 there is a good example of how using Typelists for creating a visitor pattern.

I have two questions about this example. My two questions are at the end of this topic.

Consider the code below :

void SomeOperation(DocumentItem* p)
{
    if (TextArea* pTextArea = dynamic_cast<TextArea*>(p))
    {
        ... operate on a TextArea object ...
    }
    else if (VectorGraphics* pVectorGraphics =
        dynamic_cast<VectorGraphics*>(p))
    {
        ... operate on a VectorGraphics object ...
    }
    else if (Bitmap* pBitmap = dynamic_cast<Bitmap*>(p))
    {
        ... operate on a Bitmap object ...
    }
    else
    {
        throw "Unknown type passed";
    }
}

the incovenients of this code is according to Alexandrescu are:

In addition to being thoroughly ugly, the code above has the conceptual problem of being unable to catch at compile time the "forgot to handle this type"

So are coming typelists :

    #include<iostream>

class null_typelist {};

template <class H, class T>
struct typelist
{
    typedef H head;
    typedef T tail;
};

template<class T1, class T2=null_typelist, class T3=null_typelist, class T4=null_typelist> struct cons;

template <class T1>
struct cons<T1, null_typelist, null_typelist,null_typelist>
{
    typedef typelist<T1, null_typelist> type;
};

template <class T1, class T2>
struct cons<T1, T2, null_typelist, null_typelist>
{
    typedef typelist<T1, typelist<T2,null_typelist> > type;
};

template <class T1, class T2, class T3>
struct cons<T1, T2, T3, null_typelist>
{
    typedef typelist<T1, typelist<T2, typelist<T3,null_typelist> > > type;
};

template <class T1, class T2, class T3, class T4>
struct cons
{
    typedef typelist<T1, typelist<T2, typelist<T3,typelist<T4, null_typelist> > > > type;
};


template <class tlist> class AdHocVisitor;

template <class H, class T>
class AdHocVisitor< typelist<H, T> > : public AdHocVisitor<T>
{
public:
    virtual void Visit(H*) = 0;

    template <class SomeClass>
    void StartVisit(SomeClass* p)
    {
        if (H* pFound = dynamic_cast<H*>(p))
        {
            Visit(pFound);
        }
        else
        {
            AdHocVisitor<T>::StartVisit(p);
        }
    }
};

template <class H>
class AdHocVisitor< typelist<H, null_typelist> > 
{
public:
    virtual void Visit(H*) = 0;

    template <class SomeClass>
    void StartVisit(SomeClass* p)
    {
        if (H* pFound = dynamic_cast<H*>(p))
        {
            Visit(pFound);
        }
        else
        {
            throw "Unknown type passed";
        }
    }
};

struct DocElement{virtual ~DocElement(){};};
struct TextArea: DocElement{};
struct Bitmap: DocElement{};
struct VectorGraphics: DocElement{};

int main()
{
    typedef cons<TextArea,Bitmap,VectorGraphics>::type MyHierarchy;

    DocElement *p = new Bitmap;

    struct ConcreteVisitor : AdHocVisitor<MyHierarchy>
    {
        void Visit(TextArea* p){std::cout << "I'm a textarea" << "\n";}
        void Visit(VectorGraphics* p){std::cout << "I'm a VectorGraphics" << "\n";}
        void Visit(Bitmap* p){std::cout << "I'm a Bitmap" << "\n";}
    };

    ConcreteVisitor visitor;
    visitor.StartVisit(p);
    delete p;

    std::cin.get();
}

1- We still have dynamic_cast and a virtual function. SO I don't see well the benefict of having introduce typelists ?

2- At the end of this article Alexandrescu give some advises to improve this code , but I don't see very well how to implement these, could someone can help me on this ?

Thanks


What if you have 50 DocElement types? With the first example you need 50 if statements, with the second, you just need to have a typelist of 50 elements.

You can also think about what happens when you add another DocElement. In the first example, you need to go and change the if-else statements. With type-lists you can just add the new type to the end of the type-list.

The type-list code may seem like a lot of overhead, but you write it once, then you just use it, and instead of adding ifs or cases and code (which can get quite large over time) you just add types to the type-list. From a maintenance perspective the type-list code is far better than a huge switch statement or tens or hundreds of ifs.

As for improvements, I don't know, I'm still waiting for variadic templates and type aliases to be included in VS so that I can simplify the code even more.

Whenever I see a huge pile of repeating code, I start thinking about type-lists and metaprogramming, let the compiler do the work, not bored programmers. And the best part? You get zero penalty at runtime, it's just as efficient as the ifs (if you're careful with inlining)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜