Commit ad37aadb authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

byte

Summary: [Folly] `byte`, backported from `std::byte` in C++17.

Reviewed By: Alfus, vitaut

Differential Revision: D23535105

fbshipit-source-id: 056c634362b2ca226306a97ae9994a4631d07ee5
parent 10ee4469
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cstddef>
#include <type_traits>
// include or backport:
// * std::byte
// * std::to_integer
#if __cpp_lib_byte >= 201603
namespace folly {
using std::byte;
using std::to_integer;
} // namespace folly
#else
namespace folly {
enum class byte : unsigned char {};
constexpr byte operator~(byte b) noexcept {
return static_cast<byte>(~static_cast<unsigned>(b));
}
constexpr byte operator&(byte b0, byte b1) noexcept {
return static_cast<byte>(
static_cast<unsigned>(b0) & static_cast<unsigned>(b1));
}
constexpr byte operator|(byte b0, byte b1) noexcept {
return static_cast<byte>(
static_cast<unsigned>(b0) | static_cast<unsigned>(b1));
}
constexpr byte operator^(byte b0, byte b1) noexcept {
return static_cast<byte>(
static_cast<unsigned>(b0) ^ static_cast<unsigned>(b1));
}
template <typename I, std::enable_if_t<std::is_integral<I>::value, int> = 0>
constexpr byte operator<<(byte b, I shift) noexcept {
return static_cast<byte>(static_cast<unsigned>(b) << shift);
}
template <typename I, std::enable_if_t<std::is_integral<I>::value, int> = 0>
constexpr byte operator>>(byte b, I shift) noexcept {
return static_cast<byte>(static_cast<unsigned>(b) >> shift);
}
constexpr byte& operator&=(byte& b, byte o) noexcept {
return b = b & o;
}
constexpr byte& operator|=(byte& b, byte o) noexcept {
return b = b | o;
}
constexpr byte& operator^=(byte& b, byte o) noexcept {
return b = b ^ o;
}
template <typename I, std::enable_if_t<std::is_integral<I>::value, int> = 0>
constexpr byte& operator<<=(byte& b, I shift) noexcept {
return b = b << shift;
}
template <typename I, std::enable_if_t<std::is_integral<I>::value, int> = 0>
constexpr byte& operator>>=(byte& b, I shift) noexcept {
return b = b >> shift;
}
template <typename I, std::enable_if_t<std::is_integral<I>::value, int> = 0>
constexpr I to_integer(byte b) noexcept {
return static_cast<I>(b);
}
} // namespace folly
#endif
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/lang/Byte.h>
#include <folly/portability/GTest.h>
class ByteTest : public testing::Test {};
using byte = folly::byte;
using base = unsigned char;
static_assert(sizeof(byte) == 1);
static_assert(!std::is_integral_v<byte>);
static_assert(std::is_standard_layout_v<byte>);
static_assert(std::is_trivial_v<byte>);
static_assert(std::is_same_v<std::underlying_type_t<byte>, base>);
TEST_F(ByteTest, operations) {
for (int b = 0; b <= std::numeric_limits<base>::max(); ++b) {
EXPECT_EQ(byte(~b), ~byte(b));
for (int c = 0; c <= std::numeric_limits<base>::max(); ++c) {
EXPECT_EQ(byte(b & c), byte(b) & byte(c));
EXPECT_EQ(byte(b | c), byte(b) | byte(c));
EXPECT_EQ(byte(b ^ c), byte(b) ^ byte(c));
byte o{};
EXPECT_EQ(byte(b & c), (o = byte(b)) &= byte(c));
EXPECT_EQ(byte(b | c), (o = byte(b)) |= byte(c));
EXPECT_EQ(byte(b ^ c), (o = byte(b)) ^= byte(c));
}
for (int s = 0; s < int(sizeof(byte)); ++s) {
EXPECT_EQ(byte(b << s), byte(b) << s);
EXPECT_EQ(byte(b >> s), byte(b) >> s);
byte o{};
EXPECT_EQ(byte(b << s), (o = byte(b)) <<= s);
EXPECT_EQ(byte(b >> s), (o = byte(b)) >>= s);
}
}
}
TEST_F(ByteTest, to_integer) {
EXPECT_EQ(7, folly::to_integer<int>(byte(7)));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment