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
7#ifndef BOOST_GRAPH_CONSTANT_PROPERTY_HPP
8#define BOOST_GRAPH_CONSTANT_PROPERTY_HPP
9
10#include <boost/property_map/property_map.hpp>
11
12// TODO: This should really be part of the property maps library rather than
13// the Boost.Graph library.
14
15namespace boost
16{
17
18/**
19 * A constant property is one, that regardless of the edge or vertex given,
20 * will always return a constant value.
21 */
22template < typename Key, typename Value >
23struct constant_property_map : public boost::put_get_helper< const Value&,
24 constant_property_map< Key, Value > >
25{
26 typedef Key key_type;
27 typedef Value value_type;
28 typedef const Value& reference;
29 typedef boost::readable_property_map_tag category;
30
31 constant_property_map() : m_value() {}
32
33 constant_property_map(const value_type& value) : m_value(value) {}
34
35 constant_property_map(const constant_property_map& copy)
36 : m_value(copy.m_value)
37 {
38 }
39
40 inline reference operator[](const key_type&) const { return m_value; }
41
42 value_type m_value;
43};
44
45template < typename Key, typename Value >
46inline constant_property_map< Key, Value > make_constant_property(
47 const Value& value)
48{
49 return constant_property_map< Key, Value >(value);
50}
51
52/**
53 * Same as above, but pretends to be writable as well.
54 */
55template < typename Key, typename Value > struct constant_writable_property_map
56{
57 typedef Key key_type;
58 typedef Value value_type;
59 typedef Value& reference;
60 typedef boost::read_write_property_map_tag category;
61
62 constant_writable_property_map() : m_value() {}
63
64 constant_writable_property_map(const value_type& value) : m_value(value) {}
65
66 constant_writable_property_map(const constant_writable_property_map& copy)
67 : m_value(copy.m_value)
68 {
69 }
70
71 friend Value get(const constant_writable_property_map& me, Key)
72 {
73 return me.m_value;
74 }
75 friend void put(const constant_writable_property_map&, Key, Value) {}
76
77 value_type m_value;
78};
79
80template < typename Key, typename Value >
81inline constant_writable_property_map< Key, Value >
82make_constant_writable_property(const Value& value)
83{
84 return constant_writable_property_map< Key, Value >(value);
85}
86
87} /* namespace boost */
88
89#endif
90

source code of boost/libs/graph/include/boost/graph/property_maps/constant_property_map.hpp