rippled
Loading...
Searching...
No Matches
STIssue.h
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2023 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_STISSUE_H_INCLUDED
21#define RIPPLE_PROTOCOL_STISSUE_H_INCLUDED
22
23#include <xrpl/basics/CountedObject.h>
24#include <xrpl/protocol/Asset.h>
25#include <xrpl/protocol/SField.h>
26#include <xrpl/protocol/STBase.h>
27#include <xrpl/protocol/Serializer.h>
28
29namespace ripple {
30
31class STIssue final : public STBase, CountedObject<STIssue>
32{
33private:
35
36public:
38
39 STIssue() = default;
40 STIssue(STIssue const& rhs) = default;
41
42 explicit STIssue(SerialIter& sit, SField const& name);
43
44 template <AssetType A>
45 explicit STIssue(SField const& name, A const& issue);
46
47 explicit STIssue(SField const& name);
48
49 STIssue&
50 operator=(STIssue const& rhs) = default;
51
52 template <ValidIssueType TIss>
53 TIss const&
54 get() const;
55
56 template <ValidIssueType TIss>
57 bool
58 holds() const;
59
60 value_type const&
61 value() const noexcept;
62
63 void
64 setIssue(Asset const& issue);
65
67 getSType() const override;
68
69 std::string
70 getText() const override;
71
72 Json::Value getJson(JsonOptions) const override;
73
74 void
75 add(Serializer& s) const override;
76
77 bool
78 isEquivalent(STBase const& t) const override;
79
80 bool
81 isDefault() const override;
82
83 friend constexpr bool
84 operator==(STIssue const& lhs, STIssue const& rhs);
85
86 friend constexpr std::weak_ordering
87 operator<=>(STIssue const& lhs, STIssue const& rhs);
88
89 friend constexpr bool
90 operator==(STIssue const& lhs, Asset const& rhs);
91
92 friend constexpr std::weak_ordering
93 operator<=>(STIssue const& lhs, Asset const& rhs);
94
95private:
96 STBase*
97 copy(std::size_t n, void* buf) const override;
98 STBase*
99 move(std::size_t n, void* buf) override;
100
101 friend class detail::STVar;
102};
103
104template <AssetType A>
105STIssue::STIssue(SField const& name, A const& asset)
106 : STBase{name}, asset_{asset}
107{
108 if (holds<Issue>() && !isConsistent(asset_.get<Issue>()))
109 Throw<std::runtime_error>(
110 "Invalid asset: currency and account native mismatch");
111}
112
114issueFromJson(SField const& name, Json::Value const& v);
115
116template <ValidIssueType TIss>
117bool
119{
120 return asset_.holds<TIss>();
121}
122
123template <ValidIssueType TIss>
124TIss const&
126{
127 if (!holds<TIss>(asset_))
128 Throw<std::runtime_error>("Asset doesn't hold the requested issue");
129 return std::get<TIss>(asset_);
130}
131
132inline STIssue::value_type const&
133STIssue::value() const noexcept
134{
135 return asset_;
136}
137
138inline void
140{
141 if (holds<Issue>() && !isConsistent(asset_.get<Issue>()))
142 Throw<std::runtime_error>(
143 "Invalid asset: currency and account native mismatch");
144
145 asset_ = asset;
146}
147
148constexpr bool
149operator==(STIssue const& lhs, STIssue const& rhs)
150{
151 return lhs.asset_ == rhs.asset_;
152}
153
155operator<=>(STIssue const& lhs, STIssue const& rhs)
156{
157 return lhs.asset_ <=> rhs.asset_;
158}
159
160constexpr bool
161operator==(STIssue const& lhs, Asset const& rhs)
162{
163 return lhs.asset_ == rhs;
164}
165
167operator<=>(STIssue const& lhs, Asset const& rhs)
168{
169 return lhs.asset_ <=> rhs;
170}
171
172} // namespace ripple
173
174#endif
Represents a JSON value.
Definition json_value.h:149
constexpr TIss const & get() const
constexpr bool holds() const
Definition Asset.h:132
Tracks the number of instances of an object.
A currency issued by an account.
Definition Issue.h:33
Identifies fields.
Definition SField.h:146
A type which can be exported to a well known binary format.
Definition STBase.h:135
STIssue & operator=(STIssue const &rhs)=default
void add(Serializer &s) const override
Definition STIssue.cpp:110
STIssue()=default
friend constexpr bool operator==(STIssue const &lhs, STIssue const &rhs)
Definition STIssue.h:149
friend constexpr std::weak_ordering operator<=>(STIssue const &lhs, STIssue const &rhs)
Definition STIssue.h:155
value_type const & value() const noexcept
Definition STIssue.h:133
friend class detail::STVar
Definition STIssue.h:101
STBase * copy(std::size_t n, void *buf) const override
Definition STIssue.cpp:144
Json::Value getJson(JsonOptions) const override
Definition STIssue.cpp:102
bool isEquivalent(STBase const &t) const override
Definition STIssue.cpp:131
bool isDefault() const override
Definition STIssue.cpp:138
STIssue(STIssue const &rhs)=default
void setIssue(Asset const &issue)
Definition STIssue.h:139
STBase * move(std::size_t n, void *buf) override
Definition STIssue.cpp:150
SerializedTypeID getSType() const override
Definition STIssue.cpp:90
std::string getText() const override
Definition STIssue.cpp:96
TIss const & get() const
bool holds() const
Definition STIssue.h:118
T is_same_v
JSON (JavaScript Object Notation).
Definition json_errors.h:25
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition Issue.h:115
bool isConsistent(Book const &book)
Definition Book.cpp:29
SerializedTypeID
Definition SField.h:110
Issue issueFromJson(Json::Value const &v)
Definition Issue.cpp:95
STL namespace.
Note, should be treated as flags that can be | and &.
Definition STBase.h:37