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;
121 /// \brief Writer class for quoted char array.
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 {
128 typedef const char* Value;
130 /// \brief Constructor for the writer.
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) {}
136 /// \brief 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) {
140 QuotedStringWriter(escaped).write(os, std::string(value));
150 /// \brief Writer for standard containers.
152 /// Writer for each iterable standard containers. The representation
153 /// of the container is the values enumerated between an open and a
156 /// \author Balazs Dezso
159 typename _ItemWriter = DefaultWriter<typename _Container::value_type>
161 class IterableWriter {
163 typedef _Container Value;
164 typedef _ItemWriter ItemWriter;
168 ItemWriter item_writer;
172 /// \brief Writes the values of the container to the given stream.
174 /// Writes the values of the container to the given stream.
175 void write(std::ostream& os, const Value& value) const {
176 typename Value::const_iterator it;
178 for (it = value.begin(); it != value.end(); ++it) {
179 item_writer.write(os, *it);
189 /// \brief The default item writer template class.
191 /// 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.
194 /// \author Balazs Dezso
195 template <typename _Value>
196 class DefaultWriter {
199 typedef _Value Value;
200 /// \brief Writes the value to the given stream.
202 /// Writes the value to the given stream.
203 void write(std::ostream& os, const Value& value) const {
209 class DefaultWriter<std::string>
210 : public QuotedStringWriter {};
212 template <int length>
213 class DefaultWriter<char[length]>
214 : public QuotedCharArrayWriter {};
216 template <int length>
217 class DefaultWriter<const char[length]>
218 : public QuotedCharArrayWriter {};
220 template <typename Item>
221 class DefaultWriter<std::vector<Item> >
222 : public IterableWriter<std::vector<Item> > {};
224 template <typename Item>
225 class DefaultWriter<std::deque<Item> >
226 : public IterableWriter<std::deque<Item> > {};
228 template <typename Item>
229 class DefaultWriter<std::list<Item> >
230 : public IterableWriter<std::list<Item> > {};
232 template <typename Item>
233 class DefaultWriter<std::set<Item> >
234 : public IterableWriter<std::set<Item> > {};
236 template <typename Item>
237 class DefaultWriter<std::multiset<Item> >
238 : public IterableWriter<std::multiset<Item> > {};
241 /// \brief Standard WriterTraits for the section writers.
243 /// Standard WriterTraits for the section writers.
244 /// It defines standard writing method for all type of value.
245 /// \author Balazs Dezso
246 struct DefaultWriterTraits {
248 template <typename _Value>
249 struct Writer : DefaultWriter<_Value> {
250 typedef DefaultWriter<_Value> Parent;