Some bug fixes and improvments in the io classes
authordeba
Wed, 07 Dec 2005 11:57:30 +0000
changeset 1852ffa7c6e96330
parent 1851 78b5ea23f0f1
child 1853 dd0b47adc152
Some bug fixes and improvments in the io classes
lemon/bits/item_reader.h
lemon/bits/item_writer.h
lemon/lemon_writer.h
     1.1 --- a/lemon/bits/item_reader.h	Tue Dec 06 18:44:26 2005 +0000
     1.2 +++ b/lemon/bits/item_reader.h	Wed Dec 07 11:57:30 2005 +0000
     1.3 @@ -180,6 +180,12 @@
     1.4  
     1.5    public:
     1.6  
     1.7 +    /// \brief Constructor for InsertReader
     1.8 +    ///
     1.9 +    /// Constructor for InsertReader
    1.10 +    PushBackReader(const ItemReader& _item_reader = ItemReader())
    1.11 +      : item_reader(_item_reader) {}
    1.12 +
    1.13      /// \brief Reads the values into the container from the given stream.
    1.14      ///
    1.15      /// Reads the values into the container from the given stream.
    1.16 @@ -194,7 +200,6 @@
    1.17  	value.push_back(item);
    1.18        }
    1.19        if (!is) throw DataFormatError("PushBackReader format error");
    1.20 -      is.putback(c);
    1.21      }
    1.22  
    1.23    };
    1.24 @@ -223,6 +228,12 @@
    1.25  
    1.26    public:
    1.27  
    1.28 +    /// \brief Constructor for InsertReader
    1.29 +    ///
    1.30 +    /// Constructor for InsertReader
    1.31 +    InsertReader(const ItemReader& _item_reader = ItemReader())
    1.32 +      : item_reader(_item_reader) {}
    1.33 +
    1.34      /// \brief Reads the values into the container from the given stream.
    1.35      ///
    1.36      /// Reads the values into the container from the given stream.
    1.37 @@ -237,7 +248,6 @@
    1.38  	value.insert(item);
    1.39        }
    1.40        if (!is) throw DataFormatError("PushBackReader format error");
    1.41 -      is.putback(c);
    1.42      }
    1.43  
    1.44    };
    1.45 @@ -310,10 +320,10 @@
    1.46      /// \brief Reads the line from the given stream.
    1.47      ///
    1.48      /// Reads the line from the given stream.
    1.49 -    void read(std::istream& is, Value& value) {
    1.50 +    void read(std::istream& is, Value& value) const {
    1.51        if (skipSpaces) is >> std::ws;
    1.52        if (!getline(is, value)) {
    1.53 -	throw DataFormatError("LineReader forma error");
    1.54 +	throw DataFormatError("LineReader format error");
    1.55        }
    1.56      }
    1.57    private:
    1.58 @@ -321,6 +331,60 @@
    1.59    };
    1.60  
    1.61    /// \ingroup item_io
    1.62 +  /// \brief Reader for std::pair.
    1.63 +  ///
    1.64 +  /// Reader for std::pair.
    1.65 +  ///
    1.66 +  /// \author Balazs Dezso
    1.67 +  template <typename _Pair, 
    1.68 +	    typename _FirstReader = 
    1.69 +	    DefaultReader<typename _Pair::first_type>,
    1.70 +	    typename _SecondReader = 
    1.71 +	    DefaultReader<typename _Pair::second_type> >
    1.72 +  class PairReader {
    1.73 +  public:
    1.74 +    typedef _Pair Value;
    1.75 +
    1.76 +    typedef _FirstReader FirstReader;
    1.77 +    typedef _SecondReader SecondReader;
    1.78 +
    1.79 +  private:
    1.80 +
    1.81 +    FirstReader first_reader;
    1.82 +    SecondReader second_reader;
    1.83 +
    1.84 +  public:
    1.85 +    
    1.86 +    /// \brief Constructor.
    1.87 +    ///
    1.88 +    /// Constructor for the PairReader.
    1.89 +    PairReader(const FirstReader& _first_reader = FirstReader(), 
    1.90 +	       const SecondReader& _second_reader = SecondReader()) 
    1.91 +      : first_reader(_first_reader), second_reader(_second_reader) {}
    1.92 +    
    1.93 +    /// \brief Reads the pair from the given stream.
    1.94 +    ///
    1.95 +    /// Reads the pair from the given stream.
    1.96 +    void read(std::istream& is, Value& value) const {
    1.97 +      char c;
    1.98 +      if (!(is >> c) || c != '(') {
    1.99 +	throw DataFormatError("PairReader format error");
   1.100 +      }
   1.101 +      first_reader.read(is, value.first);
   1.102 +      if (!(is >> c) || c != '=') {
   1.103 +	throw DataFormatError("PairReader format error");
   1.104 +      }
   1.105 +      if (!(is >> c) || c != '>') {
   1.106 +	throw DataFormatError("PairReader format error");
   1.107 +      }
   1.108 +      second_reader.read(is, value.second);
   1.109 +      if (!(is >> c) || c != ')') {
   1.110 +	throw DataFormatError("PairReader format error");
   1.111 +      }
   1.112 +    }
   1.113 +  };
   1.114 +
   1.115 +  /// \ingroup item_io
   1.116    /// 
   1.117    /// \brief The default item reader template class.
   1.118    ///
   1.119 @@ -389,10 +453,24 @@
   1.120    class DefaultReader<std::set<Item> > 
   1.121      : public InsertReader<std::set<Item> > {};
   1.122  
   1.123 +  template <typename Key, typename Value>
   1.124 +  class DefaultReader<std::map<Key, Value> > 
   1.125 +    : public InsertReader<std::map<Key, Value>,
   1.126 +			  DefaultReader<std::pair<Key, Value> > > {};
   1.127 +
   1.128    template <typename Item>
   1.129    class DefaultReader<std::multiset<Item> > 
   1.130      : public InsertReader<std::multiset<Item> > {};
   1.131  
   1.132 +  template <typename Key, typename Value>
   1.133 +  class DefaultReader<std::multimap<Key, Value> > 
   1.134 +    : public InsertReader<std::multimap<Key, Value>,
   1.135 +			  DefaultReader<std::pair<Key, Value> > > {};
   1.136 +
   1.137 +  template <typename First, typename Second>
   1.138 +  class DefaultReader<std::pair<First, Second> > 
   1.139 +    : public PairReader<std::pair<First, Second> > {};
   1.140 +
   1.141    /// \ingroup item_io
   1.142    /// 
   1.143    /// \brief The default item reader for skipping a value in the stream.
     2.1 --- a/lemon/bits/item_writer.h	Tue Dec 06 18:44:26 2005 +0000
     2.2 +++ b/lemon/bits/item_writer.h	Wed Dec 07 11:57:30 2005 +0000
     2.3 @@ -53,7 +53,7 @@
     2.4      /// \brief Writes a quoted string to the given stream.
     2.5      ///
     2.6      /// Writes a quoted string to the given stream.
     2.7 -    void write(std::ostream& os, const std::string& value) {
     2.8 +    void write(std::ostream& os, const std::string& value) const {
     2.9        os << "\"";
    2.10        if (escaped) {
    2.11  	std::ostringstream ls;
    2.12 @@ -136,7 +136,7 @@
    2.13      /// \brief Writes a quoted char array to the given stream.
    2.14      ///
    2.15      /// Writes a quoted char array to the given stream.
    2.16 -    void write(std::ostream& os, const char* value) {
    2.17 +    void write(std::ostream& os, const char* value) const {
    2.18        QuotedStringWriter(escaped).write(os, std::string(value));
    2.19      }
    2.20  
    2.21 @@ -169,6 +169,9 @@
    2.22  
    2.23    public:
    2.24  
    2.25 +    IterableWriter(const ItemWriter& _item_writer = ItemWriter())
    2.26 +      : item_writer(_item_writer) {}
    2.27 +
    2.28      /// \brief Writes the values of the container to the given stream.
    2.29      ///
    2.30      /// Writes the values of the container to the given stream.
    2.31 @@ -185,6 +188,53 @@
    2.32    };
    2.33  
    2.34    /// \ingroup item_io
    2.35 +  ///
    2.36 +  /// \brief Writer for standard pairs.
    2.37 +  ///
    2.38 +  /// Writer for standard pairs. The representation of a pair is
    2.39 +  /// \code ( first_value => second_value ) \endcode.
    2.40 +  /// \author Balazs Dezso
    2.41 +  template <typename _Pair, 
    2.42 +	    typename _FirstWriter = 
    2.43 +	    DefaultWriter<typename _Pair::first_type>,
    2.44 +	    typename _SecondWriter = 
    2.45 +	    DefaultWriter<typename _Pair::second_type> >
    2.46 +  class PairWriter {
    2.47 +  public:
    2.48 +
    2.49 +    typedef _Pair Value;
    2.50 +
    2.51 +    typedef _FirstWriter FirstWriter;
    2.52 +    typedef _SecondWriter SecondWriter;
    2.53 +
    2.54 +  private:
    2.55 +
    2.56 +    FirstWriter first_writer;
    2.57 +    SecondWriter second_writer;
    2.58 +
    2.59 +  public:
    2.60 +    
    2.61 +    /// \brief Constructor.
    2.62 +    ///
    2.63 +    /// Constructor for the PairWriter.
    2.64 +    PairWriter(const FirstWriter& _first_writer = FirstWriter(), 
    2.65 +	       const SecondWriter& _second_writer = SecondWriter()) 
    2.66 +      : first_writer(_first_writer), second_writer(_second_writer) {}
    2.67 +    
    2.68 +    /// \brief Writes the pair from the given stream.
    2.69 +    ///
    2.70 +    /// Writes the pair from the given stream.
    2.71 +    void write(std::ostream& os, const Value& value) const {
    2.72 +      os << "( ";
    2.73 +      first_writer.write(os, value.first);
    2.74 +      os << " => ";
    2.75 +      second_writer.write(os, value.second);
    2.76 +      os << " )";
    2.77 +    }
    2.78 +
    2.79 +  };
    2.80 +
    2.81 +  /// \ingroup item_io
    2.82    /// 
    2.83    /// \brief The default item writer template class.
    2.84    ///
    2.85 @@ -217,6 +267,14 @@
    2.86    class DefaultWriter<const char[length]> 
    2.87      : public QuotedCharArrayWriter {};
    2.88  
    2.89 +  template <>
    2.90 +  class DefaultWriter<char*> 
    2.91 +    : public QuotedCharArrayWriter {};
    2.92 +
    2.93 +  template <>
    2.94 +  class DefaultWriter<const char*> 
    2.95 +    : public QuotedCharArrayWriter {};
    2.96 +
    2.97    template <typename Item>
    2.98    class DefaultWriter<std::vector<Item> > 
    2.99      : public IterableWriter<std::vector<Item> > {};
   2.100 @@ -233,10 +291,22 @@
   2.101    class DefaultWriter<std::set<Item> > 
   2.102      : public IterableWriter<std::set<Item> > {};
   2.103  
   2.104 +  template <typename Key, typename Value>
   2.105 +  class DefaultWriter<std::map<Key, Value> > 
   2.106 +    : public IterableWriter<std::map<Key, Value> > {};
   2.107 +
   2.108    template <typename Item>
   2.109    class DefaultWriter<std::multiset<Item> > 
   2.110      : public IterableWriter<std::multiset<Item> > {};
   2.111  
   2.112 +  template <typename Key, typename Value>
   2.113 +  class DefaultWriter<std::multimap<Key, Value> > 
   2.114 +    : public IterableWriter<std::multimap<Key, Value> > {};
   2.115 +
   2.116 +  template <typename First, typename Second>
   2.117 +  class DefaultWriter<std::pair<First, Second> > 
   2.118 +    : public PairWriter<std::pair<First, Second> > {};
   2.119 +
   2.120    /// \ingroup item_io
   2.121    /// \brief Standard WriterTraits for the section writers.
   2.122    ///
     3.1 --- a/lemon/lemon_writer.h	Tue Dec 06 18:44:26 2005 +0000
     3.2 +++ b/lemon/lemon_writer.h	Wed Dec 07 11:57:30 2005 +0000
     3.3 @@ -172,7 +172,7 @@
     3.4  
     3.5        virtual ~MapWriterBase() {}
     3.6  
     3.7 -      virtual void write(std::ostream& os, const Item& item) = 0;
     3.8 +      virtual void write(std::ostream& os, const Item& item) const = 0;
     3.9      };
    3.10  
    3.11  
    3.12 @@ -192,7 +192,7 @@
    3.13  
    3.14        virtual ~MapWriter() {}
    3.15  
    3.16 -      virtual void write(std::ostream& os, const Item& item) {
    3.17 +      virtual void write(std::ostream& os, const Item& item) const {
    3.18  	Value value = map[item];
    3.19  	writer.write(os, value);
    3.20        }