| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_BIT_CAST_H_ | 5 #ifndef BASE_BIT_CAST_H_ |
| 6 #define BASE_BIT_CAST_H_ | 6 #define BASE_BIT_CAST_H_ |
| 7 | 7 |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 #include <type_traits> | 9 #include <type_traits> |
| 10 | 10 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 "bit_cast requires source and destination to be the same size"); | 65 "bit_cast requires source and destination to be the same size"); |
| 66 | 66 |
| 67 #if ((defined(__GLIBCXX__) && (__GLIBCXX__ >= 20150422)) || \ | 67 #if ((defined(__GLIBCXX__) && (__GLIBCXX__ >= 20150422)) || \ |
| 68 defined(_LIBCPP_VERSION)) | 68 defined(_LIBCPP_VERSION)) |
| 69 // GCC 5.1 contains the first libstdc++ with is_trivially_copyable. | 69 // GCC 5.1 contains the first libstdc++ with is_trivially_copyable. |
| 70 // Assume libc++ Just Works: is_trivially_copyable added on May 13th 2011. | 70 // Assume libc++ Just Works: is_trivially_copyable added on May 13th 2011. |
| 71 static_assert(std::is_trivially_copyable<Dest>::value, | 71 static_assert(std::is_trivially_copyable<Dest>::value, |
| 72 "non-trivially-copyable bit_cast is undefined"); | 72 "non-trivially-copyable bit_cast is undefined"); |
| 73 static_assert(std::is_trivially_copyable<Source>::value, | 73 static_assert(std::is_trivially_copyable<Source>::value, |
| 74 "non-trivially-copyable bit_cast is undefined"); | 74 "non-trivially-copyable bit_cast is undefined"); |
| 75 #elif __has_feature(is_trivially_copyable) | 75 #elif HAS_FEATURE(is_trivially_copyable) |
| 76 // The compiler supports an equivalent intrinsic. | 76 // The compiler supports an equivalent intrinsic. |
| 77 static_assert(__is_trivially_copyable(Dest), | 77 static_assert(__is_trivially_copyable(Dest), |
| 78 "non-trivially-copyable bit_cast is undefined"); | 78 "non-trivially-copyable bit_cast is undefined"); |
| 79 static_assert(__is_trivially_copyable(Source), | 79 static_assert(__is_trivially_copyable(Source), |
| 80 "non-trivially-copyable bit_cast is undefined"); | 80 "non-trivially-copyable bit_cast is undefined"); |
| 81 #elif COMPILER_GCC | 81 #elif COMPILER_GCC |
| 82 // Fallback to compiler intrinsic on GCC and clang (which pretends to be | 82 // Fallback to compiler intrinsic on GCC and clang (which pretends to be |
| 83 // GCC). This isn't quite the same as is_trivially_copyable but it'll do for | 83 // GCC). This isn't quite the same as is_trivially_copyable but it'll do for |
| 84 // our purpose. | 84 // our purpose. |
| 85 static_assert(__has_trivial_copy(Dest), | 85 static_assert(__has_trivial_copy(Dest), |
| 86 "non-trivially-copyable bit_cast is undefined"); | 86 "non-trivially-copyable bit_cast is undefined"); |
| 87 static_assert(__has_trivial_copy(Source), | 87 static_assert(__has_trivial_copy(Source), |
| 88 "non-trivially-copyable bit_cast is undefined"); | 88 "non-trivially-copyable bit_cast is undefined"); |
| 89 #else | 89 #else |
| 90 // Do nothing, let the bots handle it. | 90 // Do nothing, let the bots handle it. |
| 91 #endif | 91 #endif |
| 92 | 92 |
| 93 Dest dest; | 93 Dest dest; |
| 94 memcpy(&dest, &source, sizeof(dest)); | 94 memcpy(&dest, &source, sizeof(dest)); |
| 95 return dest; | 95 return dest; |
| 96 } | 96 } |
| 97 | 97 |
| 98 #endif // BASE_BIT_CAST_H_ | 98 #endif // BASE_BIT_CAST_H_ |
| OLD | NEW |