1 | // |
2 | // Copyright 2017 The Abseil Authors. |
3 | // |
4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
5 | // you may not use this file except in compliance with the License. |
6 | // You may obtain a copy of the License at |
7 | // |
8 | // https://www.apache.org/licenses/LICENSE-2.0 |
9 | // |
10 | // Unless required by applicable law or agreed to in writing, software |
11 | // distributed under the License is distributed on an "AS IS" BASIS, |
12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | // See the License for the specific language governing permissions and |
14 | // limitations under the License. |
15 | // |
16 | |
17 | #ifndef ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_ |
18 | #define ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_ |
19 | |
20 | #include <string> |
21 | #include <type_traits> |
22 | #include <utility> |
23 | |
24 | #include "absl/base/port.h" |
25 | #include "absl/meta/type_traits.h" // for void_t |
26 | |
27 | namespace absl { |
28 | namespace strings_internal { |
29 | |
30 | // Is a subclass of true_type or false_type, depending on whether or not |
31 | // T has a __resize_default_init member. |
32 | template <typename string_type, typename = void> |
33 | struct ResizeUninitializedTraits { |
34 | using HasMember = std::false_type; |
35 | static void Resize(string_type* s, size_t new_size) { s->resize(new_size); } |
36 | }; |
37 | |
38 | // __resize_default_init is provided by libc++ >= 8.0 and by Google's internal |
39 | // ::string implementation. |
40 | template <typename string_type> |
41 | struct ResizeUninitializedTraits< |
42 | string_type, absl::void_t<decltype(std::declval<string_type&>() |
43 | .__resize_default_init(237))> > { |
44 | using HasMember = std::true_type; |
45 | static void Resize(string_type* s, size_t new_size) { |
46 | s->__resize_default_init(new_size); |
47 | } |
48 | }; |
49 | |
50 | // Returns true if the std::string implementation supports a resize where |
51 | // the new characters added to the std::string are left untouched. |
52 | // |
53 | // (A better name might be "STLStringSupportsUninitializedResize", alluding to |
54 | // the previous function.) |
55 | template <typename string_type> |
56 | inline constexpr bool STLStringSupportsNontrashingResize(string_type*) { |
57 | return ResizeUninitializedTraits<string_type>::HasMember::value; |
58 | } |
59 | |
60 | // Like str->resize(new_size), except any new characters added to "*str" as a |
61 | // result of resizing may be left uninitialized, rather than being filled with |
62 | // '0' bytes. Typically used when code is then going to overwrite the backing |
63 | // store of the std::string with known data. |
64 | template <typename string_type, typename = void> |
65 | inline void STLStringResizeUninitialized(string_type* s, size_t new_size) { |
66 | ResizeUninitializedTraits<string_type>::Resize(s, new_size); |
67 | } |
68 | |
69 | } // namespace strings_internal |
70 | } // namespace absl |
71 | |
72 | #endif // ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_ |
73 | |