COIN-OR::LEMON - Graph Library

Changeset 1476:182da222fceb in lemon-0.x


Ignore:
Timestamp:
06/13/05 19:13:56 (14 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1956
Message:

Some bug fix.

Added: Concept check for maps and IdReader/Writer? classes

Some runtime check

Location:
lemon
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • lemon/graph_reader.h

    r1435 r1476  
    262262    GraphReader& readEdge(std::string name, Edge& edge) {
    263263      edge_reader.readEdge(name, edge);
     264      return *this;
    264265    }
    265266
  • lemon/lemon_reader.h

    r1435 r1476  
    3737#include <lemon/bits/item_reader.h>
    3838
     39#include <lemon/concept_check.h>
     40#include <lemon/concept/maps.h>
    3941
    4042namespace lemon {
    4143
    4244  namespace _reader_bits {
     45
     46    template <typename Item>
     47    class ItemIdReader {
     48    public:
     49
     50      bool isIdReader() { return true; }
     51
     52      Item readId(std::istream&, Item) { return Item();}
     53     
     54      template <class _ItemIdReader>
     55      struct Constraints {
     56        void constraints() {
     57          bool b = reader.isIdReader();
     58          ignore_unused_variable_warning(b);
     59          Item item = reader.readId(is, Item());
     60        }
     61        _ItemIdReader& reader;
     62        std::istream& is;
     63      };
     64
     65    };
    4366 
    4467    template <typename T>
     
    532555      typedef _Item Item;
    533556      virtual Item read(std::istream& is) const = 0;
     557      virtual bool isIdReader() const = 0;
    534558    };
    535559
     
    547571      virtual Item read(std::istream& is) const {
    548572        return boxedIdReader.readId(is, Item());
     573      }
     574
     575      virtual bool isIdReader() const {
     576        return boxedIdReader.isIdReader();
    549577      }
    550578    };
     
    818846                  const std::string& _id = std::string(),
    819847                  const DefaultSkipper& _skipper = DefaultSkipper())
    820       : Parent(_reader), graph(_graph), id(_id), skipper(_skipper),
    821         nodeIdReader(new IdReader<Node, NodeIdReader>(_nodeIdReader)) {}
    822 
     848      : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {
     849      checkConcept<_reader_bits::ItemIdReader<Node>, NodeIdReader>();
     850      nodeIdReader.reset(new IdReader<Node, NodeIdReader>(_nodeIdReader));
     851    }
    823852    /// \brief Destructor.
    824853    ///
     
    922951    /// It reads the content of the section.
    923952    virtual void read(std::istream& is) {
     953      if (!nodeIdReader->isIdReader()) {
     954        throw DataFormatError("Cannot find nodeset or ID map");
     955      }
    924956      std::vector<ReaderBase<Edge>* > index;
    925957      std::string line;
     
    10341066                       const std::string& _id = std::string(),
    10351067                       const DefaultSkipper& _skipper = DefaultSkipper())
    1036       : Parent(_reader), graph(_graph), id(_id), skipper(_skipper),
    1037         nodeIdReader(new IdReader<Node, NodeIdReader>(_nodeIdReader)) {}
    1038 
     1068      : Parent(_reader), graph(_graph), id(_id), skipper(_skipper) {
     1069      checkConcept<_reader_bits::ItemIdReader<Node>, NodeIdReader>();
     1070      nodeIdReader.reset(new IdReader<Node, NodeIdReader>(_nodeIdReader));
     1071    }
    10391072    /// \brief Destructor.
    10401073    ///
     
    11981231    /// It reads the content of the section.
    11991232    virtual void read(std::istream& is) {
     1233      if (!nodeIdReader->isIdReader()) {
     1234        throw DataFormatError("Cannot find nodeset or ID map");
     1235      }
    12001236      std::vector<ReaderBase<UndirEdge>* > index;
    12011237      std::string line;
     
    13031339    NodeReader(LemonReader& _reader, const _IdReader& _idReader,
    13041340               const std::string& _id = std::string())
    1305       : Parent(_reader), id(_id),
    1306         idReader(new IdReader<typename Graph::Node, _IdReader>(_idReader)) {}
     1341      : Parent(_reader), id(_id) {
     1342      checkConcept<_reader_bits::ItemIdReader<Node>, _IdReader>();
     1343      idReader.reset(new IdReader<Node, _IdReader>(_idReader));
     1344    }
    13071345
    13081346    /// \brief Destructor.
     
    13481386    /// It reads the content of the section.
    13491387    virtual void read(std::istream& is) {
     1388      if (!idReader->isIdReader()) {
     1389        throw DataFormatError("Cannot find nodeset or ID map");
     1390      }
    13501391      std::string line;
    13511392      while (getline(is, line)) {
     
    13951436    EdgeReader(LemonReader& _reader, const _IdReader& _idReader,
    13961437               const std::string& _id = std::string())
    1397       : Parent(_reader), id(_id),
    1398         idReader(new IdReader<typename Graph::Edge, _IdReader>(_idReader)) {}
     1438      : Parent(_reader), id(_id) {
     1439      checkConcept<_reader_bits::ItemIdReader<Edge>, _IdReader>();
     1440      idReader.reset(new IdReader<Edge, _IdReader>(_idReader));
     1441    }
    13991442
    14001443    /// \brief Destructor.
     
    14391482    /// It reads the content of the section.
    14401483    virtual void read(std::istream& is) {
     1484      if (!idReader->isIdReader()) {
     1485        throw DataFormatError("Cannot find edgeset or ID map");
     1486      }
    14411487      std::string line;
    14421488      while (getline(is, line)) {
     
    14881534    UndirEdgeReader(LemonReader& _reader, const _IdReader& _idReader,
    14891535               const std::string& _id = std::string())
    1490       : Parent(_reader), id(_id),
    1491         undirEdgeIdReader(new IdReader<UndirEdge, _IdReader>(_idReader)),
    1492         edgeIdReader(new IdReader<Edge, _IdReader>(_idReader))
    1493     {}
     1536      : Parent(_reader), id(_id) {
     1537      checkConcept<_reader_bits::ItemIdReader<UndirEdge>, _IdReader>();
     1538      checkConcept<_reader_bits::ItemIdReader<Edge>, _IdReader>();
     1539      undirEdgeIdReader.reset(new IdReader<UndirEdge, _IdReader>(_idReader));
     1540      egdeIdReader.reset(new IdReader<Edge, _IdReader>(_idReader));
     1541    }
    14941542
    14951543    /// \brief Destructor.
     
    15461594    /// It reads the content of the section.
    15471595    virtual void read(std::istream& is) {
     1596      if (!edgeIdReader->isIdReader()) {
     1597        throw DataFormatError("Cannot find undirected edgeset or ID map");
     1598      }
     1599      if (!undirEdgeIdReader->isIdReader()) {
     1600        throw DataFormatError("Cannot find undirected edgeset or ID map");
     1601      }
    15481602      std::string line;
    15491603      while (getline(is, line)) {
  • lemon/lemon_writer.h

    r1435 r1476  
    3737#include <lemon/maps.h>
    3838
     39#include <lemon/concept_check.h>
     40#include <lemon/concept/maps.h>
     41
    3942
    4043namespace lemon {
     44
     45  namespace _writer_bits {
     46   
     47    template <typename Item>
     48    class ItemIdWriter {
     49    public:
     50
     51      bool isIdWriter() { return true; }
     52
     53      void writeId(std::ostream&, const Item&) {}
     54     
     55      template <class _ItemIdWriter>
     56      struct Constraints {
     57        void constraints() {
     58          const Item item;
     59          bool b = writer.isIdWriter();
     60          ignore_unused_variable_warning(b);
     61          writer.writeId(os, item);
     62        }
     63        _ItemIdWriter& writer;
     64        std::ostream& os;
     65      };
     66
     67    };
     68
     69  }
    4170
    4271  /// \ingroup io_group
     
    230259      typedef _Item Item;
    231260      virtual void write(std::ostream&, const Item&) const = 0;
     261      virtual bool isIdWriter() const = 0;
    232262    };
    233263
     
    245275      virtual void write(std::ostream& os, const Item& item) const {
    246276        idWriter.writeId(os, item);
     277      }
     278
     279      virtual bool isIdWriter() const {
     280        return idWriter.isIdWriter();
    247281      }
    248282    };
     
    319353    NodeSetWriter& writeNodeMap(std::string name, const Map& map,
    320354                            const Writer& writer = Writer()) {
     355      checkConcept<concept::WriteMap<Node, typename Map::Value>, Map>();
    321356      writers.push_back(
    322357        make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
     
    447482                  bool _forceIdMap = true)
    448483      : Parent(_writer), idMap(0), forceIdMap(_forceIdMap),
    449         graph(_graph), id(_id),
    450         nodeIdWriter(new IdWriter<Node, NodeIdWriter>(_nodeIdWriter)) {}
     484        graph(_graph), id(_id) {
     485      checkConcept<_writer_bits::ItemIdWriter<Node>, NodeIdWriter>();
     486      nodeIdWriter.reset(new IdWriter<Node, NodeIdWriter>(_nodeIdWriter));
     487    }
    451488
    452489    /// \brief Destructor.
     
    481518    EdgeSetWriter& writeEdgeMap(std::string name, const Map& map,
    482519                            const Writer& writer = Writer()) {
     520      checkConcept<concept::WriteMap<Edge, typename Map::Value>, Map>();
    483521      writers.push_back(
    484522        make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer)));
     
    499537    /// Write the content of the section.
    500538    virtual void write(std::ostream& os) {
     539      if (!nodeIdWriter->isIdWriter()) {
     540        throw DataFormatError("Cannot find nodeset or ID map");
     541      }
    501542      for (int i = 0; i < (int)writers.size(); ++i) {
    502543        if (writers[i].first == "id") {
     
    622663                       bool _forceIdMap = true)
    623664      : Parent(_writer), idMap(0), forceIdMap(_forceIdMap),
    624         graph(_graph), id(_id),
    625         nodeIdWriter(new IdWriter<Node, NodeIdWriter>(_nodeIdWriter)) {}
     665        graph(_graph), id(_id) {
     666      checkConcept<_writer_bits::ItemIdWriter<Node>, NodeIdWriter>();
     667      nodeIdWriter.reset(new IdWriter<Node, NodeIdWriter>(_nodeIdWriter));
     668    }
    626669
    627670    /// \brief Destructor.
     
    656699    UndirEdgeSetWriter& writeUndirEdgeMap(std::string name, const Map& map,
    657700                                          const Writer& writer = Writer()) {
     701      checkConcept<concept::WriteMap<UndirEdge, typename Map::Value>, Map>();
    658702      writers.push_back(
    659703        make_pair(name, new MapWriter<UndirEdge, Map, Writer>(map, writer)));
     
    666710    template <typename Map>
    667711    UndirEdgeSetWriter& writeEdgeMap(std::string name, const Map& map) {
     712      checkConcept<concept::WriteMap<Edge, typename Map::Value>, Map>();
    668713      writeUndirEdgeMap("+" + name, composeMap(forwardMap(graph), map));
    669714      writeUndirEdgeMap("-" + name, composeMap(backwardMap(graph), map));
     
    677722    UndirEdgeSetWriter& writeEdgeMap(std::string name, const Map& map,
    678723                                     const Writer& writer = Writer()) {
     724      checkConcept<concept::WriteMap<Edge, typename Map::Value>, Map>();
    679725      writeUndirEdge("+" + name, composeMap(forwardMap(graph), map), writer);
    680       writeUndirEdge("-" + name, composeMap(forwardMap(graph), map), writer);
     726      writeUndirEdge("-" + name, composeMap(backwardMap(graph), map), writer);
    681727      return *this;
    682728    }
     
    695741    /// Write the content of the section.
    696742    virtual void write(std::ostream& os) {
     743      if (!nodeIdWriter->isIdWriter()) {
     744        throw DataFormatError("Cannot find nodeset or ID map");
     745      }
    697746      for (int i = 0; i < (int)writers.size(); ++i) {
    698747        if (writers[i].first == "id") {
     
    812861    NodeWriter(LemonWriter& _writer, const _IdWriter& _idWriter,
    813862               const std::string& _id = std::string())
    814       : Parent(_writer), id(_id),
    815         idWriter(new IdWriter<typename Graph::Node, _IdWriter>(_idWriter)) {}
     863      : Parent(_writer), id(_id) {
     864      checkConcept<_writer_bits::ItemIdWriter<Node>, _IdWriter>();
     865      idWriter.reset(new IdWriter<Node, _IdWriter>(_idWriter));
     866    }
     867
    816868
    817869    /// \brief Destructor.
     
    847899    /// Write the content of the section.
    848900    virtual void write(std::ostream& os) {
     901      if (!idWriter->isIdWriter()) {
     902        throw DataFormatError("Cannot find nodeset or ID map");
     903      }
    849904      for (int i = 0; i < (int)writers.size(); ++i) {
    850905        os << writers[i].first << ' ';
     
    888943    EdgeWriter(LemonWriter& _writer, const _IdWriter& _idWriter,
    889944               const std::string& _id = std::string())
    890       : Parent(_writer), id(_id),
    891         idWriter(new IdWriter<typename Graph::Edge, _IdWriter>(_idWriter)) {}
     945      : Parent(_writer), id(_id) {
     946      checkConcept<_writer_bits::ItemIdWriter<Edge>, _IdWriter>();
     947      idWriter.reset(new IdWriter<Edge, _IdWriter>(_idWriter));
     948    }
    892949
    893950    /// \brief Destructor.
     
    922979    /// Write the content of the section.
    923980    virtual void write(std::ostream& os) {
     981      if (!idWriter->isIdWriter()) {
     982        throw DataFormatError("Cannot find edgeset or ID map");
     983      }
    924984      for (int i = 0; i < (int)writers.size(); ++i) {
    925985        os << writers[i].first << ' ';
     
    9671027    UndirEdgeWriter(LemonWriter& _writer, const _IdWriter& _idWriter,
    9681028               const std::string& _id = std::string())
    969       : Parent(_writer), id(_id),
    970         undirEdgeIdWriter(new IdWriter<UndirEdge, _IdWriter>(_idWriter)),
    971         edgeIdWriter(new IdWriter<Edge, _IdWriter>(_idWriter)) {}
     1029      : Parent(_writer), id(_id) {
     1030      checkConcept<_writer_bits::ItemIdWriter<Edge>, _IdWriter>();
     1031      checkConcept<_writer_bits::ItemIdWriter<UndirEdge>, _IdWriter>();
     1032      undirEdgeIdWriter.reset(new IdWriter<UndirEdge, _IdWriter>(_idWriter));
     1033      edgeIdWriter.reset(new IdWriter<Edge, _IdWriter>(_idWriter));
     1034    }
    9721035
    9731036    /// \brief Destructor.
     
    10091072    /// Write the content of the section.
    10101073    virtual void write(std::ostream& os) {
     1074      if (!edgeIdWriter->isIdWriter()) {
     1075        throw DataFormatError("Cannot find undirected edgeset or ID map");
     1076      }
     1077      if (!undirEdgeIdWriter->isIdWriter()) {
     1078        throw DataFormatError("Cannot find undirected edgeset or ID map");
     1079      }
    10111080      for (int i = 0; i < (int)undirEdgeWriters.size(); ++i) {
    10121081        os << undirEdgeWriters[i].first << ' ';
Note: See TracChangeset for help on using the changeset viewer.