xml_decorator.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  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)
  6. */
  7. /*!
  8. * \file formatters/xml_decorator.hpp
  9. * \author Andrey Semashev
  10. * \date 18.11.2012
  11. *
  12. * The header contains implementation of a XML-style character decorator.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_XML_DECORATOR_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_FORMATTERS_XML_DECORATOR_HPP_INCLUDED_
  16. #include <boost/range/iterator_range_core.hpp>
  17. #include <boost/log/detail/config.hpp>
  18. #include <boost/log/expressions/formatters/char_decorator.hpp>
  19. #include <boost/log/detail/header.hpp>
  20. #ifdef BOOST_HAS_PRAGMA_ONCE
  21. #pragma once
  22. #endif
  23. namespace boost {
  24. BOOST_LOG_OPEN_NAMESPACE
  25. namespace expressions {
  26. namespace aux {
  27. template< typename >
  28. struct xml_decorator_traits;
  29. #ifdef BOOST_LOG_USE_CHAR
  30. template< >
  31. struct xml_decorator_traits< char >
  32. {
  33. static boost::iterator_range< const char* const* > get_patterns()
  34. {
  35. static const char* const patterns[] =
  36. {
  37. "&", "<", ">", "\"", "'"
  38. };
  39. return boost::make_iterator_range(patterns);
  40. }
  41. static boost::iterator_range< const char* const* > get_replacements()
  42. {
  43. static const char* const replacements[] =
  44. {
  45. "&amp;", "&lt;", "&gt;", "&quot;", "&apos;"
  46. };
  47. return boost::make_iterator_range(replacements);
  48. }
  49. };
  50. #endif // BOOST_LOG_USE_CHAR
  51. #ifdef BOOST_LOG_USE_WCHAR_T
  52. template< >
  53. struct xml_decorator_traits< wchar_t >
  54. {
  55. static boost::iterator_range< const wchar_t* const* > get_patterns()
  56. {
  57. static const wchar_t* const patterns[] =
  58. {
  59. L"&", L"<", L">", L"\"", L"'"
  60. };
  61. return boost::make_iterator_range(patterns);
  62. }
  63. static boost::iterator_range< const wchar_t* const* > get_replacements()
  64. {
  65. static const wchar_t* const replacements[] =
  66. {
  67. L"&amp;", L"&lt;", L"&gt;", L"&quot;", L"&apos;"
  68. };
  69. return boost::make_iterator_range(replacements);
  70. }
  71. };
  72. #endif // BOOST_LOG_USE_WCHAR_T
  73. template< typename CharT >
  74. struct xml_decorator_gen
  75. {
  76. typedef CharT char_type;
  77. template< typename SubactorT >
  78. BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const
  79. {
  80. typedef xml_decorator_traits< char_type > traits_type;
  81. typedef pattern_replacer< char_type > replacer_type;
  82. typedef char_decorator_actor< SubactorT, replacer_type > result_type;
  83. typedef typename result_type::terminal_type terminal_type;
  84. typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(traits_type::get_patterns(), traits_type::get_replacements())) }};
  85. return result_type(act);
  86. }
  87. };
  88. } // namespace aux
  89. /*!
  90. * XML-style decorator generator object. The decorator replaces characters that have special meaning
  91. * in XML documents with the corresponding decorated counterparts. The generator provides
  92. * <tt>operator[]</tt> that can be used to construct the actual decorator. For example:
  93. *
  94. * <code>
  95. * xml_decor[ stream << attr< std::string >("MyAttr") ]
  96. * </code>
  97. *
  98. * For wide-character formatting there is the similar \c wxml_decor decorator generator object.
  99. */
  100. #ifdef BOOST_LOG_USE_CHAR
  101. const aux::xml_decorator_gen< char > xml_decor = {};
  102. #endif
  103. #ifdef BOOST_LOG_USE_WCHAR_T
  104. const aux::xml_decorator_gen< wchar_t > wxml_decor = {};
  105. #endif
  106. /*!
  107. * The function creates an XML-style decorator generator for arbitrary character type.
  108. */
  109. template< typename CharT >
  110. BOOST_FORCEINLINE aux::xml_decorator_gen< CharT > make_xml_decor()
  111. {
  112. return aux::xml_decorator_gen< CharT >();
  113. }
  114. } // namespace expressions
  115. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  116. } // namespace boost
  117. #include <boost/log/detail/footer.hpp>
  118. #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_XML_DECORATOR_HPP_INCLUDED_