node.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. # include <iostream>
  7. // Polymorphic list node base class
  8. struct node_base
  9. {
  10. node_base() : m_next(0) {}
  11. virtual ~node_base()
  12. {
  13. delete m_next;
  14. }
  15. node_base* next() const
  16. {
  17. return m_next;
  18. }
  19. virtual void print(std::ostream& s) const = 0;
  20. virtual void double_me() = 0;
  21. void append(node_base* p)
  22. {
  23. if (m_next)
  24. m_next->append(p);
  25. else
  26. m_next = p;
  27. }
  28. private:
  29. node_base* m_next;
  30. };
  31. inline std::ostream& operator<<(std::ostream& s, node_base const& n)
  32. {
  33. n.print(s);
  34. return s;
  35. }
  36. template <class T>
  37. struct node : node_base
  38. {
  39. node(T x)
  40. : m_value(x)
  41. {}
  42. void print(std::ostream& s) const { s << this->m_value; }
  43. void double_me() { m_value += m_value; }
  44. private:
  45. T m_value;
  46. };
  47. #endif // NODE_DWA2004110_HPP