File indexing completed on 2025-01-18 09:39:20
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
0017 #define BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
0018
0019 #include <cstddef>
0020 #include <locale>
0021 #include <string>
0022 #include <boost/core/enable_if.hpp>
0023 #include <boost/log/detail/config.hpp>
0024 #include <boost/log/detail/is_character_type.hpp>
0025 #include <boost/log/detail/header.hpp>
0026
0027 #ifdef BOOST_HAS_PRAGMA_ONCE
0028 #pragma once
0029 #endif
0030
0031 namespace boost {
0032
0033 BOOST_LOG_OPEN_NAMESPACE
0034
0035 namespace aux {
0036
0037
0038
0039
0040
0041 BOOST_LOG_API bool code_convert_impl(const wchar_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0042
0043 BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale());
0044
0045 #if !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS)
0046 #if !defined(BOOST_NO_CXX11_CHAR16_T)
0047
0048 BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0049
0050 BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::u16string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0051
0052 BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale());
0053 #endif
0054 #if !defined(BOOST_NO_CXX11_CHAR32_T)
0055
0056 BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0057
0058 BOOST_LOG_API bool code_convert_impl(const char* str1, std::size_t len, std::u32string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0059
0060 BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::wstring& str2, std::size_t max_size, std::locale const& loc = std::locale());
0061 #endif
0062 #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_CHAR32_T)
0063
0064 BOOST_LOG_API bool code_convert_impl(const char16_t* str1, std::size_t len, std::u32string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0065
0066 BOOST_LOG_API bool code_convert_impl(const char32_t* str1, std::size_t len, std::u16string& str2, std::size_t max_size, std::locale const& loc = std::locale());
0067 #endif
0068 #endif
0069
0070
0071 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0072 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
0073 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& = std::locale())
0074 {
0075 std::size_t size_left = str2.size() < max_size ? max_size - str2.size() : static_cast< std::size_t >(0u);
0076 const bool overflow = len > size_left;
0077 str2.append(reinterpret_cast< const TargetCharT* >(str1), overflow ? size_left : len);
0078 return !overflow;
0079 }
0080
0081
0082 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0083 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
0084 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale())
0085 {
0086 return aux::code_convert(str1.data(), str1.size(), str2, max_size, loc);
0087 }
0088
0089
0090 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0091 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
0092 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
0093 {
0094 return aux::code_convert(str1.data(), str1.size(), str2, str2.max_size(), loc);
0095 }
0096
0097 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0098 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
0099 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
0100 {
0101 return aux::code_convert(str1, len, str2, str2.max_size(), loc);
0102 }
0103
0104
0105 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0106 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
0107 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
0108 {
0109 return aux::code_convert_impl(str1.c_str(), str1.size(), str2, str2.max_size(), loc);
0110 }
0111
0112
0113 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0114 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
0115 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
0116 {
0117 return aux::code_convert_impl(str1, len, str2, str2.max_size(), loc);
0118 }
0119
0120
0121 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0122 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
0123 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale())
0124 {
0125 return aux::code_convert_impl(str1.c_str(), str1.size(), str2, max_size, loc);
0126 }
0127
0128
0129 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
0130 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
0131 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::size_t max_size, std::locale const& loc = std::locale())
0132 {
0133 return aux::code_convert_impl(str1, len, str2, max_size, loc);
0134 }
0135
0136
0137 inline std::string const& to_narrow(std::string const& str)
0138 {
0139 return str;
0140 }
0141
0142
0143 inline std::string const& to_narrow(std::string const& str, std::locale const&)
0144 {
0145 return str;
0146 }
0147
0148
0149 inline std::string to_narrow(std::wstring const& str, std::locale const& loc = std::locale())
0150 {
0151 std::string res;
0152 aux::code_convert_impl(str.c_str(), str.size(), res, res.max_size(), loc);
0153 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_HAS_NRVO)
0154 return static_cast< std::string&& >(res);
0155 #else
0156 return res;
0157 #endif
0158 }
0159
0160
0161 inline std::wstring const& to_wide(std::wstring const& str)
0162 {
0163 return str;
0164 }
0165
0166
0167 inline std::wstring const& to_wide(std::wstring const& str, std::locale const&)
0168 {
0169 return str;
0170 }
0171
0172
0173 inline std::wstring to_wide(std::string const& str, std::locale const& loc = std::locale())
0174 {
0175 std::wstring res;
0176 aux::code_convert_impl(str.c_str(), str.size(), res, res.max_size(), loc);
0177 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_HAS_NRVO)
0178 return static_cast< std::wstring&& >(res);
0179 #else
0180 return res;
0181 #endif
0182 }
0183
0184 }
0185
0186 BOOST_LOG_CLOSE_NAMESPACE
0187
0188 }
0189
0190 #include <boost/log/detail/footer.hpp>
0191
0192 #endif