Modernized GAlib  3.0.0 current
GANode.h
1 /* ----------------------------------------------------------------------------
2  mbwall 25nov94
3  Copyright 1995 Massachusetts Institute of Technology
4 ---------------------------------------------------------------------------- */
5 
6 #pragma once
7 
8 #include <gaconfig.h>
9 
10 #include <ostream>
11 
12 
17 struct GANodeBASE
18 {
19  GANodeBASE *next, *prev, *parent, *child;
20  GANodeBASE()
21  {
22  next = nullptr;
23  prev = nullptr;
24  parent = nullptr;
25  child = nullptr;
26  }
28  {
29  next = n;
30  prev = p;
31  parent = par;
32  child = chi;
33  }
34  virtual ~GANodeBASE() = default;
35 };
36 
37 inline std::ostream &operator<<(std::ostream &os, GANodeBASE &arg)
38 {
39  os << " node: " << &arg << "\n";
40  os << " next: " << arg.next << "\n";
41  os << " prev: " << arg.prev << "\n";
42  os << " child: " << arg.child << "\n";
43  os << " parent: " << arg.parent << "\n";
44  return (os);
45 }
46 
47 /* ----------------------------------------------------------------------------
48  GANode
49 -------------------------------------------------------------------------------
50  This node is a container for any kind of object you want to put into a list.
51 Since it is a sub-class of the BASE node object, it inherits all of the next,
52 prev, etc members of that class. All we add here is support for the type-
53 specific contents.
54  Beware of what you put in for the type. Remember that it
55 can be expensive to use an object rather than a pointer (if you have big
56 objects, for example). We define this template using the object rather than
57 a pointer so that it can be use for ints, chars, or other small objects. If
58 you are going to contain a large object, you should instantiate it as a pointer
59 to the object, not the object itself.
60  Note also that we use the copy constructor to intialize the object rather
61 than doing an assignment! This assumes that the object you put into this
62 container has a properly functioning copy initializer and avoids the default
63 construction followed by assignment that you'd get with operator=.
64  You must have a copy creator defined for any object that goes into the nodes.
65 The operator= must defined as well (it is used when the node contents get
66 reassigned). We do not allow the nodes to be created without any arguments.
67  Summary of methods your object must have:
68  copy initializer
69  operator=
70  The node always owns its contents; when the node is destroyed, the contents
71 of the node get destroyed as well.
72 ---------------------------------------------------------------------------- */
78 template <class T> struct GANode : public GANodeBASE
79 {
80  T contents;
81  // GANode() : GANodeBASE(), contents() {}
82  explicit GANode(const T &t) : GANodeBASE(), contents(t) {}
83  ~GANode() override = default;
84  T &operator()(const T &t)
85  {
86  contents = t;
87  return contents;
88  }
89 };
90 
91 template <class T> std::ostream &operator<<(std::ostream &os, GANode<T> &arg)
92 {
93  os << " node: " << &arg << "\n";
94  os << " next: " << arg.next << "\n";
95  os << " prev: " << arg.prev << "\n";
96  os << " child: " << arg.child << "\n";
97  os << " parent: " << arg.parent << "\n";
98  return (os);
99 }
This is the basic node object.
Definition: GANode.h:18
Definition: GANode.h:79