derivation_value_traits.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #ifndef BOOST_INTRUSIVE_DERIVATION_VALUE_TRAITS_HPP
  13. #define BOOST_INTRUSIVE_DERIVATION_VALUE_TRAITS_HPP
  14. #include <boost/intrusive/link_mode.hpp>
  15. #include <boost/pointer_cast.hpp>
  16. #include <boost/pointer_to_other.hpp>
  17. #include <iterator>
  18. namespace boost {
  19. namespace intrusive {
  20. //!This value traits template is used to create value traits
  21. //!from user defined node traits where value_traits::value_type will
  22. //!derive from node_traits::node
  23. template<class T, class NodeTraits, link_mode_type LinkMode = safe_link>
  24. struct derivation_value_traits
  25. {
  26. public:
  27. typedef NodeTraits node_traits;
  28. typedef T value_type;
  29. typedef typename node_traits::node node;
  30. typedef typename node_traits::node_ptr node_ptr;
  31. typedef typename node_traits::const_node_ptr const_node_ptr;
  32. typedef typename boost::pointer_to_other<node_ptr, T>::type pointer;
  33. typedef typename boost::pointer_to_other<node_ptr, const T>::type const_pointer;
  34. typedef typename boost::intrusive::
  35. pointer_traits<pointer>::reference reference;
  36. typedef typename boost::intrusive::
  37. pointer_traits<const_pointer>::reference const_reference;
  38. static const link_mode_type link_mode = LinkMode;
  39. static node_ptr to_node_ptr(reference value)
  40. { return node_ptr(&value); }
  41. static const_node_ptr to_node_ptr(const_reference value)
  42. { return node_ptr(&value); }
  43. static pointer to_value_ptr(const node_ptr &n)
  44. {
  45. // This still fails in gcc < 4.4 so forget about it
  46. // using ::boost::static_pointer_cast;
  47. // return static_pointer_cast<value_type>(n));
  48. return pointer(&static_cast<value_type&>(*n));
  49. }
  50. static const_pointer to_value_ptr(const const_node_ptr &n)
  51. {
  52. // This still fails in gcc < 4.4 so forget about it
  53. // using ::boost::static_pointer_cast;
  54. // return static_pointer_cast<const value_type>(n));
  55. return const_pointer(&static_cast<const value_type&>(*n));
  56. }
  57. };
  58. } //namespace intrusive
  59. } //namespace boost
  60. #endif //BOOST_INTRUSIVE_DERIVATION_VALUE_TRAITS_HPP