Files
yoga/yoga/bits/NumericBitfield.h
Nick Gerleman 9b99e4fc22 C++ style enums 8/N: Direction (#1392)
Summary:
X-link: https://github.com/facebook/react-native/pull/39483

Pull Request resolved: https://github.com/facebook/yoga/pull/1392

Moves internal usages of YGDirection to Direction.

bypass-github-export-checks

Changelog: [Internal]

Reviewed By: rshest

Differential Revision: D49335231

fbshipit-source-id: 459fe820c91be88734cebaa8655cd3f0afda83bf
2023-09-19 16:30:02 -07:00

68 lines
1.7 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <bitset>
#include <cstdint>
#include <cstdio>
#include <type_traits>
#include <yoga/YGEnums.h>
#include <yoga/enums/YogaEnums.h>
namespace facebook::yoga::details {
constexpr uint8_t log2ceilFn(uint8_t n) {
return n < 1 ? 0 : (1 + log2ceilFn(n / 2));
}
constexpr uint32_t mask(uint8_t bitWidth, uint8_t index) {
return ((1u << bitWidth) - 1u) << index;
}
} // namespace facebook::yoga::details
namespace facebook::yoga {
// The number of bits necessary to represent enums defined with YG_ENUM_SEQ_DECL
template <
typename Enum,
std::enable_if_t<(ordinalCount<Enum>() > 0), bool> = true>
constexpr uint8_t minimumBitCount() {
return details::log2ceilFn(ordinalCount<Enum>() - 1);
}
template <typename Enum>
constexpr Enum getEnumData(uint32_t flags, uint8_t index) {
return static_cast<Enum>(
(flags & details::mask(minimumBitCount<Enum>(), index)) >> index);
}
template <typename Enum, typename Value>
void setEnumData(uint32_t& flags, uint8_t index, Value newValue) {
flags =
(flags &
~static_cast<uint32_t>(details::mask(minimumBitCount<Enum>(), index))) |
((static_cast<uint32_t>(newValue) << index) &
(details::mask(minimumBitCount<Enum>(), index)));
}
constexpr bool getBooleanData(uint32_t flags, uint8_t index) {
return (flags >> index) & 1;
}
inline void setBooleanData(uint32_t& flags, uint8_t index, bool value) {
if (value) {
flags |= 1 << index;
} else {
flags &= ~(1 << index);
}
}
} // namespace facebook::yoga