# HG changeset patch # User deba # Date 1118682836 0 # Node ID 182da222fceb835c654632c7da6bbd4137e68641 # Parent 21aa0195aab9f1ca16012b2b782158936dcfc08e Some bug fix. Added: Concept check for maps and IdReader/Writer classes Some runtime check diff -r 21aa0195aab9 -r 182da222fceb lemon/graph_reader.h --- a/lemon/graph_reader.h Mon Jun 13 16:10:36 2005 +0000 +++ b/lemon/graph_reader.h Mon Jun 13 17:13:56 2005 +0000 @@ -261,6 +261,7 @@ /// Add a new labeled edge reader for the reader. GraphReader& readEdge(std::string name, Edge& edge) { edge_reader.readEdge(name, edge); + return *this; } /// \brief Add a new attribute reader command. diff -r 21aa0195aab9 -r 182da222fceb lemon/lemon_reader.h --- a/lemon/lemon_reader.h Mon Jun 13 16:10:36 2005 +0000 +++ b/lemon/lemon_reader.h Mon Jun 13 17:13:56 2005 +0000 @@ -36,10 +36,33 @@ #include #include +#include +#include namespace lemon { namespace _reader_bits { + + template + class ItemIdReader { + public: + + bool isIdReader() { return true; } + + Item readId(std::istream&, Item) { return Item();} + + template + struct Constraints { + void constraints() { + bool b = reader.isIdReader(); + ignore_unused_variable_warning(b); + Item item = reader.readId(is, Item()); + } + _ItemIdReader& reader; + std::istream& is; + }; + + }; template bool operator<(T, T) { @@ -531,6 +554,7 @@ public: typedef _Item Item; virtual Item read(std::istream& is) const = 0; + virtual bool isIdReader() const = 0; }; template @@ -547,6 +571,10 @@ virtual Item read(std::istream& is) const { return boxedIdReader.readId(is, Item()); } + + virtual bool isIdReader() const { + return boxedIdReader.isIdReader(); + } }; class ValueReaderBase { @@ -817,9 +845,10 @@ const NodeIdReader& _nodeIdReader, const std::string& _id = std::string(), const DefaultSkipper& _skipper = DefaultSkipper()) - : Parent(_reader), graph(_graph), id(_id), skipper(_skipper), - nodeIdReader(new IdReader(_nodeIdReader)) {} - + : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) { + checkConcept<_reader_bits::ItemIdReader, NodeIdReader>(); + nodeIdReader.reset(new IdReader(_nodeIdReader)); + } /// \brief Destructor. /// /// Destructor for EdgeSetReader. @@ -921,6 +950,9 @@ /// /// It reads the content of the section. virtual void read(std::istream& is) { + if (!nodeIdReader->isIdReader()) { + throw DataFormatError("Cannot find nodeset or ID map"); + } std::vector* > index; std::string line; @@ -1033,9 +1065,10 @@ const NodeIdReader& _nodeIdReader, const std::string& _id = std::string(), const DefaultSkipper& _skipper = DefaultSkipper()) - : Parent(_reader), graph(_graph), id(_id), skipper(_skipper), - nodeIdReader(new IdReader(_nodeIdReader)) {} - + : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) { + checkConcept<_reader_bits::ItemIdReader, NodeIdReader>(); + nodeIdReader.reset(new IdReader(_nodeIdReader)); + } /// \brief Destructor. /// /// Destructor for UndirEdgeSetReader. @@ -1197,6 +1230,9 @@ /// /// It reads the content of the section. virtual void read(std::istream& is) { + if (!nodeIdReader->isIdReader()) { + throw DataFormatError("Cannot find nodeset or ID map"); + } std::vector* > index; std::string line; @@ -1302,8 +1338,10 @@ template NodeReader(LemonReader& _reader, const _IdReader& _idReader, const std::string& _id = std::string()) - : Parent(_reader), id(_id), - idReader(new IdReader(_idReader)) {} + : Parent(_reader), id(_id) { + checkConcept<_reader_bits::ItemIdReader, _IdReader>(); + idReader.reset(new IdReader(_idReader)); + } /// \brief Destructor. /// @@ -1347,6 +1385,9 @@ /// /// It reads the content of the section. virtual void read(std::istream& is) { + if (!idReader->isIdReader()) { + throw DataFormatError("Cannot find nodeset or ID map"); + } std::string line; while (getline(is, line)) { std::istringstream ls(line); @@ -1394,8 +1435,10 @@ template EdgeReader(LemonReader& _reader, const _IdReader& _idReader, const std::string& _id = std::string()) - : Parent(_reader), id(_id), - idReader(new IdReader(_idReader)) {} + : Parent(_reader), id(_id) { + checkConcept<_reader_bits::ItemIdReader, _IdReader>(); + idReader.reset(new IdReader(_idReader)); + } /// \brief Destructor. /// @@ -1438,6 +1481,9 @@ /// /// It reads the content of the section. virtual void read(std::istream& is) { + if (!idReader->isIdReader()) { + throw DataFormatError("Cannot find edgeset or ID map"); + } std::string line; while (getline(is, line)) { std::istringstream ls(line); @@ -1487,10 +1533,12 @@ template UndirEdgeReader(LemonReader& _reader, const _IdReader& _idReader, const std::string& _id = std::string()) - : Parent(_reader), id(_id), - undirEdgeIdReader(new IdReader(_idReader)), - edgeIdReader(new IdReader(_idReader)) - {} + : Parent(_reader), id(_id) { + checkConcept<_reader_bits::ItemIdReader, _IdReader>(); + checkConcept<_reader_bits::ItemIdReader, _IdReader>(); + undirEdgeIdReader.reset(new IdReader(_idReader)); + egdeIdReader.reset(new IdReader(_idReader)); + } /// \brief Destructor. /// @@ -1545,6 +1593,12 @@ /// /// It reads the content of the section. virtual void read(std::istream& is) { + if (!edgeIdReader->isIdReader()) { + throw DataFormatError("Cannot find undirected edgeset or ID map"); + } + if (!undirEdgeIdReader->isIdReader()) { + throw DataFormatError("Cannot find undirected edgeset or ID map"); + } std::string line; while (getline(is, line)) { std::istringstream ls(line); diff -r 21aa0195aab9 -r 182da222fceb lemon/lemon_writer.h --- a/lemon/lemon_writer.h Mon Jun 13 16:10:36 2005 +0000 +++ b/lemon/lemon_writer.h Mon Jun 13 17:13:56 2005 +0000 @@ -36,9 +36,38 @@ #include #include +#include +#include + namespace lemon { + namespace _writer_bits { + + template + class ItemIdWriter { + public: + + bool isIdWriter() { return true; } + + void writeId(std::ostream&, const Item&) {} + + template + struct Constraints { + void constraints() { + const Item item; + bool b = writer.isIdWriter(); + ignore_unused_variable_warning(b); + writer.writeId(os, item); + } + _ItemIdWriter& writer; + std::ostream& os; + }; + + }; + + } + /// \ingroup io_group /// \brief Lemon Format writer class. /// @@ -229,6 +258,7 @@ public: typedef _Item Item; virtual void write(std::ostream&, const Item&) const = 0; + virtual bool isIdWriter() const = 0; }; template @@ -245,6 +275,10 @@ virtual void write(std::ostream& os, const Item& item) const { idWriter.writeId(os, item); } + + virtual bool isIdWriter() const { + return idWriter.isIdWriter(); + } }; }; @@ -318,6 +352,7 @@ template NodeSetWriter& writeNodeMap(std::string name, const Map& map, const Writer& writer = Writer()) { + checkConcept, Map>(); writers.push_back( make_pair(name, new MapWriter(map, writer))); return *this; @@ -446,8 +481,10 @@ const std::string& _id = std::string(), bool _forceIdMap = true) : Parent(_writer), idMap(0), forceIdMap(_forceIdMap), - graph(_graph), id(_id), - nodeIdWriter(new IdWriter(_nodeIdWriter)) {} + graph(_graph), id(_id) { + checkConcept<_writer_bits::ItemIdWriter, NodeIdWriter>(); + nodeIdWriter.reset(new IdWriter(_nodeIdWriter)); + } /// \brief Destructor. /// @@ -480,6 +517,7 @@ template EdgeSetWriter& writeEdgeMap(std::string name, const Map& map, const Writer& writer = Writer()) { + checkConcept, Map>(); writers.push_back( make_pair(name, new MapWriter(map, writer))); return *this; @@ -498,6 +536,9 @@ /// /// Write the content of the section. virtual void write(std::ostream& os) { + if (!nodeIdWriter->isIdWriter()) { + throw DataFormatError("Cannot find nodeset or ID map"); + } for (int i = 0; i < (int)writers.size(); ++i) { if (writers[i].first == "id") { idMap = writers[i].second; @@ -621,8 +662,10 @@ const std::string& _id = std::string(), bool _forceIdMap = true) : Parent(_writer), idMap(0), forceIdMap(_forceIdMap), - graph(_graph), id(_id), - nodeIdWriter(new IdWriter(_nodeIdWriter)) {} + graph(_graph), id(_id) { + checkConcept<_writer_bits::ItemIdWriter, NodeIdWriter>(); + nodeIdWriter.reset(new IdWriter(_nodeIdWriter)); + } /// \brief Destructor. /// @@ -655,6 +698,7 @@ template UndirEdgeSetWriter& writeUndirEdgeMap(std::string name, const Map& map, const Writer& writer = Writer()) { + checkConcept, Map>(); writers.push_back( make_pair(name, new MapWriter(map, writer))); return *this; @@ -665,6 +709,7 @@ /// Add a new directed map writer command for the writer. template UndirEdgeSetWriter& writeEdgeMap(std::string name, const Map& map) { + checkConcept, Map>(); writeUndirEdgeMap("+" + name, composeMap(forwardMap(graph), map)); writeUndirEdgeMap("-" + name, composeMap(backwardMap(graph), map)); return *this; @@ -676,8 +721,9 @@ template UndirEdgeSetWriter& writeEdgeMap(std::string name, const Map& map, const Writer& writer = Writer()) { + checkConcept, Map>(); writeUndirEdge("+" + name, composeMap(forwardMap(graph), map), writer); - writeUndirEdge("-" + name, composeMap(forwardMap(graph), map), writer); + writeUndirEdge("-" + name, composeMap(backwardMap(graph), map), writer); return *this; } @@ -694,6 +740,9 @@ /// /// Write the content of the section. virtual void write(std::ostream& os) { + if (!nodeIdWriter->isIdWriter()) { + throw DataFormatError("Cannot find nodeset or ID map"); + } for (int i = 0; i < (int)writers.size(); ++i) { if (writers[i].first == "id") { idMap = writers[i].second; @@ -811,8 +860,11 @@ template NodeWriter(LemonWriter& _writer, const _IdWriter& _idWriter, const std::string& _id = std::string()) - : Parent(_writer), id(_id), - idWriter(new IdWriter(_idWriter)) {} + : Parent(_writer), id(_id) { + checkConcept<_writer_bits::ItemIdWriter, _IdWriter>(); + idWriter.reset(new IdWriter(_idWriter)); + } + /// \brief Destructor. /// @@ -846,6 +898,9 @@ /// /// Write the content of the section. virtual void write(std::ostream& os) { + if (!idWriter->isIdWriter()) { + throw DataFormatError("Cannot find nodeset or ID map"); + } for (int i = 0; i < (int)writers.size(); ++i) { os << writers[i].first << ' '; idWriter->write(os, *(writers[i].second)); @@ -887,8 +942,10 @@ template EdgeWriter(LemonWriter& _writer, const _IdWriter& _idWriter, const std::string& _id = std::string()) - : Parent(_writer), id(_id), - idWriter(new IdWriter(_idWriter)) {} + : Parent(_writer), id(_id) { + checkConcept<_writer_bits::ItemIdWriter, _IdWriter>(); + idWriter.reset(new IdWriter(_idWriter)); + } /// \brief Destructor. /// @@ -921,6 +978,9 @@ /// /// Write the content of the section. virtual void write(std::ostream& os) { + if (!idWriter->isIdWriter()) { + throw DataFormatError("Cannot find edgeset or ID map"); + } for (int i = 0; i < (int)writers.size(); ++i) { os << writers[i].first << ' '; idWriter->write(os, *(writers[i].second)); @@ -966,9 +1026,12 @@ template UndirEdgeWriter(LemonWriter& _writer, const _IdWriter& _idWriter, const std::string& _id = std::string()) - : Parent(_writer), id(_id), - undirEdgeIdWriter(new IdWriter(_idWriter)), - edgeIdWriter(new IdWriter(_idWriter)) {} + : Parent(_writer), id(_id) { + checkConcept<_writer_bits::ItemIdWriter, _IdWriter>(); + checkConcept<_writer_bits::ItemIdWriter, _IdWriter>(); + undirEdgeIdWriter.reset(new IdWriter(_idWriter)); + edgeIdWriter.reset(new IdWriter(_idWriter)); + } /// \brief Destructor. /// @@ -1008,6 +1071,12 @@ /// /// Write the content of the section. virtual void write(std::ostream& os) { + if (!edgeIdWriter->isIdWriter()) { + throw DataFormatError("Cannot find undirected edgeset or ID map"); + } + if (!undirEdgeIdWriter->isIdWriter()) { + throw DataFormatError("Cannot find undirected edgeset or ID map"); + } for (int i = 0; i < (int)undirEdgeWriters.size(); ++i) { os << undirEdgeWriters[i].first << ' '; undirEdgeIdWriter->write(os, *(undirEdgeWriters[i].second));