constant_property_map.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // (C) Copyright 2007-2009 Andrew Sutton
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GRAPH_CONSTANT_PROPERTY_HPP
  7. #define BOOST_GRAPH_CONSTANT_PROPERTY_HPP
  8. #include <boost/property_map/property_map.hpp>
  9. // TODO: This should really be part of the property maps library rather than
  10. // the Boost.Graph library.
  11. namespace boost
  12. {
  13. /**
  14. * A constant property is one, that regardless of the edge or vertex given,
  15. * will always return a constant value.
  16. */
  17. template < typename Key, typename Value >
  18. struct constant_property_map : public boost::put_get_helper< const Value&,
  19. constant_property_map< Key, Value > >
  20. {
  21. typedef Key key_type;
  22. typedef Value value_type;
  23. typedef const Value& reference;
  24. typedef boost::readable_property_map_tag category;
  25. constant_property_map() : m_value() {}
  26. constant_property_map(const value_type& value) : m_value(value) {}
  27. constant_property_map(const constant_property_map& copy)
  28. : m_value(copy.m_value)
  29. {
  30. }
  31. inline reference operator[](const key_type&) const { return m_value; }
  32. value_type m_value;
  33. };
  34. template < typename Key, typename Value >
  35. inline constant_property_map< Key, Value > make_constant_property(
  36. const Value& value)
  37. {
  38. return constant_property_map< Key, Value >(value);
  39. }
  40. /**
  41. * Same as above, but pretends to be writable as well.
  42. */
  43. template < typename Key, typename Value > struct constant_writable_property_map
  44. {
  45. typedef Key key_type;
  46. typedef Value value_type;
  47. typedef Value& reference;
  48. typedef boost::read_write_property_map_tag category;
  49. constant_writable_property_map() : m_value() {}
  50. constant_writable_property_map(const value_type& value) : m_value(value) {}
  51. constant_writable_property_map(const constant_writable_property_map& copy)
  52. : m_value(copy.m_value)
  53. {
  54. }
  55. friend Value get(const constant_writable_property_map& me, Key)
  56. {
  57. return me.m_value;
  58. }
  59. friend void put(const constant_writable_property_map&, Key, Value) {}
  60. value_type m_value;
  61. };
  62. template < typename Key, typename Value >
  63. inline constant_writable_property_map< Key, Value >
  64. make_constant_writable_property(const Value& value)
  65. {
  66. return constant_writable_property_map< Key, Value >(value);
  67. }
  68. } /* namespace boost */
  69. #endif