rippled
Loading...
Searching...
No Matches
STPathSet.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef RIPPLE_PROTOCOL_STPATHSET_H_INCLUDED
21#define RIPPLE_PROTOCOL_STPATHSET_H_INCLUDED
22
23#include <xrpl/basics/CountedObject.h>
24#include <xrpl/beast/utility/instrumentation.h>
25#include <xrpl/json/json_value.h>
26#include <xrpl/protocol/SField.h>
27#include <xrpl/protocol/STBase.h>
28#include <xrpl/protocol/UintTypes.h>
29
30#include <cstddef>
31#include <optional>
32
33namespace ripple {
34
35class STPathElement final : public CountedObject<STPathElement>
36{
37 unsigned int mType;
41
44
45public:
46 enum Type {
47 typeNone = 0x00,
49 0x01, // Rippling through an account (vs taking an offer).
50 typeCurrency = 0x10, // Currency follows.
51 typeIssuer = 0x20, // Issuer follows.
52 typeBoundary = 0xFF, // Boundary between alternate paths.
54 // Combination of all types.
55 };
56
58 STPathElement(STPathElement const&) = default;
60 operator=(STPathElement const&) = default;
61
63 std::optional<AccountID> const& account,
64 std::optional<Currency> const& currency,
65 std::optional<AccountID> const& issuer);
66
68 AccountID const& account,
69 Currency const& currency,
70 AccountID const& issuer,
71 bool forceCurrency = false);
72
74 unsigned int uType,
75 AccountID const& account,
76 Currency const& currency,
77 AccountID const& issuer);
78
79 auto
80 getNodeType() const;
81
82 bool
83 isOffer() const;
84
85 bool
86 isAccount() const;
87
88 bool
89 hasIssuer() const;
90
91 bool
92 hasCurrency() const;
93
94 bool
95 isNone() const;
96
97 // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
98 // class of offers.
99 AccountID const&
100 getAccountID() const;
101
102 Currency const&
103 getCurrency() const;
104
105 AccountID const&
106 getIssuerID() 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 get_hash(STPathElement const& element);
117};
118
119class STPath final : public CountedObject<STPath>
120{
122
123public:
124 STPath() = default;
125
127
129 size() const;
130
131 bool
132 empty() const;
133
134 void
135 push_back(STPathElement const& e);
136
137 template <typename... Args>
138 void
139 emplace_back(Args&&... args);
140
141 bool
142 hasSeen(
143 AccountID const& account,
144 Currency const& currency,
145 AccountID const& issuer) const;
146
148
150 begin() const;
151
153 end() const;
154
155 bool
156 operator==(STPath const& t) const;
157
159 back() const;
160
162 front() const;
163
165 operator[](int i);
166
167 STPathElement const&
168 operator[](int i) const;
169
170 void
171 reserve(size_t s);
172};
173
174//------------------------------------------------------------------------------
175
176// A set of zero or more payment paths
177class STPathSet final : public STBase, public CountedObject<STPathSet>
178{
180
181public:
182 STPathSet() = default;
183
184 STPathSet(SField const& n);
185 STPathSet(SerialIter& sit, SField const& name);
186
187 void
188 add(Serializer& s) const override;
189
190 Json::Value getJson(JsonOptions) const override;
191
193 getSType() const override;
194
195 bool
196 assembleAdd(STPath const& base, STPathElement const& tail);
197
198 bool
199 isEquivalent(STBase const& t) const override;
200
201 bool
202 isDefault() const override;
203
204 // std::vector like interface:
207
210
212 begin() const;
213
215 end() const;
216
218 size() const;
219
220 bool
221 empty() const;
222
223 void
224 push_back(STPath const& e);
225
226 template <typename... Args>
227 void
228 emplace_back(Args&&... args);
229
230private:
231 STBase*
232 copy(std::size_t n, void* buf) const override;
233 STBase*
234 move(std::size_t n, void* buf) override;
235
236 friend class detail::STVar;
237};
238
239// ------------ STPathElement ------------
240
241inline STPathElement::STPathElement() : mType(typeNone), is_offer_(true)
242{
243 hash_value_ = get_hash(*this);
244}
245
247 std::optional<AccountID> const& account,
248 std::optional<Currency> const& currency,
249 std::optional<AccountID> const& issuer)
250 : mType(typeNone)
251{
252 if (!account)
253 {
254 is_offer_ = true;
255 }
256 else
257 {
258 is_offer_ = false;
259 mAccountID = *account;
261 XRPL_ASSERT(
263 "ripple::STPathElement::STPathElement : account is set");
264 }
265
266 if (currency)
267 {
268 mCurrencyID = *currency;
270 }
271
272 if (issuer)
273 {
274 mIssuerID = *issuer;
275 mType |= typeIssuer;
276 XRPL_ASSERT(
277 mIssuerID != noAccount(),
278 "ripple::STPathElement::STPathElement : issuer is set");
279 }
280
281 hash_value_ = get_hash(*this);
282}
283
285 AccountID const& account,
286 Currency const& currency,
287 AccountID const& issuer,
288 bool forceCurrency)
289 : mType(typeNone)
290 , mAccountID(account)
291 , mCurrencyID(currency)
292 , mIssuerID(issuer)
293 , is_offer_(isXRP(mAccountID))
294{
295 if (!is_offer_)
297
298 if (forceCurrency || !isXRP(currency))
300
301 if (!isXRP(issuer))
302 mType |= typeIssuer;
303
304 hash_value_ = get_hash(*this);
305}
306
308 unsigned int uType,
309 AccountID const& account,
310 Currency const& currency,
311 AccountID const& issuer)
312 : mType(uType)
313 , mAccountID(account)
314 , mCurrencyID(currency)
315 , mIssuerID(issuer)
316 , is_offer_(isXRP(mAccountID))
317{
318 hash_value_ = get_hash(*this);
319}
320
321inline auto
323{
324 return mType;
325}
326
327inline bool
329{
330 return is_offer_;
331}
332
333inline bool
335{
336 return !isOffer();
337}
338
339inline bool
344
345inline bool
350
351inline bool
356
357// Nodes are either an account ID or a offer prefix. Offer prefixs denote a
358// class of offers.
359inline AccountID const&
361{
362 return mAccountID;
363}
364
365inline Currency const&
367{
368 return mCurrencyID;
369}
370
371inline AccountID const&
373{
374 return mIssuerID;
375}
376
377inline bool
379{
380 return (mType & typeAccount) == (t.mType & typeAccount) &&
383}
384
385inline bool
387{
388 return !operator==(t);
389}
390
391// ------------ STPath ------------
392
394{
395}
396
399{
400 return mPath.size();
401}
402
403inline bool
405{
406 return mPath.empty();
407}
408
409inline void
411{
412 mPath.push_back(e);
413}
414
415template <typename... Args>
416inline void
417STPath::emplace_back(Args&&... args)
418{
419 mPath.emplace_back(std::forward<Args>(args)...);
420}
421
424{
425 return mPath.begin();
426}
427
430{
431 return mPath.end();
432}
433
434inline bool
436{
437 return mPath == t.mPath;
438}
439
442{
443 return mPath.back();
444}
445
448{
449 return mPath.front();
450}
451
452inline STPathElement&
454{
455 return mPath[i];
456}
457
458inline STPathElement const&
460{
461 return mPath[i];
462}
463
464inline void
466{
467 mPath.reserve(s);
468}
469
470// ------------ STPathSet ------------
471
472inline STPathSet::STPathSet(SField const& n) : STBase(n)
473{
474}
475
476// std::vector like interface:
482
488
491{
492 return value.begin();
493}
494
497{
498 return value.end();
499}
500
503{
504 return value.size();
505}
506
507inline bool
509{
510 return value.empty();
511}
512
513inline void
515{
516 value.push_back(e);
517}
518
519template <typename... Args>
520inline void
522{
523 value.emplace_back(std::forward<Args>(args)...);
524}
525
526} // namespace ripple
527
528#endif
T back(T... args)
Represents a JSON value.
Definition json_value.h:149
Tracks the number of instances of an object.
Identifies fields.
Definition SField.h:146
A type which can be exported to a well known binary format.
Definition STBase.h:135
bool hasIssuer() const
Definition STPathSet.h:340
Currency const & getCurrency() const
Definition STPathSet.h:366
bool operator==(STPathElement const &t) const
Definition STPathSet.h:378
static std::size_t get_hash(STPathElement const &element)
Definition STPathSet.cpp:40
AccountID const & getAccountID() const
Definition STPathSet.h:360
bool isOffer() const
Definition STPathSet.h:328
AccountID const & getIssuerID() const
Definition STPathSet.h:372
auto getNodeType() const
Definition STPathSet.h:322
STPathElement & operator=(STPathElement const &)=default
unsigned int mType
Definition STPathSet.h:37
bool isNone() const
Definition STPathSet.h:352
bool operator!=(STPathElement const &t) const
Definition STPathSet.h:386
bool isAccount() const
Definition STPathSet.h:334
STPathElement(STPathElement const &)=default
bool hasCurrency() const
Definition STPathSet.h:346
std::size_t hash_value_
Definition STPathSet.h:43
STBase * move(std::size_t n, void *buf) override
bool isDefault() const override
bool empty() const
Definition STPathSet.h:508
std::vector< STPath >::const_iterator end() const
Definition STPathSet.h:496
std::vector< STPath > value
Definition STPathSet.h:179
bool isEquivalent(STBase const &t) const override
STBase * copy(std::size_t n, void *buf) const override
void push_back(STPath const &e)
Definition STPathSet.h:514
STPathSet()=default
bool assembleAdd(STPath const &base, STPathElement const &tail)
std::vector< STPath >::const_iterator begin() const
Definition STPathSet.h:490
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:478
void emplace_back(Args &&... args)
Definition STPathSet.h:521
std::vector< STPath >::size_type size() const
Definition STPathSet.h:502
void reserve(size_t s)
Definition STPathSet.h:465
bool operator==(STPath const &t) const
Definition STPathSet.h:435
STPath()=default
std::vector< STPathElement >::const_reference front() const
Definition STPathSet.h:447
std::vector< STPathElement >::const_iterator begin() const
Definition STPathSet.h:423
std::vector< STPathElement > mPath
Definition STPathSet.h:121
bool empty() const
Definition STPathSet.h:404
STPathElement & operator[](int i)
Definition STPathSet.h:453
std::vector< STPathElement >::const_iterator end() const
Definition STPathSet.h:429
Json::Value getJson(JsonOptions) const
void push_back(STPathElement const &e)
Definition STPathSet.h:410
bool hasSeen(AccountID const &account, Currency const &currency, AccountID const &issuer) const
std::vector< STPathElement >::size_type size() const
Definition STPathSet.h:398
std::vector< STPathElement >::const_reference back() const
Definition STPathSet.h:441
void emplace_back(Args &&... args)
Definition STPathSet.h:417
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:25
AccountID const & noAccount()
A placeholder for empty accounts.
bool isXRP(AccountID const &c)
Definition AccountID.h:90
SerializedTypeID
Definition SField.h:110
STL namespace.
T size(T... args)
Note, should be treated as flags that can be | and &.
Definition STBase.h:37