diff --git a/lemon/lgf_writer.h b/lemon/lgf_writer.h --- a/lemon/lgf_writer.h +++ b/lemon/lgf_writer.h @@ -304,6 +304,21 @@ } } + + template + class DigraphWriter; + + template + DigraphWriter digraphWriter(std::ostream& os, + const Digraph& digraph); + + template + DigraphWriter digraphWriter(const std::string& fn, + const Digraph& digraph); + + template + DigraphWriter digraphWriter(const char *fn, + const Digraph& digraph); /// \ingroup lemon_io /// @@ -362,7 +377,7 @@ std::ostream* _os; bool local_os; - Digraph& _digraph; + const Digraph& _digraph; std::string _nodes_caption; std::string _arcs_caption; @@ -394,7 +409,7 @@ /// /// Construct a directed graph writer, which writes to the given /// output stream. - DigraphWriter(std::ostream& is, Digraph& digraph) + DigraphWriter(std::ostream& is, const Digraph& digraph) : _os(&is), local_os(false), _digraph(digraph), _skip_nodes(false), _skip_arcs(false) {} @@ -402,7 +417,7 @@ /// /// Construct a directed graph writer, which writes to the given /// output file. - DigraphWriter(const std::string& fn, Digraph& digraph) + DigraphWriter(const std::string& fn, const Digraph& digraph) : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), _skip_nodes(false), _skip_arcs(false) {} @@ -410,33 +425,10 @@ /// /// Construct a directed graph writer, which writes to the given /// output file. - DigraphWriter(const char* fn, Digraph& digraph) + DigraphWriter(const char* fn, const Digraph& digraph) : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), _skip_nodes(false), _skip_arcs(false) {} - /// \brief Copy constructor - /// - /// The copy constructor transfers all data from the other writer, - /// therefore the copied writer will not be usable more. - DigraphWriter(DigraphWriter& other) - : _os(other._os), local_os(other.local_os), _digraph(other._digraph), - _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { - - other._os = 0; - other.local_os = false; - - _node_index.swap(other._node_index); - _arc_index.swap(other._arc_index); - - _node_maps.swap(other._node_maps); - _arc_maps.swap(other._arc_maps); - _attributes.swap(other._attributes); - - _nodes_caption = other._nodes_caption; - _arcs_caption = other._arcs_caption; - _attributes_caption = other._attributes_caption; - } - /// \brief Destructor ~DigraphWriter() { for (typename NodeMaps::iterator it = _node_maps.begin(); @@ -460,6 +452,32 @@ } private: + + friend DigraphWriter digraphWriter<>(std::ostream& os, + const Digraph& digraph); + friend DigraphWriter digraphWriter<>(const std::string& fn, + const Digraph& digraph); + friend DigraphWriter digraphWriter<>(const char *fn, + const Digraph& digraph); + + DigraphWriter(DigraphWriter& other) + : _os(other._os), local_os(other.local_os), _digraph(other._digraph), + _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { + + other._os = 0; + other.local_os = false; + + _node_index.swap(other._node_index); + _arc_index.swap(other._arc_index); + + _node_maps.swap(other._node_maps); + _arc_maps.swap(other._arc_maps); + _attributes.swap(other._attributes); + + _nodes_caption = other._nodes_caption; + _arcs_caption = other._arcs_caption; + _attributes_caption = other._attributes_caption; + } DigraphWriter& operator=(const DigraphWriter&); @@ -844,7 +862,8 @@ /// \relates DigraphWriter template - DigraphWriter digraphWriter(std::ostream& os, Digraph& digraph) { + DigraphWriter digraphWriter(std::ostream& os, + const Digraph& digraph) { DigraphWriter tmp(os, digraph); return tmp; } @@ -852,18 +871,31 @@ /// \relates DigraphWriter template DigraphWriter digraphWriter(const std::string& fn, - Digraph& digraph) { + const Digraph& digraph) { DigraphWriter tmp(fn, digraph); return tmp; } /// \relates DigraphWriter template - DigraphWriter digraphWriter(const char* fn, Digraph& digraph) { + DigraphWriter digraphWriter(const char* fn, + const Digraph& digraph) { DigraphWriter tmp(fn, digraph); return tmp; } + template + class GraphWriter; + + template + GraphWriter graphWriter(std::ostream& os, const Graph& graph); + + template + GraphWriter graphWriter(const std::string& fn, const Graph& graph); + + template + GraphWriter graphWriter(const char *fn, const Graph& graph); + /// \ingroup lemon_io /// /// \brief LGF writer for directed graphs @@ -914,7 +946,7 @@ /// /// Construct a directed graph writer, which writes to the given /// output stream. - GraphWriter(std::ostream& is, Graph& graph) + GraphWriter(std::ostream& is, const Graph& graph) : _os(&is), local_os(false), _graph(graph), _skip_nodes(false), _skip_edges(false) {} @@ -922,7 +954,7 @@ /// /// Construct a directed graph writer, which writes to the given /// output file. - GraphWriter(const std::string& fn, Graph& graph) + GraphWriter(const std::string& fn, const Graph& graph) : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), _skip_nodes(false), _skip_edges(false) {} @@ -930,33 +962,10 @@ /// /// Construct a directed graph writer, which writes to the given /// output file. - GraphWriter(const char* fn, Graph& graph) + GraphWriter(const char* fn, const Graph& graph) : _os(new std::ofstream(fn)), local_os(true), _graph(graph), _skip_nodes(false), _skip_edges(false) {} - /// \brief Copy constructor - /// - /// The copy constructor transfers all data from the other writer, - /// therefore the copied writer will not be usable more. - GraphWriter(GraphWriter& other) - : _os(other._os), local_os(other.local_os), _graph(other._graph), - _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { - - other._os = 0; - other.local_os = false; - - _node_index.swap(other._node_index); - _edge_index.swap(other._edge_index); - - _node_maps.swap(other._node_maps); - _edge_maps.swap(other._edge_maps); - _attributes.swap(other._attributes); - - _nodes_caption = other._nodes_caption; - _edges_caption = other._edges_caption; - _attributes_caption = other._attributes_caption; - } - /// \brief Destructor ~GraphWriter() { for (typename NodeMaps::iterator it = _node_maps.begin(); @@ -978,9 +987,35 @@ delete _os; } } + + private: - private: - + friend GraphWriter graphWriter<>(std::ostream& os, + const Graph& graph); + friend GraphWriter graphWriter<>(const std::string& fn, + const Graph& graph); + friend GraphWriter graphWriter<>(const char *fn, + const Graph& graph); + + GraphWriter(GraphWriter& other) + : _os(other._os), local_os(other.local_os), _graph(other._graph), + _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { + + other._os = 0; + other.local_os = false; + + _node_index.swap(other._node_index); + _edge_index.swap(other._edge_index); + + _node_maps.swap(other._node_maps); + _edge_maps.swap(other._edge_maps); + _attributes.swap(other._attributes); + + _nodes_caption = other._nodes_caption; + _edges_caption = other._edges_caption; + _attributes_caption = other._attributes_caption; + } + GraphWriter& operator=(const GraphWriter&); public: @@ -1410,21 +1445,21 @@ /// \relates GraphWriter template - GraphWriter graphWriter(std::ostream& os, Graph& graph) { + GraphWriter graphWriter(std::ostream& os, const Graph& graph) { GraphWriter tmp(os, graph); return tmp; } /// \relates GraphWriter template - GraphWriter graphWriter(const std::string& fn, Graph& graph) { + GraphWriter graphWriter(const std::string& fn, const Graph& graph) { GraphWriter tmp(fn, graph); return tmp; } /// \relates GraphWriter template - GraphWriter graphWriter(const char* fn, Graph& graph) { + GraphWriter graphWriter(const char* fn, const Graph& graph) { GraphWriter tmp(fn, graph); return tmp; }