Changeset 1845:f8bbfed86036 in lemon-0.x for lemon/lemon_writer.h
- Timestamp:
- 12/03/05 19:17:29 (19 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2404
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/lemon_writer.h
r1705 r1845 165 165 }; 166 166 167 168 template <typename _Item> 169 class MapWriterBase { 170 public: 171 typedef _Item Item; 172 173 virtual ~MapWriterBase() {} 174 175 virtual void write(std::ostream& os, const Item& item) = 0; 176 }; 177 178 179 template <typename _Item, typename _Map, typename _Writer> 180 class MapWriter : public MapWriterBase<_Item> { 181 public: 182 typedef _Map Map; 183 typedef _Writer Writer; 184 typedef typename Writer::Value Value; 185 typedef _Item Item; 186 187 typename _writer_bits::Ref<Map>::Type map; 188 Writer writer; 189 190 MapWriter(const Map& _map, const Writer& _writer) 191 : map(_map), writer(_writer) {} 192 193 virtual ~MapWriter() {} 194 195 virtual void write(std::ostream& os, const Item& item) { 196 Value value = map[item]; 197 writer.write(os, value); 198 } 199 200 }; 201 202 203 class ValueWriterBase { 204 public: 205 virtual ~ValueWriterBase() {} 206 virtual void write(std::ostream&) = 0; 207 }; 208 209 template <typename _Value, typename _Writer> 210 class ValueWriter : public ValueWriterBase { 211 public: 212 typedef _Value Value; 213 typedef _Writer Writer; 214 215 ValueWriter(const Value& _value, const Writer& _writer) 216 : value(_value), writer(_writer) {} 217 218 virtual void write(std::ostream& os) { 219 writer.write(os, value); 220 } 221 private: 222 const Value& value; 223 Writer writer; 224 }; 225 226 227 template <typename _Item> 228 class IdWriterBase { 229 public: 230 typedef _Item Item; 231 virtual ~IdWriterBase() {} 232 virtual void write(std::ostream&, const Item&) const = 0; 233 virtual bool isIdWriter() const = 0; 234 }; 235 236 template <typename _Item, typename _BoxedIdWriter> 237 class IdWriter : public IdWriterBase<_Item> { 238 public: 239 typedef _Item Item; 240 typedef _BoxedIdWriter BoxedIdWriter; 241 242 const BoxedIdWriter& idWriter; 243 244 IdWriter(const BoxedIdWriter& _idWriter) 245 : idWriter(_idWriter) {} 246 247 virtual void write(std::ostream& os, const Item& item) const { 248 idWriter.writeId(os, item); 249 } 250 251 virtual bool isIdWriter() const { 252 return idWriter.isIdWriter(); 253 } 254 }; 255 167 256 } 168 257 … … 282 371 }; 283 372 284 /// \brief Helper class for implementing the common SectionWriters.285 ///286 /// Helper class for implementing the common SectionWriters.287 class CommonSectionWriterBase : public LemonWriter::SectionWriter {288 typedef LemonWriter::SectionWriter Parent;289 protected:290 291 /// \brief Constructor for CommonSectionWriterBase.292 ///293 /// Constructor for CommonSectionWriterBase. It attach this writer to294 /// the given LemonWriter.295 CommonSectionWriterBase(LemonWriter& _writer)296 : Parent(_writer) {}297 298 template <typename _Item>299 class WriterBase {300 public:301 typedef _Item Item;302 303 virtual ~WriterBase() {}304 305 virtual void write(std::ostream& os, const Item& item) = 0;306 };307 308 309 template <typename _Item, typename _Map, typename _Writer>310 class MapWriter : public WriterBase<_Item> {311 public:312 typedef _Map Map;313 typedef _Writer Writer;314 typedef typename Writer::Value Value;315 typedef _Item Item;316 317 typename _writer_bits::Ref<Map>::Type map;318 Writer writer;319 320 MapWriter(const Map& _map, const Writer& _writer)321 : map(_map), writer(_writer) {}322 323 virtual ~MapWriter() {}324 325 virtual void write(std::ostream& os, const Item& item) {326 Value value = map[item];327 writer.write(os, value);328 }329 330 };331 332 333 class ValueWriterBase {334 public:335 virtual ~ValueWriterBase() {}336 virtual void write(std::ostream&) = 0;337 };338 339 template <typename _Value, typename _Writer>340 class ValueWriter : public ValueWriterBase {341 public:342 typedef _Value Value;343 typedef _Writer Writer;344 345 ValueWriter(const Value& _value, const Writer& _writer)346 : value(_value), writer(_writer) {}347 348 virtual void write(std::ostream& os) {349 writer.write(os, value);350 }351 private:352 const Value& value;353 Writer writer;354 };355 356 357 template <typename _Item>358 class IdWriterBase {359 public:360 typedef _Item Item;361 virtual ~IdWriterBase() {}362 virtual void write(std::ostream&, const Item&) const = 0;363 virtual bool isIdWriter() const = 0;364 };365 366 template <typename _Item, typename _BoxedIdWriter>367 class IdWriter : public IdWriterBase<_Item> {368 public:369 typedef _Item Item;370 typedef _BoxedIdWriter BoxedIdWriter;371 372 const BoxedIdWriter& idWriter;373 374 IdWriter(const BoxedIdWriter& _idWriter)375 : idWriter(_idWriter) {}376 377 virtual void write(std::ostream& os, const Item& item) const {378 idWriter.writeId(os, item);379 }380 381 virtual bool isIdWriter() const {382 return idWriter.isIdWriter();383 }384 };385 };386 387 373 /// \ingroup io_group 388 374 /// \brief SectionWriter for writing a graph's nodeset. … … 404 390 /// \relates LemonWriter 405 391 template <typename _Graph, typename _Traits = DefaultWriterTraits> 406 class NodeSetWriter : public CommonSectionWriterBase{407 typedef CommonSectionWriterBaseParent;392 class NodeSetWriter : public LemonWriter::SectionWriter { 393 typedef LemonWriter::SectionWriter Parent; 408 394 public: 409 395 … … 458 444 checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>(); 459 445 writers.push_back( 460 make_pair(name, new MapWriter<Node, Map, Writer>(map, writer))); 446 make_pair(name, new _writer_bits:: 447 MapWriter<Node, Map, Writer>(map, writer))); 461 448 return *this; 462 449 } … … 528 515 private: 529 516 530 typedef std::vector<std::pair<std::string, WriterBase<Node>*> > MapWriters; 517 typedef std::vector<std::pair<std::string, _writer_bits:: 518 MapWriterBase<Node>*> > MapWriters; 531 519 MapWriters writers; 532 520 533 WriterBase<Node>* idMap;521 _writer_bits::MapWriterBase<Node>* idMap; 534 522 bool forceIdMap; 535 523 … … 563 551 /// \relates LemonWriter 564 552 template <typename _Graph, typename _Traits = DefaultWriterTraits> 565 class EdgeSetWriter : public CommonSectionWriterBase{566 typedef CommonSectionWriterBaseParent;553 class EdgeSetWriter : public LemonWriter::SectionWriter { 554 typedef LemonWriter::SectionWriter Parent; 567 555 public: 568 556 … … 587 575 graph(_graph), id(_id) { 588 576 checkConcept<_writer_bits::ItemIdWriter<Node>, NodeIdWriter>(); 589 nodeIdWriter.reset(new IdWriter<Node, NodeIdWriter>(_nodeIdWriter)); 577 nodeIdWriter.reset(new _writer_bits:: 578 IdWriter<Node, NodeIdWriter>(_nodeIdWriter)); 590 579 } 591 580 … … 624 613 checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>(); 625 614 writers.push_back( 626 make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer))); 615 make_pair(name, new _writer_bits:: 616 MapWriter<Edge, Map, Writer>(map, writer))); 627 617 return *this; 628 618 } … … 702 692 private: 703 693 704 typedef std::vector<std::pair<std::string, WriterBase<Edge>*> > MapWriters; 694 typedef std::vector<std::pair<std::string, _writer_bits:: 695 MapWriterBase<Edge>*> > MapWriters; 705 696 MapWriters writers; 706 697 707 WriterBase<Edge>* idMap;698 _writer_bits::MapWriterBase<Edge>* idMap; 708 699 bool forceIdMap; 709 700 … … 711 702 std::string id; 712 703 713 std::auto_ptr< IdWriterBase<Node> > nodeIdWriter;704 std::auto_ptr<_writer_bits::IdWriterBase<Node> > nodeIdWriter; 714 705 }; 715 706 … … 744 735 /// \relates LemonWriter 745 736 template <typename _Graph, typename _Traits = DefaultWriterTraits> 746 class UndirEdgeSetWriter : public CommonSectionWriterBase{747 typedef CommonSectionWriterBaseParent;737 class UndirEdgeSetWriter : public LemonWriter::SectionWriter { 738 typedef LemonWriter::SectionWriter Parent; 748 739 public: 749 740 … … 769 760 graph(_graph), id(_id) { 770 761 checkConcept<_writer_bits::ItemIdWriter<Node>, NodeIdWriter>(); 771 nodeIdWriter.reset(new IdWriter<Node, NodeIdWriter>(_nodeIdWriter)); 762 nodeIdWriter.reset(new _writer_bits:: 763 IdWriter<Node, NodeIdWriter>(_nodeIdWriter)); 772 764 } 773 765 … … 806 798 checkConcept<_writer_bits::ItemWriter<typename Map::Value>, Writer>(); 807 799 writers.push_back( 808 make_pair(name, new MapWriter<UndirEdge, Map, Writer>(map, writer))); 800 make_pair(name, new _writer_bits:: 801 MapWriter<UndirEdge, Map, Writer>(map, writer))); 809 802 return *this; 810 803 } … … 929 922 private: 930 923 931 typedef std::vector<std::pair<std::string, 932 WriterBase<UndirEdge>*> > MapWriters;924 typedef std::vector<std::pair<std::string, _writer_bits:: 925 MapWriterBase<UndirEdge>*> > MapWriters; 933 926 MapWriters writers; 934 927 935 WriterBase<UndirEdge>* idMap;928 _writer_bits::MapWriterBase<UndirEdge>* idMap; 936 929 bool forceIdMap; 937 930 … … 939 932 std::string id; 940 933 941 std::auto_ptr< IdWriterBase<Node> > nodeIdWriter;934 std::auto_ptr<_writer_bits::IdWriterBase<Node> > nodeIdWriter; 942 935 }; 943 936 … … 953 946 /// \relates LemonWriter 954 947 template <typename _Graph> 955 class NodeWriter : public CommonSectionWriterBase{956 typedef CommonSectionWriterBaseParent;948 class NodeWriter : public LemonWriter::SectionWriter { 949 typedef LemonWriter::SectionWriter Parent; 957 950 typedef _Graph Graph; 958 951 typedef typename Graph::Node Node; … … 969 962 : Parent(_writer), id(_id) { 970 963 checkConcept<_writer_bits::ItemIdWriter<Node>, _IdWriter>(); 971 idWriter.reset(new IdWriter<Node, _IdWriter>(_idWriter));964 idWriter.reset(new _writer_bits::IdWriter<Node, _IdWriter>(_idWriter)); 972 965 } 973 966 … … 1021 1014 typedef std::vector<std::pair<std::string, const Node*> > NodeWriters; 1022 1015 NodeWriters writers; 1023 std::auto_ptr< IdWriterBase<Node> > idWriter;1016 std::auto_ptr<_writer_bits::IdWriterBase<Node> > idWriter; 1024 1017 }; 1025 1018 … … 1035 1028 /// \relates LemonWriter 1036 1029 template <typename _Graph> 1037 class EdgeWriter : public CommonSectionWriterBase{1038 typedef CommonSectionWriterBaseParent;1030 class EdgeWriter : public LemonWriter::SectionWriter { 1031 typedef LemonWriter::SectionWriter Parent; 1039 1032 typedef _Graph Graph; 1040 1033 typedef typename Graph::Edge Edge; … … 1051 1044 : Parent(_writer), id(_id) { 1052 1045 checkConcept<_writer_bits::ItemIdWriter<Edge>, _IdWriter>(); 1053 idWriter.reset(new IdWriter<Edge, _IdWriter>(_idWriter));1046 idWriter.reset(new _writer_bits::IdWriter<Edge, _IdWriter>(_idWriter)); 1054 1047 } 1055 1048 … … 1102 1095 EdgeWriters writers; 1103 1096 1104 std::auto_ptr< IdWriterBase<Edge> > idWriter;1097 std::auto_ptr<_writer_bits::IdWriterBase<Edge> > idWriter; 1105 1098 }; 1106 1099 … … 1116 1109 /// \relates LemonWriter 1117 1110 template <typename _Graph> 1118 class UndirEdgeWriter : public CommonSectionWriterBase{1119 typedef CommonSectionWriterBaseParent;1111 class UndirEdgeWriter : public LemonWriter::SectionWriter { 1112 typedef LemonWriter::SectionWriter Parent; 1120 1113 typedef _Graph Graph; 1121 1114 typedef typename Graph::Node Node; … … 1136 1129 checkConcept<_writer_bits::ItemIdWriter<Edge>, _IdWriter>(); 1137 1130 checkConcept<_writer_bits::ItemIdWriter<UndirEdge>, _IdWriter>(); 1138 undirEdgeIdWriter.reset(new IdWriter<UndirEdge, _IdWriter>(_idWriter)); 1139 edgeIdWriter.reset(new IdWriter<Edge, _IdWriter>(_idWriter)); 1131 undirEdgeIdWriter.reset(new _writer_bits:: 1132 IdWriter<UndirEdge, _IdWriter>(_idWriter)); 1133 edgeIdWriter.reset(new _writer_bits:: 1134 IdWriter<Edge, _IdWriter>(_idWriter)); 1140 1135 } 1141 1136 … … 1203 1198 const UndirEdge*> > UndirEdgeWriters; 1204 1199 UndirEdgeWriters undirEdgeWriters; 1205 std::auto_ptr< IdWriterBase<UndirEdge> > undirEdgeIdWriter;1200 std::auto_ptr<_writer_bits::IdWriterBase<UndirEdge> > undirEdgeIdWriter; 1206 1201 1207 1202 typedef std::vector<std::pair<std::string, const Edge*> > EdgeWriters; 1208 1203 EdgeWriters edgeWriters; 1209 std::auto_ptr< IdWriterBase<Edge> > edgeIdWriter;1204 std::auto_ptr<_writer_bits::IdWriterBase<Edge> > edgeIdWriter; 1210 1205 1211 1206 }; … … 1223 1218 /// \relates LemonWriter 1224 1219 template <typename _Traits = DefaultWriterTraits> 1225 class AttributeWriter : public CommonSectionWriterBase{1226 typedef CommonSectionWriterBaseParent;1220 class AttributeWriter : public LemonWriter::SectionWriter { 1221 typedef LemonWriter::SectionWriter Parent; 1227 1222 typedef _Traits Traits; 1228 1223 public: … … 1268 1263 const Writer& writer = Writer()) { 1269 1264 checkConcept<_writer_bits::ItemWriter<Value>, Writer>(); 1270 writers.push_back(make_pair(name, new ValueWriter<Value, Writer>1271 1265 writers.push_back(make_pair(name, new _writer_bits:: 1266 ValueWriter<Value, Writer>(value, writer))); 1272 1267 return *this; 1273 1268 } … … 1297 1292 std::string id; 1298 1293 1299 typedef std::vector<std::pair<std::string, ValueWriterBase*> > Writers; 1294 typedef std::vector<std::pair<std::string, 1295 _writer_bits::ValueWriterBase*> > Writers; 1300 1296 Writers writers; 1301 1297 };
Note: See TracChangeset
for help on using the changeset viewer.