简体   繁体   English

重载流运算符以获取GraphViz的Boost Graph捆绑包输出

[英]Overloading streaming operators for a Boost Graph bundle output for GraphViz

Is it possible to use bundled properties in the Boost Graph Library, with a standard library type, while also using that type's overload of the << stream operator to satisfy write_graphviz ? 是否可以在Boost Graph Library中使用标准库类型的捆绑属性,同时还使用<<流运算符的该类型的重载来满足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 with a Boost static assert, I modified my code to that above; 面对Boost静态断言,我将代码修改为上面的代码; adopting a suggestion from here , wherein the << implementation is defined within the boost::detail::has_left_shift_impl namespace. 此处采用建议,其中<<实现在boost::detail::has_left_shift_impl命名空间中定义。 Alas, I'm now faced with another error: las,我现在面临另一个错误:

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

Is there a way to provide an overload of << which can be used by write_graphviz ? 有没有办法提供<<的重载,可以由write_graphviz I'm using Ubuntu 14.10 and GCC 4.9.1. 我正在使用Ubuntu 14.10和GCC 4.9.1。

You couldn't even do 你甚至都做不到

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

without providing eg 没有提供例如

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 by lexical_cast at the right time is exceptionally hard to do portably (mostly because ADL won't help you with std::array and double ). 现在,很难在合适的时间通过lexical_cast看到这些重载,这很难移植(主要是因为ADL在std::arraydouble无济于事)。

Instead you can use a value transforming property map (which is readonly of course): 相反,您可以使用值转换属性映射(当然是只读的):

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

and then make_transform_value_property_map(prettyprint, get(edge_bundle, g)) 然后make_transform_value_property_map(prettyprint, get(edge_bundle, g))

Live On Coliru 生活在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}"];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM