2 * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2C_DATA_LEN_HPP
8 #define BABELTRACE_CPP_COMMON_BT2C_DATA_LEN_HPP
10 #include "safe-ops.hpp"
15 * A data length is a quantity of binary data (bits).
17 * This class can make some code clearer and safer because its
18 * constructor is private so that you need to call DataLen::fromBits()
19 * or DataLen::fromBytes() to create an instance.
21 * With a `DataLen` instance `len`, use `*len` or `len.bits()` to get
22 * the quantity in bits and `len.bytes()` to get it in bytes (floored).
24 * You can add, subtract, and compare data lengths.
29 constexpr explicit DataLen(const unsigned long long lenBits) noexcept : _mLenBits {lenBits}
35 * Creates and returns a data length instance representing `lenBits`
38 static constexpr DataLen fromBits(const unsigned long long lenBits) noexcept
40 return DataLen {lenBits};
44 * Creates and returns a data length instance representing
47 static DataLen fromBytes(const unsigned long long lenBytes) noexcept
49 return DataLen {safeMul(lenBytes, 8ULL)};
53 * Number of bits of this data length.
55 constexpr unsigned long long operator*() const noexcept
61 * Number of bits of this data length.
63 constexpr unsigned long long bits() const noexcept
69 * Number of bytes (floor) of this data length.
71 constexpr unsigned long long bytes() const noexcept
77 * Whether or not this data length represents a multiple of eight
80 constexpr bool hasExtraBits() const noexcept
82 return this->extraBitCount() > 0;
86 * Remainder of this data length, in bits, divided by eight.
88 constexpr unsigned int extraBitCount() const noexcept
94 * Returns whether or not this data length is a power of two bits.
96 constexpr bool isPowOfTwo() const noexcept
98 return ((_mLenBits & (_mLenBits - 1)) == 0) && _mLenBits > 0;
101 constexpr bool operator==(const DataLen& other) const noexcept
103 return _mLenBits == other._mLenBits;
106 constexpr bool operator!=(const DataLen& other) const noexcept
108 return !(*this == other);
111 constexpr bool operator<(const DataLen& other) const noexcept
113 return _mLenBits < other._mLenBits;
116 constexpr bool operator<=(const DataLen& other) const noexcept
118 return (*this == other) || (*this < other);
121 constexpr bool operator>(const DataLen& other) const noexcept
123 return !(*this <= other);
126 constexpr bool operator>=(const DataLen& other) const noexcept
128 return (*this > other) || (*this == other);
131 DataLen& operator+=(const DataLen len) noexcept
133 _mLenBits = safeAdd(_mLenBits, len._mLenBits);
137 DataLen& operator-=(const DataLen len) noexcept
139 _mLenBits = safeSub(_mLenBits, len._mLenBits);
143 DataLen& operator*=(const unsigned long long mul) noexcept
145 _mLenBits = safeMul(_mLenBits, mul);
150 unsigned long long _mLenBits = 0;
153 static inline DataLen operator+(const DataLen lenA, const DataLen lenB) noexcept
155 return DataLen::fromBits(safeAdd(*lenA, *lenB));
158 static inline DataLen operator-(const DataLen lenA, const DataLen lenB) noexcept
160 return DataLen::fromBits(safeSub(*lenA, *lenB));
163 static inline DataLen operator*(const DataLen len, const unsigned long long mul) noexcept
165 return DataLen::fromBits(safeMul(*len, mul));
169 * Use this namespace to access handy data length user literals, for
172 * using namespace bt2c::literals::datalen;
174 * const auto bufSize = 64_MiBytes + 8_KiBits;
179 static inline DataLen operator""_bits(const unsigned long long val) noexcept
181 return DataLen::fromBits(val);
184 static inline DataLen operator""_KiBits(const unsigned long long val) noexcept
186 return DataLen::fromBits(safeMul(val, 1024ULL));
189 static inline DataLen operator""_MiBits(const unsigned long long val) noexcept
191 return DataLen::fromBits(safeMul(val, 1024ULL * 1024));
194 static inline DataLen operator""_GiBits(const unsigned long long val) noexcept
196 return DataLen::fromBits(safeMul(val, 1024ULL * 1024 * 1024));
199 static inline DataLen operator""_bytes(const unsigned long long val) noexcept
201 return DataLen::fromBytes(val);
204 static inline DataLen operator""_KiBytes(const unsigned long long val) noexcept
206 return DataLen::fromBytes(safeMul(val, 1024ULL));
209 static inline DataLen operator""_MiBytes(const unsigned long long val) noexcept
211 return DataLen::fromBytes(safeMul(val, 1024ULL * 1024));
214 static inline DataLen operator""_GiBytes(const unsigned long long val) noexcept
216 return DataLen::fromBytes(safeMul(val, 1024ULL * 1024 * 1024));
219 } /* namespace datalen */
220 } /* namespace literals */
221 } /* namespace bt2c */
223 #endif /* BABELTRACE_CPP_COMMON_BT2C_DATA_LEN_HPP */