ipfs-chromium
Loading...
Searching...
No Matches
byte.h
1#ifndef IPFS_BYTE_H_
2#define IPFS_BYTE_H_
3
4#include <cstddef>
5#include <cstdint>
6
7#include <iomanip>
8#include <iostream>
9#include <version>
10
11#ifdef __cpp_lib_byte
12
13namespace ipfs {
14using Byte = std::byte;
15} // namespace ipfs
16
17#else
18namespace ipfs {
19enum class Byte : std::uint_least8_t {};
20} // namespace ipfs
21#endif
22
23namespace {
24[[maybe_unused]] std::ostream& operator<<(std::ostream& str, ipfs::Byte b) {
25 return str << std::hex << std::setw(2) << std::setfill('0')
26 << static_cast<unsigned>(b);
27}
28} // namespace
29
30namespace {
31// libc++ provides this, but for some reason libstdc++ does not
32[[maybe_unused]] std::uint8_t to_integer(ipfs::Byte b) {
33 return static_cast<std::uint8_t>(b);
34}
35} // namespace
36
37namespace ipfs {
38inline bool operator==(Byte a, Byte b) {
39 return to_integer(a) == to_integer(b);
40}
41} // namespace ipfs
42
43#endif // IPFS_BYTE_H_