1 // (C) Copyright Jeremy Siek 2004
2 // Distributed under the Boost Software License, Version 1.0. (See
3 // accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_PROPERTY_HPP
7 #define BOOST_PROPERTY_HPP
9 #include <boost/mpl/bool.hpp>
10 #include <boost/mpl/if.hpp>
11 #include <boost/mpl/has_xxx.hpp>
12 #include <boost/utility/enable_if.hpp>
13 #include <boost/type_traits.hpp>
14 #include <boost/static_assert.hpp>
23 template < class Tag, class T, class Base = no_property > struct property
25 typedef Base next_type;
28 property(const T& v = T()) : m_value(v) {}
29 property(const T& v, const Base& b) : m_value(v), m_base(b) {}
30 // copy constructor and assignment operator will be generated by compiler
36 // Kinds of properties
37 namespace graph_introspect_detail
39 BOOST_MPL_HAS_XXX_TRAIT_DEF(kind)
40 template < typename T, bool Cond > struct get_kind
44 template < typename T > struct get_kind< T, true >
46 typedef typename T::kind type;
50 // Having a default is to make this trait work for any type, not just valid
51 // properties, to work around VC++ <= 10 bugs related to SFINAE in
52 // compressed_sparse_row_graph's get functions and similar
53 template < class PropertyTag >
55 : graph_introspect_detail::get_kind< PropertyTag,
56 graph_introspect_detail::has_kind< PropertyTag >::value >
60 // Some standard properties defined independently of Boost.Graph:
86 // Code to look up one property in a property list:
87 template < typename PList, typename PropName, typename Enable = void >
88 struct lookup_one_property_internal
90 BOOST_STATIC_CONSTANT(bool, found = false);
94 // Special-case properties (vertex_all, edge_all, graph_all)
95 #define BGL_ALL_PROP(tag) \
96 template < typename T > struct lookup_one_property_internal< T, tag > \
98 BOOST_STATIC_CONSTANT(bool, found = true); \
100 static T& lookup(T& x, tag) { return x; } \
101 static const T& lookup(const T& x, tag) { return x; } \
103 template < typename Tag, typename T, typename Base > \
104 struct lookup_one_property_internal< property< Tag, T, Base >, tag > \
105 { /* Avoid ambiguity */ \
106 BOOST_STATIC_CONSTANT(bool, found = true); \
107 typedef property< Tag, T, Base > type; \
108 static type& lookup(type& x, tag) { return x; } \
109 static const type& lookup(const type& x, tag) { return x; } \
112 BGL_ALL_PROP(vertex_all_t)
113 BGL_ALL_PROP(edge_all_t)
114 BGL_ALL_PROP(graph_all_t)
117 // *_bundled; these need to be macros rather than inheritance to resolve
119 #define BGL_DO_ONE_BUNDLE_TYPE(kind) \
120 template < typename T > \
121 struct lookup_one_property_internal< T, BOOST_JOIN(kind, _bundle_t) > \
123 BOOST_STATIC_CONSTANT(bool, found = true); \
125 static T& lookup(T& x, BOOST_JOIN(kind, _bundle_t)) { return x; } \
126 static const T& lookup(const T& x, BOOST_JOIN(kind, _bundle_t)) \
132 template < typename Tag, typename T, typename Base > \
133 struct lookup_one_property_internal< property< Tag, T, Base >, \
134 BOOST_JOIN(kind, _bundle_t) > \
135 : lookup_one_property_internal< Base, BOOST_JOIN(kind, _bundle_t) > \
138 typedef lookup_one_property_internal< Base, \
139 BOOST_JOIN(kind, _bundle_t) > \
143 template < typename BundleTag > \
144 static typename lazy_enable_if_c< \
146 && (is_same< BundleTag, \
147 BOOST_JOIN(kind, _bundle_t) >::value)), \
148 add_reference< typename base_type::type > >::type \
149 lookup(property< Tag, T, Base >& p, BundleTag) \
151 return base_type::lookup(p.m_base, BOOST_JOIN(kind, _bundle_t)()); \
153 template < typename BundleTag > \
154 static typename lazy_enable_if_c< \
156 && (is_same< BundleTag, \
157 BOOST_JOIN(kind, _bundle_t) >::value)), \
158 add_reference< const typename base_type::type > >::type \
159 lookup(const property< Tag, T, Base >& p, BundleTag) \
161 return base_type::lookup(p.m_base, BOOST_JOIN(kind, _bundle_t)()); \
165 BGL_DO_ONE_BUNDLE_TYPE(vertex)
166 BGL_DO_ONE_BUNDLE_TYPE(edge)
167 BGL_DO_ONE_BUNDLE_TYPE(graph)
168 #undef BGL_DO_ONE_BUNDLE_TYPE
170 // Normal old-style properties; second case also handles chaining of bundled
172 template < typename Tag, typename T, typename Base >
173 struct lookup_one_property_internal< boost::property< Tag, T, Base >, Tag >
175 BOOST_STATIC_CONSTANT(bool, found = true);
176 typedef property< Tag, T, Base > prop;
178 template < typename U >
179 static typename enable_if< is_same< prop, U >, T& >::type lookup(
184 template < typename U >
185 static typename enable_if< is_same< prop, U >, const T& >::type lookup(
186 const U& prop, const Tag&)
192 template < typename Tag, typename T, typename Base, typename PropName >
193 struct lookup_one_property_internal< boost::property< Tag, T, Base >, PropName >
194 : lookup_one_property_internal< Base, PropName >
197 typedef lookup_one_property_internal< Base, PropName > base_type;
200 template < typename PL >
202 typename lazy_enable_if< is_same< PL, boost::property< Tag, T, Base > >,
203 add_reference< typename base_type::type > >::type
204 lookup(PL& prop, const PropName& tag)
206 return base_type::lookup(prop.m_base, tag);
208 template < typename PL >
210 typename lazy_enable_if< is_same< PL, boost::property< Tag, T, Base > >,
211 add_reference< const typename base_type::type > >::type
212 lookup(const PL& prop, const PropName& tag)
214 return base_type::lookup(prop.m_base, tag);
218 // Pointer-to-member access to bundled properties
219 #ifndef BOOST_GRAPH_NO_BUNDLED_PROPERTIES
220 template < typename T, typename TMaybeBase, typename R >
221 struct lookup_one_property_internal< T, R TMaybeBase::*,
222 typename enable_if< is_base_of< TMaybeBase, T > >::type >
224 BOOST_STATIC_CONSTANT(bool, found = true);
226 static R& lookup(T& x, R TMaybeBase::*ptr) { return x.*ptr; }
227 static const R& lookup(const T& x, R TMaybeBase::*ptr) { return x.*ptr; }
231 // Version of above handling const property lists properly
232 template < typename T, typename Tag >
233 struct lookup_one_property : lookup_one_property_internal< T, Tag >
237 template < typename T, typename Tag > struct lookup_one_property< const T, Tag >
239 BOOST_STATIC_CONSTANT(
240 bool, found = (lookup_one_property_internal< T, Tag >::found));
241 typedef const typename lookup_one_property_internal< T, Tag >::type type;
242 template < typename U >
243 static typename lazy_enable_if< is_same< T, U >,
244 add_reference< const typename lookup_one_property_internal< T,
245 Tag >::type > >::type
246 lookup(const U& p, Tag tag)
248 return lookup_one_property_internal< T, Tag >::lookup(p, tag);
252 // The BGL properties specialize property_kind and
253 // property_num, and use enum's for the Property type (see
254 // graph/properties.hpp), but the user may want to use a class
255 // instead with a nested kind type and num. Also, we may want to
256 // switch BGL back to using class types for properties at some point.
258 template < class P > struct has_property : boost::mpl::true_
261 template <> struct has_property< no_property > : boost::mpl::false_
267 #include <boost/pending/detail/property.hpp>
272 template < class PropertyList, class Tag >
273 struct property_value : lookup_one_property< PropertyList, Tag >
277 template < class PropertyList, class Tag >
278 inline typename lookup_one_property< PropertyList, Tag >::type&
279 get_property_value(PropertyList& p, Tag tag)
281 return lookup_one_property< PropertyList, Tag >::lookup(p, tag);
284 template < class PropertyList, class Tag >
285 inline const typename lookup_one_property< PropertyList, Tag >::type&
286 get_property_value(const PropertyList& p, Tag tag)
288 return lookup_one_property< PropertyList, Tag >::lookup(p, tag);
294 /** This trait returns true if T is no_property. */
295 template < typename T >
296 struct is_no_property : mpl::bool_< is_same< T, no_property >::value >
300 template < typename PList, typename Tag > class lookup_one_property_f;
302 template < typename PList, typename Tag, typename F >
303 struct lookup_one_property_f_result;
305 template < typename PList, typename Tag >
306 struct lookup_one_property_f_result< PList, Tag,
307 const lookup_one_property_f< PList, Tag >(PList) >
309 typedef typename lookup_one_property< PList, Tag >::type type;
312 template < typename PList, typename Tag >
313 struct lookup_one_property_f_result< PList, Tag,
314 const lookup_one_property_f< PList, Tag >(PList&) >
316 typedef typename lookup_one_property< PList, Tag >::type& type;
319 template < typename PList, typename Tag >
320 struct lookup_one_property_f_result< PList, Tag,
321 const lookup_one_property_f< PList, Tag >(const PList&) >
323 typedef const typename lookup_one_property< PList, Tag >::type& type;
326 template < typename PList, typename Tag > class lookup_one_property_f
331 lookup_one_property_f(Tag tag) : tag(tag) {}
332 template < typename F >
333 struct result : lookup_one_property_f_result< PList, Tag, F >
337 typename lookup_one_property_f_result< PList, Tag,
338 const lookup_one_property_f(PList&) >::type
339 operator()(PList& pl) const
341 return lookup_one_property< PList, Tag >::lookup(pl, tag);
345 } // namespace detail
349 // Stuff for directed_graph and undirected_graph to skip over their first
350 // vertex_index and edge_index properties when providing vertex_all and
351 // edge_all; make sure you know the exact structure of your properties if
353 struct remove_first_property
355 template < typename F > struct result
357 typedef typename boost::function_traits< F >::arg1_type a1;
358 typedef typename boost::remove_reference< a1 >::type non_ref;
359 typedef typename non_ref::next_type nx;
360 typedef typename boost::mpl::if_< boost::is_const< non_ref >,
361 boost::add_const< nx >, nx >::type with_const;
362 typedef typename boost::add_reference< with_const >::type type;
364 template < typename Prop >
365 typename Prop::next_type& operator()(Prop& p) const
369 template < typename Prop >
370 const typename Prop::next_type& operator()(const Prop& p) const
379 #endif /* BOOST_PROPERTY_HPP */