CatapultServer  v0.5.0.1 (Elephant)
MosaicUnit.h
Go to the documentation of this file.
1 
21 #pragma once
24 #include "catapult/types.h"
25 
26 namespace catapult { namespace extensions {
27 
29 
32 
34  template<size_t Divisibility>
35  class MosaicUnit {
36  public:
38  using ValueType = uint64_t;
39 
40  public:
43  {}
44 
46  explicit MosaicUnit(Amount amount) : m_value(amount.unwrap())
47  {}
48 
50  explicit MosaicUnit(BasicUnitAmount amount) : m_value(amount.unwrap() * GetAtomicUnitMultiplier())
51  {}
52 
54  MosaicUnit(const MosaicUnit& rhs) : m_value(static_cast<ValueType>(rhs.m_value))
55  {}
56 
57  private:
58  static uint64_t GetAtomicUnitMultiplier() {
59  uint64_t value = 1;
60  for (auto i = 0u; i < Divisibility; ++i)
61  value *= 10;
62 
63  return value;
64  }
65 
66  public:
69  m_value = std::move(rhs.m_value);
70  return *this;
71  }
72 
75  m_value = MosaicUnit(rhs).m_value;
76  return *this;
77  }
78 
81  m_value = MosaicUnit(rhs).m_value;
82  return *this;
83  }
84 
85  public:
89  }
90 
92  Amount atomicUnit() const {
93  return Amount(m_value);
94  }
95 
97  bool isFractional() const {
98  return 0 != m_value % GetAtomicUnitMultiplier();
99  }
100 
101  public:
103  bool operator==(MosaicUnit rhs) const {
104  return m_value == rhs.m_value;
105  }
106 
108  bool operator!=(MosaicUnit rhs) const {
109  return !(*this == rhs);
110  }
111 
113  friend std::ostream& operator<<(std::ostream& out, MosaicUnit unit) {
114  utils::StreamFormatGuard guard(out, std::ios::dec, '0');
115  out << unit.basicUnit();
116  if (unit.isFractional())
117  out << '.' << std::setw(Divisibility) << (unit.m_value % GetAtomicUnitMultiplier());
118 
119  return out;
120  }
121 
122  private:
124  };
125 }}
catapult::extensions::MosaicUnit
Represents units of a mosaic.
Definition: MosaicUnit.h:35
catapult::extensions::MosaicUnit::isFractional
bool isFractional() const
Returns true if this unit includes fractional mosaics.
Definition: MosaicUnit.h:97
catapult::extensions::MosaicUnit::MosaicUnit
MosaicUnit()
Creates a zeroed mosaic unit.
Definition: MosaicUnit.h:42
catapult::extensions::MosaicUnit::m_value
utils::ImmutableValue< ValueType > m_value
Definition: MosaicUnit.h:123
catapult::Amount
utils::BaseValue< uint64_t, Amount_tag > Amount
Definition: src/catapult/types.h:76
catapult::extensions::BasicUnitAmount
utils::BaseValue< uint64_t, BasicUnitAmount_tag > BasicUnitAmount
A non-fractional amount of a mosaic.
Definition: MosaicUnit.h:31
catapult::extensions::MosaicUnit::operator=
MosaicUnit & operator=(Amount rhs)
Assigns rhs to this.
Definition: MosaicUnit.h:74
catapult::extensions::MosaicUnit::MosaicUnit
MosaicUnit(const MosaicUnit &rhs)
Creates a copy of rhs.
Definition: MosaicUnit.h:54
catapult::extensions::MosaicUnit::operator=
MosaicUnit & operator=(MosaicUnit rhs)
Assigns rhs to this.
Definition: MosaicUnit.h:68
catapult::extensions::MosaicUnit::basicUnit
BasicUnitAmount basicUnit() const
Gets the number of (basic unit) mosaics.
Definition: MosaicUnit.h:87
catapult::extensions::MosaicUnit::operator=
MosaicUnit & operator=(BasicUnitAmount rhs)
Assigns rhs to this.
Definition: MosaicUnit.h:80
catapult::utils::BaseValue
Immutable wrapper for basic types, to provide some type-safety.
Definition: BaseValue.h:100
catapult::extensions::MosaicUnit::ValueType
uint64_t ValueType
Raw value type.
Definition: MosaicUnit.h:38
StreamFormatGuard.h
catapult::extensions::MosaicUnit::MosaicUnit
MosaicUnit(Amount amount)
Creates a mosaic unit from amount atomic units.
Definition: MosaicUnit.h:46
catapult::extensions::MosaicUnit::atomicUnit
Amount atomicUnit() const
Gets the number of (atomic unit) mosaics.
Definition: MosaicUnit.h:92
catapult::extensions::MosaicUnit::operator==
bool operator==(MosaicUnit rhs) const
Returns true if this value is equal to rhs.
Definition: MosaicUnit.h:103
ImmutableValue.h
catapult::extensions::MosaicUnit::operator!=
bool operator!=(MosaicUnit rhs) const
Returns true if this value is not equal to rhs.
Definition: MosaicUnit.h:108
catapult::utils::ImmutableValue< ValueType >
catapult::utils::StreamFormatGuard
Definition: StreamFormatGuard.h:29
types.h
catapult::extensions::BasicUnitAmount_tag
Definition: MosaicUnit.h:28
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::extensions::MosaicUnit::operator<<
friend std::ostream & operator<<(std::ostream &out, MosaicUnit unit)
Insertion operator for outputting unit to out.
Definition: MosaicUnit.h:113
catapult::extensions::MosaicUnit::GetAtomicUnitMultiplier
static uint64_t GetAtomicUnitMultiplier()
Definition: MosaicUnit.h:58
catapult::extensions::MosaicUnit::MosaicUnit
MosaicUnit(BasicUnitAmount amount)
Creates a mosaic unit from amount basic units.
Definition: MosaicUnit.h:50