ledger-core
AbstractTransactionsBulkParser.h
1 /*
2  *
3  * AbstractTransactionsBulkParser
4  *
5  * Created by El Khalil Bellakrid on 17/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_ABSTRACTTRANSACTIONSBULKPARSER_H
33 #define LEDGER_CORE_ABSTRACTTRANSACTIONSBULKPARSER_H
34 
35 #define PROXY_PARSE_TXS(method, ...) \
36  if (_depth > 0) { \
37  return getTransactionsParser().method(__VA_ARGS__); \
38  } else { \
39  return true; \
40  }
41 
42 namespace ledger {
43  namespace core {
44  template <typename BlockchainExplorerTransactionsBulk, typename TxsParser>
46  public:
47 
48  bool Null() {
49  PROXY_PARSE_TXS(Null)
50  }
51 
52  bool Bool(bool b) {
53  if (getLastKey() == "truncated" && _depth == 0) {
54  _bulk->hasNext = b;
55  }
56  PROXY_PARSE_TXS(Bool, b)
57  }
58 
59  bool Int(int i) {
60  PROXY_PARSE_TXS(Int, i)
61  }
62 
63  bool Uint(unsigned i) {
64  PROXY_PARSE_TXS(Uint, i)
65  }
66 
67  bool Int64(int64_t i) {
68  PROXY_PARSE_TXS(Int64, i)
69  }
70 
71  bool Uint64(uint64_t i) {
72  PROXY_PARSE_TXS(Uint64, i)
73  }
74 
75  bool Double(double d) {
76  PROXY_PARSE_TXS(Double, d)
77  }
78 
79  bool RawNumber(const rapidjson::Reader::Ch *str, rapidjson::SizeType length, bool copy) {
80  PROXY_PARSE_TXS(RawNumber, str, length, copy)
81  }
82 
83  bool String(const rapidjson::Reader::Ch *str, rapidjson::SizeType length, bool copy) {
84  PROXY_PARSE_TXS(String, str, length, copy)
85  }
86 
87  bool StartObject() {
88  PROXY_PARSE_TXS(StartObject)
89  }
90 
91  bool Key(const rapidjson::Reader::Ch *str, rapidjson::SizeType length, bool copy) {
92  PROXY_PARSE_TXS(Key, str, length, copy)
93  }
94 
95  bool EndObject(rapidjson::SizeType memberCount) {
96  PROXY_PARSE_TXS(EndObject, memberCount)
97  }
98 
99  bool StartArray() {
100  if (_depth >= 1 || getLastKey() == "txs") {
101  _depth += 1;
102  }
103 
104  if (_depth == 1) {
105  getTransactionsParser().init(&_bulk->transactions);
106  }
107 
108  PROXY_PARSE_TXS(StartArray)
109  }
110 
111  bool EndArray(rapidjson::SizeType elementCount) {
112  if (_depth > 0) {
113  _depth -= 1;
114  }
115 
116  PROXY_PARSE_TXS(EndArray, elementCount)
117  }
118 
119  void init(BlockchainExplorerTransactionsBulk *bulk) {
120  _bulk = bulk;
121  }
122 
123  protected:
124  virtual TxsParser &getTransactionsParser() = 0;
125  virtual std::string &getLastKey() = 0;
126  int _depth;
127  BlockchainExplorerTransactionsBulk* _bulk;
128  };
129 
130  }
131 }
132 
133 #endif //LEDGER_CORE_ABSTRACTTRANSACTIONSBULKPARSER_H
Definition: Account.cpp:8
Definition: AbstractTransactionsBulkParser.h:45