constant_property_map.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. * A constant property is one, that regardless of the edge or vertex given,
  14. * will always return a constant value.
  15. */
  16. template <typename Key, typename Value>
  17. struct constant_property_map
  18. : public boost::put_get_helper<
  19. const Value&,
  20. constant_property_map<Key, Value>
  21. >
  22. {
  23. typedef Key key_type;
  24. typedef Value value_type;
  25. typedef const Value& reference;
  26. typedef boost::readable_property_map_tag category;
  27. constant_property_map()
  28. : m_value()
  29. { }
  30. constant_property_map(const value_type &value)
  31. : m_value(value)
  32. { }
  33. constant_property_map(const constant_property_map& copy)
  34. : m_value(copy.m_value)
  35. { }
  36. inline reference operator [](const key_type&) const
  37. { return m_value; }
  38. value_type m_value;
  39. };
  40. template <typename Key, typename Value>
  41. inline constant_property_map<Key, Value>
  42. make_constant_property(const Value& value)
  43. { return constant_property_map<Key, Value>(value); }
  44. /**
  45. * Same as above, but pretends to be writable as well.
  46. */
  47. template <typename Key, typename Value>
  48. struct constant_writable_property_map {
  49. typedef Key key_type;
  50. typedef Value value_type;
  51. typedef Value& reference;
  52. typedef boost::read_write_property_map_tag category;
  53. constant_writable_property_map()
  54. : m_value()
  55. { }
  56. constant_writable_property_map(const value_type &value)
  57. : m_value(value)
  58. { }
  59. constant_writable_property_map(const constant_writable_property_map& copy)
  60. : m_value(copy.m_value)
  61. { }
  62. friend Value get(const constant_writable_property_map& me, Key) {return me.m_value;}
  63. friend void put(const constant_writable_property_map&, Key, Value) {}
  64. value_type m_value;
  65. };
  66. template <typename Key, typename Value>
  67. inline constant_writable_property_map<Key, Value>
  68. make_constant_writable_property(const Value& value)
  69. { return constant_writable_property_map<Key, Value>(value); }
  70. } /* namespace boost */
  71. #endif