/tags/dev-20000116/FreeSpeech/data-flow/include/ObjectParser.h

# · C Header · 166 lines · 129 code · 22 blank · 15 comment · 15 complexity · 2064946c1b9351106cd3e17726d31ffc MD5 · raw file

  1. // Copyright (C) 1999 Jean-Marc Valin
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but
  9. // WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. // General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this file. If not, write to the Free Software Foundation,
  15. // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. #ifndef OBJECTPARSER_H
  17. #define OBJECTPARSER_H
  18. #include <stream.h>
  19. #include <iostream.h>
  20. #include <vector>
  21. #include <Object.h>
  22. #include <map>
  23. inline bool isValidType (istream &in, string expectedType);
  24. inline ostream &operator << (ostream &out, const ObjectRef &ref)
  25. {
  26. out << *ref;
  27. return out;
  28. }
  29. template <class T>
  30. inline ostream &operator << (ostream &out, const vector<T> &v)
  31. {
  32. out << "<Vector ";
  33. for (int i=0; i < v.size(); i++)
  34. {
  35. out << " " << v[i];
  36. }
  37. out << " > ";
  38. return out;
  39. }
  40. template <class T>
  41. inline ostream &operator << (ostream &out, const vector<T*> &v)
  42. {
  43. out << "<Vector ";
  44. for (int i=0; i < v.size(); i++)
  45. {
  46. out << " " << *(v[i]);
  47. }
  48. out << " > ";
  49. return out;
  50. }
  51. template <class T>
  52. inline istream &operator >> (istream &in, vector<T> &v)
  53. {
  54. int items_found=0;
  55. if (!isValidType(in,"Vector"))
  56. return in;
  57. while (!in.eof())
  58. {
  59. T tmp;
  60. in >> tmp;
  61. if (in.fail()) break;
  62. items_found++;
  63. v.resize(items_found);
  64. v[items_found-1]=tmp;
  65. }
  66. in.clear();
  67. char ch;
  68. in >> ch;
  69. return in;
  70. }
  71. template <class T>
  72. inline istream &operator >> (istream &in, vector<T*> &v)
  73. {
  74. int items_found=0;
  75. string nimportequoi;
  76. if (!isValidType(in,"Vector"))
  77. return in;
  78. while (!in.eof())
  79. {
  80. T *tmp = new T;
  81. in >> *tmp;
  82. if (in.fail()) break;
  83. items_found++;
  84. v.resize(items_found);
  85. v[items_found-1]=tmp;
  86. }
  87. in.clear();
  88. char ch;
  89. in >> ch;
  90. return in;
  91. }
  92. class ParsingException {
  93. public:
  94. ParsingException (string _message)
  95. : message(_message)
  96. {}
  97. void print(ostream &out=cerr) const {out << message << endl;}
  98. protected:
  99. string message;
  100. };
  101. inline bool isValidType (istream &in, string expectedType)
  102. {
  103. char ch;
  104. in >> ch;
  105. if (ch == '<')
  106. {
  107. string type;
  108. in >> type;
  109. if (type != expectedType)
  110. throw ParsingException ("Parser expected type " + expectedType + " and got " + type);
  111. } else {
  112. in.putback(ch);
  113. in.clear(ios::failbit);
  114. return false;
  115. }
  116. return true;
  117. }
  118. class _ObjectFactory
  119. {
  120. public:
  121. virtual ObjectRef create() = 0;
  122. };
  123. template <class T>
  124. class ObjectFactory : public _ObjectFactory {
  125. public:
  126. virtual ObjectRef create() {return ObjectRef(new T);}
  127. };
  128. template <class T>
  129. inline istream &operator >> (istream &in, Ptr<T> &o)
  130. {
  131. char ch;
  132. in >> ch;
  133. if (ch != '<'){
  134. in.putback(ch);
  135. in.clear(ios::failbit);
  136. return in;
  137. }
  138. string type;
  139. in >> type;
  140. o = Object::newObject(type);
  141. o->readFrom(in);
  142. return in;
  143. }
  144. #endif