xrpld
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/Asset.h>
7#include <xrpl/protocol/PathAsset.h>
8#include <xrpl/protocol/SField.h>
9#include <xrpl/protocol/STBase.h>
10#include <xrpl/protocol/UintTypes.h>
11
12#include <cstddef>
13#include <optional>
14
15namespace xrpl {
16
17class STPathElement final : public CountedObject<STPathElement>
18{
19 unsigned int type_;
23
26
27public:
28 // Bitwise values (typeCurrency | typeMPT)
29 // NOLINTNEXTLINE(cppcoreguidelines-use-enum-class)
30 enum Type {
31 TypeNone = 0x00,
32 TypeAccount = 0x01, // Rippling through an account (vs taking an offer).
33 TypeCurrency = 0x10, // Currency follows.
34 TypeIssuer = 0x20, // Issuer follows.
35 TypeMpt = 0x40, // MPT follows.
36 TypeBoundary = 0xFF, // Boundary between alternate paths.
39 // Combination of all types.
40 };
41
43 STPathElement(STPathElement const&) = default;
45 operator=(STPathElement const&) = default;
46
48 std::optional<AccountID> const& account,
49 std::optional<PathAsset> const& asset,
50 std::optional<AccountID> const& issuer);
51
53 AccountID const& account,
54 PathAsset const& asset,
55 AccountID const& issuer,
56 bool forceAsset = false);
57
59 unsigned int uType,
60 AccountID const& account,
61 PathAsset const& asset,
62 AccountID const& issuer);
63
64 [[nodiscard]] auto
65 getNodeType() const;
66
67 [[nodiscard]] bool
68 isOffer() const;
69
70 [[nodiscard]] bool
71 isAccount() const;
72
73 [[nodiscard]] bool
74 hasIssuer() const;
75
76 [[nodiscard]] bool
77 hasCurrency() const;
78
79 [[nodiscard]] bool
80 hasMPT() const;
81
82 [[nodiscard]] bool
83 hasAsset() const;
84
85 [[nodiscard]] bool
86 isNone() const;
87
88 // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
89 // class of offers.
90 [[nodiscard]] AccountID const&
91 getAccountID() const;
92
93 [[nodiscard]] PathAsset const&
94 getPathAsset() const;
95
96 [[nodiscard]] Currency const&
97 getCurrency() const;
98
99 [[nodiscard]] MPTID const&
100 getMPTID() const;
101
102 [[nodiscard]] AccountID const&
103 getIssuerID() const;
104
105 [[nodiscard]] bool
106 isType(Type const& pe) const;
107
108 bool
109 operator==(STPathElement const& t) const;
110
111 bool
112 operator!=(STPathElement const& t) const;
113
114private:
115 static std::size_t
116 getHash(STPathElement const& element);
117};
118
119class STPath final : public CountedObject<STPath>
120{
122
123public:
124 STPath() = default;
125
127
129 size() const;
130
131 [[nodiscard]] bool
132 empty() const;
133
134 void
135 pushBack(STPathElement const& e);
136
137 template <typename... Args>
138 void
139 emplaceBack(Args&&... args);
140
141 [[nodiscard]] bool
142 hasSeen(AccountID const& account, PathAsset const& asset, AccountID const& issuer) const;
143
144 [[nodiscard]] json::Value getJson(JsonOptions) const;
145
146 [[nodiscard]] std::vector<STPathElement>::const_iterator
147 begin() const;
148
149 [[nodiscard]] std::vector<STPathElement>::const_iterator
150 end() const;
151
152 bool
153 operator==(STPath const& t) const;
154
156 back() const;
157
159 front() const;
160
162 operator[](int i);
163
164 STPathElement const&
165 operator[](int i) const;
166
167 void
168 reserve(size_t s);
169};
170
171//------------------------------------------------------------------------------
172
173// A set of zero or more payment paths
174class STPathSet final : public STBase, public CountedObject<STPathSet>
175{
177
178public:
179 STPathSet() = default;
180
181 STPathSet(SField const& n);
182 STPathSet(SerialIter& sit, SField const& name);
183
184 void
185 add(Serializer& s) const override;
186
187 [[nodiscard]] json::Value getJson(JsonOptions) const override;
188
189 [[nodiscard]] SerializedTypeID
190 getSType() const override;
191
192 bool
193 assembleAdd(STPath const& base, STPathElement const& tail);
194
195 [[nodiscard]] bool
196 isEquivalent(STBase const& t) const override;
197
198 [[nodiscard]] bool
199 isDefault() const override;
200
201 // std::vector like interface:
204
207
208 [[nodiscard]] std::vector<STPath>::const_iterator
209 begin() const;
210
211 [[nodiscard]] std::vector<STPath>::const_iterator
212 end() const;
213
215 size() const;
216
217 [[nodiscard]] bool
218 empty() const;
219
220 void
221 pushBack(STPath const& e);
222
223 template <typename... Args>
224 void
225 emplaceBack(Args&&... args);
226
227private:
228 STBase*
229 copy(std::size_t n, void* buf) const override;
230 STBase*
231 move(std::size_t n, void* buf) override;
232
233 friend class detail::STVar;
234};
235
236// ------------ STPathElement ------------
237
239{
240 hashValue_ = getHash(*this);
241}
242
244 std::optional<AccountID> const& account,
245 std::optional<PathAsset> const& asset,
246 std::optional<AccountID> const& issuer)
247 : type_(TypeNone)
248{
249 if (!account)
250 {
251 isOffer_ = true;
252 }
253 else
254 {
255 isOffer_ = false;
256 accountID_ = *account;
258 XRPL_ASSERT(
259 accountID_ != noAccount(), "xrpl::STPathElement::STPathElement : account is set");
260 }
261
262 if (asset)
263 {
264 assetID_ = *asset;
266 }
267
268 if (issuer)
269 {
270 issuerID_ = *issuer;
271 type_ |= TypeIssuer;
272 XRPL_ASSERT(issuerID_ != noAccount(), "xrpl::STPathElement::STPathElement : issuer is set");
273 }
274
275 hashValue_ = getHash(*this);
276}
277
279 AccountID const& account,
280 PathAsset const& asset,
281 AccountID const& issuer,
282 bool forceAsset)
283 : type_(TypeNone)
284 , accountID_(account)
285 , assetID_(asset)
286 , issuerID_(issuer)
288{
289 if (!isOffer_)
291
292 if (forceAsset || !isXRP(assetID_))
293 type_ |= asset.holds<Currency>() ? TypeCurrency : TypeMpt;
294
295 if (!isXRP(issuer))
296 type_ |= TypeIssuer;
297
298 hashValue_ = getHash(*this);
299}
300
302 unsigned int uType,
303 AccountID const& account,
304 PathAsset const& asset,
305 AccountID const& issuer)
306 : type_(uType)
307 , accountID_(account)
308 , assetID_(asset)
309 , issuerID_(issuer)
311{
312 assetID_.visit(
313 [&](Currency const&) { type_ = type_ & (~Type::TypeMpt); },
314 [&](MPTID const&) { type_ = type_ & (~Type::TypeCurrency); });
315 hashValue_ = getHash(*this);
316}
317
318inline auto
320{
321 return type_;
322}
323
324inline bool
326{
327 return isOffer_;
328}
329
330inline bool
332{
333 return !isOffer();
334}
335
336inline bool
338{
339 return (type_ & pe) != 0u;
340}
341
342inline bool
347
348inline bool
353
354inline bool
359
360inline bool
365
366inline bool
371
372// Nodes are either an account ID or a offer prefix. Offer prefixs denote a
373// class of offers.
374inline AccountID const&
376{
377 return accountID_;
378}
379
380inline PathAsset const&
382{
383 return assetID_;
384}
385
386inline Currency const&
388{
389 return assetID_.get<Currency>();
390}
391
392inline MPTID const&
394{
395 return assetID_.get<MPTID>();
396}
397
398inline AccountID const&
400{
401 return issuerID_;
402}
403
404inline bool
406{
407 return (type_ & TypeAccount) == (t.type_ & TypeAccount) && hashValue_ == t.hashValue_ &&
409}
410
411inline bool
413{
414 return !operator==(t);
415}
416
417// ------------ STPath ------------
418
420{
421}
422
425{
426 return path_.size();
427}
428
429inline bool
431{
432 return path_.empty();
433}
434
435inline void
437{
438 path_.push_back(e);
439}
440
441template <typename... Args>
442inline void
443STPath::emplaceBack(Args&&... args)
444{
445 path_.emplace_back(std::forward<Args>(args)...);
446}
447
448inline std::vector<STPathElement>::const_iterator
450{
451 return path_.begin();
452}
453
454inline std::vector<STPathElement>::const_iterator
456{
457 return path_.end();
458}
459
460inline bool
462{
463 return path_ == t.path_;
464}
465
468{
469 return path_.back();
470}
471
474{
475 return path_.front();
476}
477
478inline STPathElement&
480{
481 return path_[i];
482}
483
484inline STPathElement const&
486{
487 return path_[i];
488}
489
490inline void
492{
493 path_.reserve(s);
494}
495
496// ------------ STPathSet ------------
497
498inline STPathSet::STPathSet(SField const& n) : STBase(n)
499{
500}
501
502// std::vector like interface:
508
514
515inline std::vector<STPath>::const_iterator
517{
518 return value_.begin();
519}
520
521inline std::vector<STPath>::const_iterator
523{
524 return value_.end();
525}
526
529{
530 return value_.size();
531}
532
533inline bool
535{
536 return value_.empty();
537}
538
539inline void
541{
542 value_.push_back(e);
543}
544
545template <typename... Args>
546inline void
548{
549 value_.emplace_back(std::forward<Args>(args)...);
550}
551
552} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
constexpr bool holds() const
Definition PathAsset.h:69
Identifies fields.
Definition SField.h:130
auto getNodeType() const
Definition STPathSet.h:319
std::size_t hashValue_
Definition STPathSet.h:25
static std::size_t getHash(STPathElement const &element)
Definition STPathSet.cpp:24
AccountID const & getAccountID() const
Definition STPathSet.h:375
bool isOffer() const
Definition STPathSet.h:325
AccountID issuerID_
Definition STPathSet.h:22
unsigned int type_
Definition STPathSet.h:19
bool isNone() const
Definition STPathSet.h:367
PathAsset const & getPathAsset() const
Definition STPathSet.h:381
AccountID accountID_
Definition STPathSet.h:20
bool operator!=(STPathElement const &t) const
Definition STPathSet.h:412
MPTID const & getMPTID() const
Definition STPathSet.h:393
STPathElement(STPathElement const &)=default
bool hasAsset() const
Definition STPathSet.h:361
Currency const & getCurrency() const
Definition STPathSet.h:387
bool operator==(STPathElement const &t) const
Definition STPathSet.h:405
STPathElement & operator=(STPathElement const &)=default
bool hasMPT() const
Definition STPathSet.h:355
AccountID const & getIssuerID() const
Definition STPathSet.h:399
bool isAccount() const
Definition STPathSet.h:331
bool hasIssuer() const
Definition STPathSet.h:343
PathAsset assetID_
Definition STPathSet.h:21
bool isType(Type const &pe) const
Definition STPathSet.h:337
bool hasCurrency() const
Definition STPathSet.h:349
void add(Serializer &s) const override
std::vector< STPath >::const_iterator begin() const
Definition STPathSet.h:516
STPathSet()=default
bool assembleAdd(STPath const &base, STPathElement const &tail)
void emplaceBack(Args &&... args)
Definition STPathSet.h:547
std::vector< STPath >::const_reference operator[](std::vector< STPath >::size_type n) const
Definition STPathSet.h:504
STBase * copy(std::size_t n, void *buf) const override
std::vector< STPath >::size_type size() const
Definition STPathSet.h:528
json::Value getJson(JsonOptions) const override
void pushBack(STPath const &e)
Definition STPathSet.h:540
std::vector< STPath > value_
Definition STPathSet.h:176
STBase * move(std::size_t n, void *buf) override
std::vector< STPath >::const_iterator end() const
Definition STPathSet.h:522
bool empty() const
Definition STPathSet.h:534
bool isEquivalent(STBase const &t) const override
bool isDefault() const override
SerializedTypeID getSType() const override
std::vector< STPathElement > path_
Definition STPathSet.h:121
std::vector< STPathElement >::size_type size() const
Definition STPathSet.h:424
bool hasSeen(AccountID const &account, PathAsset const &asset, AccountID const &issuer) const
bool empty() const
Definition STPathSet.h:430
bool operator==(STPath const &t) const
Definition STPathSet.h:461
STPathElement & operator[](int i)
Definition STPathSet.h:479
void pushBack(STPathElement const &e)
Definition STPathSet.h:436
std::vector< STPathElement >::const_iterator end() const
Definition STPathSet.h:455
std::vector< STPathElement >::const_reference front() const
Definition STPathSet.h:473
void reserve(size_t s)
Definition STPathSet.h:491
void emplaceBack(Args &&... args)
Definition STPathSet.h:443
std::vector< STPathElement >::const_reference back() const
Definition STPathSet.h:467
STPath()=default
std::vector< STPathElement >::const_iterator begin() const
Definition STPathSet.h:449
json::Value getJson(JsonOptions) const
T forward(T... args)
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
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:36
SerializedTypeID
Definition SField.h:93
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition UintTypes.h:44
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
AccountID const & noAccount()
A placeholder for empty accounts.
Note, should be treated as flags that can be | and &.
Definition STBase.h:17