[Lemon-commits] [lemon_svn] deba: r2420 - in hugo/trunk/lemon: . bits

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:52:30 CET 2006


Author: deba
Date: Wed Dec  7 12:57:30 2005
New Revision: 2420

Modified:
   hugo/trunk/lemon/bits/item_reader.h
   hugo/trunk/lemon/bits/item_writer.h
   hugo/trunk/lemon/lemon_writer.h

Log:
Some bug fixes and improvments in the io classes



Modified: hugo/trunk/lemon/bits/item_reader.h
==============================================================================
--- hugo/trunk/lemon/bits/item_reader.h	(original)
+++ hugo/trunk/lemon/bits/item_reader.h	Wed Dec  7 12:57:30 2005
@@ -180,6 +180,12 @@
 
   public:
 
+    /// \brief Constructor for InsertReader
+    ///
+    /// Constructor for InsertReader
+    PushBackReader(const ItemReader& _item_reader = ItemReader())
+      : item_reader(_item_reader) {}
+
     /// \brief Reads the values into the container from the given stream.
     ///
     /// Reads the values into the container from the given stream.
@@ -194,7 +200,6 @@
 	value.push_back(item);
       }
       if (!is) throw DataFormatError("PushBackReader format error");
-      is.putback(c);
     }
 
   };
@@ -223,6 +228,12 @@
 
   public:
 
+    /// \brief Constructor for InsertReader
+    ///
+    /// Constructor for InsertReader
+    InsertReader(const ItemReader& _item_reader = ItemReader())
+      : item_reader(_item_reader) {}
+
     /// \brief Reads the values into the container from the given stream.
     ///
     /// Reads the values into the container from the given stream.
@@ -237,7 +248,6 @@
 	value.insert(item);
       }
       if (!is) throw DataFormatError("PushBackReader format error");
-      is.putback(c);
     }
 
   };
@@ -310,10 +320,10 @@
     /// \brief Reads the line from the given stream.
     ///
     /// Reads the line from the given stream.
-    void read(std::istream& is, Value& value) {
+    void read(std::istream& is, Value& value) const {
       if (skipSpaces) is >> std::ws;
       if (!getline(is, value)) {
-	throw DataFormatError("LineReader forma error");
+	throw DataFormatError("LineReader format error");
       }
     }
   private:
@@ -321,6 +331,60 @@
   };
 
   /// \ingroup item_io
+  /// \brief Reader for std::pair.
+  ///
+  /// Reader for std::pair.
+  ///
+  /// \author Balazs Dezso
+  template <typename _Pair, 
+	    typename _FirstReader = 
+	    DefaultReader<typename _Pair::first_type>,
+	    typename _SecondReader = 
+	    DefaultReader<typename _Pair::second_type> >
+  class PairReader {
+  public:
+    typedef _Pair Value;
+
+    typedef _FirstReader FirstReader;
+    typedef _SecondReader SecondReader;
+
+  private:
+
+    FirstReader first_reader;
+    SecondReader second_reader;
+
+  public:
+    
+    /// \brief Constructor.
+    ///
+    /// Constructor for the PairReader.
+    PairReader(const FirstReader& _first_reader = FirstReader(), 
+	       const SecondReader& _second_reader = SecondReader()) 
+      : first_reader(_first_reader), second_reader(_second_reader) {}
+    
+    /// \brief Reads the pair from the given stream.
+    ///
+    /// Reads the pair from the given stream.
+    void read(std::istream& is, Value& value) const {
+      char c;
+      if (!(is >> c) || c != '(') {
+	throw DataFormatError("PairReader format error");
+      }
+      first_reader.read(is, value.first);
+      if (!(is >> c) || c != '=') {
+	throw DataFormatError("PairReader format error");
+      }
+      if (!(is >> c) || c != '>') {
+	throw DataFormatError("PairReader format error");
+      }
+      second_reader.read(is, value.second);
+      if (!(is >> c) || c != ')') {
+	throw DataFormatError("PairReader format error");
+      }
+    }
+  };
+
+  /// \ingroup item_io
   /// 
   /// \brief The default item reader template class.
   ///
@@ -389,10 +453,24 @@
   class DefaultReader<std::set<Item> > 
     : public InsertReader<std::set<Item> > {};
 
+  template <typename Key, typename Value>
+  class DefaultReader<std::map<Key, Value> > 
+    : public InsertReader<std::map<Key, Value>,
+			  DefaultReader<std::pair<Key, Value> > > {};
+
   template <typename Item>
   class DefaultReader<std::multiset<Item> > 
     : public InsertReader<std::multiset<Item> > {};
 
+  template <typename Key, typename Value>
+  class DefaultReader<std::multimap<Key, Value> > 
+    : public InsertReader<std::multimap<Key, Value>,
+			  DefaultReader<std::pair<Key, Value> > > {};
+
+  template <typename First, typename Second>
+  class DefaultReader<std::pair<First, Second> > 
+    : public PairReader<std::pair<First, Second> > {};
+
   /// \ingroup item_io
   /// 
   /// \brief The default item reader for skipping a value in the stream.

Modified: hugo/trunk/lemon/bits/item_writer.h
==============================================================================
--- hugo/trunk/lemon/bits/item_writer.h	(original)
+++ hugo/trunk/lemon/bits/item_writer.h	Wed Dec  7 12:57:30 2005
@@ -53,7 +53,7 @@
     /// \brief Writes a quoted string to the given stream.
     ///
     /// Writes a quoted string to the given stream.
-    void write(std::ostream& os, const std::string& value) {
+    void write(std::ostream& os, const std::string& value) const {
       os << "\"";
       if (escaped) {
 	std::ostringstream ls;
@@ -136,7 +136,7 @@
     /// \brief Writes a quoted char array to the given stream.
     ///
     /// Writes a quoted char array to the given stream.
-    void write(std::ostream& os, const char* value) {
+    void write(std::ostream& os, const char* value) const {
       QuotedStringWriter(escaped).write(os, std::string(value));
     }
 
@@ -169,6 +169,9 @@
 
   public:
 
+    IterableWriter(const ItemWriter& _item_writer = ItemWriter())
+      : item_writer(_item_writer) {}
+
     /// \brief Writes the values of the container to the given stream.
     ///
     /// Writes the values of the container to the given stream.
@@ -185,6 +188,53 @@
   };
 
   /// \ingroup item_io
+  ///
+  /// \brief Writer for standard pairs.
+  ///
+  /// Writer for standard pairs. The representation of a pair is
+  /// \code ( first_value => second_value ) \endcode.
+  /// \author Balazs Dezso
+  template <typename _Pair, 
+	    typename _FirstWriter = 
+	    DefaultWriter<typename _Pair::first_type>,
+	    typename _SecondWriter = 
+	    DefaultWriter<typename _Pair::second_type> >
+  class PairWriter {
+  public:
+
+    typedef _Pair Value;
+
+    typedef _FirstWriter FirstWriter;
+    typedef _SecondWriter SecondWriter;
+
+  private:
+
+    FirstWriter first_writer;
+    SecondWriter second_writer;
+
+  public:
+    
+    /// \brief Constructor.
+    ///
+    /// Constructor for the PairWriter.
+    PairWriter(const FirstWriter& _first_writer = FirstWriter(), 
+	       const SecondWriter& _second_writer = SecondWriter()) 
+      : first_writer(_first_writer), second_writer(_second_writer) {}
+    
+    /// \brief Writes the pair from the given stream.
+    ///
+    /// Writes the pair from the given stream.
+    void write(std::ostream& os, const Value& value) const {
+      os << "( ";
+      first_writer.write(os, value.first);
+      os << " => ";
+      second_writer.write(os, value.second);
+      os << " )";
+    }
+
+  };
+
+  /// \ingroup item_io
   /// 
   /// \brief The default item writer template class.
   ///
@@ -217,6 +267,14 @@
   class DefaultWriter<const char[length]> 
     : public QuotedCharArrayWriter {};
 
+  template <>
+  class DefaultWriter<char*> 
+    : public QuotedCharArrayWriter {};
+
+  template <>
+  class DefaultWriter<const char*> 
+    : public QuotedCharArrayWriter {};
+
   template <typename Item>
   class DefaultWriter<std::vector<Item> > 
     : public IterableWriter<std::vector<Item> > {};
@@ -233,10 +291,22 @@
   class DefaultWriter<std::set<Item> > 
     : public IterableWriter<std::set<Item> > {};
 
+  template <typename Key, typename Value>
+  class DefaultWriter<std::map<Key, Value> > 
+    : public IterableWriter<std::map<Key, Value> > {};
+
   template <typename Item>
   class DefaultWriter<std::multiset<Item> > 
     : public IterableWriter<std::multiset<Item> > {};
 
+  template <typename Key, typename Value>
+  class DefaultWriter<std::multimap<Key, Value> > 
+    : public IterableWriter<std::multimap<Key, Value> > {};
+
+  template <typename First, typename Second>
+  class DefaultWriter<std::pair<First, Second> > 
+    : public PairWriter<std::pair<First, Second> > {};
+
   /// \ingroup item_io
   /// \brief Standard WriterTraits for the section writers.
   ///

Modified: hugo/trunk/lemon/lemon_writer.h
==============================================================================
--- hugo/trunk/lemon/lemon_writer.h	(original)
+++ hugo/trunk/lemon/lemon_writer.h	Wed Dec  7 12:57:30 2005
@@ -172,7 +172,7 @@
 
       virtual ~MapWriterBase() {}
 
-      virtual void write(std::ostream& os, const Item& item) = 0;
+      virtual void write(std::ostream& os, const Item& item) const = 0;
     };
 
 
@@ -192,7 +192,7 @@
 
       virtual ~MapWriter() {}
 
-      virtual void write(std::ostream& os, const Item& item) {
+      virtual void write(std::ostream& os, const Item& item) const {
 	Value value = map[item];
 	writer.write(os, value);
       }



More information about the Lemon-commits mailing list