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
53 unsigned int uType,
54 AccountID const& account,
55 Currency const& currency,
56 AccountID const& issuer);
57
58 auto
59 getNodeType() const;
60
61 bool
62 isOffer() const;
63
64 bool
65 isAccount() const;
66
67 bool
68 hasIssuer() const;
69
70 bool
71 hasCurrency() const;
72
73 bool
74 isNone() const;
75
76 // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
77 // class of offers.
78 AccountID const&
79 getAccountID() const;
80
81 Currency const&
82 getCurrency() const;
83
84 AccountID const&
85 getIssuerID() const;
86
87 bool
88 operator==(STPathElement const& t) const;
89
90 bool
91 operator!=(STPathElement const& t) const;
92
93private:
94 static std::size_t
95 get_hash(STPathElement const& element);
96};
97
98class STPath final : public CountedObject<STPath>
99{
101
102public:
103 STPath() = default;
104
106
108 size() const;
109
110 bool
111 empty() const;
112
113 void
114 push_back(STPathElement const& e);
115
116 template <typename... Args>
117 void
118 emplace_back(Args&&... args);
119
120 bool
121 hasSeen(AccountID const& account, Currency const& currency, AccountID const& issuer) const;
122
124
126 begin() const;
127
129 end() const;
130
131 bool
132 operator==(STPath const& t) const;
133
135 back() const;
136
138 front() const;
139
141 operator[](int i);
142
143 STPathElement const&
144 operator[](int i) const;
145
146 void
147 reserve(size_t s);
148};
149
150//------------------------------------------------------------------------------
151
152// A set of zero or more payment paths
153class STPathSet final : public STBase, public CountedObject<STPathSet>
154{
156
157public:
158 STPathSet() = default;
159
160 STPathSet(SField const& n);
161 STPathSet(SerialIter& sit, SField const& name);
162
163 void
164 add(Serializer& s) const override;
165
166 Json::Value getJson(JsonOptions) const override;
167
169 getSType() const override;
170
171 bool
172 assembleAdd(STPath const& base, STPathElement const& tail);
173
174 bool
175 isEquivalent(STBase const& t) const override;
176
177 bool
178 isDefault() const override;
179
180 // std::vector like interface:
183
186
188 begin() const;
189
191 end() const;
192
194 size() const;
195
196 bool
197 empty() const;
198
199 void
200 push_back(STPath const& e);
201
202 template <typename... Args>
203 void
204 emplace_back(Args&&... args);
205
206private:
207 STBase*
208 copy(std::size_t n, void* buf) const override;
209 STBase*
210 move(std::size_t n, void* buf) override;
211
212 friend class detail::STVar;
213};
214
215// ------------ STPathElement ------------
216
217inline STPathElement::STPathElement() : mType(typeNone), is_offer_(true)
218{
219 hash_value_ = get_hash(*this);
220}
221
223 std::optional<AccountID> const& account,
224 std::optional<Currency> const& currency,
225 std::optional<AccountID> const& issuer)
226 : mType(typeNone)
227{
228 if (!account)
229 {
230 is_offer_ = true;
231 }
232 else
233 {
234 is_offer_ = false;
235 mAccountID = *account;
237 XRPL_ASSERT(
238 mAccountID != noAccount(), "xrpl::STPathElement::STPathElement : account is set");
239 }
240
241 if (currency)
242 {
243 mCurrencyID = *currency;
245 }
246
247 if (issuer)
248 {
249 mIssuerID = *issuer;
250 mType |= typeIssuer;
251 XRPL_ASSERT(mIssuerID != noAccount(), "xrpl::STPathElement::STPathElement : issuer is set");
252 }
253
254 hash_value_ = get_hash(*this);
255}
256
258 AccountID const& account,
259 Currency const& currency,
260 AccountID const& issuer,
261 bool forceCurrency)
262 : mType(typeNone)
263 , mAccountID(account)
264 , mCurrencyID(currency)
265 , mIssuerID(issuer)
266 , is_offer_(isXRP(mAccountID))
267{
268 if (!is_offer_)
270
271 if (forceCurrency || !isXRP(currency))
273
274 if (!isXRP(issuer))
275 mType |= typeIssuer;
276
277 hash_value_ = get_hash(*this);
278}
279
281 unsigned int uType,
282 AccountID const& account,
283 Currency const& currency,
284 AccountID const& issuer)
285 : mType(uType)
286 , mAccountID(account)
287 , mCurrencyID(currency)
288 , mIssuerID(issuer)
289 , is_offer_(isXRP(mAccountID))
290{
291 hash_value_ = get_hash(*this);
292}
293
294inline auto
296{
297 return mType;
298}
299
300inline bool
302{
303 return is_offer_;
304}
305
306inline bool
308{
309 return !isOffer();
310}
311
312inline bool
317
318inline bool
323
324inline bool
329
330// Nodes are either an account ID or a offer prefix. Offer prefixs denote a
331// class of offers.
332inline AccountID const&
334{
335 return mAccountID;
336}
337
338inline Currency const&
340{
341 return mCurrencyID;
342}
343
344inline AccountID const&
346{
347 return mIssuerID;
348}
349
350inline bool
356
357inline bool
359{
360 return !operator==(t);
361}
362
363// ------------ STPath ------------
364
366{
367}
368
371{
372 return mPath.size();
373}
374
375inline bool
377{
378 return mPath.empty();
379}
380
381inline void
383{
384 mPath.push_back(e);
385}
386
387template <typename... Args>
388inline void
389STPath::emplace_back(Args&&... args)
390{
391 mPath.emplace_back(std::forward<Args>(args)...);
392}
393
396{
397 return mPath.begin();
398}
399
402{
403 return mPath.end();
404}
405
406inline bool
408{
409 return mPath == t.mPath;
410}
411
414{
415 return mPath.back();
416}
417
420{
421 return mPath.front();
422}
423
424inline STPathElement&
426{
427 return mPath[i];
428}
429
430inline STPathElement const&
432{
433 return mPath[i];
434}
435
436inline void
438{
439 mPath.reserve(s);
440}
441
442// ------------ STPathSet ------------
443
444inline STPathSet::STPathSet(SField const& n) : STBase(n)
445{
446}
447
448// std::vector like interface:
454
460
463{
464 return value.begin();
465}
466
469{
470 return value.end();
471}
472
475{
476 return value.size();
477}
478
479inline bool
481{
482 return value.empty();
483}
484
485inline void
487{
488 value.push_back(e);
489}
490
491template <typename... Args>
492inline void
494{
495 value.emplace_back(std::forward<Args>(args)...);
496}
497
498} // 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:295
AccountID mIssuerID
Definition STPathSet.h:20
AccountID const & getAccountID() const
Definition STPathSet.h:333
bool isOffer() const
Definition STPathSet.h:301
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:325
bool operator!=(STPathElement const &t) const
Definition STPathSet.h:358
Currency mCurrencyID
Definition STPathSet.h:19
unsigned int mType
Definition STPathSet.h:17
STPathElement(STPathElement const &)=default
Currency const & getCurrency() const
Definition STPathSet.h:339
bool operator==(STPathElement const &t) const
Definition STPathSet.h:351
STPathElement & operator=(STPathElement const &)=default
AccountID const & getIssuerID() const
Definition STPathSet.h:345
bool isAccount() const
Definition STPathSet.h:307
bool hasIssuer() const
Definition STPathSet.h:313
bool hasCurrency() const
Definition STPathSet.h:319
void add(Serializer &s) const override
std::vector< STPath >::const_iterator begin() const
Definition STPathSet.h:462
STPathSet()=default
bool assembleAdd(STPath const &base, STPathElement const &tail)
void push_back(STPath const &e)
Definition STPathSet.h:486
std::vector< STPath > value
Definition STPathSet.h:155
std::vector< STPath >::const_reference operator[](std::vector< STPath >::size_type n) const
Definition STPathSet.h:450
STBase * copy(std::size_t n, void *buf) const override
Definition STPathSet.cpp:94
std::vector< STPath >::size_type size() const
Definition STPathSet.h:474
void emplace_back(Args &&... args)
Definition STPathSet.h:493
STBase * move(std::size_t n, void *buf) override
std::vector< STPath >::const_iterator end() const
Definition STPathSet.h:468
bool empty() const
Definition STPathSet.h:480
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:370
Json::Value getJson(JsonOptions) const
bool empty() const
Definition STPathSet.h:376
void push_back(STPathElement const &e)
Definition STPathSet.h:382
bool operator==(STPath const &t) const
Definition STPathSet.h:407
STPathElement & operator[](int i)
Definition STPathSet.h:425
std::vector< STPathElement >::const_iterator end() const
Definition STPathSet.h:401
bool hasSeen(AccountID const &account, Currency const &currency, AccountID const &issuer) const
void emplace_back(Args &&... args)
Definition STPathSet.h:389
std::vector< STPathElement >::const_reference front() const
Definition STPathSet.h:419
void reserve(size_t s)
Definition STPathSet.h:437
std::vector< STPathElement >::const_reference back() const
Definition STPathSet.h:413
std::vector< STPathElement > mPath
Definition STPathSet.h:100
STPath()=default
std::vector< STPathElement >::const_iterator begin() const
Definition STPathSet.h:395
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