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