@@ -67,6 +67,20 @@ namespace ecolab
6767 }
6868 };
6969
70+ template <class T >
71+ struct DotConversion
72+ {
73+ std::string dot () const {
74+ std::ostringstream os;
75+ os<<static_cast <const T&>(*this );
76+ return os.str ();
77+ }
78+ void importDot (const std::string& dot) {
79+ std::istringstream is (dot);
80+ is>>static_cast <T&>(*this );
81+ }
82+ };
83+
7084 // / To support back_insert_iterators for Graph, define a special
7185 // / version that converts from BoostGraph edges
7286 template <class Graph , class BG >
@@ -90,7 +104,7 @@ namespace ecolab
90104 /* *
91105 Abstract base class for graph algorithms.
92106 */
93- struct Graph
107+ struct Graph : public DotConversion <Graph>
94108 {
95109 typedef Edge value_type;
96110 typedef std::size_t size_type;
@@ -255,15 +269,12 @@ class ConcreteGraph: public GraphAdaptor<G>
255269public:
256270 ConcreteGraph (unsigned nodes=0 ): GraphAdaptor<G>(g), g(nodes) {}
257271 template <class H > explicit ConcreteGraph (const H& g1): GraphAdaptor<G>(g), g(g1) {}
272+ template <class H > void asg (const H& g) {operator =(ConcreteGraph (g));}
258273 bool operator ==(const ConcreteGraph& x) {return x.g ==g;}
259274 bool operator !=(const ConcreteGraph& x) {return x.g !=g;}
260275};
261276
262- // default is not directed
263- template <class G > bool GraphAdaptor<G>::directed() const
264- {return true ;}
265-
266- class DiGraph : private std ::set<Edge>
277+ class DiGraph : private std ::set<Edge>, public DotConversion<DiGraph>
267278{
268279 unsigned num_nodes;
269280 CLASSDESC_ACCESS (DiGraph);
@@ -353,7 +364,7 @@ class BiDirectionalGraph_const_iterator/*:
353364
354365// / A graph in which each link is bidirectional
355366// base class is protected, because viewing this thing as a Graph is not correct
356- class BiDirectionalGraph
367+ class BiDirectionalGraph : public DotConversion <BiDirectionalGraph>
357368{
358369 DiGraph graph; // /< the representation
359370 CLASSDESC_ACCESS (BiDirectionalGraph);
@@ -369,10 +380,9 @@ class BiDirectionalGraph
369380 graph.push_back (e);
370381 }
371382 void clear (unsigned nodes=0 ) {graph.clear (nodes);}
372-
373383 BiDirectionalGraph (unsigned nodes=0 ): graph(nodes) {}
374384 // / initialise Graph using Graph "duck-typed" object
375- template <class G > BiDirectionalGraph (const G& g) {asg (g);}
385+ template <class G > explicit BiDirectionalGraph (const G& g) {asg (g);}
376386 template <class G > void asg (const G& g) {
377387 clear (g.nodes ());
378388 for (typename G::const_iterator i=g.begin (); i!=g.end (); ++i) push_back (*i);
@@ -389,8 +399,20 @@ class BiDirectionalGraph
389399 bool operator <(const BiDirectionalGraph& x) const {return graph<x.graph ;}
390400};
391401
392- template <> inline
393- bool GraphAdaptor<BiDirectionalGraph>::directed() const {return false ;}
402+ template <class G >
403+ struct Directed { const static bool value=true ;};
404+
405+ template <>
406+ struct Directed <ecolab::BiDirectionalGraph>{ const static bool value=false ;};
407+
408+ template <>
409+ struct Directed <const ecolab::BiDirectionalGraph>{ const static bool value=false ;};
410+
411+ template <class G > bool GraphAdaptor<G>::directed() const
412+ {
413+ return Directed<G>::value;
414+ }
415+
394416
395417// uses the sign of the off-diagonal term to indicate link
396418// direction. Links point in the same direction have their weights
@@ -518,11 +540,14 @@ namespace classdesc_access
518540
519541namespace std
520542{
521- // / for use with TCL_obj. Graphviz format is used with the netgraph command.
543+ // / Graphviz format is used with the netgraph command.
522544 std::ostream& operator <<(std::ostream& s, const ecolab::Graph& x);
523545 template <class G >
524546 std::ostream& operator <<(std::ostream& s, const ecolab::ConcreteGraph<G>& x)
525547 {return s<<static_cast <const ecolab::Graph&>(x);}
548+ template <class G >
549+ std::ostream& operator <<(std::ostream& s, const ecolab::GraphAdaptor<G>& x)
550+ {return s<<static_cast <const ecolab::Graph&>(x);}
526551
527552 inline std::ostream& operator <<(std::ostream& s, const ecolab::DiGraph& x)
528553 {return s<<ecolab::GraphAdaptor<const ecolab::DiGraph>(x);}
@@ -537,18 +562,25 @@ namespace std
537562
538563 inline std::istream& operator >>(std::istream& s, ecolab::DiGraph& x)
539564 {ecolab::GraphAdaptor<ecolab::DiGraph> g (x); return s>>g;}
565+
540566 inline std::istream& operator >>(std::istream& s, ecolab::BiDirectionalGraph& x)
541- {ecolab::GraphAdaptor<ecolab::BiDirectionalGraph> g (x); return s>>g;}
567+ {ecolab::GraphAdaptor<ecolab::BiDirectionalGraph> g (x); std::cout<<g. directed ()<<std::endl; return s>>g;}
542568}
543569
544570#if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
545571#pragma GCC diagnostic push
546572#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
547573#endif
548574
575+ // override json_pack with above GraphViz serialiser
576+ // #define CLASSDESC_json_pack___ecolab__GraphAdaptor_G_
577+ // #define CLASSDESC_json_unpack___ecolab__GraphAdaptor_G_
578+ // #define CLASSDESC_json_pack___ecolab__DiGraph
579+ // #define CLASSDESC_json_unpack___ecolab__BiDirectionalGraph
549580#include " graph.cd"
550581
551582#if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
552583#pragma GCC diagnostic pop
553584#endif
554585#endif
586+
0 commit comments