Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
byte.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_BYTE_BYTE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_BYTE_BYTE_HPP_
7
8// IWYU pragma: private, include "arene/base/byte.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A7_1_5-a-2 "Trailing return syntax permitted by A7-1-5 Permit #1 v1.0.0"
12
13// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
14#include "arene/base/compiler_support/attributes.hpp"
15#include "arene/base/constraints/constraints.hpp"
16#include "arene/base/contracts/contract.hpp"
17#include "arene/base/stdlib_choice/climits.hpp"
18#include "arene/base/stdlib_choice/cstdint.hpp"
19#include "arene/base/stdlib_choice/enable_if.hpp"
20#include "arene/base/stdlib_choice/is_integral.hpp"
21#include "arene/base/stdlib_choice/is_signed.hpp"
22#include "arene/base/utility/to_underlying.hpp"
23// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
24
25namespace arene {
26namespace base {
27
28/// @brief A type that represents a byte of data.
29///
30/// Use this for cases where you are dealing with serialised data in bytes
31/// or inspecting byte-representations of values.
32///
33/// This type is equivalent to std::byte added in C++17.
34///
35/// This type supports only bit-wise operations and does not support arithmetic
36/// operations like +, -, *, / or %.
37// Need to tag the type as "may_alias" to have the same aliasing behaviour
38// as 'char', 'unsigned char' and 'std::byte'.
39enum class ARENE_MAY_ALIAS byte : std::uint8_t {};
40
41/// @brief Convert an integer to an @c arene::base::byte
42/// @tparam ValueType The type of the integer.
43/// @param val The value to convert
44/// @return The supplied value, cast to @c byte
45template <typename ValueType, constraints<std::enable_if_t<std::is_integral<ValueType>::value>> = nullptr>
46constexpr auto to_byte(ValueType const val) noexcept -> byte {
47 // parasoft-begin-suppress AUTOSAR-A7_2_1-a-2 "All uint8 values are valid values for byte"
48 // parasoft-begin-suppress CERT_CPP-INT50-a-3 "All uint8 values are valid values for byte"
49 return static_cast<byte>(static_cast<std::uint8_t>(val));
50 // parasoft-end-suppress AUTOSAR-A7_2_1-a-2
51 // parasoft-end-suppress CERT_CPP-INT50-a-3
52}
53
54/// @brief convert the byte value to an integral representation.
55/// @tparam IntegerType The type of integer to convert to.
56/// @param val The byte to convert.
57/// @return The integral value of @c val.
58template <typename IntegerType, constraints<std::enable_if_t<std::is_integral<IntegerType>::value>> = nullptr>
59constexpr auto to_integer(byte const val) noexcept -> IntegerType {
60 return static_cast<IntegerType>(to_underlying(val));
61}
62
63// parasoft-begin-suppress AUTOSAR-M5_8_1-a "False positive: There is a precondition check before the shift"
64
65/// @brief Left-shift the bits of @c val by @c shift.
66/// @tparam IntegerType The type of integer to shift by
67/// @param val The byte to shift
68/// @param shift The number of bits to shift by
69/// @return The result of shifting @c val left by @c shift bits
70/// @pre <c>shift < CHAR_BIT</c>, otherwise ARENE_PRECONDITION violation
71template <typename IntegerType, constraints<std::enable_if_t<std::is_integral<IntegerType>::value>> = nullptr>
72constexpr auto operator<<(byte const val, IntegerType const shift) noexcept -> byte {
73 static_assert(!std::is_signed<IntegerType>::value, "Shifting by a signed integer is not permitted");
74
76
77 return to_byte(static_cast<std::uint32_t>(to_underlying(val)) << shift);
78}
79
80/// @brief Right-shift the bits of @c val by @c shift.
81/// @tparam IntegerType The type of integer to shift by.
82/// @param val The byte to shift
83/// @param shift The number of bits to shift by
84/// @return The result of shifting @c val right by @c shift bits
85/// @pre <c>shift < CHAR_BIT</c>, otherwise ARENE_PRECONDITION violation
86template <typename IntegerType, constraints<std::enable_if_t<std::is_integral<IntegerType>::value>> = nullptr>
87constexpr auto operator>>(byte const val, IntegerType const shift) noexcept -> byte {
88 static_assert(!std::is_signed<IntegerType>::value, "Shifting by a signed integer is not permitted");
89
91
92 return to_byte(static_cast<std::uint32_t>(to_underlying(val)) >> shift);
93}
94
95// parasoft-end-suppress AUTOSAR-M5_8_1-a
96
97// parasoft-begin-suppress AUTOSAR-M5_17_1-a-2 "False positive: The compound operator uses the non-compound operator in
98// the implementation"
99/// @brief Bit-wise or of the byte representations.
100/// @param lhs The first value
101/// @param rhs The second value
102/// @return The result of bitwise or of @c lhs and @c rhs
103constexpr auto operator|(byte const lhs, byte const rhs) noexcept -> byte {
104 return to_byte(static_cast<std::uint8_t>(to_underlying(lhs) | to_underlying(rhs)));
105}
106
107/// @brief Bit-wise and of the byte representations.
108/// @param lhs The first value
109/// @param rhs The second value
110/// @return The result of bitwise and of @c lhs and @c rhs
111constexpr auto operator&(byte const lhs, byte const rhs) noexcept -> byte { // CODEQLFP(M5-3-3)
112 return to_byte(static_cast<std::uint8_t>(to_underlying(lhs) & to_underlying(rhs)));
113}
114
115/// @brief Bit-wise xor of the byte representations.
116/// @param lhs The first value
117/// @param rhs The second value
118/// @return The result of bitwise xor of @c lhs and @c rhs
119constexpr auto operator^(byte const lhs, byte const rhs) noexcept -> byte {
120 return to_byte(static_cast<std::uint8_t>(to_underlying(lhs) ^ to_underlying(rhs)));
121}
122// parasoft-end-suppress AUTOSAR-M5_17_1-a-2
123
124/// @brief Bit-wise negation of the byte representation.
125/// @param val The value to negate
126/// @return The result of bitwise negation of @c val
127constexpr auto operator~(byte const val) noexcept -> byte {
128 return to_byte(~static_cast<std::uint32_t>(to_underlying(val)));
129}
130
131/// @brief Modify @c val by shifting its bits left by @c shift.
132/// @param val The value to shift
133/// @param shift The number of bits to shift by
134/// @return A reference to @c val
135template <typename IntegerType, constraints<std::enable_if_t<std::is_integral<IntegerType>::value>> = nullptr>
136constexpr auto operator<<=(byte& val, IntegerType const shift) noexcept -> byte& {
137 val = val << shift;
138 return val;
139}
140
141/// @brief Modify @c val by shifting its bits right by @c shift.
142/// @param val The value to shift
143/// @param shift The number of bits to shift by
144/// @return A reference to @c val
145template <typename IntegerType, constraints<std::enable_if_t<std::is_integral<IntegerType>::value>> = nullptr>
146constexpr auto operator>>=(byte& val, IntegerType const shift) noexcept -> byte& {
147 val = val >> shift;
148 return val;
149}
150
151/// @brief Modify @c lhs by bit-wise or'ing it with the bits of @c rhs.
152/// @param lhs The value to modify
153/// @param rhs The second value
154/// @return A reference to @c lhs
155constexpr auto operator|=(byte& lhs, byte const rhs) noexcept -> byte& {
156 lhs = lhs | rhs;
157 return lhs;
158}
159
160/// @brief Modify @c lhs by bit-wise and'ing it with the bits of @c rhs.
161/// @param lhs The value to modify
162/// @param rhs The second value
163/// @return A reference to @c lhs
164constexpr auto operator&=(byte& lhs, byte const rhs) noexcept -> byte& {
165 lhs = lhs & rhs;
166 return lhs;
167}
168
169/// @brief Modify @c lhs by bit-wise xor'ing it with the bits of @c rhs.
170/// @param lhs The value to modify
171/// @param rhs The second value
172/// @return A reference to @c val
173constexpr auto operator^=(byte& lhs, byte const rhs) noexcept -> byte& {
174 lhs = lhs ^ rhs;
175 return lhs;
176}
177
178namespace literals {
179
180///
181/// @brief Literal helper for creating @c byte instances.
182///
183/// @param value The literal to cast to @c byte .
184/// @return byte Equivalent to calling @c to_byte(value) .
185///
186// parasoft-begin-suppress AUTOSAR-A3_9_1-b-2 "False positive: UDLs must take long long"
187// NOLINTNEXTLINE (google-runtime-init) literal operator
188constexpr byte operator""_byte(unsigned long long int const value) noexcept { return to_byte(value); }
189// parasoft-end-suppress AUTOSAR-A3_9_1-b-2
190
191} // namespace literals
192
193} // namespace base
194} // namespace arene
195
196#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_BYTE_BYTE_HPP_
constexpr byte operator""_byte(unsigned long long int const value) noexcept
Literal helper for creating byte instances.
Definition byte.hpp:188
Definition array_exceptions_disabled.cpp:11
constexpr auto operator|=(byte &lhs, byte const rhs) noexcept -> byte &
Modify lhs by bit-wise or'ing it with the bits of rhs.
Definition byte.hpp:155
constexpr auto operator&(byte const lhs, byte const rhs) noexcept -> byte
Bit-wise and of the byte representations.
Definition byte.hpp:111
constexpr auto operator|(byte const lhs, byte const rhs) noexcept -> byte
Bit-wise or of the byte representations.
Definition byte.hpp:103
constexpr auto operator&=(byte &lhs, byte const rhs) noexcept -> byte &
Modify lhs by bit-wise and'ing it with the bits of rhs.
Definition byte.hpp:164
constexpr auto operator^(byte const lhs, byte const rhs) noexcept -> byte
Bit-wise xor of the byte representations.
Definition byte.hpp:119
enum ARENE_MAY_ALIAS byte
A type that represents a byte of data.
Definition byte.hpp:39
constexpr auto operator~(byte const val) noexcept -> byte
Bit-wise negation of the byte representation.
Definition byte.hpp:127
constexpr auto operator^=(byte &lhs, byte const rhs) noexcept -> byte &
Modify lhs by bit-wise xor'ing it with the bits of rhs.
Definition byte.hpp:173
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10