Better doc.
2 * lemon/bits/item_reader.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2006 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) const {
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) const {
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 IterableWriter(const ItemWriter& _item_writer = ItemWriter())
173 : item_writer(_item_writer) {}
175 /// \brief Writes the values of the container to the given stream.
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;
181 for (it = value.begin(); it != value.end(); ++it) {
182 item_writer.write(os, *it);
192 /// \brief Writer for standard pairs.
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> >
207 typedef _FirstWriter FirstWriter;
208 typedef _SecondWriter SecondWriter;
212 FirstWriter first_writer;
213 SecondWriter second_writer;
217 /// \brief Constructor.
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) {}
224 /// \brief Writes the pair from the given stream.
226 /// Writes the pair from the given stream.
227 void write(std::ostream& os, const Value& value) const {
229 first_writer.write(os, value.first);
231 second_writer.write(os, value.second);
239 /// \brief The default item writer template class.
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.
244 /// \author Balazs Dezso
245 template <typename _Value>
246 class DefaultWriter {
249 typedef _Value Value;
250 /// \brief Writes the value to the given stream.
252 /// Writes the value to the given stream.
253 void write(std::ostream& os, const Value& value) const {
259 class DefaultWriter<std::string>
260 : public QuotedStringWriter {};
262 template <int length>
263 class DefaultWriter<char[length]>
264 : public QuotedCharArrayWriter {};
266 template <int length>
267 class DefaultWriter<const char[length]>
268 : public QuotedCharArrayWriter {};
271 class DefaultWriter<char*>
272 : public QuotedCharArrayWriter {};
275 class DefaultWriter<const char*>
276 : public QuotedCharArrayWriter {};
278 template <typename Item>
279 class DefaultWriter<std::vector<Item> >
280 : public IterableWriter<std::vector<Item> > {};
282 template <typename Item>
283 class DefaultWriter<std::deque<Item> >
284 : public IterableWriter<std::deque<Item> > {};
286 template <typename Item>
287 class DefaultWriter<std::list<Item> >
288 : public IterableWriter<std::list<Item> > {};
290 template <typename Item>
291 class DefaultWriter<std::set<Item> >
292 : public IterableWriter<std::set<Item> > {};
294 template <typename Key, typename Value>
295 class DefaultWriter<std::map<Key, Value> >
296 : public IterableWriter<std::map<Key, Value> > {};
298 template <typename Item>
299 class DefaultWriter<std::multiset<Item> >
300 : public IterableWriter<std::multiset<Item> > {};
302 template <typename Key, typename Value>
303 class DefaultWriter<std::multimap<Key, Value> >
304 : public IterableWriter<std::multimap<Key, Value> > {};
306 template <typename First, typename Second>
307 class DefaultWriter<std::pair<First, Second> >
308 : public PairWriter<std::pair<First, Second> > {};
311 /// \brief Standard WriterTraits for the section writers.
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 {
318 template <typename _Value>
319 struct Writer : DefaultWriter<_Value> {
320 typedef DefaultWriter<_Value> Parent;