wrap_formatter.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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/wrap_formatter.hpp
  9. * \author Andrey Semashev
  10. * \date 24.11.2012
  11. *
  12. * The header contains a formatter function wrapper that enables third-party functions to participate in formatting expressions.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_WRAP_FORMATTER_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_FORMATTERS_WRAP_FORMATTER_HPP_INCLUDED_
  16. #include <string>
  17. #include <boost/move/core.hpp>
  18. #include <boost/move/utility_core.hpp>
  19. #include <boost/mpl/has_xxx.hpp>
  20. #include <boost/phoenix/core/actor.hpp>
  21. #include <boost/phoenix/core/terminal_fwd.hpp>
  22. #include <boost/phoenix/core/is_nullary.hpp>
  23. #include <boost/phoenix/core/environment.hpp>
  24. #include <boost/type_traits/remove_cv.hpp>
  25. #include <boost/type_traits/remove_reference.hpp>
  26. #include <boost/fusion/sequence/intrinsic/at_c.hpp>
  27. #include <boost/log/detail/config.hpp>
  28. #include <boost/log/detail/custom_terminal_spec.hpp>
  29. #include <boost/log/detail/function_traits.hpp>
  30. #include <boost/log/utility/formatting_ostream.hpp>
  31. #include <boost/log/detail/header.hpp>
  32. #ifdef BOOST_HAS_PRAGMA_ONCE
  33. #pragma once
  34. #endif
  35. namespace boost {
  36. BOOST_LOG_OPEN_NAMESPACE
  37. namespace expressions {
  38. namespace aux {
  39. //! Wrapped formatter stream output terminal
  40. template< typename LeftT, typename FunT >
  41. class wrapped_formatter_output_terminal
  42. {
  43. private:
  44. //! Self type
  45. typedef wrapped_formatter_output_terminal< LeftT, FunT > this_type;
  46. public:
  47. #ifndef BOOST_LOG_DOXYGEN_PASS
  48. //! Internal typedef for type categorization
  49. typedef void _is_boost_log_terminal;
  50. #endif
  51. //! Wrapped function type
  52. typedef FunT function_type;
  53. //! Result type definition
  54. template< typename >
  55. struct result;
  56. template< typename ThisT, typename ContextT >
  57. struct result< ThisT(ContextT) >
  58. {
  59. typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
  60. typedef typename phoenix::evaluator::impl<
  61. typename LeftT::proto_base_expr&,
  62. context_type,
  63. phoenix::unused
  64. >::result_type type;
  65. };
  66. private:
  67. //! Left argument actor
  68. LeftT m_left;
  69. //! Wrapped function
  70. function_type m_fun;
  71. public:
  72. //! Initializing constructor
  73. wrapped_formatter_output_terminal(LeftT const& left, function_type const& fun) : m_left(left), m_fun(fun)
  74. {
  75. }
  76. //! Copy constructor
  77. wrapped_formatter_output_terminal(wrapped_formatter_output_terminal const& that) : m_left(that.m_left), m_fun(that.m_fun)
  78. {
  79. }
  80. //! Invokation operator
  81. template< typename ContextT >
  82. typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx)
  83. {
  84. typedef typename result< this_type(ContextT const&) >::type result_type;
  85. result_type strm = phoenix::eval(m_left, ctx);
  86. m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()), strm);
  87. return strm;
  88. }
  89. //! Invokation operator
  90. template< typename ContextT >
  91. typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const
  92. {
  93. typedef typename result< const this_type(ContextT const&) >::type result_type;
  94. result_type strm = phoenix::eval(m_left, ctx);
  95. m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()), strm);
  96. return strm;
  97. }
  98. BOOST_DELETED_FUNCTION(wrapped_formatter_output_terminal())
  99. };
  100. BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_char_type, char_type, false)
  101. template<
  102. typename FunT,
  103. bool HasCharTypeV = has_char_type< FunT >::value,
  104. bool HasSecondArgumentV = boost::log::aux::has_second_argument_type< FunT >::value,
  105. bool HasArg2V = boost::log::aux::has_arg2_type< FunT >::value
  106. >
  107. struct default_char_type
  108. {
  109. // Use this char type if all detection fails
  110. typedef char type;
  111. };
  112. template< typename FunT, bool HasSecondArgumentV, bool HasArg2V >
  113. struct default_char_type< FunT, true, HasSecondArgumentV, HasArg2V >
  114. {
  115. typedef typename FunT::char_type type;
  116. };
  117. template< typename FunT, bool HasArg2V >
  118. struct default_char_type< FunT, false, true, HasArg2V >
  119. {
  120. typedef typename remove_cv< typename remove_reference< typename FunT::second_argument_type >::type >::type argument_type;
  121. typedef typename argument_type::char_type type;
  122. };
  123. template< typename FunT >
  124. struct default_char_type< FunT, false, false, true >
  125. {
  126. typedef typename remove_cv< typename remove_reference< typename FunT::arg2_type >::type >::type argument_type;
  127. typedef typename argument_type::char_type type;
  128. };
  129. } // namespace aux
  130. /*!
  131. * Formatter function wrapper terminal.
  132. */
  133. template< typename FunT, typename CharT >
  134. class wrapped_formatter_terminal
  135. {
  136. public:
  137. #ifndef BOOST_LOG_DOXYGEN_PASS
  138. //! Internal typedef for type categorization
  139. typedef void _is_boost_log_terminal;
  140. #endif
  141. //! Character type
  142. typedef CharT char_type;
  143. //! String type
  144. typedef std::basic_string< char_type > string_type;
  145. //! Formatting stream type
  146. typedef basic_formatting_ostream< char_type > stream_type;
  147. //! Wrapped function type
  148. typedef FunT function_type;
  149. //! Formatter result type
  150. typedef string_type result_type;
  151. private:
  152. //! Wrapped function
  153. function_type m_fun;
  154. public:
  155. //! Initializing construction
  156. explicit wrapped_formatter_terminal(function_type const& fun) : m_fun(fun)
  157. {
  158. }
  159. //! Copy constructor
  160. wrapped_formatter_terminal(wrapped_formatter_terminal const& that) : m_fun(that.m_fun)
  161. {
  162. }
  163. //! Returns the wrapped function
  164. function_type const& get_function() const
  165. {
  166. return m_fun;
  167. }
  168. //! Invokation operator
  169. template< typename ContextT >
  170. result_type operator() (ContextT const& ctx)
  171. {
  172. string_type str;
  173. stream_type strm(str);
  174. m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()), strm);
  175. strm.flush();
  176. return BOOST_LOG_NRVO_RESULT(str);
  177. }
  178. //! Invokation operator
  179. template< typename ContextT >
  180. result_type operator() (ContextT const& ctx) const
  181. {
  182. string_type str;
  183. stream_type strm(str);
  184. m_fun(fusion::at_c< 0 >(phoenix::env(ctx).args()), strm);
  185. strm.flush();
  186. return BOOST_LOG_NRVO_RESULT(str);
  187. }
  188. };
  189. /*!
  190. * Wrapped formatter function actor.
  191. */
  192. template< typename FunT, typename CharT, template< typename > class ActorT = phoenix::actor >
  193. class wrapped_formatter_actor :
  194. public ActorT< wrapped_formatter_terminal< FunT, CharT > >
  195. {
  196. public:
  197. //! Character type
  198. typedef CharT char_type;
  199. //! Wrapped function type
  200. typedef FunT function_type;
  201. //! Base terminal type
  202. typedef wrapped_formatter_terminal< function_type, char_type > terminal_type;
  203. //! Base actor type
  204. typedef ActorT< terminal_type > base_type;
  205. public:
  206. //! Initializing constructor
  207. explicit wrapped_formatter_actor(base_type const& act) : base_type(act)
  208. {
  209. }
  210. /*!
  211. * \returns The wrapped function
  212. */
  213. function_type const& get_function() const
  214. {
  215. return this->proto_expr_.child0.get_function();
  216. }
  217. };
  218. #ifndef BOOST_LOG_DOXYGEN_PASS
  219. #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
  220. template< typename LeftExprT, typename FunT, typename CharT >\
  221. BOOST_FORCEINLINE phoenix::actor< aux::wrapped_formatter_output_terminal< phoenix::actor< LeftExprT >, FunT > >\
  222. operator<< (phoenix::actor< LeftExprT > left_ref left, wrapped_formatter_actor< FunT, CharT > right_ref right)\
  223. {\
  224. typedef aux::wrapped_formatter_output_terminal< phoenix::actor< LeftExprT >, FunT > terminal_type;\
  225. phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_function()) }};\
  226. return actor;\
  227. }
  228. #include <boost/log/detail/generate_overloads.hpp>
  229. #undef BOOST_LOG_AUX_OVERLOAD
  230. #endif // BOOST_LOG_DOXYGEN_PASS
  231. /*!
  232. * The function wraps a function object in order it to be able to participate in formatting expressions. The wrapped
  233. * function object must be compatible with the following signature:
  234. *
  235. * <pre>
  236. * void (record_view const&, basic_formatting_ostream< CharT >&)
  237. * </pre>
  238. *
  239. * where \c CharT is the character type of the formatting expression.
  240. */
  241. template< typename FunT >
  242. BOOST_FORCEINLINE wrapped_formatter_actor< FunT, typename aux::default_char_type< FunT >::type > wrap_formatter(FunT const& fun)
  243. {
  244. typedef wrapped_formatter_actor< FunT, typename aux::default_char_type< FunT >::type > actor_type;
  245. typedef typename actor_type::terminal_type terminal_type;
  246. typename actor_type::base_type act = {{ terminal_type(fun) }};
  247. return actor_type(act);
  248. }
  249. /*!
  250. * The function wraps a function object in order it to be able to participate in formatting expressions. The wrapped
  251. * function object must be compatible with the following signature:
  252. *
  253. * <pre>
  254. * void (record_view const&, basic_formatting_ostream< CharT >&)
  255. * </pre>
  256. *
  257. * where \c CharT is the character type of the formatting expression.
  258. */
  259. template< typename CharT, typename FunT >
  260. BOOST_FORCEINLINE wrapped_formatter_actor< FunT, CharT > wrap_formatter(FunT const& fun)
  261. {
  262. typedef wrapped_formatter_actor< FunT, CharT > actor_type;
  263. typedef typename actor_type::terminal_type terminal_type;
  264. typename actor_type::base_type act = {{ terminal_type(fun) }};
  265. return actor_type(act);
  266. }
  267. } // namespace expressions
  268. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  269. #ifndef BOOST_LOG_DOXYGEN_PASS
  270. namespace phoenix {
  271. namespace result_of {
  272. template< typename LeftT, typename FunT >
  273. struct is_nullary< custom_terminal< boost::log::expressions::aux::wrapped_formatter_output_terminal< LeftT, FunT > > > :
  274. public mpl::false_
  275. {
  276. };
  277. template< typename FunT, typename CharT >
  278. struct is_nullary< custom_terminal< boost::log::expressions::wrapped_formatter_terminal< FunT, CharT > > > :
  279. public mpl::false_
  280. {
  281. };
  282. } // namespace result_of
  283. } // namespace phoenix
  284. #endif
  285. } // namespace boost
  286. #include <boost/log/detail/footer.hpp>
  287. #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_WRAP_FORMATTER_HPP_INCLUDED_