c++ - Overloading streaming operators for a Boost Graph bundle output for GraphViz -


is possible use bundled properties in boost graph library, standard library type, while using type's overload of << stream operator satisfy write_graphviz?

#include <boost/graph/graphviz.hpp>  namespace boost {   namespace detail {     namespace has_left_shift_impl {       template <typename t>       inline std::ostream &operator<<(std::ostream &o, const std::array<t,2> &a) {         o << a[0] << ',' << a[1];         return o;       }     }   } }  static_assert(boost::has_left_shift<                 std::basic_ostream<char>,                 std::array<double,2>               >::value,"");  int main() {   using namespace boost;   typedef adjacency_list<vecs, vecs, directeds, no_property, std::array<double,2>> graph;   graph g;   add_edge(0, 1, {123,456}, g);   write_graphviz(std::cout, g, default_writer(),                    make_label_writer(boost::get(edge_bundle,g)));   return 0; } 

faced boost static assert, modified code above; adopting suggestion here, wherein << implementation defined within boost::detail::has_left_shift_impl namespace. alas, i'm faced error:

/usr/include/boost/lexical_cast.hpp:1624:50: error: cannot bind ‘std::basic_ostream<char>’ lvalue ‘std::basic_ostream<char>&&’                  bool const result = !(out_stream << input).fail(); 

is there way provide overload of << can used write_graphviz? i'm using ubuntu 14.10 , gcc 4.9.1.

you couldn't

std::cout << g[add_edge(0,1,{123,456},g)]; 

without providing e.g.

inline static std::ostream& operator<<(std::ostream& os, std::array<double, 2> const& doubles) {     return os << "{" << doubles[0] << "," << doubles[1] << "}"; } 

now getting these overloads seen lexical_cast @ right time exceptionally hard portably (mostly because adl won't std::array , double).

instead can use value transforming property map (which readonly of course):

std::string prettyprint(std::array<double, 2> const& arr) {     std::ostringstream oss;     oss << "{" << arr[0] << "," << arr[1] << "}";     return oss.str(); } 

and make_transform_value_property_map(prettyprint, get(edge_bundle, g))

live on coliru

#include <boost/graph/adjacency_list.hpp> #include <boost/graph/graphviz.hpp>  using namespace boost;  std::string prettyprint(std::array<double, 2> const& arr) {     std::ostringstream oss;     oss << "{" << arr[0] << "," << arr[1] << "}";     return oss.str(); }  int main() {     using namespace boost;     typedef adjacency_list<vecs, vecs, directeds, no_property, std::array<double,2>> graph;     graph g;     add_edge(0, 1, {123,456}, g);     write_graphviz(std::cout, g, default_writer(),             make_label_writer(make_transform_value_property_map(&prettyprint, get(edge_bundle, g)))); } 

prints

digraph g { 0; 1; 0->1 [label="{123,456}"]; } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -