fork(1) download
  1. #include <string>
  2. #include <iostream>
  3. #include <stdexcept>
  4.  
  5. class XML{};
  6.  
  7. class XMLParser1 {
  8. public:
  9. virtual XML Parse(const std::string&) = 0;
  10. };
  11.  
  12. class XMLParser11 : public XMLParser1 {
  13. public:
  14. XML Parse(const std::string& xml) {
  15. XML xmlTree; // or std::auto_ptr<XML> xmlTree(new XML); if it must be a pointer
  16. // ...
  17. std::cout << "XMLParserll is processing " << xml << '\n';
  18. // ...
  19. return xmlTree;
  20. }
  21. };
  22.  
  23. class XMLParser2 {
  24. public:
  25. virtual XML Parse(std::istream&) = 0;
  26. };
  27.  
  28. class XMLParser11Adapter : public XMLParser2 {
  29. public:
  30. XML Parse(std::istream& xmlStream) {
  31. if ( !xmlStream ) {
  32. throw std::runtime_error("XMLParserlldapter::Parse() cannot read from xmlStream");
  33. }
  34. std::string xml;
  35. std::string line;
  36. while ( getline( xmlStream, line ) ) {
  37. xml += line + '\n';
  38. }
  39. return xmlParser11.Parse(xml);
  40. }
  41. private:
  42. XMLParser11 xmlParser11;
  43. };
  44.  
  45. int main()
  46. {
  47. XMLParser11Adapter xmlp;
  48. xmlp.Parse(std::cin);
  49. }
Success #stdin #stdout 0s 2864KB
stdin
<hi></hi>
stdout
XMLParserll is processing <hi></hi>