3  * This file is a part of LEMON, a generic C++ optimization library
 
     5  * Copyright (C) 2003-2006
 
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
 
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
 
     9  * Permission to use, modify and distribute this software is granted
 
    10  * provided that this copyright notice appears in all copies. For
 
    11  * precise terms see the accompanying LICENSE file.
 
    13  * This software is provided "AS IS" with no warranty of any kind,
 
    14  * express or implied, and with no claim as to its suitability for any
 
    21 /// \brief Item writer bits for lemon output.
 
    23 #ifndef LEMON_BITS_ITEM_WRITER_H
 
    24 #define LEMON_BITS_ITEM_WRITER_H
 
    36   template <typename Value>
 
    40   /// \brief Writer class for quoted strings.
 
    42   /// Writer class for quoted strings. It can process the escape
 
    43   /// sequences in the string.
 
    44   /// \author Balazs Dezso
 
    45   class QuotedStringWriter {
 
    47     typedef std::string Value;
 
    49     /// \brief Constructor for the writer.
 
    51     /// Constructor for the writer. If the given parameter is true
 
    52     /// the writer creates escape sequences from special characters.
 
    53     QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {}
 
    55     /// \brief Writes a quoted string to the given stream.
 
    57     /// Writes a quoted string to the given stream.
 
    58     void write(std::ostream& os, const std::string& value) const {
 
    61 	std::ostringstream ls;
 
    62 	for (int i = 0; i < (int)value.size(); ++i) {
 
    63 	  writeEscape(ls, value[i]);
 
    74     static void writeEscape(std::ostream& os, char c) {
 
   111 	  os << '\\' << std::oct << (int)c;
 
   123   /// \brief Writer class for quoted char array.
 
   125   /// Writer class for quoted char array. It can process the escape
 
   126   /// sequences in the char array.
 
   127   /// \author Balazs Dezso
 
   128   class QuotedCharArrayWriter {
 
   130     typedef const char* Value;
 
   132     /// \brief Constructor for the writer.
 
   134     /// Constructor for the writer. If the given parameter is true
 
   135     /// the writer creates escape sequences from special characters.
 
   136     QuotedCharArrayWriter(bool _escaped = true) : escaped(_escaped) {}
 
   138     /// \brief Writes a quoted char array to the given stream.
 
   140     /// Writes a quoted char array to the given stream.
 
   141     void write(std::ostream& os, const char* value) const {
 
   142       QuotedStringWriter(escaped).write(os, std::string(value));
 
   152   /// \brief Writer for standard containers.
 
   154   /// Writer for each iterable standard containers. The representation
 
   155   /// of the container is the values enumerated between an open and a
 
   158   /// \author Balazs Dezso
 
   161     typename _ItemWriter = DefaultWriter<typename _Container::value_type> 
 
   163   class IterableWriter {
 
   165     typedef _Container Value;
 
   166     typedef _ItemWriter ItemWriter;
 
   170     ItemWriter item_writer;
 
   174     IterableWriter(const ItemWriter& _item_writer = ItemWriter())
 
   175       : item_writer(_item_writer) {}
 
   177     /// \brief Writes the values of the container to the given stream.
 
   179     /// Writes the values of the container to the given stream.
 
   180     void write(std::ostream& os, const Value& value) const {
 
   181       typename Value::const_iterator it;
 
   183       for (it = value.begin(); it != value.end(); ++it) {
 
   184 	item_writer.write(os, *it);
 
   194   /// \brief Writer for standard pairs.
 
   196   /// Writer for standard pairs. The representation of a pair is
 
   197   ///\code ( first_value => second_value ) \endcode.
 
   198   /// \author Balazs Dezso
 
   199   template <typename _Pair, 
 
   200 	    typename _FirstWriter = 
 
   201 	    DefaultWriter<typename _Pair::first_type>,
 
   202 	    typename _SecondWriter = 
 
   203 	    DefaultWriter<typename _Pair::second_type> >
 
   209     typedef _FirstWriter FirstWriter;
 
   210     typedef _SecondWriter SecondWriter;
 
   214     FirstWriter first_writer;
 
   215     SecondWriter second_writer;
 
   219     /// \brief Constructor.
 
   221     /// Constructor for the PairWriter.
 
   222     PairWriter(const FirstWriter& _first_writer = FirstWriter(), 
 
   223 	       const SecondWriter& _second_writer = SecondWriter()) 
 
   224       : first_writer(_first_writer), second_writer(_second_writer) {}
 
   226     /// \brief Writes the pair from the given stream.
 
   228     /// Writes the pair from the given stream.
 
   229     void write(std::ostream& os, const Value& value) const {
 
   231       first_writer.write(os, value.first);
 
   233       second_writer.write(os, value.second);
 
   241   /// \brief The default item writer template class.
 
   243   /// The default item writer template class. If some section writer
 
   244   /// needs to write a value to the stream it will give the default way for it.
 
   246   /// \author Balazs Dezso
 
   247   template <typename _Value>
 
   248   class DefaultWriter {
 
   251     typedef _Value Value;
 
   252     /// \brief Writes the value to the given stream.
 
   254     /// Writes the value to the given stream.
 
   255     void write(std::ostream& os, const Value& value) const {
 
   261   class DefaultWriter<std::string> 
 
   262     : public QuotedStringWriter {};
 
   264   template <int length>
 
   265   class DefaultWriter<char[length]> 
 
   266     : public QuotedCharArrayWriter {};
 
   268   template <int length>
 
   269   class DefaultWriter<const char[length]> 
 
   270     : public QuotedCharArrayWriter {};
 
   273   class DefaultWriter<char*> 
 
   274     : public QuotedCharArrayWriter {};
 
   277   class DefaultWriter<const char*> 
 
   278     : public QuotedCharArrayWriter {};
 
   280   template <typename Item>
 
   281   class DefaultWriter<std::vector<Item> > 
 
   282     : public IterableWriter<std::vector<Item> > {};
 
   284   template <typename Item>
 
   285   class DefaultWriter<std::deque<Item> > 
 
   286     : public IterableWriter<std::deque<Item> > {};
 
   288   template <typename Item>
 
   289   class DefaultWriter<std::list<Item> > 
 
   290     : public IterableWriter<std::list<Item> > {};
 
   292   template <typename Item>
 
   293   class DefaultWriter<std::set<Item> > 
 
   294     : public IterableWriter<std::set<Item> > {};
 
   296   template <typename Key, typename Value>
 
   297   class DefaultWriter<std::map<Key, Value> > 
 
   298     : public IterableWriter<std::map<Key, Value> > {};
 
   300   template <typename Item>
 
   301   class DefaultWriter<std::multiset<Item> > 
 
   302     : public IterableWriter<std::multiset<Item> > {};
 
   304   template <typename Key, typename Value>
 
   305   class DefaultWriter<std::multimap<Key, Value> > 
 
   306     : public IterableWriter<std::multimap<Key, Value> > {};
 
   308   template <typename First, typename Second>
 
   309   class DefaultWriter<std::pair<First, Second> > 
 
   310     : public PairWriter<std::pair<First, Second> > {};
 
   313   /// \brief Standard WriterTraits for the section writers.
 
   315   /// Standard WriterTraits for the section writers.
 
   316   /// It defines standard writing method for all type of value. 
 
   317   /// \author Balazs Dezso
 
   318   struct DefaultWriterTraits {
 
   320     template <typename _Value>
 
   321     struct Writer : DefaultWriter<_Value> {
 
   322       typedef DefaultWriter<_Value> Parent;