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 ripple {
15
16class STPathElement final : public CountedObject<STPathElement>
17{
18 unsigned int mType;
22
25
26public:
27 enum Type {
28 typeNone = 0x00,
30 0x01, // Rippling through an account (vs taking an offer).
31 typeCurrency = 0x10, // Currency follows.
32 typeIssuer = 0x20, // Issuer follows.
33 typeBoundary = 0xFF, // Boundary between alternate paths.
35 // Combination of all types.
36 };
37
39 STPathElement(STPathElement const&) = default;
41 operator=(STPathElement const&) = default;
42
44 std::optional<AccountID> const& account,
45 std::optional<Currency> const& currency,
46 std::optional<AccountID> const& issuer);
47
49 AccountID const& account,
50 Currency const& currency,
51 AccountID const& issuer,
52 bool forceCurrency = false);
53
55 unsigned int uType,
56 AccountID const& account,
57 Currency const& currency,
58 AccountID const& issuer);
59
60 auto
61 getNodeType() const;
62
63 bool
64 isOffer() const;
65
66 bool
67 isAccount() const;
68
69 bool
70 hasIssuer() const;
71
72 bool
73 hasCurrency() const;
74
75 bool
76 isNone() const;
77
78 // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
79 // class of offers.
80 AccountID const&
81 getAccountID() const;
82
83 Currency const&
84 getCurrency() const;
85
86 AccountID const&
87 getIssuerID() const;
88
89 bool
90 operator==(STPathElement const& t) const;
91
92 bool
93 operator!=(STPathElement const& t) const;
94
95private:
96 static std::size_t
97 get_hash(STPathElement const& element);
98};
99
100class STPath final : public CountedObject<STPath>
101{
103
104public:
105 STPath() = default;
106
108
110 size() const;
111
112 bool
113 empty() const;
114
115 void
116 push_back(STPathElement const& e);
117
118 template <typename... Args>
119 void
120 emplace_back(Args&&... args);
121
122 bool
123 hasSeen(
124 AccountID const& account,
125 Currency const& currency,
126 AccountID const& issuer) const;
127
129
131 begin() const;
132
134 end() const;
135
136 bool
137 operator==(STPath const& t) const;
138
140 back() const;
141
143 front() const;
144
146 operator[](int i);
147
148 STPathElement const&
149 operator[](int i) const;
150
151 void
152 reserve(size_t s);
153};
154
155//------------------------------------------------------------------------------
156
157// A set of zero or more payment paths
158class STPathSet final : public STBase, public CountedObject<STPathSet>
159{
161
162public:
163 STPathSet() = default;
164
165 STPathSet(SField const& n);
166 STPathSet(SerialIter& sit, SField const& name);
167
168 void
169 add(Serializer& s) const override;
170
171 Json::Value getJson(JsonOptions) const override;
172
174 getSType() const override;
175
176 bool
177 assembleAdd(STPath const& base, STPathElement const& tail);
178
179 bool
180 isEquivalent(STBase const& t) const override;
181
182 bool
183 isDefault() const override;
184
185 // std::vector like interface:
188
191
193 begin() const;
194
196 end() const;
197
199 size() const;
200
201 bool
202 empty() const;
203
204 void
205 push_back(STPath const& e);
206
207 template <typename... Args>
208 void
209 emplace_back(Args&&... args);
210
211private:
212 STBase*
213 copy(std::size_t n, void* buf) const override;
214 STBase*
215 move(std::size_t n, void* buf) override;
216
217 friend class detail::STVar;
218};
219
220// ------------ STPathElement ------------
221
222inline STPathElement::STPathElement() : mType(typeNone), is_offer_(true)
223{
224 hash_value_ = get_hash(*this);
225}
226
228 std::optional<AccountID> const& account,
229 std::optional<Currency> const& currency,
230 std::optional<AccountID> const& issuer)
231 : mType(typeNone)
232{
233 if (!account)
234 {
235 is_offer_ = true;
236 }
237 else
238 {
239 is_offer_ = false;
240 mAccountID = *account;
242 XRPL_ASSERT(
244 "ripple::STPathElement::STPathElement : account is set");
245 }
246
247 if (currency)
248 {
249 mCurrencyID = *currency;
251 }
252
253 if (issuer)
254 {
255 mIssuerID = *issuer;
256 mType |= typeIssuer;
257 XRPL_ASSERT(
258 mIssuerID != noAccount(),
259 "ripple::STPathElement::STPathElement : issuer is set");
260 }
261
262 hash_value_ = get_hash(*this);
263}
264
266 AccountID const& account,
267 Currency const& currency,
268 AccountID const& issuer,
269 bool forceCurrency)
270 : mType(typeNone)
271 , mAccountID(account)
272 , mCurrencyID(currency)
273 , mIssuerID(issuer)
274 , is_offer_(isXRP(mAccountID))
275{
276 if (!is_offer_)
278
279 if (forceCurrency || !isXRP(currency))
281
282 if (!isXRP(issuer))
283 mType |= typeIssuer;
284
285 hash_value_ = get_hash(*this);
286}
287
289 unsigned int uType,
290 AccountID const& account,
291 Currency const& currency,
292 AccountID const& issuer)
293 : mType(uType)
294 , mAccountID(account)
295 , mCurrencyID(currency)
296 , mIssuerID(issuer)
297 , is_offer_(isXRP(mAccountID))
298{
299 hash_value_ = get_hash(*this);
300}
301
302inline auto
304{
305 return mType;
306}
307
308inline bool
310{
311 return is_offer_;
312}
313
314inline bool
316{
317 return !isOffer();
318}
319
320inline bool
325
326inline bool
331
332inline bool
337
338// Nodes are either an account ID or a offer prefix. Offer prefixs denote a
339// class of offers.
340inline AccountID const&
342{
343 return mAccountID;
344}
345
346inline Currency const&
348{
349 return mCurrencyID;
350}
351
352inline AccountID const&
354{
355 return mIssuerID;
356}
357
358inline bool
360{
361 return (mType & typeAccount) == (t.mType & typeAccount) &&
364}
365
366inline bool
368{
369 return !operator==(t);
370}
371
372// ------------ STPath ------------
373
375{
376}
377
380{
381 return mPath.size();
382}
383
384inline bool
386{
387 return mPath.empty();
388}
389
390inline void
392{
393 mPath.push_back(e);
394}
395
396template <typename... Args>
397inline void
398STPath::emplace_back(Args&&... args)
399{
400 mPath.emplace_back(std::forward<Args>(args)...);
401}
402
405{
406 return mPath.begin();
407}
408
411{
412 return mPath.end();
413}
414
415inline bool
417{
418 return mPath == t.mPath;
419}
420
423{
424 return mPath.back();
425}
426
429{
430 return mPath.front();
431}
432
433inline STPathElement&
435{
436 return mPath[i];
437}
438
439inline STPathElement const&
441{
442 return mPath[i];
443}
444
445inline void
447{
448 mPath.reserve(s);
449}
450
451// ------------ STPathSet ------------
452
453inline STPathSet::STPathSet(SField const& n) : STBase(n)
454{
455}
456
457// std::vector like interface:
463
469
472{
473 return value.begin();
474}
475
478{
479 return value.end();
480}
481
484{
485 return value.size();
486}
487
488inline bool
490{
491 return value.empty();
492}
493
494inline void
496{
497 value.push_back(e);
498}
499
500template <typename... Args>
501inline void
503{
504 value.emplace_back(std::forward<Args>(args)...);
505}
506
507} // namespace ripple
508
509#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
bool hasIssuer() const
Definition STPathSet.h:321
Currency const & getCurrency() const
Definition STPathSet.h:347
bool operator==(STPathElement const &t) const
Definition STPathSet.h:359
static std::size_t get_hash(STPathElement const &element)
Definition STPathSet.cpp:21
AccountID const & getAccountID() const
Definition STPathSet.h:341
bool isOffer() const
Definition STPathSet.h:309
AccountID const & getIssuerID() const
Definition STPathSet.h:353
auto getNodeType() const
Definition STPathSet.h:303
STPathElement & operator=(STPathElement const &)=default
unsigned int mType
Definition STPathSet.h:18
bool isNone() const
Definition STPathSet.h:333
bool operator!=(STPathElement const &t) const
Definition STPathSet.h:367
bool isAccount() const
Definition STPathSet.h:315
STPathElement(STPathElement const &)=default
bool hasCurrency() const
Definition STPathSet.h:327
std::size_t hash_value_
Definition STPathSet.h:24
STBase * move(std::size_t n, void *buf) override
bool isDefault() const override
bool empty() const
Definition STPathSet.h:489
std::vector< STPath >::const_iterator end() const
Definition STPathSet.h:477
std::vector< STPath > value
Definition STPathSet.h:160
bool isEquivalent(STBase const &t) const override
STBase * copy(std::size_t n, void *buf) const override
Definition STPathSet.cpp:96
void push_back(STPath const &e)
Definition STPathSet.h:495
STPathSet()=default
bool assembleAdd(STPath const &base, STPathElement const &tail)
std::vector< STPath >::const_iterator begin() const
Definition STPathSet.h:471
Json::Value getJson(JsonOptions) const override
SerializedTypeID getSType() const override
void add(Serializer &s) const override
std::vector< STPath >::const_reference operator[](std::vector< STPath >::size_type n) const
Definition STPathSet.h:459
void emplace_back(Args &&... args)
Definition STPathSet.h:502
std::vector< STPath >::size_type size() const
Definition STPathSet.h:483
void reserve(size_t s)
Definition STPathSet.h:446
bool operator==(STPath const &t) const
Definition STPathSet.h:416
STPath()=default
std::vector< STPathElement >::const_reference front() const
Definition STPathSet.h:428
std::vector< STPathElement >::const_iterator begin() const
Definition STPathSet.h:404
std::vector< STPathElement > mPath
Definition STPathSet.h:102
bool empty() const
Definition STPathSet.h:385
STPathElement & operator[](int i)
Definition STPathSet.h:434
std::vector< STPathElement >::const_iterator end() const
Definition STPathSet.h:410
Json::Value getJson(JsonOptions) const
void push_back(STPathElement const &e)
Definition STPathSet.h:391
bool hasSeen(AccountID const &account, Currency const &currency, AccountID const &issuer) const
std::vector< STPathElement >::size_type size() const
Definition STPathSet.h:379
std::vector< STPathElement >::const_reference back() const
Definition STPathSet.h:422
void emplace_back(Args &&... args)
Definition STPathSet.h:398
T front(T... args)
T is_same_v
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
AccountID const & noAccount()
A placeholder for empty accounts.
bool isXRP(AccountID const &c)
Definition AccountID.h:71
SerializedTypeID
Definition SField.h:91
STL namespace.
T size(T... args)
Note, should be treated as flags that can be | and &.
Definition STBase.h:18