| 861 | template <typename CharT, |
| 862 | template <typename> class allocator_t = default_allocator> |
| 863 | class basic_string_borrower final { |
| 864 | using stl_string = std::basic_string<CharT>; |
| 865 | using allocator_type = allocator_t<stl_string>; |
| 866 | |
| 867 | static inline allocator_type &get_allocator() |
| 868 | { |
| 869 | static allocator_type allocator; |
| 870 | return allocator; |
| 871 | } |
| 872 | |
| 873 | void *m_data = nullptr; |
| 874 | bool m_own = false; |
| 875 | |
| 876 | void destroy() |
| 877 | { |
| 878 | if (m_own && m_data) { |
| 879 | stl_string *p = static_cast<stl_string *>(m_data); |
| 880 | p->~basic_string(); |
| 881 | get_allocator().deallocate(p, 1); |
| 882 | } |
| 883 | m_data = nullptr; |
| 884 | m_own = false; |
| 885 | } |
| 886 | |
| 887 | public: |
| 888 | basic_string_borrower() noexcept = default; |
| 889 | |
| 890 | basic_string_borrower(const CharT *str) noexcept : m_data(const_cast<CharT *>(str)), m_own(false) {} |
| 891 | |
| 892 | basic_string_borrower(const stl_string &str) noexcept : m_data(const_cast<CharT *>(str.data())), m_own(false) {} |
| 893 | |
| 894 | basic_string_borrower(stl_string &&str) : m_own(true) |
| 895 | { |
| 896 | stl_string *p = get_allocator().allocate(1); |
| 897 | ::new (p) stl_string(std::move(str)); |
| 898 | m_data = p; |
| 899 | } |
| 900 | |
| 901 | basic_string_borrower(const basic_string_borrower &other) : m_own(other.m_own) |
| 902 | { |
| 903 | if (other.m_own) { |
| 904 | stl_string *p = get_allocator().allocate(1); |
| 905 | ::new (p) stl_string(*static_cast<const stl_string *>(other.m_data)); |
| 906 | m_data = p; |
| 907 | } |
| 908 | else |
| 909 | m_data = other.m_data; |
| 910 | } |
| 911 | |
| 912 | basic_string_borrower(basic_string_borrower &&other) noexcept |
| 913 | : m_data(other.m_data), m_own(other.m_own) |
| 914 | { |
| 915 | other.m_data = nullptr; |
| 916 | other.m_own = false; |
| 917 | } |
| 918 | |
| 919 | basic_string_borrower &operator=(const basic_string_borrower &other) |
| 920 | { |