1// Helper for accessing __cxx11::string from the ABI -*- C++ -*- 2 3// Copyright (C) 2014-2022 Free Software Foundation, Inc. 4// 5// This file is part of the GNU ISO C++ Library. This library is free 6// software; you can redistribute it and/or modify it under the 7// terms of the GNU General Public License as published by the 8// Free Software Foundation; either version 3, or (at your option) 9// any later version. 10 11// This library is distributed in the hope that it will be useful, 12// but WITHOUT ANY WARRANTY; without even the implied warranty of 13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14// GNU General Public License for more details. 15 16// Under Section 7 of GPL version 3, you are granted additional 17// permissions described in the GCC Runtime Library Exception, version 18// 3.1, as published by the Free Software Foundation. 19 20// You should have received a copy of the GNU General Public License and 21// a copy of the GCC Runtime Library Exception along with this program; 22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23// <http://www.gnu.org/licenses/>. 24 25 26#define _GLIBCXX_USE_CXX11_ABI 1 27#define __sso_string __sso_stringxxx 28#include <string> 29#include <stdexcept> 30#undef __sso_string 31 32#if ! _GLIBCXX_USE_DUAL_ABI 33# error This file should not be compiled for this configuration. 34#endif 35 36namespace std _GLIBCXX_VISIBILITY(default) 37{ 38_GLIBCXX_BEGIN_NAMESPACE_VERSION 39 40#pragma GCC diagnostic push 41#pragma GCC diagnostic ignored "-Wabi-tag" 42 // Redefine __sso_string so that we can define and export its members 43 // in terms of the SSO std::string. 44 struct __sso_string 45 { 46 struct __str 47 { 48 const char* _M_p; 49 size_t _M_string_length; 50 char _M_local_buf[16]; 51 }; 52 53 union { 54 __str _M_s; 55 char _M_bytes[sizeof(_M_s)]; 56 std::string _M_str; 57 }; 58 59 __sso_string(); 60 __sso_string(const std::string& s); 61 __sso_string(const char*, size_t n); 62 __sso_string(const __sso_string&) noexcept; 63 __sso_string& operator=(const __sso_string&) noexcept; 64 ~__sso_string(); 65 __sso_string(__sso_string&&) noexcept; 66 __sso_string& operator=(__sso_string&&) noexcept; 67 }; 68#pragma GCC diagnostic pop 69 70 __sso_string::__sso_string() : _M_str() { } 71 72#if _GLIBCXX_USE_CXX11_ABI 73 static_assert(sizeof(__sso_string) == sizeof(std::string), 74 "sizeof(std::string) has changed"); 75 static_assert(alignof(__sso_string) == alignof(std::string), 76 "alignof(std::string) has changed"); 77 78 // This constructor is defined in src/c++11/cow-stdexcept.cc for COW strings 79 __sso_string::__sso_string(const std::string& s) : _M_str(s) { } 80#endif 81 82 __sso_string::__sso_string(const char* s, size_t n) : _M_str(s, n) { } 83 84 __sso_string::__sso_string(const __sso_string& s) noexcept 85 : _M_str(s._M_str) { } 86 87 __sso_string& 88 __sso_string::operator=(const __sso_string& s) noexcept 89 { 90 _M_str = s._M_str; 91 return *this; 92 } 93 94 __sso_string::~__sso_string() { _M_str.~basic_string(); } 95 96 __sso_string::__sso_string(__sso_string&& s) noexcept 97 : _M_str(std::move(s._M_str)) { } 98 99 __sso_string& 100 __sso_string::operator=(__sso_string&& s) noexcept 101 { 102 _M_str = std::move(s._M_str); 103 return *this; 104 } 105 106_GLIBCXX_END_NAMESPACE_VERSION 107} // namespace 108