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#include <cstddef>
30#include <optional>
31
32namespace ripple {
33
34class STPathElement final : public CountedObject<STPathElement>
35{
36 unsigned int mType;
40
43
44public:
45 enum Type {
46 typeNone = 0x00,
48 0x01, // Rippling through an account (vs taking an offer).
49 typeCurrency = 0x10, // Currency follows.
50 typeIssuer = 0x20, // Issuer follows.
51 typeBoundary = 0xFF, // Boundary between alternate paths.
53 // Combination of all types.
54 };
55
57 STPathElement(STPathElement const&) = default;
59 operator=(STPathElement const&) = default;
60
62 std::optional<AccountID> const& account,
63 std::optional<Currency> const& currency,
64 std::optional<AccountID> const& issuer);
65
67 AccountID const& account,
68 Currency const& currency,
69 AccountID const& issuer,
70 bool forceCurrency = false);
71
73 unsigned int uType,
74 AccountID const& account,
75 Currency const& currency,
76 AccountID const& issuer);
77
78 auto
79 getNodeType() const;
80
81 bool
82 isOffer() const;
83
84 bool
85 isAccount() const;
86
87 bool
88 hasIssuer() const;
89
90 bool
91 hasCurrency() const;
92
93 bool
94 isNone() const;
95
96 // Nodes are either an account ID or a offer prefix. Offer prefixs denote a
97 // class of offers.
98 AccountID const&
99 getAccountID() const;
100
101 Currency const&
102 getCurrency() const;
103
104 AccountID const&
105 getIssuerID() const;
106
107 bool
108 operator==(const STPathElement& t) const;
109
110 bool
111 operator!=(const STPathElement& t) const;
112
113private:
114 static std::size_t
115 get_hash(STPathElement const& element);
116};
117
118class STPath final : public CountedObject<STPath>
119{
121
122public:
123 STPath() = default;
124
126
128 size() const;
129
130 bool
131 empty() const;
132
133 void
134 push_back(STPathElement const& e);
135
136 template <typename... Args>
137 void
138 emplace_back(Args&&... args);
139
140 bool
141 hasSeen(
142 AccountID const& account,
143 Currency const& currency,
144 AccountID const& issuer) const;
145
147
149 begin() const;
150
152 end() const;
153
154 bool
155 operator==(STPath const& t) const;
156
158 back() const;
159
161 front() const;
162
164 operator[](int i);
165
166 const STPathElement&
167 operator[](int i) const;
168
169 void
170 reserve(size_t s);
171};
172
173//------------------------------------------------------------------------------
174
175// A set of zero or more payment paths
176class STPathSet final : public STBase, public CountedObject<STPathSet>
177{
179
180public:
181 STPathSet() = default;
182
183 STPathSet(SField const& n);
184 STPathSet(SerialIter& sit, SField const& name);
185
186 void
187 add(Serializer& s) const override;
188
189 Json::Value getJson(JsonOptions) const override;
190
192 getSType() const override;
193
194 bool
195 assembleAdd(STPath const& base, STPathElement const& tail);
196
197 bool
198 isEquivalent(const STBase& t) const override;
199
200 bool
201 isDefault() const override;
202
203 // std::vector like interface:
206
209
211 begin() const;
212
214 end() const;
215
217 size() const;
218
219 bool
220 empty() const;
221
222 void
223 push_back(STPath const& e);
224
225 template <typename... Args>
226 void
227 emplace_back(Args&&... args);
228
229private:
230 STBase*
231 copy(std::size_t n, void* buf) const override;
232 STBase*
233 move(std::size_t n, void* buf) override;
234
235 friend class detail::STVar;
236};
237
238// ------------ STPathElement ------------
239
240inline STPathElement::STPathElement() : mType(typeNone), is_offer_(true)
241{
242 hash_value_ = get_hash(*this);
243}
244
246 std::optional<AccountID> const& account,
247 std::optional<Currency> const& currency,
248 std::optional<AccountID> const& issuer)
249 : mType(typeNone)
250{
251 if (!account)
252 {
253 is_offer_ = true;
254 }
255 else
256 {
257 is_offer_ = false;
258 mAccountID = *account;
260 XRPL_ASSERT(
262 "ripple::STPathElement::STPathElement : account is set");
263 }
264
265 if (currency)
266 {
267 mCurrencyID = *currency;
269 }
270
271 if (issuer)
272 {
273 mIssuerID = *issuer;
274 mType |= typeIssuer;
275 XRPL_ASSERT(
276 mIssuerID != noAccount(),
277 "ripple::STPathElement::STPathElement : issuer is set");
278 }
279
280 hash_value_ = get_hash(*this);
281}
282
284 AccountID const& account,
285 Currency const& currency,
286 AccountID const& issuer,
287 bool forceCurrency)
288 : mType(typeNone)
289 , mAccountID(account)
290 , mCurrencyID(currency)
291 , mIssuerID(issuer)
292 , is_offer_(isXRP(mAccountID))
293{
294 if (!is_offer_)
296
297 if (forceCurrency || !isXRP(currency))
299
300 if (!isXRP(issuer))
301 mType |= typeIssuer;
302
303 hash_value_ = get_hash(*this);
304}
305
307 unsigned int uType,
308 AccountID const& account,
309 Currency const& currency,
310 AccountID const& issuer)
311 : mType(uType)
312 , mAccountID(account)
313 , mCurrencyID(currency)
314 , mIssuerID(issuer)
315 , is_offer_(isXRP(mAccountID))
316{
317 hash_value_ = get_hash(*this);
318}
319
320inline auto
322{
323 return mType;
324}
325
326inline bool
328{
329 return is_offer_;
330}
331
332inline bool
334{
335 return !isOffer();
336}
337
338inline bool
340{
342}
343
344inline bool
346{
348}
349
350inline bool
352{
354}
355
356// Nodes are either an account ID or a offer prefix. Offer prefixs denote a
357// class of offers.
358inline AccountID const&
360{
361 return mAccountID;
362}
363
364inline Currency const&
366{
367 return mCurrencyID;
368}
369
370inline AccountID const&
372{
373 return mIssuerID;
374}
375
376inline bool
378{
379 return (mType & typeAccount) == (t.mType & typeAccount) &&
382}
383
384inline bool
386{
387 return !operator==(t);
388}
389
390// ------------ STPath ------------
391
393{
394}
395
398{
399 return mPath.size();
400}
401
402inline bool
404{
405 return mPath.empty();
406}
407
408inline void
410{
411 mPath.push_back(e);
412}
413
414template <typename... Args>
415inline void
416STPath::emplace_back(Args&&... args)
417{
418 mPath.emplace_back(std::forward<Args>(args)...);
419}
420
423{
424 return mPath.begin();
425}
426
429{
430 return mPath.end();
431}
432
433inline bool
435{
436 return mPath == t.mPath;
437}
438
441{
442 return mPath.back();
443}
444
447{
448 return mPath.front();
449}
450
451inline STPathElement&
453{
454 return mPath[i];
455}
456
457inline const STPathElement&
459{
460 return mPath[i];
461}
462
463inline void
465{
466 mPath.reserve(s);
467}
468
469// ------------ STPathSet ------------
470
471inline STPathSet::STPathSet(SField const& n) : STBase(n)
472{
473}
474
475// std::vector like interface:
478{
479 return value[n];
480}
481
484{
485 return value[n];
486}
487
490{
491 return value.begin();
492}
493
496{
497 return value.end();
498}
499
502{
503 return value.size();
504}
505
506inline bool
508{
509 return value.empty();
510}
511
512inline void
514{
515 value.push_back(e);
516}
517
518template <typename... Args>
519inline void
521{
522 value.emplace_back(std::forward<Args>(args)...);
523}
524
525} // namespace ripple
526
527#endif
Represents a JSON value.
Definition: json_value.h:147
Tracks the number of instances of an object.
Identifies fields.
Definition: SField.h:144
A type which can be exported to a well known binary format.
Definition: STBase.h:124
bool hasIssuer() const
Definition: STPathSet.h:339
Currency const & getCurrency() const
Definition: STPathSet.h:365
static std::size_t get_hash(STPathElement const &element)
Definition: STPathSet.cpp:30
AccountID const & getAccountID() const
Definition: STPathSet.h:359
bool isOffer() const
Definition: STPathSet.h:327
AccountID const & getIssuerID() const
Definition: STPathSet.h:371
auto getNodeType() const
Definition: STPathSet.h:321
STPathElement & operator=(STPathElement const &)=default
unsigned int mType
Definition: STPathSet.h:36
Currency mCurrencyID
Definition: STPathSet.h:38
bool operator!=(const STPathElement &t) const
Definition: STPathSet.h:385
AccountID mIssuerID
Definition: STPathSet.h:39
bool isNone() const
Definition: STPathSet.h:351
bool isAccount() const
Definition: STPathSet.h:333
AccountID mAccountID
Definition: STPathSet.h:37
bool operator==(const STPathElement &t) const
Definition: STPathSet.h:377
STPathElement(STPathElement const &)=default
bool hasCurrency() const
Definition: STPathSet.h:345
std::size_t hash_value_
Definition: STPathSet.h:42
STBase * move(std::size_t n, void *buf) override
Definition: STPathSet.cpp:111
bool isDefault() const override
Definition: STPathSet.cpp:145
bool empty() const
Definition: STPathSet.h:507
std::vector< STPath >::const_iterator end() const
Definition: STPathSet.h:495
std::vector< STPath > value
Definition: STPathSet.h:178
STBase * copy(std::size_t n, void *buf) const override
Definition: STPathSet.cpp:105
void push_back(STPath const &e)
Definition: STPathSet.h:513
STPathSet()=default
bool assembleAdd(STPath const &base, STPathElement const &tail)
Definition: STPathSet.cpp:117
std::vector< STPath >::const_iterator begin() const
Definition: STPathSet.h:489
Json::Value getJson(JsonOptions) const override
Definition: STPathSet.cpp:194
bool isEquivalent(const STBase &t) const override
Definition: STPathSet.cpp:138
SerializedTypeID getSType() const override
Definition: STPathSet.cpp:204
void add(Serializer &s) const override
Definition: STPathSet.cpp:210
std::vector< STPath >::const_reference operator[](std::vector< STPath >::size_type n) const
Definition: STPathSet.h:477
void emplace_back(Args &&... args)
Definition: STPathSet.h:520
std::vector< STPath >::size_type size() const
Definition: STPathSet.h:501
void reserve(size_t s)
Definition: STPathSet.h:464
bool operator==(STPath const &t) const
Definition: STPathSet.h:434
STPath()=default
std::vector< STPathElement >::const_reference front() const
Definition: STPathSet.h:446
std::vector< STPathElement >::const_iterator begin() const
Definition: STPathSet.h:422
std::vector< STPathElement > mPath
Definition: STPathSet.h:120
bool empty() const
Definition: STPathSet.h:403
STPathElement & operator[](int i)
Definition: STPathSet.h:452
std::vector< STPathElement >::const_iterator end() const
Definition: STPathSet.h:428
Json::Value getJson(JsonOptions) const
Definition: STPathSet.cpp:167
void push_back(STPathElement const &e)
Definition: STPathSet.h:409
bool hasSeen(AccountID const &account, Currency const &currency, AccountID const &issuer) const
Definition: STPathSet.cpp:151
std::vector< STPathElement >::size_type size() const
Definition: STPathSet.h:397
std::vector< STPathElement >::const_reference back() const
Definition: STPathSet.h:440
void emplace_back(Args &&... args)
Definition: STPathSet.h:416
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
AccountID const & noAccount()
A placeholder for empty accounts.
Definition: AccountID.cpp:177
bool isXRP(AccountID const &c)
Definition: AccountID.h:91
SerializedTypeID
Definition: SField.h:108
STL namespace.
Note, should be treated as flags that can be | and &.
Definition: STBase.h:36