Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Side by Side Diff: base/bit_cast.h

Issue 2583353002: Make bit_cast fail if the source or dest are not trivially copyable (Closed)
Patch Set: bitcast: fixes Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/bit_cast_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/template_util.h"
12 #include "build/build_config.h" 13 #include "build/build_config.h"
13 14
14 // bit_cast<Dest,Source> is a template function that implements the equivalent 15 // bit_cast<Dest,Source> is a template function that implements the equivalent
15 // of "*reinterpret_cast<Dest*>(&source)". We need this in very low-level 16 // of "*reinterpret_cast<Dest*>(&source)". We need this in very low-level
16 // functions like the protobuf library and fast math support. 17 // functions like the protobuf library and fast math support.
17 // 18 //
18 // float f = 3.14159265358979; 19 // float f = 3.14159265358979;
19 // int i = bit_cast<int32_t>(f); 20 // int i = bit_cast<int32_t>(f);
20 // // i = 0x40490fdb 21 // // i = 0x40490fdb
21 // 22 //
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 // 57 //
57 // Fortunately memcpy() is very fast. In optimized mode, compilers replace 58 // Fortunately memcpy() is very fast. In optimized mode, compilers replace
58 // calls to memcpy() with inline object code when the size argument is a 59 // calls to memcpy() with inline object code when the size argument is a
59 // compile-time constant. On a 32-bit system, memcpy(d,s,4) compiles to one 60 // compile-time constant. On a 32-bit system, memcpy(d,s,4) compiles to one
60 // load and one store, and memcpy(d,s,8) compiles to two loads and two stores. 61 // load and one store, and memcpy(d,s,8) compiles to two loads and two stores.
61 62
62 template <class Dest, class Source> 63 template <class Dest, class Source>
63 inline Dest bit_cast(const Source& source) { 64 inline Dest bit_cast(const Source& source) {
64 static_assert(sizeof(Dest) == sizeof(Source), 65 static_assert(sizeof(Dest) == sizeof(Source),
65 "bit_cast requires source and destination to be the same size"); 66 "bit_cast requires source and destination to be the same size");
66 67 static_assert(base::is_trivially_copyable<Dest>::value,
67 #if (__GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ >= 1) || \ 68 "bit_cast requires the destination type to be copyable");
68 (defined(__clang__) && defined(_LIBCPP_VERSION))) 69 static_assert(base::is_trivially_copyable<Source>::value,
69 // GCC 5.1 contains the first libstdc++ with is_trivially_copyable. 70 "bit_cast requires the source type to be copyable");
70 // Assume libc++ Just Works: is_trivially_copyable added on May 13th 2011.
71 // However, with libc++ when GCC is the compiler the trait is buggy, see
72 // crbug.com/607158, so fall back to the less strict variant for non-clang.
73 static_assert(std::is_trivially_copyable<Dest>::value,
74 "non-trivially-copyable bit_cast is undefined");
75 static_assert(std::is_trivially_copyable<Source>::value,
76 "non-trivially-copyable bit_cast is undefined");
77 #elif HAS_FEATURE(is_trivially_copyable)
78 // The compiler supports an equivalent intrinsic.
79 static_assert(__is_trivially_copyable(Dest),
80 "non-trivially-copyable bit_cast is undefined");
81 static_assert(__is_trivially_copyable(Source),
82 "non-trivially-copyable bit_cast is undefined");
83 #elif COMPILER_GCC
84 // Fallback to compiler intrinsic on GCC and clang (which pretends to be
85 // GCC). This isn't quite the same as is_trivially_copyable but it'll do for
86 // our purpose.
87 static_assert(__has_trivial_copy(Dest),
88 "non-trivially-copyable bit_cast is undefined");
89 static_assert(__has_trivial_copy(Source),
90 "non-trivially-copyable bit_cast is undefined");
91 #else
92 // Do nothing, let the bots handle it.
93 #endif
94 71
95 Dest dest; 72 Dest dest;
96 memcpy(&dest, &source, sizeof(dest)); 73 memcpy(&dest, &source, sizeof(dest));
97 return dest; 74 return dest;
98 } 75 }
99 76
100 #endif // BASE_BIT_CAST_H_ 77 #endif // BASE_BIT_CAST_H_
OLDNEW
« no previous file with comments | « no previous file | base/bit_cast_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698