I am currently trying to use the boost graph library in an existing c++ project. I would like to store objects of a custom class in a boost graph. Below is a small example with a custom class definition with two members (a string and an int) and their corresponding getter methods.
I have a few questions:
- How can I include the string and int value in the Graphviz output? I found this answer to a similar question using
boost::make_label_writer
but I not really sure if my example can be adopted to this (I am using a custom class and shared pointers). - It should not be possible to store an identical object (same string and int value) more than one time in the graph. Therefore I overloaded two comparison operators in my custom class. I also read that I have to change the second template parameter of the graph type definition to
boost::setS
but that results in a really long error message of the compiler ... Let's say I created a new object of my custom class: How can I check if it is already stored in the graph?
#include <iostream> #include <boost/graph/graphviz.hpp> class my_custom_class { public: my_custom_class(const std::string &my_string, int my_int) : my_string(my_string), my_int(my_int) {} virtual ~my_custom_class() { } std::string get_my_string() const { return my_string; } int get_int() const { return my_int; } bool operator==(const my_custom_class &rhs) const { return my_string == rhs.my_string && my_int == rhs.my_int; } bool operator!=(const my_custom_class &rhs) const { return !(rhs == *this); } private: std::string my_string; int my_int; }; namespace boost { enum vertex_my_custom_class_t { vertex_my_custom_class = 123 }; BOOST_INSTALL_PROPERTY(vertex, my_custom_class); } int main() { typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS, boost::property<boost::vertex_my_custom_class_t, std::shared_ptr<my_custom_class>>> graph_t; typedef boost::graph_traits<graph_t>::vertex_descriptor vertex_t; std::shared_ptr<my_custom_class> object_one = std::make_shared<my_custom_class>("Lorem", 123); std::shared_ptr<my_custom_class> object_two = std::make_shared<my_custom_class>("ipsum", 456); std::shared_ptr<my_custom_class> object_three = std::make_shared<my_custom_class>("Lorem", 123); std::cout << "object one: " << object_one->get_int() << "; " << object_one->get_my_string() << std::endl; std::cout << "object two: " << object_two->get_int() << "; " << object_two->get_my_string() << std::endl; std::cout << "object three: " << object_three->get_int() << "; " << object_three->get_my_string() << std::endl; std::cout << std::endl; std::cout << "object one == object two: " << (*object_one == *object_two) << std::endl; std::cout << "object one == object three: " << (*object_one == *object_three) << std::endl; std::cout << std::endl; graph_t graph; vertex_t vertex_one = boost::add_vertex(object_one, graph); vertex_t vertex_two = boost::add_vertex(object_two, graph); vertex_t vertex_three = boost::add_vertex(object_three, graph); boost::add_edge(vertex_one, vertex_two, graph); boost::add_edge(vertex_one, vertex_three, graph); boost::write_graphviz(std::cout, graph); return 0; }
Program output:
object one: 123; Lorem
object two: 456; ipsum
object three: 123; Lorem
object one == object two: 0
object one == object three: 1
digraph G {
0;
1;
2;
0->1 ;
0->2 ;
}