resize_uninitialized.h
Go to the documentation of this file.
00001 //
00002 // Copyright 2017 The Abseil Authors.
00003 //
00004 // Licensed under the Apache License, Version 2.0 (the "License");
00005 // you may not use this file except in compliance with the License.
00006 // You may obtain a copy of the License at
00007 //
00008 //      https://www.apache.org/licenses/LICENSE-2.0
00009 //
00010 // Unless required by applicable law or agreed to in writing, software
00011 // distributed under the License is distributed on an "AS IS" BASIS,
00012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013 // See the License for the specific language governing permissions and
00014 // limitations under the License.
00015 //
00016 
00017 #ifndef ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_
00018 #define ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_
00019 
00020 #include <string>
00021 #include <type_traits>
00022 #include <utility>
00023 
00024 #include "absl/base/port.h"
00025 #include "absl/meta/type_traits.h"  //  for void_t
00026 
00027 namespace absl {
00028 namespace strings_internal {
00029 
00030 // Is a subclass of true_type or false_type, depending on whether or not
00031 // T has a __resize_default_init member.
00032 template <typename string_type, typename = void>
00033 struct ResizeUninitializedTraits {
00034   using HasMember = std::false_type;
00035   static void Resize(string_type* s, size_t new_size) { s->resize(new_size); }
00036 };
00037 
00038 // __resize_default_init is provided by libc++ >= 8.0 and by Google's internal
00039 // ::string implementation.
00040 template <typename string_type>
00041 struct ResizeUninitializedTraits<
00042     string_type, absl::void_t<decltype(std::declval<string_type&>()
00043                                            .__resize_default_init(237))> > {
00044   using HasMember = std::true_type;
00045   static void Resize(string_type* s, size_t new_size) {
00046     s->__resize_default_init(new_size);
00047   }
00048 };
00049 
00050 // Returns true if the std::string implementation supports a resize where
00051 // the new characters added to the std::string are left untouched.
00052 //
00053 // (A better name might be "STLStringSupportsUninitializedResize", alluding to
00054 // the previous function.)
00055 template <typename string_type>
00056 inline constexpr bool STLStringSupportsNontrashingResize(string_type*) {
00057   return ResizeUninitializedTraits<string_type>::HasMember::value;
00058 }
00059 
00060 // Like str->resize(new_size), except any new characters added to "*str" as a
00061 // result of resizing may be left uninitialized, rather than being filled with
00062 // '0' bytes. Typically used when code is then going to overwrite the backing
00063 // store of the std::string with known data.
00064 template <typename string_type, typename = void>
00065 inline void STLStringResizeUninitialized(string_type* s, size_t new_size) {
00066   ResizeUninitializedTraits<string_type>::Resize(s, new_size);
00067 }
00068 
00069 }  // namespace strings_internal
00070 }  // namespace absl
00071 
00072 #endif  // ABSL_STRINGS_INTERNAL_RESIZE_UNINITIALIZED_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15