virtual_base_test.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #include <boost/intrusive/list.hpp>
  13. #include <boost/intrusive/slist.hpp>
  14. #include <boost/intrusive/set.hpp>
  15. #include <boost/intrusive/unordered_set.hpp>
  16. #include <vector>
  17. using namespace boost::intrusive;
  18. struct VirtualBase
  19. {
  20. virtual ~VirtualBase(){}
  21. };
  22. struct VirtualBase2
  23. {
  24. virtual ~VirtualBase2(){}
  25. };
  26. struct VirtualBase3
  27. {
  28. };
  29. class NonVirtualBase
  30. : public virtual VirtualBase
  31. , public virtual VirtualBase2
  32. {
  33. protected:
  34. NonVirtualBase()
  35. : dummy()
  36. {}
  37. //<-
  38. const int *get_dummy() const { return dummy; }
  39. //->
  40. private:
  41. int dummy[10];
  42. };
  43. class MyClass
  44. : public NonVirtualBase
  45. , public virtual VirtualBase3
  46. {
  47. int int_;
  48. public:
  49. list_member_hook<> list_hook_;
  50. MyClass(int i = 0)
  51. : int_(i)
  52. {}
  53. //<-
  54. int get_int() const { return int_; }
  55. //->
  56. };
  57. //Define a list that will store MyClass using the public base hook
  58. typedef member_hook< MyClass, list_member_hook<>, &MyClass::list_hook_ > MemberHook;
  59. typedef list<MyClass, MemberHook> List;
  60. int main()
  61. {
  62. #ifndef _MSC_VER
  63. typedef std::vector<MyClass>::iterator VectIt;
  64. typedef std::vector<MyClass>::reverse_iterator VectRit;
  65. //Create several MyClass objects, each one with a different value
  66. std::vector<MyClass> values;
  67. for(int i = 0; i < 100; ++i) values.push_back(MyClass(i));
  68. List my_list;
  69. //Now insert them in the reverse order
  70. //in the base hook intrusive list
  71. for(VectIt it(values.begin()), itend(values.end()); it != itend; ++it)
  72. my_list.push_front(*it);
  73. //Now test lists
  74. {
  75. List::const_iterator list_it(my_list.cbegin());
  76. VectRit vect_it(values.rbegin()), vect_itend(values.rend());
  77. //Test the objects inserted in the base hook list
  78. for(; vect_it != vect_itend; ++vect_it, ++list_it)
  79. if(&*list_it != &*vect_it)
  80. return 1;
  81. }
  82. #endif
  83. return 0;
  84. }