if.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 formatters/if.hpp
  9. * \author Andrey Semashev
  10. * \date 17.11.2012
  11. *
  12. * The header contains implementation of a conditional formatter.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_IF_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_FORMATTERS_IF_HPP_INCLUDED_
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/phoenix/core/actor.hpp>
  18. #include <boost/phoenix/core/meta_grammar.hpp>
  19. #include <boost/phoenix/core/terminal_fwd.hpp>
  20. #include <boost/phoenix/core/is_nullary.hpp>
  21. #include <boost/phoenix/core/environment.hpp>
  22. #include <boost/fusion/sequence/intrinsic/at_c.hpp>
  23. #include <boost/type_traits/remove_cv.hpp>
  24. #include <boost/type_traits/remove_reference.hpp>
  25. #include <boost/log/detail/config.hpp>
  26. #include <boost/log/detail/custom_terminal_spec.hpp>
  27. #include <boost/log/detail/header.hpp>
  28. #ifdef BOOST_HAS_PRAGMA_ONCE
  29. #pragma once
  30. #endif
  31. namespace boost {
  32. BOOST_LOG_OPEN_NAMESPACE
  33. namespace expressions {
  34. namespace aux {
  35. template< typename LeftT, typename CondT, typename ThenT >
  36. class if_output_terminal
  37. {
  38. private:
  39. //! Self type
  40. typedef if_output_terminal this_type;
  41. public:
  42. //! Internal typedef for type categorization
  43. typedef void _is_boost_log_terminal;
  44. //! Result type definition
  45. template< typename >
  46. struct result;
  47. template< typename ContextT >
  48. struct result< this_type(ContextT) >
  49. {
  50. typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
  51. typedef typename phoenix::evaluator::impl<
  52. typename LeftT::proto_base_expr&,
  53. context_type,
  54. phoenix::unused
  55. >::result_type type;
  56. };
  57. template< typename ContextT >
  58. struct result< const this_type(ContextT) >
  59. {
  60. typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
  61. typedef typename phoenix::evaluator::impl<
  62. typename LeftT::proto_base_expr const&,
  63. context_type,
  64. phoenix::unused
  65. >::result_type type;
  66. };
  67. private:
  68. //! Left argument actor
  69. LeftT m_left;
  70. //! Condition expression
  71. CondT m_cond;
  72. //! Positive branch
  73. ThenT m_then;
  74. public:
  75. //! Initializing constructor
  76. if_output_terminal(LeftT const& left, CondT const& cond, ThenT const& then_) : m_left(left), m_cond(cond), m_then(then_)
  77. {
  78. }
  79. //! Invokation operator
  80. template< typename ContextT >
  81. typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx)
  82. {
  83. typedef typename result< this_type(ContextT const&) >::type result_type;
  84. result_type strm = phoenix::eval(m_left, ctx);
  85. if (phoenix::eval(m_cond, ctx))
  86. phoenix::eval(m_then, ctx);
  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. if (phoenix::eval(m_cond, ctx))
  96. phoenix::eval(m_then, ctx);
  97. return strm;
  98. }
  99. BOOST_DELETED_FUNCTION(if_output_terminal())
  100. };
  101. template< typename LeftT, typename CondT, typename ThenT, typename ElseT >
  102. class if_else_output_terminal
  103. {
  104. private:
  105. //! Self type
  106. typedef if_else_output_terminal this_type;
  107. public:
  108. //! Internal typedef for type categorization
  109. typedef void _is_boost_log_terminal;
  110. //! Result type definition
  111. template< typename >
  112. struct result;
  113. template< typename ContextT >
  114. struct result< this_type(ContextT) >
  115. {
  116. typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
  117. typedef typename phoenix::evaluator::impl<
  118. typename LeftT::proto_base_expr&,
  119. context_type,
  120. phoenix::unused
  121. >::result_type type;
  122. };
  123. template< typename ContextT >
  124. struct result< const this_type(ContextT) >
  125. {
  126. typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
  127. typedef typename phoenix::evaluator::impl<
  128. typename LeftT::proto_base_expr const&,
  129. context_type,
  130. phoenix::unused
  131. >::result_type type;
  132. };
  133. private:
  134. //! Left argument actor
  135. LeftT m_left;
  136. //! Condition expression
  137. CondT m_cond;
  138. //! Positive branch
  139. ThenT m_then;
  140. //! Negative branch
  141. ElseT m_else;
  142. public:
  143. //! Initializing constructor
  144. if_else_output_terminal(LeftT const& left, CondT const& cond, ThenT const& then_, ElseT const& else_) : m_left(left), m_cond(cond), m_then(then_), m_else(else_)
  145. {
  146. }
  147. //! Invokation operator
  148. template< typename ContextT >
  149. typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx)
  150. {
  151. typedef typename result< this_type(ContextT const&) >::type result_type;
  152. result_type strm = phoenix::eval(m_left, ctx);
  153. if (phoenix::eval(m_cond, ctx))
  154. phoenix::eval(m_then, ctx);
  155. else
  156. phoenix::eval(m_else, ctx);
  157. return strm;
  158. }
  159. //! Invokation operator
  160. template< typename ContextT >
  161. typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const
  162. {
  163. typedef typename result< const this_type(ContextT const&) >::type result_type;
  164. result_type strm = phoenix::eval(m_left, ctx);
  165. if (phoenix::eval(m_cond, ctx))
  166. phoenix::eval(m_then, ctx);
  167. else
  168. phoenix::eval(m_else, ctx);
  169. return strm;
  170. }
  171. BOOST_DELETED_FUNCTION(if_else_output_terminal())
  172. };
  173. template< typename CondT, typename ThenT, typename ElseT >
  174. struct if_then_else_gen
  175. {
  176. CondT m_cond;
  177. ThenT m_then;
  178. ElseT m_else;
  179. if_then_else_gen(CondT const& cond, ThenT const& then_, ElseT const& else_) : m_cond(cond), m_then(then_), m_else(else_)
  180. {
  181. }
  182. };
  183. #ifndef BOOST_LOG_DOXYGEN_PASS
  184. #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
  185. template< typename LeftExprT, typename CondT, typename ThenT, typename ElseT >\
  186. BOOST_FORCEINLINE phoenix::actor< if_else_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT, ElseT > >\
  187. operator<< (phoenix::actor< LeftExprT > left_ref left, if_then_else_gen< CondT, ThenT, ElseT > right_ref right)\
  188. {\
  189. typedef if_else_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT, ElseT > terminal_type;\
  190. phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.m_cond, right.m_then, right.m_else) }};\
  191. return actor;\
  192. }
  193. #include <boost/log/detail/generate_overloads.hpp>
  194. #undef BOOST_LOG_AUX_OVERLOAD
  195. #endif // BOOST_LOG_DOXYGEN_PASS
  196. template< typename CondT, typename ThenT >
  197. struct if_then_gen
  198. {
  199. struct else_gen
  200. {
  201. CondT m_cond;
  202. ThenT m_then;
  203. else_gen(CondT const& cond, ThenT const& then_) : m_cond(cond), m_then(then_)
  204. {
  205. }
  206. template< typename ElseT >
  207. BOOST_FORCEINLINE if_then_else_gen< CondT, ThenT, ElseT > operator[] (ElseT const& el)
  208. {
  209. return if_then_else_gen< CondT, ThenT, ElseT >(m_cond, m_then, el);
  210. }
  211. }
  212. else_;
  213. if_then_gen(CondT const& cond, ThenT const& then_) : else_(cond, then_) {}
  214. };
  215. #ifndef BOOST_LOG_DOXYGEN_PASS
  216. #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
  217. template< typename LeftExprT, typename CondT, typename ThenT >\
  218. BOOST_FORCEINLINE phoenix::actor< if_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT > >\
  219. operator<< (phoenix::actor< LeftExprT > left_ref left, if_then_gen< CondT, ThenT > right_ref right)\
  220. {\
  221. typedef if_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT > terminal_type;\
  222. phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.else_.m_cond, right.else_.m_then) }};\
  223. return actor;\
  224. }
  225. #include <boost/log/detail/generate_overloads.hpp>
  226. #undef BOOST_LOG_AUX_OVERLOAD
  227. #endif // BOOST_LOG_DOXYGEN_PASS
  228. template< typename CondT >
  229. class if_gen
  230. {
  231. private:
  232. CondT const& m_cond;
  233. public:
  234. explicit if_gen(CondT const& cond) : m_cond(cond)
  235. {
  236. }
  237. template< typename ThenT >
  238. BOOST_FORCEINLINE if_then_gen< CondT, ThenT > operator[] (ThenT const& then_) const
  239. {
  240. return if_then_gen< CondT, ThenT >(m_cond, then_);
  241. }
  242. };
  243. } // namespace aux
  244. /*!
  245. * The function returns a conditional formatter generator object. The generator provides <tt>operator[]</tt> that can be used
  246. * to construct the actual formatter. The formatter must participate in a streaming expression.
  247. *
  248. * \param cond A filter expression that will be used as the condition
  249. */
  250. template< typename CondT >
  251. BOOST_FORCEINLINE aux::if_gen< CondT > if_(CondT const& cond)
  252. {
  253. return aux::if_gen< CondT >(cond);
  254. }
  255. } // namespace expressions
  256. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  257. #ifndef BOOST_LOG_DOXYGEN_PASS
  258. namespace phoenix {
  259. namespace result_of {
  260. template< typename LeftT, typename CondT, typename ThenT >
  261. struct is_nullary< custom_terminal< boost::log::expressions::aux::if_output_terminal< LeftT, CondT, ThenT > > > :
  262. public mpl::false_
  263. {
  264. };
  265. template< typename LeftT, typename CondT, typename ThenT, typename ElseT >
  266. struct is_nullary< custom_terminal< boost::log::expressions::aux::if_else_output_terminal< LeftT, CondT, ThenT, ElseT > > > :
  267. public mpl::false_
  268. {
  269. };
  270. } // namespace result_of
  271. } // namespace phoenix
  272. #endif
  273. } // namespace boost
  274. #include <boost/log/detail/footer.hpp>
  275. #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_IF_HPP_INCLUDED_