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
41 explicit STIssue(SerialIter& sit, SField const& name);
42
43 template <AssetType A>
44 explicit STIssue(SField const& name, A const& issue);
45
46 explicit STIssue(SField const& name);
47
48 template <ValidIssueType TIss>
49 TIss const&
50 get() const;
51
52 template <ValidIssueType TIss>
53 bool
54 holds() const;
55
56 value_type const&
57 value() const noexcept;
58
59 void
60 setIssue(Asset const& issue);
61
63 getSType() const override;
64
65 std::string
66 getText() const override;
67
68 Json::Value getJson(JsonOptions) const override;
69
70 void
71 add(Serializer& s) const override;
72
73 bool
74 isEquivalent(const STBase& t) const override;
75
76 bool
77 isDefault() const override;
78
79 friend constexpr bool
80 operator==(STIssue const& lhs, STIssue const& rhs);
81
82 friend constexpr std::weak_ordering
83 operator<=>(STIssue const& lhs, STIssue const& rhs);
84
85 friend constexpr bool
86 operator==(STIssue const& lhs, Asset const& rhs);
87
88 friend constexpr std::weak_ordering
89 operator<=>(STIssue const& lhs, Asset const& rhs);
90
91private:
92 STBase*
93 copy(std::size_t n, void* buf) const override;
94 STBase*
95 move(std::size_t n, void* buf) override;
96
97 friend class detail::STVar;
98};
99
100template <AssetType A>
101STIssue::STIssue(SField const& name, A const& asset)
102 : STBase{name}, asset_{asset}
103{
104 if (holds<Issue>() && !isConsistent(asset_.get<Issue>()))
105 Throw<std::runtime_error>(
106 "Invalid asset: currency and account native mismatch");
107}
108
110issueFromJson(SField const& name, Json::Value const& v);
111
112template <ValidIssueType TIss>
113bool
115{
116 return asset_.holds<TIss>();
117}
118
119template <ValidIssueType TIss>
120TIss const&
122{
123 if (!holds<TIss>(asset_))
124 Throw<std::runtime_error>("Asset doesn't hold the requested issue");
125 return std::get<TIss>(asset_);
126}
127
128inline STIssue::value_type const&
129STIssue::value() const noexcept
130{
131 return asset_;
132}
133
134inline void
136{
137 if (holds<Issue>() && !isConsistent(asset_.get<Issue>()))
138 Throw<std::runtime_error>(
139 "Invalid asset: currency and account native mismatch");
140
141 asset_ = asset;
142}
143
144constexpr bool
145operator==(STIssue const& lhs, STIssue const& rhs)
146{
147 return lhs.asset_ == rhs.asset_;
148}
149
151operator<=>(STIssue const& lhs, STIssue const& rhs)
152{
153 return lhs.asset_ <=> rhs.asset_;
154}
155
156constexpr bool
157operator==(STIssue const& lhs, Asset const& rhs)
158{
159 return lhs.asset_ == rhs;
160}
161
163operator<=>(STIssue const& lhs, Asset const& rhs)
164{
165 return lhs.asset_ <=> rhs;
166}
167
168} // namespace ripple
169
170#endif
Represents a JSON value.
Definition: json_value.h:147
constexpr TIss const & get() const
constexpr bool holds() const
Definition: Asset.h:119
Tracks the number of instances of an object.
A currency issued by an account.
Definition: Issue.h:36
Identifies fields.
Definition: SField.h:144
A type which can be exported to a well known binary format.
Definition: STBase.h:124
Asset asset_
Definition: STIssue.h:34
void add(Serializer &s) const override
Definition: STIssue.cpp:106
STIssue()=default
friend constexpr bool operator==(STIssue const &lhs, STIssue const &rhs)
Definition: STIssue.h:145
friend constexpr std::weak_ordering operator<=>(STIssue const &lhs, STIssue const &rhs)
Definition: STIssue.h:151
value_type const & value() const noexcept
Definition: STIssue.h:129
friend class detail::STVar
Definition: STIssue.h:97
STBase * copy(std::size_t n, void *buf) const override
Definition: STIssue.cpp:140
Json::Value getJson(JsonOptions) const override
Definition: STIssue.cpp:98
bool isDefault() const override
Definition: STIssue.cpp:134
bool isEquivalent(const STBase &t) const override
Definition: STIssue.cpp:127
void setIssue(Asset const &issue)
Definition: STIssue.h:135
STBase * move(std::size_t n, void *buf) override
Definition: STIssue.cpp:146
SerializedTypeID getSType() const override
Definition: STIssue.cpp:86
std::string getText() const override
Definition: STIssue.cpp:92
TIss const & get() const
bool holds() const
Definition: STIssue.h:114
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:26
Issue const & xrpIssue()
Returns an asset specifier that represents XRP.
Definition: Issue.h:118
bool isConsistent(Book const &book)
Definition: Book.cpp:25
SerializedTypeID
Definition: SField.h:108
Issue issueFromJson(Json::Value const &v)
Definition: Issue.cpp:90
STL namespace.
Note, should be treated as flags that can be | and &.
Definition: STBase.h:36