By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,652 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,652 IT Pros & Developers. It's quick & easy.

Template recursive class

P: n/a
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;
}
Sep 27 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Does it work?

Ben
Sep 28 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.