lemon/bits/item_writer.h
changeset 1868 24bf4b8299e7
parent 1535 e667cd5c0886
child 1875 98698b69a902
equal deleted inserted replaced
2:19a09cf84220 3:1f53c920d8a0
    51     QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
    51     QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
    52 
    52 
    53     /// \brief Writes a quoted string to the given stream.
    53     /// \brief Writes a quoted string to the given stream.
    54     ///
    54     ///
    55     /// Writes a quoted string to the given stream.
    55     /// Writes a quoted string to the given stream.
    56     void write(std::ostream& os, const std::string& value) {
    56     void write(std::ostream& os, const std::string& value) const {
    57       os << "\"";
    57       os << "\"";
    58       if (escaped) {
    58       if (escaped) {
    59 	std::ostringstream ls;
    59 	std::ostringstream ls;
    60 	for (int i = 0; i < (int)value.size(); ++i) {
    60 	for (int i = 0; i < (int)value.size(); ++i) {
    61 	  writeEscape(ls, value[i]);
    61 	  writeEscape(ls, value[i]);
   134     QuotedCharArrayWriter(bool _escaped = true) : escaped(_escaped) {}
   134     QuotedCharArrayWriter(bool _escaped = true) : escaped(_escaped) {}
   135 
   135 
   136     /// \brief Writes a quoted char array to the given stream.
   136     /// \brief Writes a quoted char array to the given stream.
   137     ///
   137     ///
   138     /// Writes a quoted char array to the given stream.
   138     /// Writes a quoted char array to the given stream.
   139     void write(std::ostream& os, const char* value) {
   139     void write(std::ostream& os, const char* value) const {
   140       QuotedStringWriter(escaped).write(os, std::string(value));
   140       QuotedStringWriter(escaped).write(os, std::string(value));
   141     }
   141     }
   142 
   142 
   143   private:    
   143   private:    
   144     bool escaped;
   144     bool escaped;
   166   private:
   166   private:
   167 
   167 
   168     ItemWriter item_writer;
   168     ItemWriter item_writer;
   169 
   169 
   170   public:
   170   public:
       
   171 
       
   172     IterableWriter(const ItemWriter& _item_writer = ItemWriter())
       
   173       : item_writer(_item_writer) {}
   171 
   174 
   172     /// \brief Writes the values of the container to the given stream.
   175     /// \brief Writes the values of the container to the given stream.
   173     ///
   176     ///
   174     /// Writes the values of the container to the given stream.
   177     /// Writes the values of the container to the given stream.
   175     void write(std::ostream& os, const Value& value) const {
   178     void write(std::ostream& os, const Value& value) const {
   183     }
   186     }
   184 
   187 
   185   };
   188   };
   186 
   189 
   187   /// \ingroup item_io
   190   /// \ingroup item_io
       
   191   ///
       
   192   /// \brief Writer for standard pairs.
       
   193   ///
       
   194   /// Writer for standard pairs. The representation of a pair is
       
   195   /// \code ( first_value => second_value ) \endcode.
       
   196   /// \author Balazs Dezso
       
   197   template <typename _Pair, 
       
   198 	    typename _FirstWriter = 
       
   199 	    DefaultWriter<typename _Pair::first_type>,
       
   200 	    typename _SecondWriter = 
       
   201 	    DefaultWriter<typename _Pair::second_type> >
       
   202   class PairWriter {
       
   203   public:
       
   204 
       
   205     typedef _Pair Value;
       
   206 
       
   207     typedef _FirstWriter FirstWriter;
       
   208     typedef _SecondWriter SecondWriter;
       
   209 
       
   210   private:
       
   211 
       
   212     FirstWriter first_writer;
       
   213     SecondWriter second_writer;
       
   214 
       
   215   public:
       
   216     
       
   217     /// \brief Constructor.
       
   218     ///
       
   219     /// Constructor for the PairWriter.
       
   220     PairWriter(const FirstWriter& _first_writer = FirstWriter(), 
       
   221 	       const SecondWriter& _second_writer = SecondWriter()) 
       
   222       : first_writer(_first_writer), second_writer(_second_writer) {}
       
   223     
       
   224     /// \brief Writes the pair from the given stream.
       
   225     ///
       
   226     /// Writes the pair from the given stream.
       
   227     void write(std::ostream& os, const Value& value) const {
       
   228       os << "( ";
       
   229       first_writer.write(os, value.first);
       
   230       os << " => ";
       
   231       second_writer.write(os, value.second);
       
   232       os << " )";
       
   233     }
       
   234 
       
   235   };
       
   236 
       
   237   /// \ingroup item_io
   188   /// 
   238   /// 
   189   /// \brief The default item writer template class.
   239   /// \brief The default item writer template class.
   190   ///
   240   ///
   191   /// The default item writer template class. If some section writer
   241   /// The default item writer template class. If some section writer
   192   /// needs to write a value to the stream it will give the default way for it.
   242   /// needs to write a value to the stream it will give the default way for it.
   215 
   265 
   216   template <int length>
   266   template <int length>
   217   class DefaultWriter<const char[length]> 
   267   class DefaultWriter<const char[length]> 
   218     : public QuotedCharArrayWriter {};
   268     : public QuotedCharArrayWriter {};
   219 
   269 
       
   270   template <>
       
   271   class DefaultWriter<char*> 
       
   272     : public QuotedCharArrayWriter {};
       
   273 
       
   274   template <>
       
   275   class DefaultWriter<const char*> 
       
   276     : public QuotedCharArrayWriter {};
       
   277 
   220   template <typename Item>
   278   template <typename Item>
   221   class DefaultWriter<std::vector<Item> > 
   279   class DefaultWriter<std::vector<Item> > 
   222     : public IterableWriter<std::vector<Item> > {};
   280     : public IterableWriter<std::vector<Item> > {};
   223 
   281 
   224   template <typename Item>
   282   template <typename Item>
   231   
   289   
   232   template <typename Item>
   290   template <typename Item>
   233   class DefaultWriter<std::set<Item> > 
   291   class DefaultWriter<std::set<Item> > 
   234     : public IterableWriter<std::set<Item> > {};
   292     : public IterableWriter<std::set<Item> > {};
   235 
   293 
       
   294   template <typename Key, typename Value>
       
   295   class DefaultWriter<std::map<Key, Value> > 
       
   296     : public IterableWriter<std::map<Key, Value> > {};
       
   297 
   236   template <typename Item>
   298   template <typename Item>
   237   class DefaultWriter<std::multiset<Item> > 
   299   class DefaultWriter<std::multiset<Item> > 
   238     : public IterableWriter<std::multiset<Item> > {};
   300     : public IterableWriter<std::multiset<Item> > {};
       
   301 
       
   302   template <typename Key, typename Value>
       
   303   class DefaultWriter<std::multimap<Key, Value> > 
       
   304     : public IterableWriter<std::multimap<Key, Value> > {};
       
   305 
       
   306   template <typename First, typename Second>
       
   307   class DefaultWriter<std::pair<First, Second> > 
       
   308     : public PairWriter<std::pair<First, Second> > {};
   239 
   309 
   240   /// \ingroup item_io
   310   /// \ingroup item_io
   241   /// \brief Standard WriterTraits for the section writers.
   311   /// \brief Standard WriterTraits for the section writers.
   242   ///
   312   ///
   243   /// Standard WriterTraits for the section writers.
   313   /// Standard WriterTraits for the section writers.