code_conversion.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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)
  6. */
  7. /*!
  8. * \file code_conversion.hpp
  9. * \author Andrey Semashev
  10. * \date 08.11.2008
  11. *
  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.
  14. */
  15. #ifndef BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_
  17. #include <locale>
  18. #include <string>
  19. #include <boost/log/detail/config.hpp>
  20. #include <boost/log/detail/header.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. namespace boost {
  25. BOOST_LOG_OPEN_NAMESPACE
  26. namespace aux {
  27. //! The function converts one string to the character type of another
  28. BOOST_LOG_API void code_convert(const wchar_t* str1, std::size_t len, std::string& str2, std::locale const& loc = std::locale());
  29. //! The function converts one string to the character type of another
  30. BOOST_LOG_API void code_convert(const char* str1, std::size_t len, std::wstring& str2, std::locale const& loc = std::locale());
  31. #if !defined(BOOST_NO_CXX11_CHAR16_T)
  32. //! The function converts one string to the character type of another
  33. BOOST_LOG_API void code_convert(const char16_t* str1, std::size_t len, std::string& str2, std::locale const& loc = std::locale());
  34. //! The function converts one string to the character type of another
  35. BOOST_LOG_API void code_convert(const char* str1, std::size_t len, std::u16string& str2, std::locale const& loc = std::locale());
  36. #endif
  37. #if !defined(BOOST_NO_CXX11_CHAR32_T)
  38. //! The function converts one string to the character type of another
  39. BOOST_LOG_API void code_convert(const char32_t* str1, std::size_t len, std::string& str2, std::locale const& loc = std::locale());
  40. //! The function converts one string to the character type of another
  41. BOOST_LOG_API void code_convert(const char* str1, std::size_t len, std::u32string& str2, std::locale const& loc = std::locale());
  42. #endif
  43. //! The function converts one string to the character type of another
  44. template< typename CharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetTraitsT, typename TargetAllocatorT >
  45. inline void code_convert(std::basic_string< CharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< CharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& = std::locale())
  46. {
  47. str2.append(str1.c_str(), str1.size());
  48. }
  49. //! The function converts one string to the character type of another
  50. template< typename CharT, typename TargetTraitsT, typename TargetAllocatorT >
  51. inline void code_convert(const CharT* str1, std::size_t len, std::basic_string< CharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& = std::locale())
  52. {
  53. str2.append(str1, len);
  54. }
  55. //! The function converts one string to the character type of another
  56. template< typename SourceCharT, typename SourceTraitsT, typename SourceAllocatorT, typename TargetCharT, typename TargetTraitsT, typename TargetAllocatorT >
  57. inline void code_convert(std::basic_string< SourceCharT, SourceTraitsT, SourceAllocatorT > const& str1, std::basic_string< TargetCharT, TargetTraitsT, TargetAllocatorT >& str2, std::locale const& loc = std::locale())
  58. {
  59. aux::code_convert(str1.c_str(), str1.size(), str2, loc);
  60. }
  61. //! The function converts the passed string to the narrow-character encoding
  62. inline std::string const& to_narrow(std::string const& str)
  63. {
  64. return str;
  65. }
  66. //! The function converts the passed string to the narrow-character encoding
  67. inline std::string const& to_narrow(std::string const& str, std::locale const&)
  68. {
  69. return str;
  70. }
  71. //! The function converts the passed string to the narrow-character encoding
  72. inline std::string to_narrow(std::wstring const& str, std::locale const& loc = std::locale())
  73. {
  74. std::string res;
  75. aux::code_convert(str, res, loc);
  76. return res;
  77. }
  78. //! The function converts the passed string to the wide-character encoding
  79. inline std::wstring const& to_wide(std::wstring const& str)
  80. {
  81. return str;
  82. }
  83. //! The function converts the passed string to the wide-character encoding
  84. inline std::wstring const& to_wide(std::wstring const& str, std::locale const&)
  85. {
  86. return str;
  87. }
  88. //! The function converts the passed string to the wide-character encoding
  89. inline std::wstring to_wide(std::string const& str, std::locale const& loc = std::locale())
  90. {
  91. std::wstring res;
  92. aux::code_convert(str, res, loc);
  93. return res;
  94. }
  95. } // namespace aux
  96. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  97. } // namespace boost
  98. #include <boost/log/detail/footer.hpp>
  99. #endif // BOOST_LOG_DETAIL_CODE_CONVERSION_HPP_INCLUDED_