|
| 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/protocol/SField.h> |
| 23 | + |
| 24 | +namespace ripple { |
| 25 | + |
| 26 | +STNumber::STNumber(SField const& field, Number const& value) |
| 27 | + : STBase(field), value_(value) |
| 28 | +{ |
| 29 | +} |
| 30 | + |
| 31 | +STNumber::STNumber(SerialIter& sit, SField const& field) : STBase(field) |
| 32 | +{ |
| 33 | + // We must call these methods in separate statements |
| 34 | + // to guarantee their order of execution. |
| 35 | + auto mantissa = sit.geti64(); |
| 36 | + auto exponent = sit.geti32(); |
| 37 | + value_ = Number{mantissa, exponent}; |
| 38 | +} |
| 39 | + |
| 40 | +SerializedTypeID |
| 41 | +STNumber::getSType() const |
| 42 | +{ |
| 43 | + return STI_NUMBER; |
| 44 | +} |
| 45 | + |
| 46 | +std::string |
| 47 | +STNumber::getText() const |
| 48 | +{ |
| 49 | + return to_string(value_); |
| 50 | +} |
| 51 | + |
| 52 | +void |
| 53 | +STNumber::add(Serializer& s) const |
| 54 | +{ |
| 55 | + assert(getFName().isBinary()); |
| 56 | + assert(getFName().fieldType == getSType()); |
| 57 | + s.add64(value_.mantissa()); |
| 58 | + s.add32(value_.exponent()); |
| 59 | +} |
| 60 | + |
| 61 | +Number const& |
| 62 | +STNumber::value() const |
| 63 | +{ |
| 64 | + return value_; |
| 65 | +} |
| 66 | + |
| 67 | +void |
| 68 | +STNumber::setValue(Number const& v) |
| 69 | +{ |
| 70 | + value_ = v; |
| 71 | +} |
| 72 | + |
| 73 | +STBase* |
| 74 | +STNumber::copy(std::size_t n, void* buf) const |
| 75 | +{ |
| 76 | + return emplace(n, buf, *this); |
| 77 | +} |
| 78 | + |
| 79 | +STBase* |
| 80 | +STNumber::move(std::size_t n, void* buf) |
| 81 | +{ |
| 82 | + return emplace(n, buf, std::move(*this)); |
| 83 | +} |
| 84 | + |
| 85 | +bool |
| 86 | +STNumber::isEquivalent(STBase const& t) const |
| 87 | +{ |
| 88 | + assert(t.getSType() == this->getSType()); |
| 89 | + STNumber const& v = dynamic_cast<STNumber const&>(t); |
| 90 | + return value_ == v; |
| 91 | +} |
| 92 | + |
| 93 | +bool |
| 94 | +STNumber::isDefault() const |
| 95 | +{ |
| 96 | + return value_ == Number(); |
| 97 | +} |
| 98 | + |
| 99 | +std::ostream& |
| 100 | +operator<<(std::ostream& out, STNumber const& rhs) |
| 101 | +{ |
| 102 | + return out << rhs.getText(); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace ripple |
0 commit comments