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/basics/Number.h>
21#include <xrpl/beast/utility/instrumentation.h>
22#include <xrpl/protocol/SField.h>
23#include <xrpl/protocol/STBase.h>
24#include <xrpl/protocol/STNumber.h>
25#include <xrpl/protocol/Serializer.h>
26
27#include <cstddef>
28#include <ostream>
29#include <string>
30#include <utility>
31
32namespace ripple {
33
34STNumber::STNumber(SField const& field, Number const& value)
35 : STBase(field), value_(value)
36{
37}
38
39STNumber::STNumber(SerialIter& sit, SField const& field) : STBase(field)
40{
41 // We must call these methods in separate statements
42 // to guarantee their order of execution.
43 auto mantissa = sit.geti64();
44 auto exponent = sit.geti32();
45 value_ = Number{mantissa, exponent};
46}
47
50{
51 return STI_NUMBER;
52}
53
56{
57 return to_string(value_);
58}
59
60void
62{
63 XRPL_ASSERT(
64 getFName().isBinary(), "ripple::STNumber::add : field is binary");
65 XRPL_ASSERT(
66 getFName().fieldType == getSType(),
67 "ripple::STNumber::add : field type match");
70}
71
72Number const&
74{
75 return value_;
76}
77
78void
80{
81 value_ = v;
82}
83
84STBase*
85STNumber::copy(std::size_t n, void* buf) const
86{
87 return emplace(n, buf, *this);
88}
89
90STBase*
92{
93 return emplace(n, buf, std::move(*this));
94}
95
96bool
98{
99 XRPL_ASSERT(
100 t.getSType() == this->getSType(),
101 "ripple::STNumber::isEquivalent : field type match");
102 STNumber const& v = dynamic_cast<STNumber const&>(t);
103 return value_ == v;
104}
105
106bool
108{
109 return value_ == Number();
110}
111
114{
115 return out << rhs.getText();
116}
117
118} // 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:126
virtual SerializedTypeID getSType() const
Definition: STBase.cpp:75
SField const & getFName() const
Definition: STBase.cpp:141
static STBase * emplace(std::size_t n, void *buf, T &&val)
Definition: STBase.h:224
A serializable number.
Definition: STNumber.h:43
SerializedTypeID getSType() const override
Definition: STNumber.cpp:49
bool isDefault() const override
Definition: STNumber.cpp:107
void add(Serializer &s) const override
Definition: STNumber.cpp:61
STNumber()=default
std::string getText() const override
Definition: STNumber.cpp:55
void setValue(Number const &v)
Definition: STNumber.cpp:79
STBase * move(std::size_t n, void *buf) override
Definition: STNumber.cpp:91
Number const & value() const
Definition: STNumber.cpp:73
Number value_
Definition: STNumber.h:45
STBase * copy(std::size_t n, void *buf) const override
Definition: STNumber.cpp:85
bool isEquivalent(STBase const &t) const override
Definition: STNumber.cpp:97
std::int64_t geti64()
Definition: Serializer.cpp:417
std::int32_t geti32()
Definition: Serializer.cpp:405
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:637
std::string to_string(base_uint< Bits, Tag > const &a)
Definition: base_uint.h:630