1
0

node.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #ifndef _NODE_H
  2. #define _NODE_H
  3. #include "ptrlist.h"
  4. //
  5. // node.h
  6. //
  7. // Some node classes.
  8. //
  9. // ==========================================================================
  10. //
  11. // class Node
  12. //
  13. // A generic "node" object from which one may subclass and create a set of
  14. // disparate objects that may be assembled into a hierarchical tree.
  15. //
  16. class Node {
  17. private:
  18. PtrList<Node> childlist;
  19. Node * parent;
  20. protected:
  21. #ifdef _DEBUG
  22. static int count;
  23. #endif//_DEBUG
  24. public:
  25. Node(Node * myParent = NULL) : childlist(), parent(myParent) {
  26. #ifdef _DEBUG
  27. count++;
  28. #endif//_DEBUG
  29. }
  30. Node(const Node & a) : childlist(a.childlist), parent(a.parent) {
  31. #ifdef _DEBUG
  32. count++;
  33. #endif//_DEBUG
  34. }
  35. virtual ~Node() {
  36. #ifdef _DEBUG
  37. count--;
  38. #endif//_DEBUG
  39. };
  40. int nodeCount() {
  41. #ifdef _DEBUG
  42. return count;
  43. #else //_DEBUG
  44. return -1;
  45. #endif//_DEBUG
  46. }
  47. //
  48. // Const Data Accessor Methods
  49. int getNumChildren() const {
  50. return childlist.getNumItems();
  51. }
  52. Node * enumChild(int index) const {
  53. return childlist[index];
  54. }
  55. Node * getParent() const {
  56. return parent;
  57. }
  58. //
  59. // Nonconst Data Manipulator Methods
  60. Node * addChild(Node * child) {
  61. child->setParent(this);
  62. return childlist.addItem(child);
  63. }
  64. Node * setParent(Node * myParent) {
  65. return parent = myParent;
  66. }
  67. PtrList< Node > & ChildList() {
  68. return childlist;
  69. }
  70. };
  71. // ==========================================================================
  72. //
  73. // class NodeC< TPayload >
  74. //
  75. // If you would rather use Node as a container class than as a base class,
  76. // this object will allow you to contain a payload instance and be accessed
  77. // as if it were the payload instance using a reference operator and an
  78. // implicit cast operator.
  79. //
  80. template < class TPayload >
  81. class NodeC : public Node {
  82. protected:
  83. TPayload payload;
  84. public:
  85. NodeC( const TPayload & myPayload, NodeC * myParent = NULL ) : Node( myParent ), payload( myPayload ) {}
  86. NodeC( const NodeC & a ) : Node( a ), payload( a.payload ) {}
  87. //
  88. // In addition to being able to call all of the Node methods, you can also
  89. // simply treat the node objects themselves as if they were the payload
  90. // instantiation through these simple methods:
  91. // Explicit reference operator - for lvalues and force rvalues.
  92. TPayload & operator () ( void ) { return payload; }
  93. // Implicit cast operator - for rvalues that can accept a TPayload &
  94. operator TPayload & ( void ) { return payload; }
  95. };
  96. #endif//_NODE_H