Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost Graph: determining type of bundled property
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2010-04-07 22:04:20


On Wed, 7 Apr 2010, Trevor Harmon wrote:

> Hi,
>
> I wrote some Boost Graph code that uses a custom pointer type as the vertex:
>
> adjacency_list<vecS, vecS, directedS, Foo*, property<edge_index_t,
> std::size_t> >
>
> I now need to expand the graph so that the Foo pointer is just one of several
> vertex properties. I assume bundled properties are the way to go:
>
> http://www.boost.org/doc/libs/1_42_0/libs/graph/doc/bundles.html
>
> I also need my vertices to be of different types (but all as children of a
> base type), such that my vertex bundles look something like this:
>
> class BaseVertex { };
> class EntryVertex : public BaseVertex { };
> class ExitVertex : public BaseVertex { };
> class FooVertex : public BaseVertex {
> Foo *foo;
> // ... other properties follow ...
> };

If you need that, you'll need to keep a (smart) pointer to the base class
rather than a copy. I would recommend using boost::shared_ptr<BaseVertex>
as the property type.

> My goal is to be able to iterate through the vertices and perform different
> actions depending on the type of vertex. (e.g., if vertex type is EntryVertex
> do this, if vertex type is ExitVertex do that, etc.) However, I don't know
> how to determine a vertex's type. The bundles document mentions something
> about vertex_bundle_type<Graph>::type, but I have no idea how to use this. I
> couldn't find any code in the Boost examples that use it, either. Any tips to
> get me started?

That won't help you -- you need the dynamic type of the property, not the
static type that would come from vertex_bundle_type. You need to just
have a virtual member function (and/or a virtual destructor most likely)
in BaseVertex then use typeid or dynamic_cast to find out the subclass
stored in any given pointer.

> Also, note that I've added an index property to my graph's edges, declared
> explicitly in the adjacency_list instantiation (as seen above). Would it make
> sense to convert this to a bundled property as well, or should I leave it
> as-is? (I don't think I'll ever need any edge properties other than the
> index.)

No, there's no reason to change that. You can mix bundled and non-bundled
properties, and having an edge_index property with that name will make
some algorithms easier to call.

-- 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