Changeset 1852:ffa7c6e96330 in lemon-0.x
- Timestamp:
- 12/07/05 12:57:30 (19 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2420
- Location:
- lemon
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/bits/item_reader.h
r1744 r1852 181 181 public: 182 182 183 /// \brief Constructor for InsertReader 184 /// 185 /// Constructor for InsertReader 186 PushBackReader(const ItemReader& _item_reader = ItemReader()) 187 : item_reader(_item_reader) {} 188 183 189 /// \brief Reads the values into the container from the given stream. 184 190 /// … … 195 201 } 196 202 if (!is) throw DataFormatError("PushBackReader format error"); 197 is.putback(c);198 203 } 199 204 … … 223 228 224 229 public: 230 231 /// \brief Constructor for InsertReader 232 /// 233 /// Constructor for InsertReader 234 InsertReader(const ItemReader& _item_reader = ItemReader()) 235 : item_reader(_item_reader) {} 225 236 226 237 /// \brief Reads the values into the container from the given stream. … … 238 249 } 239 250 if (!is) throw DataFormatError("PushBackReader format error"); 240 is.putback(c);241 251 } 242 252 … … 311 321 /// 312 322 /// Reads the line from the given stream. 313 void read(std::istream& is, Value& value) {323 void read(std::istream& is, Value& value) const { 314 324 if (skipSpaces) is >> std::ws; 315 325 if (!getline(is, value)) { 316 throw DataFormatError("LineReader forma error");326 throw DataFormatError("LineReader format error"); 317 327 } 318 328 } 319 329 private: 320 330 bool skipSpaces; 331 }; 332 333 /// \ingroup item_io 334 /// \brief Reader for std::pair. 335 /// 336 /// Reader for std::pair. 337 /// 338 /// \author Balazs Dezso 339 template <typename _Pair, 340 typename _FirstReader = 341 DefaultReader<typename _Pair::first_type>, 342 typename _SecondReader = 343 DefaultReader<typename _Pair::second_type> > 344 class PairReader { 345 public: 346 typedef _Pair Value; 347 348 typedef _FirstReader FirstReader; 349 typedef _SecondReader SecondReader; 350 351 private: 352 353 FirstReader first_reader; 354 SecondReader second_reader; 355 356 public: 357 358 /// \brief Constructor. 359 /// 360 /// Constructor for the PairReader. 361 PairReader(const FirstReader& _first_reader = FirstReader(), 362 const SecondReader& _second_reader = SecondReader()) 363 : first_reader(_first_reader), second_reader(_second_reader) {} 364 365 /// \brief Reads the pair from the given stream. 366 /// 367 /// Reads the pair from the given stream. 368 void read(std::istream& is, Value& value) const { 369 char c; 370 if (!(is >> c) || c != '(') { 371 throw DataFormatError("PairReader format error"); 372 } 373 first_reader.read(is, value.first); 374 if (!(is >> c) || c != '=') { 375 throw DataFormatError("PairReader format error"); 376 } 377 if (!(is >> c) || c != '>') { 378 throw DataFormatError("PairReader format error"); 379 } 380 second_reader.read(is, value.second); 381 if (!(is >> c) || c != ')') { 382 throw DataFormatError("PairReader format error"); 383 } 384 } 321 385 }; 322 386 … … 390 454 : public InsertReader<std::set<Item> > {}; 391 455 456 template <typename Key, typename Value> 457 class DefaultReader<std::map<Key, Value> > 458 : public InsertReader<std::map<Key, Value>, 459 DefaultReader<std::pair<Key, Value> > > {}; 460 392 461 template <typename Item> 393 462 class DefaultReader<std::multiset<Item> > 394 463 : public InsertReader<std::multiset<Item> > {}; 395 464 465 template <typename Key, typename Value> 466 class DefaultReader<std::multimap<Key, Value> > 467 : public InsertReader<std::multimap<Key, Value>, 468 DefaultReader<std::pair<Key, Value> > > {}; 469 470 template <typename First, typename Second> 471 class DefaultReader<std::pair<First, Second> > 472 : public PairReader<std::pair<First, Second> > {}; 473 396 474 /// \ingroup item_io 397 475 /// -
lemon/bits/item_writer.h
r1535 r1852 54 54 /// 55 55 /// Writes a quoted string to the given stream. 56 void write(std::ostream& os, const std::string& value) {56 void write(std::ostream& os, const std::string& value) const { 57 57 os << "\""; 58 58 if (escaped) { … … 137 137 /// 138 138 /// Writes a quoted char array to the given stream. 139 void write(std::ostream& os, const char* value) {139 void write(std::ostream& os, const char* value) const { 140 140 QuotedStringWriter(escaped).write(os, std::string(value)); 141 141 } … … 169 169 170 170 public: 171 172 IterableWriter(const ItemWriter& _item_writer = ItemWriter()) 173 : item_writer(_item_writer) {} 171 174 172 175 /// \brief Writes the values of the container to the given stream. … … 186 189 187 190 /// \ingroup 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 /// \ingroup item_io 188 238 /// 189 239 /// \brief The default item writer template class. … … 218 268 : public QuotedCharArrayWriter {}; 219 269 270 template <> 271 class DefaultWriter<char*> 272 : public QuotedCharArrayWriter {}; 273 274 template <> 275 class DefaultWriter<const char*> 276 : public QuotedCharArrayWriter {}; 277 220 278 template <typename Item> 221 279 class DefaultWriter<std::vector<Item> > … … 234 292 : public IterableWriter<std::set<Item> > {}; 235 293 294 template <typename Key, typename Value> 295 class DefaultWriter<std::map<Key, Value> > 296 : public IterableWriter<std::map<Key, Value> > {}; 297 236 298 template <typename Item> 237 299 class DefaultWriter<std::multiset<Item> > 238 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> > {}; 239 309 240 310 /// \ingroup item_io -
lemon/lemon_writer.h
r1845 r1852 173 173 virtual ~MapWriterBase() {} 174 174 175 virtual void write(std::ostream& os, const Item& item) = 0;175 virtual void write(std::ostream& os, const Item& item) const = 0; 176 176 }; 177 177 … … 193 193 virtual ~MapWriter() {} 194 194 195 virtual void write(std::ostream& os, const Item& item) {195 virtual void write(std::ostream& os, const Item& item) const { 196 196 Value value = map[item]; 197 197 writer.write(os, value);
Note: See TracChangeset
for help on using the changeset viewer.