Specialization for string literals.
authordeba
Mon, 04 Jul 2005 16:11:00 +0000
changeset 153343c7b3085212
parent 1532 aa7428d22aaf
child 1534 b86aad11f842
Specialization for string literals.
lemon/bits/item_writer.h
     1.1 --- a/lemon/bits/item_writer.h	Mon Jul 04 15:03:25 2005 +0000
     1.2 +++ b/lemon/bits/item_writer.h	Mon Jul 04 16:11:00 2005 +0000
     1.3 @@ -56,7 +56,7 @@
     1.4      void write(std::ostream& os, const std::string& value) {
     1.5        os << "\"";
     1.6        if (escaped) {
     1.7 -	std::ostringstream ls;
     1.8 +	std::ostringstream ls(value);
     1.9  	for (int i = 0; i < (int)value.size(); ++i) {
    1.10  	  writeEscape(ls, value[i]);
    1.11  	}
    1.12 @@ -118,6 +118,34 @@
    1.13    };
    1.14  
    1.15    /// \ingroup item_io
    1.16 +  /// \brief Writer class for quoted char array.
    1.17 +  ///
    1.18 +  /// Writer class for quoted char array. It can process the escape
    1.19 +  /// sequences in the char array.
    1.20 +  /// \author Balazs Dezso
    1.21 +  class QuotedCharArrayWriter {
    1.22 +  public:
    1.23 +    typedef const char* Value;
    1.24 +
    1.25 +    /// \brief Constructor for the writer.
    1.26 +    ///
    1.27 +    /// Constructor for the writer. If the given parameter is true
    1.28 +    /// the writer creates escape sequences from special characters.
    1.29 +    QuotedCharArrayWriter(bool _escaped = true) : escaped(_escaped) {}
    1.30 +
    1.31 +    /// \brief Writes a quoted char array to the given stream.
    1.32 +    ///
    1.33 +    /// Writes a quoted char array to the given stream.
    1.34 +    void write(std::ostream& os, const char* value) {
    1.35 +      QuotedStringWriter(escaped).write(os, std::string(value));
    1.36 +    }
    1.37 +
    1.38 +  private:    
    1.39 +    bool escaped;
    1.40 +  };
    1.41 +
    1.42 +
    1.43 +  /// \ingroup item_io
    1.44    ///
    1.45    /// \brief Writer for standard containers.
    1.46    ///
    1.47 @@ -181,6 +209,14 @@
    1.48    class DefaultWriter<std::string> 
    1.49      : public QuotedStringWriter {};
    1.50  
    1.51 +  template <int length>
    1.52 +  class DefaultWriter<char[length]> 
    1.53 +    : public QuotedCharArrayWriter {};
    1.54 +
    1.55 +  template <int length>
    1.56 +  class DefaultWriter<const char[length]> 
    1.57 +    : public QuotedCharArrayWriter {};
    1.58 +
    1.59    template <typename Item>
    1.60    class DefaultWriter<std::vector<Item> > 
    1.61      : public IterableWriter<std::vector<Item> > {};
    1.62 @@ -210,7 +246,9 @@
    1.63    struct DefaultWriterTraits {
    1.64  
    1.65      template <typename _Value>
    1.66 -    struct Writer : DefaultWriter<_Value> {};
    1.67 +    struct Writer : DefaultWriter<_Value> {
    1.68 +      typedef DefaultWriter<_Value> Parent;
    1.69 +    };
    1.70  
    1.71    };
    1.72