2 * lemon/bits/item_reader.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
19 /// \brief Item writer bits for lemon output.
21 #ifndef LEMON_BITS_ITEM_WRITER_H
22 #define LEMON_BITS_ITEM_WRITER_H
34 template <typename Value>
38 /// \brief Writer class for quoted strings.
40 /// Writer class for quoted strings. It can process the escape
41 /// sequences in the string.
42 /// \author Balazs Dezso
43 class QuotedStringWriter {
45 typedef std::string Value;
47 /// \brief Constructor for the writer.
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) {}
53 /// \brief 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) {
59 std::ostringstream ls;
60 for (int i = 0; i < (int)value.size(); ++i) {
61 writeEscape(ls, value[i]);
72 static void writeEscape(std::ostream& os, char c) {
109 os << '\\' << std::oct << (int)c;
122 /// \brief Writer for standard containers.
124 /// Writer for each iterable standard containers. The representation
125 /// of the container is the values enumerated between an open and a
128 /// \author Balazs Dezso
131 typename _ItemWriter = DefaultWriter<typename _Container::value_type>
133 class IterableWriter {
135 typedef _Container Value;
136 typedef _ItemWriter ItemWriter;
140 ItemWriter item_writer;
144 /// \brief Writes the values of the container to the given stream.
146 /// Writes the values of the container to the given stream.
147 void write(std::ostream& os, const Value& value) const {
148 typename Value::const_iterator it;
150 for (it = value.begin(); it != value.end(); ++it) {
151 item_writer.write(os, *it);
161 /// \brief The default item writer template class.
163 /// The default item writer template class. If some section writer
164 /// needs to write a value to the stream it will give the default way for it.
166 /// \author Balazs Dezso
167 template <typename _Value>
168 class DefaultWriter {
171 typedef _Value Value;
172 /// \brief Writes the value to the given stream.
174 /// Writes the value to the given stream.
175 void write(std::ostream& os, const Value& value) const {
181 class DefaultWriter<std::string>
182 : public QuotedStringWriter {};
184 template <typename Item>
185 class DefaultWriter<std::vector<Item> >
186 : public IterableWriter<std::vector<Item> > {};
188 template <typename Item>
189 class DefaultWriter<std::deque<Item> >
190 : public IterableWriter<std::deque<Item> > {};
192 template <typename Item>
193 class DefaultWriter<std::list<Item> >
194 : public IterableWriter<std::list<Item> > {};
196 template <typename Item>
197 class DefaultWriter<std::set<Item> >
198 : public IterableWriter<std::set<Item> > {};
200 template <typename Item>
201 class DefaultWriter<std::multiset<Item> >
202 : public IterableWriter<std::multiset<Item> > {};
205 /// \brief Standard WriterTraits for the section writers.
207 /// Standard WriterTraits for the section writers.
208 /// It defines standard writing method for all type of value.
209 /// \author Balazs Dezso
210 struct DefaultWriterTraits {
212 template <typename _Value>
213 struct Writer : DefaultWriter<_Value> {};