streamConsumer.hh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * streamConsumer.hh for jsonstroller
  3. *
  4. * Author: isundil <isundill@gmail.com>
  5. **/
  6. #pragma once
  7. #include <istream>
  8. #include "params.hh"
  9. #include "jsonObject.hh"
  10. #include "jsonArray.hh"
  11. #include "jsonPrimitive.hh"
  12. #include "linearHistory.hh"
  13. #include "warning.hh"
  14. class StreamConsumer
  15. {
  16. public:
  17. /**
  18. * constructor
  19. * does not read yet
  20. **/
  21. StreamConsumer(std::istream &stream);
  22. virtual ~StreamConsumer();
  23. /**
  24. * read from stream and set root data
  25. * @return current instance
  26. **/
  27. StreamConsumer *read();
  28. /**
  29. * get root node
  30. **/
  31. const JSonElement * getRoot() const;
  32. JSonElement * getRoot();
  33. /**
  34. * return non-blocking error messages
  35. **/
  36. const std::list<Warning> &getMessages() const;
  37. /**
  38. * set builder's params
  39. **/
  40. StreamConsumer *withConfig(const AParams *);
  41. private:
  42. /**
  43. * @return non-null on successfully read JSonElement, or null if token (',', '[', ...)
  44. **/
  45. JSonElement *consumeToken(JSonContainer *parent, std::stringstream &buf);
  46. JSonElement *consumeString(JSonContainer *parent, std::stringstream &buf);
  47. JSonElement *consumeBool(JSonContainer *parent, std::stringstream &buf, char c);
  48. JSonElement *consumeNumber(JSonContainer *parent, std::stringstream &buf, char c);
  49. JSonElement *consumeNull(JSonContainer *parent, std::stringstream &buf);
  50. /**
  51. * read next item, fill object or array if found
  52. **/
  53. JSonElement *readNext(JSonContainer *parent);
  54. /**
  55. * fill object
  56. **/
  57. JSonObject *readObject(JSonContainer *parent);
  58. /**
  59. * fill array
  60. **/
  61. JSonArray *readArray(JSonContainer *parent);
  62. /**
  63. * out of token, should we ignore that char ?
  64. **/
  65. bool ignoreChar(char c) const noexcept;
  66. /**
  67. * compute unicode value and append it to buffer
  68. **/
  69. static void appendUnicode(const char [4], std::stringstream &);
  70. /**
  71. * input stream
  72. **/
  73. std::istream &stream;
  74. /**
  75. * once read, contains root element
  76. **/
  77. JSonElement *root;
  78. /**
  79. * builder params (program's one in fact)
  80. **/
  81. const AParams *params;
  82. /**
  83. * non-blocking errors
  84. **/
  85. std::list<Warning> warnings;
  86. /**
  87. * last read input
  88. **/
  89. LinearHistory history;
  90. };