doc_advanced_value_traits.cpp 3.9 KB

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