Boost logo

Boost Users :

Subject: Re: [Boost-users] [graph] How to find out at compile time if a graph has some concrete internal property?
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2009-03-19 10:15:13


On Thu, 19 Mar 2009, Andrew Sutton wrote:

(snip)

> Unfortunately, I don't think such a function actually exists. However, if you can detect these at compile time, you can
> easily reflect that as a runtime query:
>
> template <typename Graph, typename Prop>
> bool has_property(Graph const& g)
> { return has_property_metafunc<Prop, Graph>::value; }

It appears that the value_type of an undefined non-bundled property map is
always boost::detail::error_property_not_found, so you can use the
following metafunction to detect them:

#include <boost/graph/properties.hpp>
#include <boost/type_traits.hpp>
#include <boost/property_map.hpp>
#include <boost/mpl/not.hpp>

template <typename Graph, typename PropertyTag>
struct graph_has_property: public
          boost::mpl::not_<
            boost::is_same<
              typename boost::property_traits<
                typename boost::property_map<Graph, PropertyTag>::type
>::value_type,
              boost::detail::error_property_not_found
>
>
{};

This always returns true when given a bundled property, however (it does
work for testing for non-bundled properties in graphs that have bundled
properties). I have not figured out how to test for bundled properties;
there is a compile error when trying to get a particular value from a
nonexistent member of a bundled property map. Is that a case that is
important to you? There are several Boost graph algorithms that make a
decision based on whether a property exists; look for the handling of the
color map at the bottom of boost/graph/breadth_first_search.hpp for an
example (look at the definition of boost::detail::bfs_dispatch). The code
there is a more complicated version of the metafunction above, but can
also handle an optionally user-specified color map.

-- Jeremiah Willcock


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net