doc_value_traits.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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_value_traits_code_legacy
  13. #include <boost/intrusive/link_mode.hpp>
  14. #include <boost/intrusive/list.hpp>
  15. #include <boost/intrusive/slist.hpp>
  16. //<-
  17. #include <boost/intrusive/trivial_value_traits.hpp>
  18. //->
  19. #include <vector>
  20. //This node is the legacy type we can't modify and we want to insert in
  21. //intrusive list and slist containers using only two pointers, since
  22. //we know the object will never be at the same time in both lists.
  23. struct legacy_value
  24. {
  25. legacy_value *prev_;
  26. legacy_value *next_;
  27. int id_;
  28. };
  29. //]
  30. //[doc_value_traits_value_traits
  31. //Define our own NodeTraits that will configure singly and doubly linked
  32. //list algorithms. Note that this node traits is compatible with
  33. //circular_slist_algorithms and circular_list_algorithms.
  34. namespace bi = boost::intrusive;
  35. struct legacy_node_traits
  36. {
  37. typedef legacy_value node;
  38. typedef legacy_value * node_ptr;
  39. typedef const legacy_value * const_node_ptr;
  40. static node *get_next(const node *n) { return n->next_; }
  41. static void set_next(node *n, node *next) { n->next_ = next; }
  42. static node *get_previous(const node *n) { return n->prev_; }
  43. static void set_previous(node *n, node *prev) { n->prev_ = prev; }
  44. };
  45. //This ValueTraits will configure list and slist. In this case,
  46. //legacy_node_traits::node is the same as the
  47. //legacy_value_traits::value_type so to_node_ptr/to_value_ptr
  48. //functions are trivial.
  49. struct legacy_value_traits
  50. {
  51. typedef legacy_node_traits node_traits;
  52. typedef node_traits::node_ptr node_ptr;
  53. typedef node_traits::const_node_ptr const_node_ptr;
  54. typedef legacy_value value_type;
  55. typedef legacy_value * pointer;
  56. typedef const legacy_value * const_pointer;
  57. static const bi::link_mode_type link_mode = bi::normal_link;
  58. static node_ptr to_node_ptr (value_type &value) { return node_ptr(&value); }
  59. static const_node_ptr to_node_ptr (const value_type &value) { return const_node_ptr(&value); }
  60. static pointer to_value_ptr(node_ptr n) { return pointer(n); }
  61. static const_pointer to_value_ptr(const_node_ptr n) { return const_pointer(n); }
  62. };
  63. //]
  64. //[doc_value_traits_trivial
  65. typedef bi::trivial_value_traits<legacy_node_traits, bi::normal_link> trivial_legacy_value_traits;
  66. //]
  67. //[doc_value_traits_test
  68. //Now define an intrusive list and slist that will store legacy_value objects
  69. typedef bi::value_traits<legacy_value_traits> ValueTraitsOption;
  70. typedef bi::value_traits<trivial_legacy_value_traits> TrivialValueTraitsOption;
  71. typedef bi::list<legacy_value, ValueTraitsOption> LegacyAbiList;
  72. typedef bi::slist<legacy_value, ValueTraitsOption> LegacyAbiSlist;
  73. typedef bi::list<legacy_value, TrivialValueTraitsOption> TrivialLegacyAbiList;
  74. typedef bi::slist<legacy_value, TrivialValueTraitsOption> TrivialLegacyAbiSlist;
  75. template<class List>
  76. bool test_list()
  77. {
  78. typedef std::vector<legacy_value> Vect;
  79. //Create legacy_value objects, with a different internal number
  80. Vect legacy_vector;
  81. for(int i = 0; i < 100; ++i){
  82. legacy_value value; value.id_ = i; legacy_vector.push_back(value);
  83. }
  84. //Create the list with the objects
  85. List mylist(legacy_vector.begin(), legacy_vector.end());
  86. //Now test both lists
  87. typename List::const_iterator bit(mylist.begin()), bitend(mylist.end());
  88. typename Vect::const_iterator it(legacy_vector.begin()), itend(legacy_vector.end());
  89. //Test the objects inserted in our list
  90. for(; it != itend; ++it, ++bit)
  91. if(&*bit != &*it) return false;
  92. return true;
  93. }
  94. int main()
  95. {
  96. return test_list<LegacyAbiList>() && test_list<LegacyAbiSlist>() &&
  97. test_list<TrivialLegacyAbiList>() && test_list<TrivialLegacyAbiSlist>()
  98. ? 0 : 1;
  99. }
  100. //]