COIN-OR::LEMON - Graph Library

Changeset 1476:182da222fceb in lemon-0.x for lemon/lemon_writer.h


Ignore:
Timestamp:
06/13/05 19:13:56 (15 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.