COIN-OR::LEMON - Graph Library

Changeset 1845:f8bbfed86036 in lemon-0.x for lemon/lemon_writer.h


Ignore:
Timestamp:
12/03/05 19:17:29 (14 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2404
Message:

Implementation redesign
Throws exception

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/lemon_writer.h

    r1705 r1845  
    165165    };
    166166
     167
     168    template <typename _Item>   
     169    class MapWriterBase {
     170    public:
     171      typedef _Item Item;
     172
     173      virtual ~MapWriterBase() {}
     174
     175      virtual void write(std::ostream& os, const Item& item) = 0;
     176    };
     177
     178
     179    template <typename _Item, typename _Map, typename _Writer>
     180    class MapWriter : public MapWriterBase<_Item> {
     181    public:
     182      typedef _Map Map;
     183      typedef _Writer Writer;
     184      typedef typename Writer::Value Value;
     185      typedef _Item Item;
     186     
     187      typename _writer_bits::Ref<Map>::Type map;
     188      Writer writer;
     189
     190      MapWriter(const Map& _map, const Writer& _writer)
     191        : map(_map), writer(_writer) {}
     192
     193      virtual ~MapWriter() {}
     194
     195      virtual void write(std::ostream& os, const Item& item) {
     196        Value value = map[item];
     197        writer.write(os, value);
     198      }
     199
     200    };
     201
     202
     203    class ValueWriterBase {
     204    public:
     205      virtual ~ValueWriterBase() {}
     206      virtual void write(std::ostream&) = 0;
     207    };
     208
     209    template <typename _Value, typename _Writer>
     210    class ValueWriter : public ValueWriterBase {
     211    public:
     212      typedef _Value Value;
     213      typedef _Writer Writer;
     214
     215      ValueWriter(const Value& _value, const Writer& _writer)
     216        : value(_value), writer(_writer) {}
     217
     218      virtual void write(std::ostream& os) {
     219        writer.write(os, value);
     220      }
     221    private:
     222      const Value& value;
     223      Writer writer;
     224    };
     225   
     226
     227    template <typename _Item>
     228    class IdWriterBase {
     229    public:
     230      typedef _Item Item;
     231      virtual ~IdWriterBase() {}
     232      virtual void write(std::ostream&, const Item&) const = 0;
     233      virtual bool isIdWriter() const = 0;
     234    };
     235
     236    template <typename _Item, typename _BoxedIdWriter>
     237    class IdWriter : public IdWriterBase<_Item> {
     238    public:
     239      typedef _Item Item;
     240      typedef _BoxedIdWriter BoxedIdWriter;
     241
     242      const BoxedIdWriter& idWriter;
     243
     244      IdWriter(const BoxedIdWriter& _idWriter)
     245        : idWriter(_idWriter) {}
     246
     247      virtual void write(std::ostream& os, const Item& item) const {
     248        idWriter.writeId(os, item);
     249      }
     250
     251      virtual bool isIdWriter() const {
     252        return idWriter.isIdWriter();
     253      }
     254    };
     255
    167256  }
    168257
     
    282371  };
    283372
    284   /// \brief Helper class for implementing the common SectionWriters.
    285   ///
    286   /// Helper class for implementing the common SectionWriters.
    287   class CommonSectionWriterBase : public LemonWriter::SectionWriter {
    288     typedef LemonWriter::SectionWriter Parent;
    289   protected:
    290    
    291     /// \brief Constructor for CommonSectionWriterBase.
    292     ///
    293     /// Constructor for CommonSectionWriterBase. It attach this writer to
    294     /// the given LemonWriter.
    295     CommonSectionWriterBase(LemonWriter& _writer)
    296       : Parent(_writer) {}
    297 
    298     template <typename _Item>   
    299     class WriterBase {
    300     public:
    301       typedef _Item Item;
    302 
    303       virtual ~WriterBase() {}
    304 
    305       virtual void write(std::ostream& os, const Item& item) = 0;
    306     };
    307 
    308 
    309     template <typename _Item, typename _Map, typename _Writer>
    310     class MapWriter : public WriterBase<_Item> {
    311     public:
    312       typedef _Map Map;
    313       typedef _Writer Writer;
    314       typedef typename Writer::Value Value;
    315       typedef _Item Item;
    316      
    317       typename _writer_bits::Ref<Map>::Type map;
    318       Writer writer;
    319 
    320       MapWriter(const Map& _map, const Writer& _writer)
    321         : map(_map), writer(_writer) {}
    322 
    323       virtual ~MapWriter() {}
    324 
    325       virtual void write(std::ostream& os, const Item& item) {
    326         Value value = map[item];
    327         writer.write(os, value);
    328       }
    329 
    330     };
    331 
    332 
    333     class ValueWriterBase {
    334     public:
    335       virtual ~ValueWriterBase() {}
    336       virtual void write(std::ostream&) = 0;
    337     };
    338 
    339     template <typename _Value, typename _Writer>
    340     class ValueWriter : public ValueWriterBase {
    341     public:
    342       typedef _Value Value;
    343       typedef _Writer Writer;
    344 
    345       ValueWriter(const Value& _value, const Writer& _writer)
    346         : value(_value), writer(_writer) {}
    347 
    348       virtual void write(std::ostream& os) {
    349         writer.write(os, value);
    350       }
    351     private:
    352       const Value& value;
    353       Writer writer;
    354     };
    355    
    356 
    357     template <typename _Item>
    358     class IdWriterBase {
    359     public:
    360       typedef _Item Item;
    361       virtual ~IdWriterBase() {}
    362       virtual void write(std::ostream&, const Item&) const = 0;
    363       virtual bool isIdWriter() const = 0;
    364     };
    365 
    366     template <typename _Item, typename _BoxedIdWriter>
    367     class IdWriter : public IdWriterBase<_Item> {
    368     public:
    369       typedef _Item Item;
    370       typedef _BoxedIdWriter BoxedIdWriter;
    371 
    372       const BoxedIdWriter& idWriter;
    373 
    374       IdWriter(const BoxedIdWriter& _idWriter)
    375         : idWriter(_idWriter) {}
    376 
    377       virtual void write(std::ostream& os, const Item& item) const {
    378         idWriter.writeId(os, item);
    379       }
    380 
    381       virtual bool isIdWriter() const {
    382         return idWriter.isIdWriter();
    383       }
    384     };
    385   };
    386 
    387373  /// \ingroup io_group
    388374  /// \brief SectionWriter for writing a graph's nodeset.
     
    404390  /// \relates LemonWriter
    405391  template <typename _Graph, typename _Traits = DefaultWriterTraits>
    406   class NodeSetWriter : public CommonSectionWriterBase {
    407     typedef CommonSectionWriterBase Parent;
     392  class NodeSetWriter : public LemonWriter::SectionWriter {
     393    typedef LemonWriter::SectionWriter Parent;
    408394  public:
    409395
     
    458444      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
    459445      writers.push_back(
    460         make_pair(name, new MapWriter<Node, Map, Writer>(map, writer)));
     446        make_pair(name, new _writer_bits::
     447                  MapWriter<Node, Map, Writer>(map, writer)));
    461448      return *this;
    462449    }
     
    528515  private:
    529516
    530     typedef std::vector<std::pair<std::string, WriterBase<Node>*> > MapWriters;
     517    typedef std::vector<std::pair<std::string, _writer_bits::
     518                                  MapWriterBase<Node>*> > MapWriters;
    531519    MapWriters writers;
    532520
    533     WriterBase<Node>* idMap;
     521    _writer_bits::MapWriterBase<Node>* idMap;
    534522    bool forceIdMap;
    535523   
     
    563551  /// \relates LemonWriter
    564552  template <typename _Graph, typename _Traits = DefaultWriterTraits>
    565   class EdgeSetWriter : public CommonSectionWriterBase {
    566     typedef CommonSectionWriterBase Parent;
     553  class EdgeSetWriter : public LemonWriter::SectionWriter {
     554    typedef LemonWriter::SectionWriter Parent;
    567555  public:
    568556
     
    587575        graph(_graph), id(_id) {
    588576      checkConcept<_writer_bits::ItemIdWriter<Node>, NodeIdWriter>();
    589       nodeIdWriter.reset(new IdWriter<Node, NodeIdWriter>(_nodeIdWriter));
     577      nodeIdWriter.reset(new _writer_bits::
     578                         IdWriter<Node, NodeIdWriter>(_nodeIdWriter));
    590579    }
    591580
     
    624613      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
    625614      writers.push_back(
    626         make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer)));
     615        make_pair(name, new _writer_bits::
     616                  MapWriter<Edge, Map, Writer>(map, writer)));
    627617      return *this;
    628618    }
     
    702692  private:
    703693
    704     typedef std::vector<std::pair<std::string, WriterBase<Edge>*> > MapWriters;
     694    typedef std::vector<std::pair<std::string, _writer_bits::
     695                                  MapWriterBase<Edge>*> > MapWriters;
    705696    MapWriters writers;
    706697
    707     WriterBase<Edge>* idMap;
     698    _writer_bits::MapWriterBase<Edge>* idMap;
    708699    bool forceIdMap;
    709700   
     
    711702    std::string id;
    712703
    713     std::auto_ptr<IdWriterBase<Node> > nodeIdWriter;
     704    std::auto_ptr<_writer_bits::IdWriterBase<Node> > nodeIdWriter;
    714705  };
    715706
     
    744735  /// \relates LemonWriter
    745736  template <typename _Graph, typename _Traits = DefaultWriterTraits>
    746   class UndirEdgeSetWriter : public CommonSectionWriterBase {
    747     typedef CommonSectionWriterBase Parent;
     737  class UndirEdgeSetWriter : public LemonWriter::SectionWriter {
     738    typedef LemonWriter::SectionWriter Parent;
    748739  public:
    749740
     
    769760        graph(_graph), id(_id) {
    770761      checkConcept<_writer_bits::ItemIdWriter<Node>, NodeIdWriter>();
    771       nodeIdWriter.reset(new IdWriter<Node, NodeIdWriter>(_nodeIdWriter));
     762      nodeIdWriter.reset(new _writer_bits::
     763                         IdWriter<Node, NodeIdWriter>(_nodeIdWriter));
    772764    }
    773765
     
    806798      checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>();
    807799      writers.push_back(
    808         make_pair(name, new MapWriter<UndirEdge, Map, Writer>(map, writer)));
     800        make_pair(name, new _writer_bits::
     801                  MapWriter<UndirEdge, Map, Writer>(map, writer)));
    809802      return *this;
    810803    }
     
    929922  private:
    930923
    931     typedef std::vector<std::pair<std::string,
    932                                   WriterBase<UndirEdge>*> > MapWriters;
     924    typedef std::vector<std::pair<std::string, _writer_bits::
     925                                  MapWriterBase<UndirEdge>*> > MapWriters;
    933926    MapWriters writers;
    934927
    935     WriterBase<UndirEdge>* idMap;
     928    _writer_bits::MapWriterBase<UndirEdge>* idMap;
    936929    bool forceIdMap;
    937930   
     
    939932    std::string id;
    940933
    941     std::auto_ptr<IdWriterBase<Node> > nodeIdWriter;
     934    std::auto_ptr<_writer_bits::IdWriterBase<Node> > nodeIdWriter;
    942935  };
    943936
     
    953946  /// \relates LemonWriter
    954947  template <typename _Graph>
    955   class NodeWriter : public CommonSectionWriterBase {
    956     typedef CommonSectionWriterBase Parent;
     948  class NodeWriter : public LemonWriter::SectionWriter {
     949    typedef LemonWriter::SectionWriter Parent;
    957950    typedef _Graph Graph;
    958951    typedef typename Graph::Node Node;
     
    969962      : Parent(_writer), id(_id) {
    970963      checkConcept<_writer_bits::ItemIdWriter<Node>, _IdWriter>();
    971       idWriter.reset(new IdWriter<Node, _IdWriter>(_idWriter));
     964      idWriter.reset(new _writer_bits::IdWriter<Node, _IdWriter>(_idWriter));
    972965    }
    973966
     
    10211014    typedef std::vector<std::pair<std::string, const Node*> > NodeWriters;
    10221015    NodeWriters writers;
    1023     std::auto_ptr<IdWriterBase<Node> > idWriter;
     1016    std::auto_ptr<_writer_bits::IdWriterBase<Node> > idWriter;
    10241017  };
    10251018
     
    10351028  /// \relates LemonWriter
    10361029  template <typename _Graph>
    1037   class EdgeWriter : public CommonSectionWriterBase {
    1038     typedef CommonSectionWriterBase Parent;
     1030  class EdgeWriter : public LemonWriter::SectionWriter {
     1031    typedef LemonWriter::SectionWriter Parent;
    10391032    typedef _Graph Graph;
    10401033    typedef typename Graph::Edge Edge;
     
    10511044      : Parent(_writer), id(_id) {
    10521045      checkConcept<_writer_bits::ItemIdWriter<Edge>, _IdWriter>();
    1053       idWriter.reset(new IdWriter<Edge, _IdWriter>(_idWriter));
     1046      idWriter.reset(new _writer_bits::IdWriter<Edge, _IdWriter>(_idWriter));
    10541047    }
    10551048
     
    11021095    EdgeWriters writers;
    11031096
    1104     std::auto_ptr<IdWriterBase<Edge> > idWriter;
     1097    std::auto_ptr<_writer_bits::IdWriterBase<Edge> > idWriter;
    11051098  };
    11061099
     
    11161109  /// \relates LemonWriter
    11171110  template <typename _Graph>
    1118   class UndirEdgeWriter : public CommonSectionWriterBase {
    1119     typedef CommonSectionWriterBase Parent;
     1111  class UndirEdgeWriter : public LemonWriter::SectionWriter {
     1112    typedef LemonWriter::SectionWriter Parent;
    11201113    typedef _Graph Graph;
    11211114    typedef typename Graph::Node Node;
     
    11361129      checkConcept<_writer_bits::ItemIdWriter<Edge>, _IdWriter>();
    11371130      checkConcept<_writer_bits::ItemIdWriter<UndirEdge>, _IdWriter>();
    1138       undirEdgeIdWriter.reset(new IdWriter<UndirEdge, _IdWriter>(_idWriter));
    1139       edgeIdWriter.reset(new IdWriter<Edge, _IdWriter>(_idWriter));
     1131      undirEdgeIdWriter.reset(new _writer_bits::
     1132                              IdWriter<UndirEdge, _IdWriter>(_idWriter));
     1133      edgeIdWriter.reset(new _writer_bits::
     1134                         IdWriter<Edge, _IdWriter>(_idWriter));
    11401135    }
    11411136
     
    12031198                                  const UndirEdge*> > UndirEdgeWriters;
    12041199    UndirEdgeWriters undirEdgeWriters;
    1205     std::auto_ptr<IdWriterBase<UndirEdge> > undirEdgeIdWriter;
     1200    std::auto_ptr<_writer_bits::IdWriterBase<UndirEdge> > undirEdgeIdWriter;
    12061201
    12071202    typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters;
    12081203    EdgeWriters edgeWriters;
    1209     std::auto_ptr<IdWriterBase<Edge> > edgeIdWriter;
     1204    std::auto_ptr<_writer_bits::IdWriterBase<Edge> > edgeIdWriter;
    12101205
    12111206  };
     
    12231218  /// \relates LemonWriter
    12241219  template <typename _Traits = DefaultWriterTraits>
    1225   class AttributeWriter : public CommonSectionWriterBase {
    1226     typedef CommonSectionWriterBase Parent;
     1220  class AttributeWriter : public LemonWriter::SectionWriter {
     1221    typedef LemonWriter::SectionWriter Parent;
    12271222    typedef _Traits Traits;
    12281223  public:
     
    12681263                                    const Writer& writer = Writer()) {
    12691264      checkConcept<_writer_bits::ItemWriter<Value>, Writer>();
    1270       writers.push_back(make_pair(name, new ValueWriter<Value, Writer>
    1271                                (value, writer)));
     1265      writers.push_back(make_pair(name, new _writer_bits::
     1266                                  ValueWriter<Value, Writer>(value, writer)));
    12721267      return *this;
    12731268    }
     
    12971292    std::string id;
    12981293
    1299     typedef std::vector<std::pair<std::string, ValueWriterBase*> > Writers;
     1294    typedef std::vector<std::pair<std::string,
     1295                                  _writer_bits::ValueWriterBase*> > Writers;
    13001296    Writers writers; 
    13011297  };
Note: See TracChangeset for help on using the changeset viewer.