Template recursive class

Discussion in 'C++' started by Jon Slaughter, Sep 28, 2005.

  1. I've managed to put together a template class that basicaly creates a
    recursive tree that lets you easily specify the "base" class of that tree
    and and ending notes and lets you stop the recursive process if you want.

    The problem now is to make a Type list so I can specify more than one node
    at a time to "attach" a class to. I think I will be able to handle this but
    I want to run the code by you guys to see there are any major design flaws.


    The code is just a "prototype" and will be rewritten once I get the idea
    done. The idea is to get something like

    sTree<4, 4, AllClass, MyEndClassLeft, MyEndClassRight, TypeList<Node<2, 3,
    MyClassA>, Node<3, 3, MyClassB>, Node<1, 3, MyClassC>> > S;

    Which would produce a tree that as for each node a class of AllClass except
    the nodes given in the type list and the last nodes are give by
    MyEndClassLeft and MyEndClassRight.

    Ofcourse right now I can only specify one Node type because I have not
    implemented a TypeList.


    Anyways, while this models my original problem more than enough I thought it
    would be nice to make it as general as I possibly could. But maybe it could
    be done much easier? One thing I'm not sure is a good idea is to have the
    "user attached" classes be the base classes for the nodes because this might
    cause problems with casting? But that was the only way I could see to make
    it simple enough without having the user have to worry about deriving things
    and all that.

    Any ideas?

    Thanks, Jon

    (The code, again, isn't ment to be perfect... each function in the classes
    are just for "looks" and I could have done a better job at something but
    please only stick to the real issues)

    Oh, one more thing, is there an easy way to have the user choose from using
    a vector based container(or whatever type of container) instead of just a
    type..

    i.e.


    sTree<I-1, J, A, L, R, T> Left;

    into something like

    list<*sTree<I-1, J, A, L, R, T> > Left;

    without having to do a crap load of specializing?

    i.e.

    sTree<4, 4, List, Vector, AllClass, MyEndClassLeft, MyEndClassRight, Node<2,
    3, MyClass> > S;

    Where sTree uses lists for the Left branches and vectors for the right.



    ----------------------------------------

    #include <iostream>

    using namespace std;

    struct NullClass { void Null() { } };

    template <unsigned int i, unsigned int j, typename T, unsigned int s = 0>
    struct Node
    {
    enum {I = i};
    enum {J = j};
    enum {S = s};
    typedef T Class;
    };


    template <int I, int J, typename A, typename L, typename R, typename T>
    struct sTree : A
    {
    sTree<I-1, J, A, L, R, T> Left;
    sTree<I, J-1, A, L, R, T> Right;
    void sTreeFunc() { }
    };


    // Specializes node type for full recursive process
    template <typename A, typename L, typename R, unsigned int I, unsigned int
    J, typename T>
    struct sTree< I, J, A, L, R, Node<I,J,T,0> > : Node<I,J,T,0>::Class
    {
    sTree<I, J, A, L, R, T> Left;
    sTree<I, J, A, L, R, T> Right;
    void Spec() {}
    };

    // Specializes node type to end left recursive process
    template <typename A, typename L, typename R, unsigned int I, unsigned int
    J, typename T>
    struct sTree< I, J, A, L, R, Node<I,J,T,1> > : Node<I,J,T,1>::Class
    {
    sTree<I, J, A, L, R, T> Right;
    void Spec() {}
    };

    // Specializes node type to end right recursive process
    template <typename A, typename L, typename R, unsigned int I, unsigned int
    J, typename T>
    struct sTree< I, J, A, L, R, Node<I,J,T,2> > : Node<I,J,T,2>::Class
    {
    sTree<I, J, A, L, R, T> Left;
    void Spec() {}
    };

    // Specializes node type to end recursive process
    template <typename A, typename L, typename R, unsigned int I, unsigned int
    J, typename T>
    struct sTree< I, J, A, L, R, Node<I,J,T,3> > : Node<I,J,T,3>::Class
    {
    void Spec() {}
    };

    // Specializes last "left" node
    template <unsigned int J, typename A, typename L, typename R, typename T>
    struct sTree<0, J, A, L, R, T> : L
    {
    void End() { }
    };

    // Specializes last "right" node
    template <unsigned int I, typename A, typename L, typename R, typename T>
    struct sTree<I, 0, A, L, R, T> : R
    {
    void End() { }
    };



    // User defined classes for "attaching" to the tree
    struct AllClass { void All() { } };
    struct MyClass { void MyFunc() { } };
    struct MyEndClassLeft { void MyEndLeft() { } };
    struct MyEndClassRight { void MyEndRight() { } };



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

    sTree<4, 4, AllClass, MyEndClassLeft, MyEndClassRight, Node<2, 3, MyClass> >
    S;

    system("PAUSE");
    return 0;
    }
    Jon Slaughter, Sep 28, 2005
    #1
    1. Advertising

  2. Jon Slaughter

    benben Guest

    Does it work?

    Ben
    benben, Sep 28, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. christopher diggins
    Replies:
    16
    Views:
    728
    Pete Becker
    May 4, 2005
  2. IR
    Replies:
    3
    Views:
    409
  3. n00m
    Replies:
    12
    Views:
    1,098
  4. vamsi
    Replies:
    21
    Views:
    2,041
    Keith Thompson
    Mar 9, 2009
  5. A L
    Replies:
    1
    Views:
    499
    Alf P. Steinbach /Usenet
    Aug 25, 2010
Loading...

Share This Page