CatapultServer  v0.5.0.1 (Elephant)
ChainScore.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "catapult/functions.h"
23 #include <boost/multiprecision/cpp_int.hpp>
24 #include <iosfwd>
25 
26 namespace catapult { namespace model {
27 
30  class ChainScore {
31  private:
32  using ArrayType = std::array<uint64_t, 2>;
33  static constexpr uint32_t Bits_Per_Value = 64;
34 
35  public:
38  {}
39 
41  explicit ChainScore(uint64_t score) : m_score(score)
42  {}
43 
45  explicit ChainScore(uint64_t scoreHigh, uint64_t scoreLow) {
46  m_score = scoreHigh;
48  m_score += scoreLow;
49  }
50 
51  public:
52  ChainScore(const ChainScore&) = default;
53  ChainScore& operator=(const ChainScore&) = default;
54 
55  public:
57  ArrayType toArray() const {
58  return {{
59  static_cast<uint64_t>(m_score >> Bits_Per_Value),
60  static_cast<uint64_t>(m_score & std::numeric_limits<uint64_t>::max())
61  }};
62  }
63 
64  public:
67  m_score += rhs.m_score;
68  return *this;
69  }
70 
73  m_score -= rhs.m_score;
74  return *this;
75  }
76 
77  public:
79  bool operator==(const ChainScore& rhs) const {
80  return m_score == rhs.m_score;
81  }
82 
84  bool operator!=(const ChainScore& rhs) const {
85  return m_score != rhs.m_score;
86  }
87 
89  bool operator<(const ChainScore& rhs) const {
90  return m_score < rhs.m_score;
91  }
92 
94  bool operator<=(const ChainScore& rhs) const {
95  return m_score <= rhs.m_score;
96  }
97 
99  bool operator>(const ChainScore& rhs) const {
100  return m_score > rhs.m_score;
101  }
102 
104  bool operator>=(const ChainScore& rhs) const {
105  return m_score >= rhs.m_score;
106  }
107 
108  public:
110  friend std::ostream& operator<<(std::ostream& out, const ChainScore& score) {
111  out << score.m_score;
112  return out;
113  }
114 
115  private:
116  boost::multiprecision::uint128_t m_score;
117  };
118 
121 }}
catapult::model::ChainScore::ArrayType
std::array< uint64_t, 2 > ArrayType
Definition: ChainScore.h:32
catapult::model::ChainScore::ChainScore
ChainScore()
Creates a default chain score.
Definition: ChainScore.h:37
catapult::model::ChainScore::Bits_Per_Value
static constexpr uint32_t Bits_Per_Value
Definition: ChainScore.h:33
catapult::model::ChainScore::m_score
boost::multiprecision::uint128_t m_score
Definition: ChainScore.h:116
catapult::model::ChainScore::operator>
bool operator>(const ChainScore &rhs) const
Returns true if this score is greater than rhs.
Definition: ChainScore.h:99
catapult::model::ChainScore::operator-=
ChainScore & operator-=(const ChainScore &rhs)
Subtracts rhs from this chain score.
Definition: ChainScore.h:72
catapult::model::ChainScore::ChainScore
ChainScore(uint64_t scoreHigh, uint64_t scoreLow)
Creates a chain score from a 128-bit value composed of two 64-bit values (scoreHigh and scoreLow).
Definition: ChainScore.h:45
catapult::model::ChainScore::operator<
bool operator<(const ChainScore &rhs) const
Returns true if this score is less than rhs.
Definition: ChainScore.h:89
catapult::model::ChainScore::operator=
ChainScore & operator=(const ChainScore &)=default
catapult::model::ChainScore::operator+=
ChainScore & operator+=(const ChainScore &rhs)
Adds rhs to this chain score.
Definition: ChainScore.h:66
functions.h
catapult::supplier
std::function< T()> supplier
A (stateless) supplier function.
Definition: functions.h:39
catapult::model::ChainScore
Definition: ChainScore.h:30
catapult::model::ChainScoreSupplier
supplier< ChainScore > ChainScoreSupplier
Prototype for a function that returns a chain score.
Definition: ChainScore.h:120
catapult::model::ChainScore::operator==
bool operator==(const ChainScore &rhs) const
Returns true if this score is equal to rhs.
Definition: ChainScore.h:79
catapult::model::ChainScore::operator>=
bool operator>=(const ChainScore &rhs) const
Returns true if this score is greater than or equal to rhs.
Definition: ChainScore.h:104
catapult::model::ChainScore::operator!=
bool operator!=(const ChainScore &rhs) const
Returns true if this score is not equal to rhs.
Definition: ChainScore.h:84
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::model::ChainScore::ChainScore
ChainScore(uint64_t score)
Creates a chain score from a 64-bit value (score).
Definition: ChainScore.h:41
catapult::model::ChainScore::operator<<
friend std::ostream & operator<<(std::ostream &out, const ChainScore &score)
Insertion operator for outputting score to out.
Definition: ChainScore.h:110
catapult::model::ChainScore::operator<=
bool operator<=(const ChainScore &rhs) const
Returns true if this score is less than or equal to rhs.
Definition: ChainScore.h:94
catapult::model::ChainScore::toArray
ArrayType toArray() const
Gets an array representing the underlying score.
Definition: ChainScore.h:57