lemon/bits/item_writer.h
author deba
Tue, 31 Jan 2006 20:04:36 +0000
changeset 1933 a876a3d6a4c7
parent 1875 98698b69a902
child 1946 17eb3eaad9f8
permissions -rw-r--r--
Revising the bpugraph concept

We need a public but very limited ANode and BNode class
It can be used with ItemSetTraits and with some special maps

By example:
DescriptorMap<Graph, ANode>
InvertableMap<Graph, ANode, string>
IterableBoolMap<Graph, ANode>
IterableIntMap<Graph, ANode>
IterableValueMap<Graph, ANode, string>
     1 /* -*- C++ -*-
     2  * lemon/bits/item_reader.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Groin on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 /// \ingroin item_io
    18 /// \file
    19 /// \brief Item writer bits for lemon output.
    20 
    21 #ifndef LEMON_BITS_ITEM_WRITER_H
    22 #define LEMON_BITS_ITEM_WRITER_H
    23 
    24 #include <iostream>
    25 #include <string>
    26 
    27 #include <vector>
    28 #include <deque>
    29 #include <list>
    30 #include <set>
    31 
    32 namespace lemon {
    33   
    34   template <typename Value>
    35   class DefaultWriter;
    36 
    37   /// \ingroin item_io
    38   /// \brief Writer class for quoted strings.
    39   ///
    40   /// Writer class for quoted strings. It can process the escape
    41   /// sequences in the string.
    42   /// \author Balazs Dezso
    43   class QuotedStringWriter {
    44   public:
    45     typedef std::string Value;
    46 
    47     /// \brief Constructor for the writer.
    48     ///
    49     /// Constructor for the writer. If the given parameter is true
    50     /// the writer creates escape sequences from special characters.
    51     QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
    52 
    53     /// \brief Writes a quoted string to the given stream.
    54     ///
    55     /// Writes a quoted string to the given stream.
    56     void write(std::ostream& os, const std::string& value) const {
    57       os << "\"";
    58       if (escaped) {
    59 	std::ostringstream ls;
    60 	for (int i = 0; i < (int)value.size(); ++i) {
    61 	  writeEscape(ls, value[i]);
    62 	}
    63 	os << ls.str();
    64       } else {
    65 	os << value;
    66       }
    67       os << "\"";
    68     }
    69 
    70   private:
    71     
    72     static void writeEscape(std::ostream& os, char c) {
    73       switch (c) {
    74       case '\\':
    75 	os << "\\\\";
    76 	return;
    77       case '\"':
    78 	os << "\\\"";
    79 	return;
    80       case '\'':
    81 	os << "\\\'";
    82 	return;
    83       case '\?':
    84 	os << "\\\?";
    85 	return;
    86       case '\a':
    87 	os << "\\a";
    88 	return;
    89       case '\b':
    90 	os << "\\b";
    91 	return;
    92       case '\f':
    93 	os << "\\f";
    94 	return;
    95       case '\r':
    96 	os << "\\r";
    97 	return;
    98       case '\n':
    99 	os << "\\n";
   100 	return;
   101       case '\t':
   102 	os << "\\t";
   103 	return;
   104       case '\v':
   105 	os << "\\v";
   106 	return;
   107       default:
   108 	if (c < 0x20) {
   109 	  os << '\\' << std::oct << (int)c;
   110 	} else {
   111 	  os << c;
   112 	}
   113 	return;
   114       }     
   115     }
   116   private:
   117     bool escaped;
   118   };
   119 
   120   /// \ingroin item_io
   121   /// \brief Writer class for quoted char array.
   122   ///
   123   /// Writer class for quoted char array. It can process the escape
   124   /// sequences in the char array.
   125   /// \author Balazs Dezso
   126   class QuotedCharArrayWriter {
   127   public:
   128     typedef const char* Value;
   129 
   130     /// \brief Constructor for the writer.
   131     ///
   132     /// Constructor for the writer. If the given parameter is true
   133     /// the writer creates escape sequences from special characters.
   134     QuotedCharArrayWriter(bool _escaped = true) : escaped(_escaped) {}
   135 
   136     /// \brief Writes a quoted char array to the given stream.
   137     ///
   138     /// Writes a quoted char array to the given stream.
   139     void write(std::ostream& os, const char* value) const {
   140       QuotedStringWriter(escaped).write(os, std::string(value));
   141     }
   142 
   143   private:    
   144     bool escaped;
   145   };
   146 
   147 
   148   /// \ingroin item_io
   149   ///
   150   /// \brief Writer for standard containers.
   151   ///
   152   /// Writer for each iterable standard containers. The representation
   153   /// of the container is the values enumerated between an open and a
   154   /// close parse. 
   155   ///
   156   /// \author Balazs Dezso
   157   template <
   158     typename _Container, 
   159     typename _ItemWriter = DefaultWriter<typename _Container::value_type> 
   160   >
   161   class IterableWriter {
   162   public:
   163     typedef _Container Value;
   164     typedef _ItemWriter ItemWriter;
   165 
   166   private:
   167 
   168     ItemWriter item_writer;
   169 
   170   public:
   171 
   172     IterableWriter(const ItemWriter& _item_writer = ItemWriter())
   173       : item_writer(_item_writer) {}
   174 
   175     /// \brief Writes the values of the container to the given stream.
   176     ///
   177     /// Writes the values of the container to the given stream.
   178     void write(std::ostream& os, const Value& value) const {
   179       typename Value::const_iterator it;
   180       os << '(';
   181       for (it = value.begin(); it != value.end(); ++it) {
   182 	item_writer.write(os, *it);
   183 	os << ' ';
   184       }
   185       os << ')';
   186     }
   187 
   188   };
   189 
   190   /// \ingroin 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   /// \ingroin item_io
   238   /// 
   239   /// \brief The default item writer template class.
   240   ///
   241   /// The default item writer template class. If some section writer
   242   /// needs to write a value to the stream it will give the default way for it.
   243   ///
   244   /// \author Balazs Dezso
   245   template <typename _Value>
   246   class DefaultWriter {
   247   public:
   248     /// The value type.
   249     typedef _Value Value;
   250     /// \brief Writes the value to the given stream.
   251     ///
   252     /// Writes the value to the given stream.
   253     void write(std::ostream& os, const Value& value) const {
   254       os << value;
   255     }
   256   };
   257 
   258   template <>
   259   class DefaultWriter<std::string> 
   260     : public QuotedStringWriter {};
   261 
   262   template <int length>
   263   class DefaultWriter<char[length]> 
   264     : public QuotedCharArrayWriter {};
   265 
   266   template <int length>
   267   class DefaultWriter<const char[length]> 
   268     : public QuotedCharArrayWriter {};
   269 
   270   template <>
   271   class DefaultWriter<char*> 
   272     : public QuotedCharArrayWriter {};
   273 
   274   template <>
   275   class DefaultWriter<const char*> 
   276     : public QuotedCharArrayWriter {};
   277 
   278   template <typename Item>
   279   class DefaultWriter<std::vector<Item> > 
   280     : public IterableWriter<std::vector<Item> > {};
   281 
   282   template <typename Item>
   283   class DefaultWriter<std::deque<Item> > 
   284     : public IterableWriter<std::deque<Item> > {};
   285 
   286   template <typename Item>
   287   class DefaultWriter<std::list<Item> > 
   288     : public IterableWriter<std::list<Item> > {};
   289   
   290   template <typename Item>
   291   class DefaultWriter<std::set<Item> > 
   292     : public IterableWriter<std::set<Item> > {};
   293 
   294   template <typename Key, typename Value>
   295   class DefaultWriter<std::map<Key, Value> > 
   296     : public IterableWriter<std::map<Key, Value> > {};
   297 
   298   template <typename Item>
   299   class DefaultWriter<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> > {};
   309 
   310   /// \ingroin item_io
   311   /// \brief Standard WriterTraits for the section writers.
   312   ///
   313   /// Standard WriterTraits for the section writers.
   314   /// It defines standard writing method for all type of value. 
   315   /// \author Balazs Dezso
   316   struct DefaultWriterTraits {
   317 
   318     template <typename _Value>
   319     struct Writer : DefaultWriter<_Value> {
   320       typedef DefaultWriter<_Value> Parent;
   321     };
   322 
   323   };
   324 
   325 }
   326 
   327 #endif