CatapultServer  v0.5.0.1 (Elephant)
BlockSpan.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "Casting.h"
23 #include "TimeSpan.h"
24 
25 namespace catapult { namespace utils {
26 
28  class BlockSpan final {
29  private:
30  constexpr explicit BlockSpan(uint64_t hours) : m_hours(hours)
31  {}
32 
33  public:
35  constexpr BlockSpan() : BlockSpan(0)
36  {}
37 
38  public:
40  static constexpr BlockSpan FromHours(uint64_t hours) {
41  return BlockSpan(hours);
42  }
43 
45  static constexpr BlockSpan FromDays(uint64_t days) {
46  return BlockSpan(days * 24);
47  }
48 
49  public:
51  constexpr uint64_t hours() const {
52  return m_hours;
53  }
54 
56  constexpr uint64_t days() const {
57  return m_hours / 24;
58  }
59 
61  BlockDuration blocks(const TimeSpan& generationTargetTime) const {
62  auto millisPerHour = TimeSpan::FromHours(1).millis();
63  if (m_hours > std::numeric_limits<uint64_t>::max() / millisPerHour)
64  CATAPULT_THROW_RUNTIME_ERROR_1("overflow while calculating blocks from hours", m_hours);
65 
66  return BlockDuration(m_hours * millisPerHour / generationTargetTime.millis());
67  }
68 
69  public:
71  constexpr bool operator==(const BlockSpan& rhs) const {
72  return m_hours == rhs.m_hours;
73  }
74 
76  constexpr bool operator!=(const BlockSpan& rhs) const {
77  return !(*this == rhs);
78  }
79 
81  constexpr bool operator>=(const BlockSpan& rhs) const {
82  return m_hours >= rhs.m_hours;
83  }
84 
86  constexpr bool operator>(const BlockSpan& rhs) const {
87  return m_hours > rhs.m_hours;
88  }
89 
91  constexpr bool operator<=(const BlockSpan& rhs) const {
92  return m_hours <= rhs.m_hours;
93  }
94 
96  constexpr bool operator<(const BlockSpan& rhs) const {
97  return m_hours < rhs.m_hours;
98  }
99 
100  private:
101  uint64_t m_hours;
102  };
103 
105  std::ostream& operator<<(std::ostream& out, const BlockSpan& blockSpan);
106 }}
catapult::utils::BlockSpan::FromDays
static constexpr BlockSpan FromDays(uint64_t days)
Creates a block span from the given number of days.
Definition: BlockSpan.h:45
catapult::BlockDuration
utils::BaseValue< uint64_t, BlockDuration_tag > BlockDuration
Definition: src/catapult/types.h:88
catapult::utils::BlockSpan::operator!=
constexpr bool operator!=(const BlockSpan &rhs) const
Returns true if this block span is not equal to rhs.
Definition: BlockSpan.h:76
catapult::utils::BlockSpan::m_hours
uint64_t m_hours
Definition: BlockSpan.h:101
catapult::utils::TimeSpan
Represents a time duration.
Definition: TimeSpan.h:30
IntegerMath.h
catapult::utils::BaseValue< uint64_t, BlockDuration_tag >
catapult::utils::BlockSpan::operator==
constexpr bool operator==(const BlockSpan &rhs) const
Returns true if this block span is equal to rhs.
Definition: BlockSpan.h:71
catapult::utils::BlockSpan::operator>=
constexpr bool operator>=(const BlockSpan &rhs) const
Returns true if this block span is greater than or equal to rhs.
Definition: BlockSpan.h:81
catapult::utils::BlockSpan
Represents a block duration.
Definition: BlockSpan.h:28
CATAPULT_THROW_RUNTIME_ERROR_1
#define CATAPULT_THROW_RUNTIME_ERROR_1(MESSAGE, PARAM1)
Macro used to throw a catapult runtime error with a single parameter.
Definition: exceptions.h:171
catapult::utils::TimeSpan::FromHours
static constexpr TimeSpan FromHours(uint64_t hours)
Creates a time span from the given number of hours.
Definition: TimeSpan.h:44
catapult::utils::BlockSpan::operator<
constexpr bool operator<(const BlockSpan &rhs) const
Returns true if this block span is less than rhs.
Definition: BlockSpan.h:96
catapult::utils::BlockSpan::BlockSpan
constexpr BlockSpan()
Creates a default (zero) block span.
Definition: BlockSpan.h:35
catapult::utils::operator<<
std::ostream & operator<<(std::ostream &out, const BlockSpan &blockSpan)
Insertion operator for outputting blockSpan to out.
Definition: BlockSpan.cpp:27
catapult::utils::TimeSpan::millis
constexpr uint64_t millis() const
Returns the number of milliseconds.
Definition: TimeSpan.h:91
BlockSpan.h
TimeSpan.h
catapult::utils::BlockSpan::FromHours
static constexpr BlockSpan FromHours(uint64_t hours)
Creates a block span from the given number of hours.
Definition: BlockSpan.h:40
catapult::utils::BlockSpan::operator<=
constexpr bool operator<=(const BlockSpan &rhs) const
Returns true if this block span is less than or equal to rhs.
Definition: BlockSpan.h:91
catapult::utils::BlockSpan::operator>
constexpr bool operator>(const BlockSpan &rhs) const
Returns true if this block span is greater than rhs.
Definition: BlockSpan.h:86
Casting.h
catapult::utils::BlockSpan::hours
constexpr uint64_t hours() const
Returns the number of hours.
Definition: BlockSpan.h:51
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::BlockSpan::BlockSpan
constexpr BlockSpan(uint64_t hours)
Definition: BlockSpan.h:30
catapult::utils::BlockSpan::days
constexpr uint64_t days() const
Returns the number of days.
Definition: BlockSpan.h:56
catapult::utils::BlockSpan::blocks
BlockDuration blocks(const TimeSpan &generationTargetTime) const
Returns the approximate number of blocks given the generation target time (generationTargetTime).
Definition: BlockSpan.h:61