doc_stateful_value_traits.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. //[doc_stateful_value_traits
  13. #include <boost/intrusive/list.hpp>
  14. using namespace boost::intrusive;
  15. //This type is not modifiable so we can't store hooks or custom nodes
  16. typedef int identifier_t;
  17. //This value traits will associate elements from an array of identifiers with
  18. //elements of an array of nodes. The element i of the value array will use the
  19. //node i of the node array:
  20. struct stateful_value_traits
  21. {
  22. typedef list_node_traits<void*> node_traits;
  23. typedef node_traits::node node;
  24. typedef node * node_ptr;
  25. typedef const node * const_node_ptr;
  26. typedef identifier_t value_type;
  27. typedef identifier_t * pointer;
  28. typedef const identifier_t * const_pointer;
  29. static const link_mode_type link_mode = normal_link;
  30. stateful_value_traits(pointer ids, node_ptr node_array)
  31. : ids_(ids), nodes_(node_array)
  32. {}
  33. ///Note: non static functions!
  34. node_ptr to_node_ptr (value_type &value) const
  35. { return this->nodes_ + (&value - this->ids_); }
  36. const_node_ptr to_node_ptr (const value_type &value) const
  37. { return this->nodes_ + (&value - this->ids_); }
  38. pointer to_value_ptr(node_ptr n) const
  39. { return this->ids_ + (n - this->nodes_); }
  40. const_pointer to_value_ptr(const_node_ptr n) const
  41. { return this->ids_ + (n - this->nodes_); }
  42. private:
  43. pointer ids_;
  44. node_ptr nodes_;
  45. };
  46. int main()
  47. {
  48. const int NumElements = 100;
  49. //This is an array of ids that we want to "store"
  50. identifier_t ids [NumElements];
  51. //This is an array of nodes that is necessary to form the linked list
  52. list_node_traits<void*>::node nodes [NumElements];
  53. //Initialize id objects, each one with a different number
  54. for(int i = 0; i != NumElements; ++i) ids[i] = i;
  55. //Define a list that will "link" identifiers using external nodes
  56. typedef list<identifier_t, value_traits<stateful_value_traits> > List;
  57. //This list will store ids without modifying identifier_t instances
  58. //Stateful value traits must be explicitly passed in the constructor.
  59. List my_list (stateful_value_traits (ids, nodes));
  60. //Insert ids in reverse order in the list
  61. for(identifier_t * it(&ids[0]), *itend(&ids[NumElements]); it != itend; ++it)
  62. my_list.push_front(*it);
  63. //Now test lists
  64. List::const_iterator list_it (my_list.cbegin());
  65. identifier_t *it_val(&ids[NumElements]), *it_rbeg_val(&ids[0]);
  66. //Test the objects inserted in the base hook list
  67. for(; it_val != it_rbeg_val; --it_val, ++list_it)
  68. if(&*list_it != &it_val[-1]) return 1;
  69. return 0;
  70. }
  71. //]