forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1297306 - part4:rename IsEnumFittingWithin with EnumTypeFitsWithi…
…n and move it to mfbt/EnumTypeTraits.h. r=froydnj With this change, we could share this EnumTypeTraits between files easily. MozReview-Commit-ID: 9Q2augati7l
- Loading branch information
1 parent
2286173
commit e54a266
Showing
8 changed files
with
215 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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/. */ | ||
|
||
/* Type traits for enums. */ | ||
|
||
#ifndef mozilla_EnumTypeTraits_h | ||
#define mozilla_EnumTypeTraits_h | ||
|
||
#include <type_traits> | ||
|
||
namespace mozilla { | ||
|
||
namespace detail { | ||
|
||
template<size_t EnumSize, bool EnumSigned, size_t StorageSize, bool StorageSigned> | ||
struct EnumFitsWithinHelper; | ||
|
||
// Signed enum, signed storage. | ||
template<size_t EnumSize, size_t StorageSize> | ||
struct EnumFitsWithinHelper<EnumSize, true, StorageSize, true> | ||
: public std::integral_constant<bool, (EnumSize <= StorageSize)> | ||
{}; | ||
|
||
// Signed enum, unsigned storage. | ||
template<size_t EnumSize, size_t StorageSize> | ||
struct EnumFitsWithinHelper<EnumSize, true, StorageSize, false> | ||
: public std::integral_constant<bool, false> | ||
{}; | ||
|
||
// Unsigned enum, signed storage. | ||
template<size_t EnumSize, size_t StorageSize> | ||
struct EnumFitsWithinHelper<EnumSize, false, StorageSize, true> | ||
: public std::integral_constant<bool, (EnumSize * 2 <= StorageSize)> | ||
{}; | ||
|
||
// Unsigned enum, unsigned storage. | ||
template<size_t EnumSize, size_t StorageSize> | ||
struct EnumFitsWithinHelper<EnumSize, false, StorageSize, false> | ||
: public std::integral_constant<bool, (EnumSize <= StorageSize)> | ||
{}; | ||
|
||
} // namespace detail | ||
|
||
/* | ||
* Type trait that determines whether the enum type T can fit within the | ||
* integral type Storage without data loss. This trait should be used with | ||
* caution with an enum type whose underlying type has not been explicitly | ||
* specified: for such enums, the C++ implementation is free to choose a type | ||
* no smaller than int whose range encompasses all possible values of the enum. | ||
* So for an enum with only small non-negative values, the underlying type may | ||
* be either int or unsigned int, depending on the whims of the implementation. | ||
*/ | ||
template<typename T, typename Storage> | ||
struct EnumTypeFitsWithin | ||
: public detail::EnumFitsWithinHelper< | ||
sizeof(T), | ||
std::is_signed<typename std::underlying_type<T>::type>::value, | ||
sizeof(Storage), | ||
std::is_signed<Storage>::value | ||
> | ||
{ | ||
static_assert(std::is_enum<T>::value, "must provide an enum type"); | ||
static_assert(std::is_integral<Storage>::value, "must provide an integral type"); | ||
}; | ||
|
||
} // namespace mozilla | ||
|
||
#endif /* mozilla_EnumTypeTraits_h */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* 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/EnumTypeTraits.h" | ||
|
||
using namespace mozilla; | ||
|
||
/* Feature check for EnumTypeFitsWithin. */ | ||
|
||
#define MAKE_FIXED_EMUM_FOR_TYPE(IntType) \ | ||
enum FixedEnumFor_##IntType : IntType { \ | ||
A_##IntType, \ | ||
B_##IntType, \ | ||
C_##IntType, \ | ||
}; | ||
|
||
template<typename EnumType, typename IntType> | ||
static void | ||
TestShouldFit() | ||
{ | ||
static_assert(EnumTypeFitsWithin<EnumType, IntType>::value, | ||
"Should fit within exact/promoted integral type"); | ||
} | ||
|
||
template<typename EnumType, typename IntType> | ||
static void | ||
TestShouldNotFit() | ||
{ | ||
static_assert(!EnumTypeFitsWithin<EnumType, IntType>::value, | ||
"Should not fit within"); | ||
} | ||
|
||
int | ||
main() | ||
{ | ||
// check for int8_t | ||
MAKE_FIXED_EMUM_FOR_TYPE(int8_t); | ||
TestShouldFit<FixedEnumFor_int8_t, int8_t>(); | ||
TestShouldFit<FixedEnumFor_int8_t, int16_t>(); | ||
TestShouldFit<FixedEnumFor_int8_t, int32_t>(); | ||
TestShouldFit<FixedEnumFor_int8_t, int64_t>(); | ||
|
||
TestShouldNotFit<FixedEnumFor_int8_t, uint8_t>(); | ||
TestShouldNotFit<FixedEnumFor_int8_t, uint16_t>(); | ||
TestShouldNotFit<FixedEnumFor_int8_t, uint32_t>(); | ||
TestShouldNotFit<FixedEnumFor_int8_t, uint64_t>(); | ||
|
||
// check for uint8_t | ||
MAKE_FIXED_EMUM_FOR_TYPE(uint8_t); | ||
TestShouldFit<FixedEnumFor_uint8_t, uint8_t>(); | ||
TestShouldFit<FixedEnumFor_uint8_t, uint16_t>(); | ||
TestShouldFit<FixedEnumFor_uint8_t, uint32_t>(); | ||
TestShouldFit<FixedEnumFor_uint8_t, uint64_t>(); | ||
|
||
TestShouldNotFit<FixedEnumFor_uint8_t, int8_t>(); | ||
TestShouldFit<FixedEnumFor_uint8_t, int16_t>(); | ||
TestShouldFit<FixedEnumFor_uint8_t, int32_t>(); | ||
TestShouldFit<FixedEnumFor_uint8_t, int64_t>(); | ||
|
||
// check for int16_t | ||
MAKE_FIXED_EMUM_FOR_TYPE(int16_t); | ||
TestShouldNotFit<FixedEnumFor_int16_t, int8_t>(); | ||
TestShouldFit<FixedEnumFor_int16_t, int16_t>(); | ||
TestShouldFit<FixedEnumFor_int16_t, int32_t>(); | ||
TestShouldFit<FixedEnumFor_int16_t, int64_t>(); | ||
|
||
TestShouldNotFit<FixedEnumFor_int16_t, uint8_t>(); | ||
TestShouldNotFit<FixedEnumFor_int16_t, uint16_t>(); | ||
TestShouldNotFit<FixedEnumFor_int16_t, uint32_t>(); | ||
TestShouldNotFit<FixedEnumFor_int16_t, uint64_t>(); | ||
|
||
// check for uint16_t | ||
MAKE_FIXED_EMUM_FOR_TYPE(uint16_t); | ||
TestShouldNotFit<FixedEnumFor_uint16_t, uint8_t>(); | ||
TestShouldFit<FixedEnumFor_uint16_t, uint16_t>(); | ||
TestShouldFit<FixedEnumFor_uint16_t, uint32_t>(); | ||
TestShouldFit<FixedEnumFor_uint16_t, uint64_t>(); | ||
|
||
TestShouldNotFit<FixedEnumFor_uint16_t, int8_t>(); | ||
TestShouldNotFit<FixedEnumFor_uint16_t, int16_t>(); | ||
TestShouldFit<FixedEnumFor_uint16_t, int32_t>(); | ||
TestShouldFit<FixedEnumFor_uint16_t, int64_t>(); | ||
|
||
// check for int32_t | ||
MAKE_FIXED_EMUM_FOR_TYPE(int32_t); | ||
TestShouldNotFit<FixedEnumFor_int32_t, int8_t>(); | ||
TestShouldNotFit<FixedEnumFor_int32_t, int16_t>(); | ||
TestShouldFit<FixedEnumFor_int32_t, int32_t>(); | ||
TestShouldFit<FixedEnumFor_int32_t, int64_t>(); | ||
|
||
TestShouldNotFit<FixedEnumFor_int32_t, uint8_t>(); | ||
TestShouldNotFit<FixedEnumFor_int32_t, uint16_t>(); | ||
TestShouldNotFit<FixedEnumFor_int32_t, uint32_t>(); | ||
TestShouldNotFit<FixedEnumFor_int32_t, uint64_t>(); | ||
|
||
// check for uint32_t | ||
MAKE_FIXED_EMUM_FOR_TYPE(uint32_t); | ||
TestShouldNotFit<FixedEnumFor_uint32_t, uint8_t>(); | ||
TestShouldNotFit<FixedEnumFor_uint32_t, uint16_t>(); | ||
TestShouldFit<FixedEnumFor_uint32_t, uint32_t>(); | ||
TestShouldFit<FixedEnumFor_uint32_t, uint64_t>(); | ||
|
||
TestShouldNotFit<FixedEnumFor_uint32_t, int8_t>(); | ||
TestShouldNotFit<FixedEnumFor_uint32_t, int16_t>(); | ||
TestShouldNotFit<FixedEnumFor_uint32_t, int32_t>(); | ||
TestShouldFit<FixedEnumFor_uint32_t, int64_t>(); | ||
|
||
// check for int64_t | ||
MAKE_FIXED_EMUM_FOR_TYPE(int64_t); | ||
TestShouldNotFit<FixedEnumFor_int64_t, int8_t>(); | ||
TestShouldNotFit<FixedEnumFor_int64_t, int16_t>(); | ||
TestShouldNotFit<FixedEnumFor_int64_t, int32_t>(); | ||
TestShouldFit<FixedEnumFor_int64_t, int64_t>(); | ||
|
||
TestShouldNotFit<FixedEnumFor_int64_t, uint8_t>(); | ||
TestShouldNotFit<FixedEnumFor_int64_t, uint16_t>(); | ||
TestShouldNotFit<FixedEnumFor_int64_t, uint32_t>(); | ||
TestShouldNotFit<FixedEnumFor_int64_t, uint64_t>(); | ||
|
||
// check for uint64_t | ||
MAKE_FIXED_EMUM_FOR_TYPE(uint64_t); | ||
TestShouldNotFit<FixedEnumFor_uint64_t, uint8_t>(); | ||
TestShouldNotFit<FixedEnumFor_uint64_t, uint16_t>(); | ||
TestShouldNotFit<FixedEnumFor_uint64_t, uint32_t>(); | ||
TestShouldFit<FixedEnumFor_uint64_t, uint64_t>(); | ||
|
||
TestShouldNotFit<FixedEnumFor_uint64_t, int8_t>(); | ||
TestShouldNotFit<FixedEnumFor_uint64_t, int16_t>(); | ||
TestShouldNotFit<FixedEnumFor_uint64_t, int32_t>(); | ||
TestShouldNotFit<FixedEnumFor_uint64_t, int64_t>(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters