incomplete_type_test.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Boost.Pointer Container
  3. //
  4. // Copyright Thorsten Ottosen 2003-2005. Use, modification and
  5. // distribution is subject to the Boost Software License, Version
  6. // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // For more information, see http://www.boost.org/libs/ptr_container/
  10. //
  11. #include "test_data.hpp"
  12. #include <boost/ptr_container/ptr_vector.hpp>
  13. #include <boost/utility.hpp>
  14. #include <algorithm>
  15. #include <iostream>
  16. using namespace std;
  17. using namespace boost;
  18. //
  19. // Forward declare 'allocate_clone()' to be able
  20. // use clonability of 'Composite' inline in the class.
  21. // This is normally not needed when using .hpp + .cpp files.
  22. //
  23. class Composite;
  24. Composite* new_clone( const Composite& );
  25. class Composite
  26. {
  27. typedef ptr_vector<Composite> composite_t;
  28. typedef composite_t::iterator iterator;
  29. typedef composite_t::const_iterator const_iterator;
  30. typedef composite_t::size_type size_type;
  31. composite_t elements_;
  32. //
  33. // only used internally for 'clone()'
  34. //
  35. Composite( const Composite& r ) : elements_( r.elements_.clone() )
  36. { }
  37. //
  38. // this class is not Copyable nor Assignable
  39. //
  40. void operator=( const Composite& );
  41. public:
  42. Composite()
  43. { }
  44. //
  45. // of course detructor is virtual
  46. //
  47. virtual ~Composite()
  48. { }
  49. //
  50. // one way of adding new elements
  51. //
  52. void add( Composite* c )
  53. {
  54. elements_.push_back( c );
  55. }
  56. //
  57. // second way of adding new elements
  58. //
  59. void add( Composite& c )
  60. {
  61. elements_.push_back( new_clone( c ) );
  62. }
  63. void remove( iterator where )
  64. {
  65. elements_.erase( where );
  66. }
  67. //
  68. // recusively count the elements
  69. //
  70. size_type size() const
  71. {
  72. size_type res = 0;
  73. for( const_iterator i = elements_.begin(); i != elements_.end(); ++i )
  74. res += i->size();
  75. return 1 /* this */ + res;
  76. }
  77. void foo()
  78. {
  79. do_foo();
  80. for( iterator i = elements_.begin(); i != elements_.end(); ++i )
  81. i->foo();
  82. }
  83. //
  84. // this class is clonable and this is the callback for 'allocate_clone()'
  85. //
  86. Composite* clone() const
  87. {
  88. return do_clone();
  89. }
  90. private:
  91. virtual void do_foo()
  92. {
  93. cout << "composite base" << "\n";
  94. }
  95. virtual Composite* do_clone() const
  96. {
  97. return new Composite( *this );
  98. }
  99. };
  100. //
  101. // make 'Composite' clonable; note that we do not need to overload
  102. // the function in the 'boost' namespace.
  103. //
  104. Composite* new_clone( const Composite& c )
  105. {
  106. return c.clone();
  107. }
  108. class ConcreteComposite1 : public Composite
  109. {
  110. virtual void do_foo()
  111. {
  112. cout << "composite 1" << "\n";
  113. }
  114. virtual Composite* do_clone() const
  115. {
  116. return new ConcreteComposite1();
  117. }
  118. };
  119. class ConcreteComposite2 : public Composite
  120. {
  121. virtual void do_foo()
  122. {
  123. cout << "composite 2" << "\n";
  124. }
  125. virtual Composite* do_clone() const
  126. {
  127. return new ConcreteComposite2();
  128. }
  129. };
  130. void test_incomplete()
  131. {
  132. Composite c;
  133. c.add( new ConcreteComposite1 );
  134. c.add( new ConcreteComposite2 );
  135. BOOST_CHECK_EQUAL( c.size(), 3u );
  136. c.add( new_clone( c ) ); // add c to itself
  137. BOOST_CHECK_EQUAL( c.size(), 6u );
  138. c.add( c ); // add c to itself
  139. BOOST_CHECK_EQUAL( c.size(), 12u );
  140. c.foo();
  141. }
  142. using namespace boost;
  143. #include <boost/test/unit_test.hpp>
  144. using boost::unit_test::test_suite;
  145. test_suite* init_unit_test_suite( int argc, char* argv[] )
  146. {
  147. test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
  148. test->add( BOOST_TEST_CASE( &test_incomplete ) );
  149. return test;
  150. }
  151. //
  152. // todo: remake example with shared_ptr
  153. //