-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a new serialized type: STNumber #5121
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
This file is part of rippled: https://github.com/ripple/rippled | ||
Copyright (c) 2024 Ripple Labs Inc. | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
//============================================================================== | ||
|
||
#ifndef XRPL_PROTOCOL_STNUMBER_H_INCLUDED | ||
#define XRPL_PROTOCOL_STNUMBER_H_INCLUDED | ||
|
||
#include <xrpl/basics/CountedObject.h> | ||
#include <xrpl/basics/Number.h> | ||
#include <xrpl/protocol/STBase.h> | ||
|
||
#include <ostream> | ||
|
||
namespace ripple { | ||
|
||
/** | ||
* A serializable number. | ||
* | ||
* This type is-a `Number`, and can be used everywhere that is accepted. | ||
* This type simply integrates `Number` with the serialization framework, | ||
* letting it be used for fields in ledger entries and transactions. | ||
* It is effectively an `STAmount` sans `Asset`: | ||
* it can represent a value of any token type (XRP, IOU, or MPT) | ||
* without paying the storage cost of duplicating asset information | ||
* that may be deduced from the context. | ||
*/ | ||
class STNumber : public STBase, public CountedObject<STNumber> | ||
{ | ||
private: | ||
Number value_; | ||
|
||
public: | ||
using value_type = Number; | ||
|
||
STNumber() = default; | ||
explicit STNumber(SField const& field, Number const& value = Number()); | ||
STNumber(SerialIter& sit, SField const& field); | ||
|
||
thejohnfreeman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SerializedTypeID | ||
getSType() const override; | ||
std::string | ||
getText() const override; | ||
void | ||
add(Serializer& s) const override; | ||
|
||
Number const& | ||
value() const; | ||
void | ||
setValue(Number const& v); | ||
thejohnfreeman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
bool | ||
isEquivalent(STBase const& t) const override; | ||
bool | ||
isDefault() const override; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add overload for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't it just use the existing overload for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's ambiguous because both |
||
|
||
operator Number() const | ||
{ | ||
return value_; | ||
} | ||
|
||
private: | ||
STBase* | ||
copy(std::size_t n, void* buf) const override; | ||
STBase* | ||
move(std::size_t n, void* buf) override; | ||
}; | ||
|
||
std::ostream& | ||
operator<<(std::ostream& out, STNumber const& rhs); | ||
|
||
} // namespace ripple | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
//------------------------------------------------------------------------------ | ||
/* | ||
This file is part of rippled: https://github.com/ripple/rippled | ||
Copyright (c) 2023 Ripple Labs Inc. | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
//============================================================================== | ||
|
||
#include <xrpl/protocol/STNumber.h> | ||
|
||
#include <xrpl/protocol/SField.h> | ||
|
||
namespace ripple { | ||
|
||
STNumber::STNumber(SField const& field, Number const& value) | ||
: STBase(field), value_(value) | ||
{ | ||
} | ||
|
||
STNumber::STNumber(SerialIter& sit, SField const& field) : STBase(field) | ||
{ | ||
// We must call these methods in separate statements | ||
// to guarantee their order of execution. | ||
auto mantissa = sit.geti64(); | ||
auto exponent = sit.geti32(); | ||
value_ = Number{mantissa, exponent}; | ||
} | ||
|
||
SerializedTypeID | ||
STNumber::getSType() const | ||
{ | ||
return STI_NUMBER; | ||
} | ||
|
||
std::string | ||
STNumber::getText() const | ||
{ | ||
return to_string(value_); | ||
} | ||
|
||
void | ||
STNumber::add(Serializer& s) const | ||
{ | ||
assert(getFName().isBinary()); | ||
assert(getFName().fieldType == getSType()); | ||
s.add64(value_.mantissa()); | ||
s.add32(value_.exponent()); | ||
} | ||
|
||
Number const& | ||
STNumber::value() const | ||
{ | ||
return value_; | ||
} | ||
|
||
void | ||
STNumber::setValue(Number const& v) | ||
{ | ||
value_ = v; | ||
} | ||
|
||
STBase* | ||
STNumber::copy(std::size_t n, void* buf) const | ||
{ | ||
return emplace(n, buf, *this); | ||
} | ||
|
||
STBase* | ||
STNumber::move(std::size_t n, void* buf) | ||
{ | ||
return emplace(n, buf, std::move(*this)); | ||
} | ||
|
||
bool | ||
STNumber::isEquivalent(STBase const& t) const | ||
{ | ||
assert(t.getSType() == this->getSType()); | ||
STNumber const& v = dynamic_cast<STNumber const&>(t); | ||
return value_ == v; | ||
} | ||
|
||
bool | ||
STNumber::isDefault() const | ||
{ | ||
return value_ == Number(); | ||
} | ||
|
||
std::ostream& | ||
operator<<(std::ostream& out, STNumber const& rhs) | ||
{ | ||
return out << rhs.getText(); | ||
} | ||
|
||
} // namespace ripple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a brief description of the class.