ledger-core
AbstractTransactionsParser.h
1 /*
2  *
3  * AbstractTransactionsParser
4  *
5  * Created by El Khalil Bellakrid on 29/07/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 #include <rapidjson/reader.h>
32 #include <net/HttpClient.hpp>
33 #include <vector>
34 
35 
36 #ifndef LEDGER_CORE_ABSTRACTTRANSACTIONSPARSER_H
37 #define LEDGER_CORE_ABSTRACTTRANSACTIONSPARSER_H
38 
39 #define PROXY_PARSE_TX(method, ...) \
40  if (_arrayDepth > 0) { \
41  return getTransactionParser().method(__VA_ARGS__); \
42  } else { \
43  return true; \
44  }
45 
46 namespace ledger {
47  namespace core {
48  template <typename BlockchainExplorerTransaction, typename TxParser>
50 
51  public:
52  bool Null() {
53  PROXY_PARSE_TX(Null)
54  };
55 
56  bool Bool(bool b) {
57  PROXY_PARSE_TX(Bool, b)
58  };
59 
60  bool Int(int i) {
61  PROXY_PARSE_TX(Int, i)
62  };
63 
64  bool Uint(unsigned i) {
65  PROXY_PARSE_TX(Uint, i)
66  };
67 
68  bool Int64(int64_t i) {
69  PROXY_PARSE_TX(Int64, i)
70  };
71 
72  bool Uint64(uint64_t i) {
73  PROXY_PARSE_TX(Uint64, i)
74  };
75 
76  bool Double(double d) {
77  PROXY_PARSE_TX(Double, d)
78  };
79 
80  bool
81  RawNumber(const rapidjson::Reader::Ch *str, rapidjson::SizeType length, bool copy) {
82  PROXY_PARSE_TX(RawNumber, str, length, copy)
83  };
84 
85  bool String(const rapidjson::Reader::Ch *str, rapidjson::SizeType length, bool copy) {
86  PROXY_PARSE_TX(String, str, length, copy)
87  };
88 
89  bool StartObject() {
90  _objectDepth += 1;
91 
92  if (_arrayDepth == 1 && _objectDepth == 1) {
93  BlockchainExplorerTransaction transaction;
94  _transactions->push_back(transaction);
95  getTransactionParser().init(&_transactions->back());
96  }
97 
98  PROXY_PARSE_TX(StartObject)
99  };
100 
101  bool Key(const rapidjson::Reader::Ch *str, rapidjson::SizeType length, bool copy) {
102  PROXY_PARSE_TX(Key, str, length, copy)
103  };
104 
105  bool EndObject(rapidjson::SizeType memberCount) {
106  if (_arrayDepth > 0) {
107  _objectDepth -= 1;
108  auto result = getTransactionParser().EndObject(memberCount);
109  return result;
110  } else {
111  return true;
112  }
113  };
114 
115  bool StartArray() {
116  if (_arrayDepth > 0) {
117  _arrayDepth = _arrayDepth + 1;
118  return getTransactionParser().StartArray();
119  } else {
120  _arrayDepth = _arrayDepth + 1;
121  return true;
122  }
123  };
124 
125  bool EndArray(rapidjson::SizeType elementCount) {
126  _arrayDepth -= 1;
127  PROXY_PARSE_TX(EndArray, elementCount)
128  return true;
129  };
130 
131 
132  void init(
133  std::vector<BlockchainExplorerTransaction> *transactions) {
134  _transactions = transactions;
135  };
136 
137  protected:
138  virtual TxParser &getTransactionParser() = 0;
139  std::vector<BlockchainExplorerTransaction>* _transactions;
140  uint32_t _arrayDepth;
141  uint32_t _objectDepth;
142  };
143  }
144 }
145 
146 #endif //LEDGER_CORE_ABSTRACTTRANSACTIONSPARSER_H
Definition: AbstractTransactionsParser.h:49
Definition: Account.cpp:8