xrpld
Loading...
Searching...
No Matches
libxrpl/protocol/MPTAmount.cpp
1#include <xrpl/protocol/MPTAmount.h>
2
3#include <xrpl/basics/MathUtilities.h>
4#include <xrpl/basics/contract.h>
5#include <xrpl/protocol/Feature.h>
6#include <xrpl/protocol/Rules.h>
7
8#include <stdexcept>
9
10namespace xrpl {
11
14{
15 if (auto const result = checkedAdd(value_, other.value()))
16 {
17 value_ = *result;
18 return *this;
19 }
20 if (isFeatureEnabled(fixCleanup3_5_0, /*resultIfNoRules*/ true))
21 Throw<std::overflow_error>("MPTAmount::operator+= overflow");
22 value_ += other.value();
23 return *this;
24}
25
28{
29 if (auto const result = checkedSub(value_, other.value()))
30 {
31 value_ = *result;
32 return *this;
33 }
34 if (isFeatureEnabled(fixCleanup3_5_0, /*resultIfNoRules*/ true))
35 Throw<std::overflow_error>("MPTAmount::operator-= overflow");
36 value_ -= other.value();
37 return *this;
38}
39
42{
43 return MPTAmount{-value_};
44}
45
46bool
48{
49 return value_ == other.value_;
50}
51
52bool
54{
55 return value_ == other;
56}
57
58bool
59MPTAmount::operator<(MPTAmount const& other) const
60{
61 return value_ < other.value_;
62}
63
69
70} // namespace xrpl
std::int64_t value_type
Definition MPTAmount.h:25
bool operator==(MPTAmount const &other) const
static MPTAmount minPositiveAmount()
MPTAmount & operator-=(MPTAmount const &other)
constexpr value_type value() const
Returns the underlying value.
Definition MPTAmount.h:135
bool operator<(MPTAmount const &other) const
MPTAmount & operator+=(MPTAmount const &other)
value_type value_
Definition MPTAmount.h:28
MPTAmount()=default
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr std::optional< std::int64_t > checkedSub(std::int64_t a, std::int64_t b) noexcept
Subtract two signed 64-bit integers, returning std::nullopt when the exact mathematical difference is...
constexpr std::optional< std::int64_t > checkedAdd(std::int64_t a, std::int64_t b) noexcept
Add two signed 64-bit integers, returning std::nullopt when the exact mathematical sum is not represe...
bool isFeatureEnabled(UInt256 const &feature, bool resultIfNoRules)
Check whether a feature is enabled in the current ledger rules.
Definition Rules.cpp:199
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52