Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:54:15

0001 //
0002 // Copyright (c) 2023-2025 Alexander Grund
0003 //
0004 // Distributed under the Boost Software License, Version 1.0.
0005 // https://www.boost.org/LICENSE_1_0.txt
0006 
0007 #ifndef BOOST_LOCALE_DETAIL_ANY_STRING_HPP_INCLUDED
0008 #define BOOST_LOCALE_DETAIL_ANY_STRING_HPP_INCLUDED
0009 
0010 #include <boost/locale/config.hpp>
0011 #include <boost/assert.hpp>
0012 #include <boost/core/detail/string_view.hpp>
0013 #include <memory>
0014 #include <stdexcept>
0015 #include <string>
0016 
0017 /// \cond INTERNAL
0018 namespace boost { namespace locale { namespace detail {
0019     /// Type-erased std::basic_string
0020     class any_string {
0021         struct BOOST_SYMBOL_VISIBLE base {
0022             virtual ~base() = default;
0023             virtual base* clone() const = 0;
0024 
0025         protected:
0026             base() = default;
0027             base(const base&) = default;
0028             base(base&&) = delete;
0029             base& operator=(const base&) = default;
0030             base& operator=(base&&) = delete;
0031         };
0032         template<typename Char>
0033         struct BOOST_SYMBOL_VISIBLE impl : base {
0034             explicit impl(const core::basic_string_view<Char> value) : s(value) {}
0035             impl* clone() const override { return new impl(*this); }
0036             std::basic_string<Char> s;
0037         };
0038 
0039         std::unique_ptr<const base> s_;
0040 
0041     public:
0042         any_string() = default;
0043         any_string(const any_string& other) : s_(other.s_ ? other.s_->clone() : nullptr) {}
0044         any_string(any_string&&) = default;
0045         any_string& operator=(any_string other) // Covers the copy and move assignment
0046         {
0047             s_.swap(other.s_);
0048             return *this;
0049         }
0050 
0051         template<typename Char>
0052         void set(const core::basic_string_view<Char> s)
0053         {
0054             BOOST_ASSERT(!s.empty());
0055             s_.reset(new impl<Char>(s));
0056         }
0057 
0058         template<typename Char>
0059         std::basic_string<Char> get() const
0060         {
0061             if(!s_)
0062                 throw std::bad_cast();
0063             return dynamic_cast<const impl<Char>&>(*s_).s;
0064         }
0065     };
0066 
0067 }}} // namespace boost::locale::detail
0068 
0069 /// \endcond
0070 
0071 #endif