2 * Copyright Andrey Semashev 2007 - 2015.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
8 * \file code_conversion.hpp
9 * \author Andrey Semashev
12 * \brief This header is the Boost.Log library implementation, see the library documentation
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
16 #ifndef BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
17 #define BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
22 #include <boost/core/enable_if.hpp>
23 #include <boost/log/detail/config.hpp>
24 #include <boost/log/detail/is_character_type.hpp>
25 #include <boost/log/detail/header.hpp>
27 #ifdef BOOST_HAS_PRAGMA_ONCE
33 BOOST_LOG_OPEN_NAMESPACE
37 // Implementation note: We have to implement char<->wchar_t conversions even in the absence of the native wchar_t
38 // type. These conversions are used in sinks, e.g. to convert multibyte strings to wide-character filesystem paths.
40 //! The function converts one string to the character type of another
41 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());
42 //! The function converts one string to the character type of another
43 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());
45 #if !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS)
46 #if !defined(BOOST_NO_CXX11_CHAR16_T)
47 //! The function converts one string to the character type of another
48 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());
49 //! The function converts one string to the character type of another
50 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());
51 //! The function converts one string to the character type of another
52 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());
54 #if !defined(BOOST_NO_CXX11_CHAR32_T)
55 //! The function converts one string to the character type of another
56 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());
57 //! The function converts one string to the character type of another
58 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());
59 //! The function converts one string to the character type of another
60 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());
62 #if !defined(BOOST_NO_CXX11_CHAR16_T) && !defined(BOOST_NO_CXX11_CHAR32_T)
63 //! The function converts one string to the character type of another
64 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());
65 //! The function converts one string to the character type of another
66 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());
68 #endif // !defined(BOOST_LOG_NO_CXX11_CODECVT_FACETS)
70 //! The function converts one string to the character type of another
71 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
72 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
73 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())
75 std::size_t size_left = str2.size() < max_size ? max_size - str2.size() : static_cast< std::size_t >(0u);
76 const bool overflow = len > size_left;
77 str2.append(reinterpret_cast< const TargetCharT* >(str1), overflow ? size_left : len);
81 //! The function converts one string to the character type of another
82 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
83 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
84 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())
86 return aux::code_convert(str1.data(), str1.size(), str2, max_size, loc);
89 //! The function converts one string to the character type of another
90 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
91 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
92 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
94 return aux::code_convert(str1.data(), str1.size(), str2, str2.max_size(), loc);
96 //! The function converts one string to the character type of another
97 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
98 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) == sizeof(TargetCharT), bool >::type
99 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
101 return aux::code_convert(str1, len, str2, str2.max_size(), loc);
104 //! The function converts one string to the character type of another
105 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
106 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
107 code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
109 return aux::code_convert_impl(str1.c_str(), str1.size(), str2, str2.max_size(), loc);
112 //! The function converts one string to the character type of another
113 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
114 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
115 code_convert(const SourceCharT* str1, std::size_t len, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
117 return aux::code_convert_impl(str1, len, str2, str2.max_size(), loc);
120 //! The function converts one string to the character type of another
121 template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
122 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
123 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())
125 return aux::code_convert_impl(str1.c_str(), str1.size(), str2, max_size, loc);
128 //! The function converts one string to the character type of another
129 template< typename SourceCharT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
130 inline typename boost::enable_if_c< is_character_type< SourceCharT >::value && is_character_type< TargetCharT >::value && sizeof(SourceCharT) != sizeof(TargetCharT), bool >::type
131 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())
133 return aux::code_convert_impl(str1, len, str2, max_size, loc);
136 //! The function converts the passed string to the narrow-character encoding
137 inline std::string const& to_narrow(std::string const& str)
142 //! The function converts the passed string to the narrow-character encoding
143 inline std::string const& to_narrow(std::string const& str, std::locale const&)
148 //! The function converts the passed string to the narrow-character encoding
149 inline std::string to_narrow(std::wstring const& str, std::locale const& loc = std::locale())
152 aux::code_convert_impl(str.c_str(), str.size(), res, res.max_size(), loc);
153 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_HAS_NRVO)
154 return static_cast< std::string&& >(res);
160 //! The function converts the passed string to the wide-character encoding
161 inline std::wstring const& to_wide(std::wstring const& str)
166 //! The function converts the passed string to the wide-character encoding
167 inline std::wstring const& to_wide(std::wstring const& str, std::locale const&)
172 //! The function converts the passed string to the wide-character encoding
173 inline std::wstring to_wide(std::string const& str, std::locale const& loc = std::locale())
176 aux::code_convert_impl(str.c_str(), str.size(), res, res.max_size(), loc);
177 #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_HAS_NRVO)
178 return static_cast< std::wstring&& >(res);
186 BOOST_LOG_CLOSE_NAMESPACE // namespace log
190 #include <boost/log/detail/footer.hpp>
192 #endif // BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_