Bug 1656034 - Add UnderlyingValue() to mfbt. - r=froydnj

Differential Revision: https://phabricator.services.mozilla.com/D85495
This commit is contained in:
Jeff Gilbert 2020-07-31 23:04:44 +00:00
Родитель cae88d4491
Коммит fa50c42c4b
7 изменённых файлов: 60 добавлений и 12 удалений

Просмотреть файл

@ -3728,7 +3728,7 @@ static std::string ToString(const js::Scalar::Type type) {
break;
}
MOZ_ASSERT(false);
return std::string("#") + std::to_string(EnumValue(type));
return std::string("#") + std::to_string(UnderlyingValue(type));
}
/////////////////////////////////////////////////

Просмотреть файл

@ -208,7 +208,8 @@ struct NotLostData final {
UniquePtr<HostWebGLContext> inProcess;
webgl::ContextGenerationInfo state;
std::array<RefPtr<ClientWebGLExtensionBase>, EnumValue(WebGLExtensionID::Max)>
std::array<RefPtr<ClientWebGLExtensionBase>,
UnderlyingValue(WebGLExtensionID::Max)>
extensions;
explicit NotLostData(ClientWebGLContext& context);
@ -2076,7 +2077,7 @@ class ClientWebGLContext final : public nsICanvasRenderingContextInternal,
public:
bool IsExtensionEnabled(const WebGLExtensionID id) const {
return bool(mNotLost->extensions[EnumValue(id)]);
return bool(mNotLost->extensions[UnderlyingValue(id)]);
}
void AddCompressedFormat(GLenum);

Просмотреть файл

@ -121,7 +121,7 @@ RefPtr<ClientWebGLExtensionBase> ClientWebGLContext::GetExtension(
if (!IsSupported(ext, callerType)) return nullptr;
auto& extSlot = mNotLost->extensions[EnumValue(ext)];
auto& extSlot = mNotLost->extensions[UnderlyingValue(ext)];
if (MOZ_UNLIKELY(!extSlot)) {
extSlot = [&]() -> RefPtr<ClientWebGLExtensionBase> {
switch (ext) {

Просмотреть файл

@ -184,7 +184,7 @@ bool WebGLContext::ValidateAttribArraySetter(uint32_t setterElemSize,
static webgl::Limits MakeLimits(const WebGLContext& webgl) {
webgl::Limits limits;
for (const auto i : IntegerRange(EnumValue(WebGLExtensionID::Max))) {
for (const auto i : IntegerRange(UnderlyingValue(WebGLExtensionID::Max))) {
const auto ext = WebGLExtensionID(i);
limits.supportedExtensions[ext] = webgl.IsExtensionSupported(ext);
}

Просмотреть файл

@ -246,11 +246,6 @@ enum class WebGLExtensionID : uint8_t {
Max
};
template <typename T>
inline constexpr auto EnumValue(const T v) {
return static_cast<typename std::underlying_type<T>::type>(v);
}
class UniqueBuffer {
// Like UniquePtr<>, but for void* and malloc/calloc/free.
void* mBuffer;

Просмотреть файл

@ -84,6 +84,30 @@ struct EnumTypeFitsWithin
template <typename T>
struct MaxEnumValue; // no need to define the primary template
/**
* Get the underlying value of an enum, but typesafe.
*
* example:
*
* enum class Pet : int16_t {
* Cat,
* Dog,
* Fish
* };
* enum class Plant {
* Flower,
* Tree,
* Vine
* };
* UnderlyingValue(Pet::Fish) -> int16_t(2)
* UnderlyingValue(Plant::Tree) -> int(1)
*/
template <typename T>
inline constexpr auto UnderlyingValue(const T v) {
static_assert(std::is_enum_v<T>);
return static_cast<typename std::underlying_type<T>::type>(v);
}
} // namespace mozilla
#endif /* mozilla_EnumTypeTraits_h */

Просмотреть файл

@ -3,8 +3,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/IntegerTypeTraits.h"
#include "mozilla/Assertions.h"
#include "mozilla/EnumTypeTraits.h"
#include "mozilla/IntegerTypeTraits.h"
using namespace mozilla;
@ -29,7 +30,7 @@ static void TestShouldNotFit() {
"Should not fit within");
}
int main() {
void TestFitForTypes() {
// check for int8_t
MAKE_FIXED_EMUM_FOR_TYPE(int8_t);
TestShouldFit<FixedEnumFor_int8_t, int8_t>();
@ -125,6 +126,33 @@ int main() {
TestShouldNotFit<FixedEnumFor_uint64_t, int16_t>();
TestShouldNotFit<FixedEnumFor_uint64_t, int32_t>();
TestShouldNotFit<FixedEnumFor_uint64_t, int64_t>();
}
// -
template <typename T, typename U>
static constexpr void AssertSameTypeAndValue(T a, U b) {
static_assert(std::is_same_v<T, U>);
MOZ_ASSERT(a == b);
}
void TestUnderlyingValue() {
enum class Pet : int16_t { Cat, Dog, Fish };
enum class Plant { Flower, Tree, Vine };
AssertSameTypeAndValue(UnderlyingValue(Pet::Cat), int16_t(0));
AssertSameTypeAndValue(UnderlyingValue(Pet::Dog), int16_t(1));
AssertSameTypeAndValue(UnderlyingValue(Pet::Fish), int16_t(2));
AssertSameTypeAndValue(UnderlyingValue(Plant::Flower), int(0));
AssertSameTypeAndValue(UnderlyingValue(Plant::Tree), int(1));
AssertSameTypeAndValue(UnderlyingValue(Plant::Vine), int(2));
}
// -
int main() {
TestFitForTypes();
TestUnderlyingValue();
return 0;
}