Advertisement
cepxuozab

Examle Builder

Sep 2nd, 2023
1,469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <set>
  4. #include <string>
  5.  
  6. class Json { };
  7.  
  8. class Builder {
  9.     class BuilderContext;
  10.     class DictValueContext;
  11.  
  12. public:
  13.     Builder();
  14.     DictValueContext Key(std::string key);
  15.  
  16. private:
  17.     Json* json;
  18.     class BaseContext {
  19.     public:
  20.         BaseContext(Builder& builder)
  21.             : builder_(builder)
  22.         {
  23.         }
  24.         DictValueContext Key(std::string key)
  25.         {
  26.             return builder_.Key(std::move(key));
  27.         }
  28.  
  29.     private:
  30.         Builder& builder_;
  31.     };
  32.     class DictValueContext : public BaseContext {
  33.     public:
  34.         DictValueContext(BaseContext base)
  35.             : BaseContext(base)
  36.         {
  37.         }
  38.         DictValueContext Key(std::string key) = delete;
  39.     };
  40. };
  41.  
  42. Builder::Builder()
  43.     : json(new Json)
  44. {
  45. }
  46.  
  47. Builder::DictValueContext Builder::Key(std::string key)
  48. {
  49.     return BaseContext { *this };
  50. }
  51.  
  52. int main() {
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement