doc_member_value_traits.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2006-2014
  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/link_mode.hpp>
  13. #include <boost/intrusive/list.hpp>
  14. #include <boost/intrusive/member_value_traits.hpp>
  15. #include <vector>
  16. struct simple_node
  17. {
  18. simple_node *prev_;
  19. simple_node *next_;
  20. };
  21. //Define the node traits. A single node_traits will be enough.
  22. struct simple_node_traits
  23. {
  24. typedef simple_node node;
  25. typedef node * node_ptr;
  26. typedef const node * const_node_ptr;
  27. static node *get_next(const node *n) { return n->next_; }
  28. static void set_next(node *n, node *next) { n->next_ = next; }
  29. static node *get_previous(const node *n) { return n->prev_; }
  30. static void set_previous(node *n, node *prev) { n->prev_ = prev; }
  31. };
  32. //[doc_member_value_traits_value_traits
  33. class base_1{};
  34. class base_2{};
  35. struct value_1 : public base_1, public simple_node
  36. {
  37. int id_;
  38. simple_node node_;
  39. };
  40. struct value_2 : public base_1, public base_2, public simple_node
  41. {
  42. simple_node node_;
  43. float id_;
  44. };
  45. using namespace boost::intrusive;
  46. typedef member_value_traits
  47. <value_1, simple_node_traits, &value_1::node_, normal_link> ValueTraits1;
  48. typedef member_value_traits
  49. <value_2, simple_node_traits, &value_2::node_, normal_link> ValueTraits2;
  50. //Now define two intrusive lists. Both lists will use the same algorithms:
  51. // circular_list_algorithms<simple_node_traits>
  52. typedef list <value_1, value_traits<ValueTraits1> > Value1List;
  53. typedef list <value_2, value_traits<ValueTraits2> > Value2List;
  54. //]
  55. //[doc_member_value_traits_test
  56. int main()
  57. {
  58. typedef std::vector<value_1> Vect1;
  59. typedef std::vector<value_2> Vect2;
  60. //Create values, with a different internal number
  61. Vect1 values1;
  62. Vect2 values2;
  63. for(int i = 0; i < 100; ++i){
  64. value_1 v1; v1.id_ = i; values1.push_back(v1);
  65. value_2 v2; v2.id_ = (float)i; values2.push_back(v2);
  66. }
  67. //Create the lists with the objects
  68. Value1List list1(values1.begin(), values1.end());
  69. Value2List list2(values2.begin(), values2.end());
  70. //Now test both lists
  71. Value1List::const_iterator bit1(list1.begin()), bitend1(list1.end());
  72. Value2List::const_iterator bit2(list2.begin()), bitend2(list2.end());
  73. Vect1::const_iterator it1(values1.begin()), itend1(values1.end());
  74. Vect2::const_iterator it2(values2.begin()), itend2(values2.end());
  75. //Test the objects inserted in our lists
  76. for(; it1 != itend1; ++it1, ++bit1, ++it2, ++bit2){
  77. if(&*bit1 != &*it1 || &*bit2 != &*it2) return 1;
  78. }
  79. return 0;
  80. }
  81. //]