1 // Copyright David Abrahams 2004. Use, modification and distribution is
2 // subject to the Boost Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4 #ifndef NODE_DWA2004110_HPP
5 # define NODE_DWA2004110_HPP
6 
7 # include <iostream>
8 
9 // Polymorphic list node base class
10 
11 struct node_base
12 {
node_basenode_base13     node_base() : m_next(0) {}
14 
~node_basenode_base15     virtual ~node_base()
16     {
17         delete m_next;
18     }
19 
nextnode_base20     node_base* next() const
21     {
22         return m_next;
23     }
24 
25     virtual void print(std::ostream& s) const = 0;
26     virtual void double_me() = 0;
27 
appendnode_base28     void append(node_base* p)
29     {
30         if (m_next)
31             m_next->append(p);
32         else
33             m_next = p;
34     }
35 
36  private:
37     node_base* m_next;
38 };
39 
operator <<(std::ostream & s,node_base const & n)40 inline std::ostream& operator<<(std::ostream& s, node_base const& n)
41 {
42     n.print(s);
43     return s;
44 }
45 
46 template <class T>
47 struct node : node_base
48 {
nodenode49     node(T x)
50       : m_value(x)
51     {}
52 
printnode53     void print(std::ostream& s) const { s << this->m_value; }
double_menode54     void double_me() { m_value += m_value; }
55 
56  private:
57     T m_value;
58 };
59 
60 #endif // NODE_DWA2004110_HPP
61