CatapultServer  v0.5.0.1 (Elephant)
ClampedBaseValue.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "BaseValue.h"
23 
24 namespace catapult { namespace utils {
25 
26  template<typename TValue, typename TRange>
28 
29  namespace detail {
32  template<typename TRange>
33  struct TaggedRange {};
34 
36  template<typename TValue, typename TRange>
39 
41 
43 
45  };
46  }
47 
49  template<typename TValue, typename TRange>
50  struct ClampedBaseValue : public detail::ClampedBaseValueTraits<TValue, TRange>::ClampedBaseType {
51  private:
53 
54  public:
56  using Unclamped = typename Traits::UnclampedType;
57 
58  public:
60  constexpr explicit ClampedBaseValue(TValue value = TRange::Default_Value)
61  : Traits::ClampedBaseType(Clamp(value))
62  {}
63 
64  private:
65  static constexpr TValue Clamp(TValue value) {
66  return value < TRange::Min_Value
67  ? TRange::Min_Value
68  : value > TRange::Max_Value ? TRange::Max_Value : value;
69  }
70 
71  public:
74  static constexpr ClampedBaseValue Min() {
75  return ClampedBaseValue(TRange::Min_Value);
76  }
77 
80  static constexpr ClampedBaseValue Max() {
81  return ClampedBaseValue(TRange::Max_Value);
82  }
83 
84  public:
86  constexpr ClampedBaseValue operator+(Unclamped rhs) const {
87  return ClampedBaseValue(this->unwrap() + rhs.unwrap());
88  }
89 
91  constexpr Unclamped operator-(ClampedBaseValue rhs) const {
92  return Unclamped(this->unwrap() - rhs.unwrap());
93  }
94  };
95 }}
catapult::utils::detail::TaggedRange
Definition: ClampedBaseValue.h:33
catapult::utils::ClampedBaseValue::ClampedBaseValue
constexpr ClampedBaseValue(TValue value=TRange::Default_Value)
Creates a clamped base value from a raw value.
Definition: ClampedBaseValue.h:60
catapult::utils::ClampedBaseValue::Clamp
static constexpr TValue Clamp(TValue value)
Definition: ClampedBaseValue.h:65
catapult::utils::BasicBaseValue::unwrap
constexpr ValueType unwrap() const
Unwraps this value and returns the underlying raw value.
Definition: BaseValue.h:53
catapult::utils::ClampedBaseValue::operator+
constexpr ClampedBaseValue operator+(Unclamped rhs) const
Adds rhs and this value and returns a new value.
Definition: ClampedBaseValue.h:86
catapult::utils::detail::ClampedBaseValueTraits::UnclampedType
BaseValue< TValue, TaggedRangeType > UnclampedType
Definition: ClampedBaseValue.h:40
catapult::utils::BaseValue
Immutable wrapper for basic types, to provide some type-safety.
Definition: BaseValue.h:100
catapult::utils::BasicBaseValue
Base class for immutable wrappers of basic types, to provide some type-safety.
Definition: BaseValue.h:30
catapult::utils::ClampedBaseValue::Max
static constexpr ClampedBaseValue Max()
Definition: ClampedBaseValue.h:80
catapult::utils::ClampedBaseValue< uint64_t, Difficulty_tag >::Unclamped
typename Traits::UnclampedType Unclamped
Compatible unclamped type.
Definition: ClampedBaseValue.h:56
catapult::utils::detail::ClampedBaseValueTraits
Defines types used in the definition of ClampedBaseValue.
Definition: ClampedBaseValue.h:37
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::ClampedBaseValue
Base values that are constrained to a range of values.
Definition: ClampedBaseValue.h:27
catapult::utils::ClampedBaseValue::operator-
constexpr Unclamped operator-(ClampedBaseValue rhs) const
Subtracts rhs from this value and returns a new value.
Definition: ClampedBaseValue.h:91
catapult::utils::ClampedBaseValue::Min
static constexpr ClampedBaseValue Min()
Definition: ClampedBaseValue.h:74
BaseValue.h