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