rippled
Loading...
Searching...
No Matches
STPathSet.h
1#pragma once
2
3#include <xrpl/basics/CountedObject.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/json/json_value.h>
6#include <xrpl/protocol/SField.h>
7#include <xrpl/protocol/STBase.h>
8#include <xrpl/protocol/UintTypes.h>
9
10#include <cstddef>
11#include <optional>
12
13namespace xrpl {
14
15class STPathElement final : public CountedObject<STPathElement>
16{
17 unsigned int mType;
21
24
25public:
26 enum Type {
27 typeNone = 0x00,
28 typeAccount = 0x01, // Rippling through an account (vs taking an offer).
29 typeCurrency = 0x10, // Currency follows.
30 typeIssuer = 0x20, // Issuer follows.
31 typeBoundary = 0xFF, // Boundary between alternate paths.
33 // Combination of all types.
34 };
35
37 STPathElement(STPathElement const&) = default;
39 operator=(STPathElement const&) = default;
40
42 std::optional<AccountID> const& account,
43 std::optional<Currency> const& currency,
44 std::optional<AccountID> const& issuer);
45
47 AccountID const& account,
48 Currency const& currency,
49 AccountID const& issuer,
50 bool forceCurrency = false);
51
52 STPathElement(unsigned int uType, AccountID const& account, Currency const& currency, AccountID const& issuer);
53
54 auto
55 getNodeType() const;
56
57 bool
58 isOffer() const;
59
60 bool
61 isAccount() const;
62
63 bool
64 hasIssuer() const;
65
66 bool
67 hasCurrency() const;
68
69 bool
70 isNone() const;
71
72 // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
73 // class of offers.
74 AccountID const&
75 getAccountID() const;
76
77 Currency const&
78 getCurrency() const;
79
80 AccountID const&
81 getIssuerID() const;
82
83 bool
84 operator==(STPathElement const& t) const;
85
86 bool
87 operator!=(STPathElement const& t) const;
88
89private:
90 static std::size_t
91 get_hash(STPathElement const& element);
92};
93
94class STPath final : public CountedObject<STPath>
95{
97
98public:
99 STPath() = default;
100
102
104 size() const;
105
106 bool
107 empty() const;
108
109 void
110 push_back(STPathElement const& e);
111
112 template <typename... Args>
113 void
114 emplace_back(Args&&... args);
115
116 bool
117 hasSeen(AccountID const& account, Currency const& currency, AccountID const& issuer) const;
118
120
122 begin() const;
123
125 end() const;
126
127 bool
128 operator==(STPath const& t) const;
129
131 back() const;
132
134 front() const;
135
137 operator[](int i);
138
139 STPathElement const&
140 operator[](int i) const;
141
142 void
143 reserve(size_t s);
144};
145
146//------------------------------------------------------------------------------
147
148// A set of zero or more payment paths
149class STPathSet final : public STBase, public CountedObject<STPathSet>
150{
152
153public:
154 STPathSet() = default;
155
156 STPathSet(SField const& n);
157 STPathSet(SerialIter& sit, SField const& name);
158
159 void
160 add(Serializer& s) const override;
161
162 Json::Value getJson(JsonOptions) const override;
163
165 getSType() const override;
166
167 bool
168 assembleAdd(STPath const& base, STPathElement const& tail);
169
170 bool
171 isEquivalent(STBase const& t) const override;
172
173 bool
174 isDefault() const override;
175
176 // std::vector like interface:
179
182
184 begin() const;
185
187 end() const;
188
190 size() const;
191
192 bool
193 empty() const;
194
195 void
196 push_back(STPath const& e);
197
198 template <typename... Args>
199 void
200 emplace_back(Args&&... args);
201
202private:
203 STBase*
204 copy(std::size_t n, void* buf) const override;
205 STBase*
206 move(std::size_t n, void* buf) override;
207
208 friend class detail::STVar;
209};
210
211// ------------ STPathElement ------------
212
213inline STPathElement::STPathElement() : mType(typeNone), is_offer_(true)
214{
215 hash_value_ = get_hash(*this);
216}
217
219 std::optional<AccountID> const& account,
220 std::optional<Currency> const& currency,
221 std::optional<AccountID> const& issuer)
222 : mType(typeNone)
223{
224 if (!account)
225 {
226 is_offer_ = true;
227 }
228 else
229 {
230 is_offer_ = false;
231 mAccountID = *account;
233 XRPL_ASSERT(mAccountID != noAccount(), "xrpl::STPathElement::STPathElement : account is set");
234 }
235
236 if (currency)
237 {
238 mCurrencyID = *currency;
240 }
241
242 if (issuer)
243 {
244 mIssuerID = *issuer;
245 mType |= typeIssuer;
246 XRPL_ASSERT(mIssuerID != noAccount(), "xrpl::STPathElement::STPathElement : issuer is set");
247 }
248
249 hash_value_ = get_hash(*this);
250}
251
253 AccountID const& account,
254 Currency const& currency,
255 AccountID const& issuer,
256 bool forceCurrency)
257 : mType(typeNone), mAccountID(account), mCurrencyID(currency), mIssuerID(issuer), is_offer_(isXRP(mAccountID))
258{
259 if (!is_offer_)
261
262 if (forceCurrency || !isXRP(currency))
264
265 if (!isXRP(issuer))
266 mType |= typeIssuer;
267
268 hash_value_ = get_hash(*this);
269}
270
272 unsigned int uType,
273 AccountID const& account,
274 Currency const& currency,
275 AccountID const& issuer)
276 : mType(uType), mAccountID(account), mCurrencyID(currency), mIssuerID(issuer), is_offer_(isXRP(mAccountID))
277{
278 hash_value_ = get_hash(*this);
279}
280
281inline auto
283{
284 return mType;
285}
286
287inline bool
289{
290 return is_offer_;
291}
292
293inline bool
295{
296 return !isOffer();
297}
298
299inline bool
304
305inline bool
310
311inline bool
316
317// Nodes are either an account ID or a offer prefix. Offer prefixs denote a
318// class of offers.
319inline AccountID const&
321{
322 return mAccountID;
323}
324
325inline Currency const&
327{
328 return mCurrencyID;
329}
330
331inline AccountID const&
333{
334 return mIssuerID;
335}
336
337inline bool
343
344inline bool
346{
347 return !operator==(t);
348}
349
350// ------------ STPath ------------
351
353{
354}
355
358{
359 return mPath.size();
360}
361
362inline bool
364{
365 return mPath.empty();
366}
367
368inline void
370{
371 mPath.push_back(e);
372}
373
374template <typename... Args>
375inline void
376STPath::emplace_back(Args&&... args)
377{
378 mPath.emplace_back(std::forward<Args>(args)...);
379}
380
383{
384 return mPath.begin();
385}
386
389{
390 return mPath.end();
391}
392
393inline bool
395{
396 return mPath == t.mPath;
397}
398
401{
402 return mPath.back();
403}
404
407{
408 return mPath.front();
409}
410
411inline STPathElement&
413{
414 return mPath[i];
415}
416
417inline STPathElement const&
419{
420 return mPath[i];
421}
422
423inline void
425{
426 mPath.reserve(s);
427}
428
429// ------------ STPathSet ------------
430
431inline STPathSet::STPathSet(SField const& n) : STBase(n)
432{
433}
434
435// std::vector like interface:
441
447
450{
451 return value.begin();
452}
453
456{
457 return value.end();
458}
459
462{
463 return value.size();
464}
465
466inline bool
468{
469 return value.empty();
470}
471
472inline void
474{
475 value.push_back(e);
476}
477
478template <typename... Args>
479inline void
481{
482 value.emplace_back(std::forward<Args>(args)...);
483}
484
485} // namespace xrpl
T back(T... args)
Represents a JSON value.
Definition json_value.h:130
Tracks the number of instances of an object.
Identifies fields.
Definition SField.h:126
A type which can be exported to a well known binary format.
Definition STBase.h:115
auto getNodeType() const
Definition STPathSet.h:282
AccountID mIssuerID
Definition STPathSet.h:20
AccountID const & getAccountID() const
Definition STPathSet.h:320
bool isOffer() const
Definition STPathSet.h:288
std::size_t hash_value_
Definition STPathSet.h:23
AccountID mAccountID
Definition STPathSet.h:18
static std::size_t get_hash(STPathElement const &element)
Definition STPathSet.cpp:21
bool isNone() const
Definition STPathSet.h:312
bool operator!=(STPathElement const &t) const
Definition STPathSet.h:345
Currency mCurrencyID
Definition STPathSet.h:19
unsigned int mType
Definition STPathSet.h:17
STPathElement(STPathElement const &)=default
Currency const & getCurrency() const
Definition STPathSet.h:326
bool operator==(STPathElement const &t) const
Definition STPathSet.h:338
STPathElement & operator=(STPathElement const &)=default
AccountID const & getIssuerID() const
Definition STPathSet.h:332
bool isAccount() const
Definition STPathSet.h:294
bool hasIssuer() const
Definition STPathSet.h:300
bool hasCurrency() const
Definition STPathSet.h:306
void add(Serializer &s) const override
std::vector< STPath >::const_iterator begin() const
Definition STPathSet.h:449
STPathSet()=default
bool assembleAdd(STPath const &base, STPathElement const &tail)
void push_back(STPath const &e)
Definition STPathSet.h:473
std::vector< STPath > value
Definition STPathSet.h:151
std::vector< STPath >::const_reference operator[](std::vector< STPath >::size_type n) const
Definition STPathSet.h:437
STBase * copy(std::size_t n, void *buf) const override
Definition STPathSet.cpp:94
std::vector< STPath >::size_type size() const
Definition STPathSet.h:461
void emplace_back(Args &&... args)
Definition STPathSet.h:480
STBase * move(std::size_t n, void *buf) override
std::vector< STPath >::const_iterator end() const
Definition STPathSet.h:455
bool empty() const
Definition STPathSet.h:467
Json::Value getJson(JsonOptions) const override
bool isEquivalent(STBase const &t) const override
bool isDefault() const override
SerializedTypeID getSType() const override
std::vector< STPathElement >::size_type size() const
Definition STPathSet.h:357
Json::Value getJson(JsonOptions) const
bool empty() const
Definition STPathSet.h:363
void push_back(STPathElement const &e)
Definition STPathSet.h:369
bool operator==(STPath const &t) const
Definition STPathSet.h:394
STPathElement & operator[](int i)
Definition STPathSet.h:412
std::vector< STPathElement >::const_iterator end() const
Definition STPathSet.h:388
bool hasSeen(AccountID const &account, Currency const &currency, AccountID const &issuer) const
void emplace_back(Args &&... args)
Definition STPathSet.h:376
std::vector< STPathElement >::const_reference front() const
Definition STPathSet.h:406
void reserve(size_t s)
Definition STPathSet.h:424
std::vector< STPathElement >::const_reference back() const
Definition STPathSet.h:400
std::vector< STPathElement > mPath
Definition STPathSet.h:96
STPath()=default
std::vector< STPathElement >::const_iterator begin() const
Definition STPathSet.h:382
T front(T... args)
T is_same_v
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isXRP(AccountID const &c)
Definition AccountID.h:70
SerializedTypeID
Definition SField.h:90
AccountID const & noAccount()
A placeholder for empty accounts.
T size(T... args)
Note, should be treated as flags that can be | and &.
Definition STBase.h:17