Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. class RootContext
  2. {
  3. public:
  4.   RootContext(std::iostream out_){}
  5. };
  6.  
  7. template <class ParentContext>
  8. class InsideArrayContext;
  9. template <class ParentContext>
  10. class InsideObjectExpectingValue;
  11. template <class ParentContext>
  12. class InsideObjectExpectingKey;
  13. template <class ParentContext>
  14. class EmptyContext;
  15.  
  16. template <class ParentContext>
  17. class InsideArrayContext
  18. {
  19. private:
  20.   std::ostream &out_;
  21.   bool is_closed_;
  22.   bool is_started_;
  23.   void checkForComma()
  24.   {
  25.     if (!is_started_)
  26.     {
  27.       is_started_ = true;
  28.     }
  29.     else
  30.     {
  31.       out_.put(',');
  32.     }
  33.   }
  34.  
  35. public:
  36.   InsideArrayContext(std::ostream &out) : out_(out),
  37.                                           is_closed_(false),
  38.                                           is_started_(false)
  39.   {
  40.     out_.put('[');
  41.   }
  42.   ~InsideArrayContext()
  43.   {
  44.     if (!is_closed_)
  45.     {
  46.       out_.put(']');
  47.     }
  48.   }
  49.   InsideArrayContext<ParentContext> &Number(int64_t number)
  50.   {
  51.     checkForComma();
  52.  
  53.     out_ << number;
  54.     return (*this);
  55.   }
  56.   InsideArrayContext<ParentContext> &String(std::string_view text)
  57.   {
  58.     checkForComma();
  59.     out_.put('"');
  60.     out_ << text;
  61.     out_.put('"');
  62.     return (*this);
  63.   }
  64.   // InsideArrayContext<ParentContext> Boolean(bool)
  65.   // {
  66.   //     // return something
  67.   // }
  68.   // InsideArrayContext<ParentContext> Null()
  69.   // {
  70.   //     // return something
  71.   // }
  72.   // InsideArrayContext<InsideArrayContext> BeginArray()
  73.   // {
  74.   //     InsideArrayContext<InsideArrayContext> inner(out_, this);
  75.   //     return inner;
  76.   // }
  77.   ParentContext EndArray()
  78.   {
  79.       is_closed_ = true;
  80.       out_.put(']');
  81.       ParentContext context
  82.       // return ;
  83.   }
  84.   // InsideObjectExpectingKey<InsideArrayContext> BeginObject()
  85.   // {
  86.   //     // return something
  87.   // }
  88. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement