rippled
Loading...
Searching...
No Matches
STNumber.cpp
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#include <xrpl/protocol/STNumber.h>
21
22#include <xrpl/beast/utility/instrumentation.h>
23#include <xrpl/protocol/SField.h>
24
25namespace ripple {
26
27STNumber::STNumber(SField const& field, Number const& value)
28 : STBase(field), value_(value)
29{
30}
31
32STNumber::STNumber(SerialIter& sit, SField const& field) : STBase(field)
33{
34 // We must call these methods in separate statements
35 // to guarantee their order of execution.
36 auto mantissa = sit.geti64();
37 auto exponent = sit.geti32();
38 value_ = Number{mantissa, exponent};
39}
40
43{
44 return STI_NUMBER;
45}
46
49{
50 return to_string(value_);
51}
52
53void
55{
56 XRPL_ASSERT(
57 getFName().isBinary(), "ripple::STNumber::add : field is binary");
58 XRPL_ASSERT(
59 getFName().fieldType == getSType(),
60 "ripple::STNumber::add : field type match");
63}
64
65Number const&
67{
68 return value_;
69}
70
71void
73{
74 value_ = v;
75}
76
77STBase*
78STNumber::copy(std::size_t n, void* buf) const
79{
80 return emplace(n, buf, *this);
81}
82
83STBase*
85{
86 return emplace(n, buf, std::move(*this));
87}
88
89bool
91{
92 XRPL_ASSERT(
93 t.getSType() == this->getSType(),
94 "ripple::STNumber::isEquivalent : field type match");
95 STNumber const& v = dynamic_cast<STNumber const&>(t);
96 return value_ == v;
97}
98
99bool
101{
102 return value_ == Number();
103}
104
107{
108 return out << rhs.getText();
109}
110
111} // namespace ripple
constexpr int exponent() const noexcept
Definition: Number.h:218
constexpr rep mantissa() const noexcept
Definition: Number.h:212
Identifies fields.
Definition: SField.h:144
A type which can be exported to a well known binary format.
Definition: STBase.h:124
virtual SerializedTypeID getSType() const
Definition: STBase.cpp:68
SField const & getFName() const
Definition: STBase.cpp:134
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:222
A serializable number.
Definition: STNumber.h:43
SerializedTypeID getSType() const override
Definition: STNumber.cpp:42
bool isDefault() const override
Definition: STNumber.cpp:100
void add(Serializer &s) const override
Definition: STNumber.cpp:54
STNumber()=default
std::string getText() const override
Definition: STNumber.cpp:48
void setValue(Number const &v)
Definition: STNumber.cpp:72
STBase * move(std::size_t n, void *buf) override
Definition: STNumber.cpp:84
Number const & value() const
Definition: STNumber.cpp:66
Number value_
Definition: STNumber.h:45
STBase * copy(std::size_t n, void *buf) const override
Definition: STNumber.cpp:78
bool isEquivalent(STBase const &t) const override
Definition: STNumber.cpp:90
std::int64_t geti64()
Definition: Serializer.cpp:404
std::int32_t geti32()
Definition: Serializer.cpp:392
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
SerializedTypeID
Definition: SField.h:108
std::ostream & operator<<(std::ostream &out, base_uint< Bits, Tag > const &u)
Definition: base_uint.h:636
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:629