/Dependencies/boost/include/boost/log/expressions/formatter.hpp

https://bitbucket.org/sonofusion/.simulation · C++ Header · 196 lines · 115 code · 26 blank · 55 comment · 0 complexity · 61e36a1366aafe30477e3df49f7458b0 MD5 · raw file

  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 formatter.hpp
  9. * \author Andrey Semashev
  10. * \date 13.07.2012
  11. *
  12. * The header contains a formatter function object definition.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_FORMATTER_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_FORMATTER_HPP_INCLUDED_
  16. #include <boost/move/core.hpp>
  17. #include <boost/move/utility.hpp>
  18. #include <boost/utility/enable_if.hpp>
  19. #include <boost/log/detail/config.hpp>
  20. #include <boost/log/detail/light_function.hpp>
  21. #include <boost/log/attributes/attribute_value_set.hpp>
  22. #include <boost/log/attributes/value_visitation.hpp>
  23. #include <boost/log/core/record_view.hpp>
  24. #include <boost/log/utility/formatting_ostream.hpp>
  25. #include <boost/log/utility/functional/bind_output.hpp>
  26. #include <boost/log/expressions/message.hpp>
  27. #include <boost/log/detail/header.hpp>
  28. #ifdef BOOST_LOG_HAS_PRAGMA_ONCE
  29. #pragma once
  30. #endif
  31. namespace boost {
  32. BOOST_LOG_OPEN_NAMESPACE
  33. /*!
  34. * Log record formatter function wrapper.
  35. */
  36. template< typename CharT >
  37. class basic_formatter
  38. {
  39. typedef basic_formatter this_type;
  40. BOOST_COPYABLE_AND_MOVABLE(this_type)
  41. public:
  42. //! Result type
  43. typedef void result_type;
  44. //! Character type
  45. typedef CharT char_type;
  46. //! Output stream type
  47. typedef basic_formatting_ostream< char_type > stream_type;
  48. private:
  49. //! Filter function type
  50. typedef boost::log::aux::light_function< void (record_view const&, stream_type&) > formatter_type;
  51. //! Default formatter, always returns \c true
  52. struct default_formatter
  53. {
  54. typedef void result_type;
  55. default_formatter() : m_MessageName(expressions::tag::message::get_name())
  56. {
  57. }
  58. result_type operator() (record_view const& rec, stream_type& strm) const
  59. {
  60. boost::log::visit< expressions::tag::message::value_type >(m_MessageName, rec, boost::log::bind_output(strm));
  61. }
  62. private:
  63. const attribute_name m_MessageName;
  64. };
  65. private:
  66. //! Formatter function
  67. formatter_type m_Formatter;
  68. public:
  69. /*!
  70. * Default constructor. Creates a formatter that only outputs log message.
  71. */
  72. basic_formatter() : m_Formatter(default_formatter())
  73. {
  74. }
  75. /*!
  76. * Copy constructor
  77. */
  78. basic_formatter(basic_formatter const& that) : m_Formatter(that.m_Formatter)
  79. {
  80. }
  81. /*!
  82. * Move constructor
  83. */
  84. basic_formatter(BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT : m_Formatter(boost::move(that.m_Formatter))
  85. {
  86. }
  87. /*!
  88. * Initializing constructor. Creates a formatter which will invoke the specified function object.
  89. */
  90. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  91. template< typename FunT >
  92. basic_formatter(FunT const& fun)
  93. #else
  94. template< typename FunT >
  95. basic_formatter(FunT const& fun, typename disable_if< move_detail::is_rv< FunT >, int >::type = 0)
  96. #endif
  97. : m_Formatter(fun)
  98. {
  99. }
  100. /*!
  101. * Move assignment.
  102. */
  103. basic_formatter& operator= (BOOST_RV_REF(this_type) that) BOOST_NOEXCEPT
  104. {
  105. m_Formatter.swap(that.m_Formatter);
  106. return *this;
  107. }
  108. /*!
  109. * Copy assignment.
  110. */
  111. basic_formatter& operator= (BOOST_COPY_ASSIGN_REF(this_type) that)
  112. {
  113. m_Formatter = that.m_Formatter;
  114. return *this;
  115. }
  116. /*!
  117. * Initializing assignment. Sets the specified function object to the formatter.
  118. */
  119. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  120. template< typename FunT >
  121. basic_formatter& operator= (FunT const& fun)
  122. #else
  123. template< typename FunT >
  124. typename disable_if< is_same< typename remove_cv< FunT >::type, this_type >, this_type& >::type
  125. operator= (FunT const& fun)
  126. #endif
  127. {
  128. this_type(fun).swap(*this);
  129. return *this;
  130. }
  131. /*!
  132. * Formatting operator.
  133. *
  134. * \param rec A log record to format.
  135. * \param strm A stream to put the formatted characters to.
  136. */
  137. result_type operator() (record_view const& rec, stream_type& strm) const
  138. {
  139. m_Formatter(rec, strm);
  140. }
  141. /*!
  142. * Resets the formatter to the default. The default formatter only outputs message text.
  143. */
  144. void reset()
  145. {
  146. m_Formatter = default_formatter();
  147. }
  148. /*!
  149. * Swaps two formatters
  150. */
  151. void swap(basic_formatter& that) BOOST_NOEXCEPT
  152. {
  153. m_Formatter.swap(that.m_Formatter);
  154. }
  155. };
  156. template< typename CharT >
  157. inline void swap(basic_formatter< CharT >& left, basic_formatter< CharT >& right) BOOST_NOEXCEPT
  158. {
  159. left.swap(right);
  160. }
  161. #ifdef BOOST_LOG_USE_CHAR
  162. typedef basic_formatter< char > formatter;
  163. #endif
  164. #ifdef BOOST_LOG_USE_WCHAR_T
  165. typedef basic_formatter< wchar_t > wformatter;
  166. #endif
  167. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  168. } // namespace boost
  169. #include <boost/log/detail/footer.hpp>
  170. #endif // BOOST_LOG_EXPRESSIONS_FORMATTER_HPP_INCLUDED_