ledger-core
AbstractWebSocketNotificationParser.h
1 /*
2  *
3  * AbstractWebSocketNotificationParser
4  *
5  * Created by El Khalil Bellakrid on 29/11/2018.
6  *
7  * The MIT License (MIT)
8  *
9  * Copyright (c) 2018 Ledger
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in all
19  * copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  *
29  */
30 
31 
32 #ifndef LEDGER_CORE_ABSTRACTWEBSOCKETNOTIFICATIONPARSER_H
33 #define LEDGER_CORE_ABSTRACTWEBSOCKETNOTIFICATIONPARSER_H
34 
35 #define PROXY_PARSE_WS(method, ...) \
36  auto& currentObject = _currentObject; \
37  if (currentObject == "block") { \
38  return getBlockParser().method(__VA_ARGS__); \
39  } else if (currentObject == "transaction") { \
40  return getTransactionParser().method(__VA_ARGS__); \
41  } else \
42 
43 namespace ledger {
44  namespace core {
45  template<typename BlockchainExplorerTransaction, typename BlockchainExplorerBlock, typename TransactionParser, typename BlockParser>
47  public:
48  struct Result {
49  BlockchainExplorerTransaction transaction;
50  BlockchainExplorerBlock block;
51  std::string type;
52  std::string blockchain;
53  };
54 
55  void init(Result *result) {
56  _result = result;
57  };
58 
59  bool Null() {
60  PROXY_PARSE_WS(Null) {
61  return true;
62  }
63  };
64 
65  bool Bool(bool b) {
66  PROXY_PARSE_WS(Bool, b) {
67  return true;
68  }
69  };
70 
71  bool Int(int i) {
72  PROXY_PARSE_WS(Int, i) {
73  return true;
74  }
75  };
76 
77  bool Uint(unsigned i) {
78  PROXY_PARSE_WS(Uint, i) {
79  return true;
80  }
81  };
82 
83  bool Int64(int64_t i) {
84  PROXY_PARSE_WS(Int64, i) {
85  return true;
86  }
87  };
88 
89  bool Uint64(uint64_t i) {
90  PROXY_PARSE_WS(Uint64, i) {
91  return true;
92  }
93  };
94 
95  bool Double(double d) {
96  PROXY_PARSE_WS(Double, d) {
97  return true;
98  }
99  };
100 
101  bool RawNumber(const rapidjson::Reader::Ch *str, rapidjson::SizeType length,
102  bool copy) {
103  PROXY_PARSE_WS(RawNumber, str, length, copy) {
104  return true;
105  }
106  };
107 
108  bool
109  String(const rapidjson::Reader::Ch *str, rapidjson::SizeType length, bool copy) {
110  PROXY_PARSE_WS(String, str, length, copy) {
111  if (getLastKey() == "type") {
112  _result->type = std::string(str, length);
113  } else if (getLastKey() == "block_chain") {
114  _result->blockchain = std::string(str, length);
115  }
116  return true;
117  }
118  };
119 
120  bool StartObject() {
121  {
122  _depth += 1;
123  if (_depth == 3) {
124  _currentObject = getLastKey();
125  }
126  auto& currentObject = _currentObject;
127 
128  if (currentObject == "transaction") {
129  getTransactionParser().init(&_result->transaction);
130  } else if (currentObject == "block") {
131  getBlockParser().init(&_result->block);
132  }
133  }
134  PROXY_PARSE_WS(StartObject) {
135  return true;
136  }
137  };
138 
139 
140  virtual bool Key(const rapidjson::Reader::Ch *str, rapidjson::SizeType length, bool copy) {
141  PROXY_PARSE_WS(Key, str, length, copy) {
142  return true;
143  }
144  };
145 
146  bool EndObject(rapidjson::SizeType memberCount) {
147  _depth -= 1;
148  if (_depth == 2)
149  _currentObject = "";
150  PROXY_PARSE_WS(EndObject, memberCount) {
151  return true;
152  }
153  };
154 
155  bool StartArray() {
156  PROXY_PARSE_WS(StartArray) {
157  return true;
158  }
159  };
160 
161  bool EndArray(rapidjson::SizeType elementCount) {
162  PROXY_PARSE_WS(EndArray, elementCount) {
163  return false;
164  }
165  };
166 
167  protected:
168  virtual TransactionParser &getTransactionParser() = 0;
169  virtual BlockParser &getBlockParser() = 0;
170  virtual std::string &getLastKey() = 0;
171  std::string _currentObject = "";
172  Result* _result;
173  int32_t _depth = 0;
174  };
175  }
176 }
177 
178 #endif //LEDGER_CORE_ABSTRACTWEBSOCKETNOTIFICATIONPARSER_H
Definition: BlockParser.hpp:39
Definition: AbstractWebSocketNotificationParser.h:48
Definition: TransactionParser.hpp:47
Definition: Account.cpp:8
Definition: AbstractWebSocketNotificationParser.h:46