Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:27

0001 // Copyright (c) 2022 Dvir Yitzchaki.
0002 // Use, modification and distribution are subject to the Boost Software License,
0003 // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
0004 
0005 #ifndef BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP
0006 #define BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP
0007 
0008 #ifdef BOOST_NO_CXX17_HDR_CHARCONV
0009 #error "This header requires <charconv> which is unavailable"
0010 #endif // BOOST_NO_CXX17_HDR_CHARCONV
0011 
0012 #ifdef BOOST_NO_CXX17_STRUCTURED_BINDINGS
0013 #error "This header requires structured bindings which is unavailable"
0014 #endif // BOOST_NO_CXX17_STRUCTURED_BINDINGS
0015 
0016 #ifdef BOOST_NO_CXX17_IF_CONSTEXPR
0017 #error "This header requires constexpr if which is unavailable"
0018 #endif // BOOST_NO_CXX17_IF_CONSTEXPR
0019 
0020 #include <boost/convert/base.hpp>
0021 #include <charconv>
0022 #include <type_traits>
0023 
0024 namespace boost { namespace cnv { struct charconv; }}
0025 
0026 /// @brief std::to/from_chars-based extended converter
0027 /// @details The converter offers good overall performance and moderate formatting facilities.
0028 
0029 struct boost::cnv::charconv : boost::cnv::cnvbase<boost::cnv::charconv>
0030 {
0031     using this_type = boost::cnv::charconv;
0032     using base_type = boost::cnv::cnvbase<this_type>;
0033 
0034     private:
0035 
0036     friend struct boost::cnv::cnvbase<this_type>;
0037 
0038     template<typename in_type>
0039     cnv::range<char*>
0040     to_str(in_type value_in, char* buf) const
0041     {
0042         const auto [ptr, ec] = [&]{
0043             if constexpr (std::is_integral_v<in_type>) {
0044                 return std::to_chars(buf, buf + bufsize_, value_in, static_cast<int>(base_));
0045             } else {
0046                 return std::to_chars(buf, buf + bufsize_, value_in, chars_format(), precision_);
0047             }
0048         }();
0049         bool success = ec == std::errc{};
0050 
0051         return cnv::range<char*>(buf, success ? ptr : buf);
0052     }
0053 
0054     template<typename string_type, typename out_type>
0055     void
0056     str_to(cnv::range<string_type> range, optional<out_type>& result_out) const
0057     {
0058         out_type result = boost::make_default<out_type>();
0059         const auto [ptr, ec] = [&]{
0060             if constexpr (std::is_integral_v<out_type>) {
0061                 return std::from_chars(&*range.begin(), &*range.end(), result, static_cast<int>(base_));
0062             } else {
0063                 return std::from_chars(&*range.begin(), &*range.end(), result, chars_format());
0064             }
0065         }();
0066 
0067         if (ec == std::errc{})
0068             result_out = result;
0069     }
0070 
0071     std::chars_format chars_format() const
0072     {
0073         static constexpr std::chars_format format[] =
0074         {
0075             std::chars_format::fixed,
0076             std::chars_format::scientific,
0077             std::chars_format::hex
0078         };
0079         return format[int(notation_)];
0080     }
0081 
0082     std::chars_format fmt_ = std::chars_format::fixed;
0083 };
0084 
0085 #endif // BOOST_CONVERT_CHARCONV_BASED_CONVERTER_HPP