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)
8 * \file formatters/if.hpp
9 * \author Andrey Semashev
12 * The header contains implementation of a conditional formatter.
15 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_IF_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_IF_HPP_INCLUDED_
18 #include <boost/mpl/bool.hpp>
19 #include <boost/phoenix/core/actor.hpp>
20 #include <boost/phoenix/core/meta_grammar.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/fusion/sequence/intrinsic/at_c.hpp>
25 #include <boost/type_traits/remove_cv.hpp>
26 #include <boost/type_traits/remove_reference.hpp>
27 #include <boost/log/detail/config.hpp>
28 #include <boost/log/detail/custom_terminal_spec.hpp>
29 #include <boost/log/detail/header.hpp>
31 #ifdef BOOST_HAS_PRAGMA_ONCE
37 BOOST_LOG_OPEN_NAMESPACE
39 namespace expressions {
43 template< typename LeftT, typename CondT, typename ThenT >
44 class if_output_terminal
48 typedef if_output_terminal this_type;
51 #ifndef BOOST_LOG_DOXYGEN_PASS
52 //! Internal typedef for type categorization
53 typedef void _is_boost_log_terminal;
56 //! Result type definition
60 template< typename ThisT, typename ContextT >
61 struct result< ThisT(ContextT) >
63 typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
64 typedef typename phoenix::evaluator::impl<
65 typename LeftT::proto_base_expr&,
72 //! Left argument actor
74 //! Condition expression
80 //! Initializing constructor
81 if_output_terminal(LeftT const& left, CondT const& cond, ThenT const& then_) : m_left(left), m_cond(cond), m_then(then_)
85 //! Invokation operator
86 template< typename ContextT >
87 typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx)
89 typedef typename result< this_type(ContextT const&) >::type result_type;
90 result_type strm = phoenix::eval(m_left, ctx);
91 if (phoenix::eval(m_cond, ctx))
92 phoenix::eval(m_then, ctx);
96 //! Invokation operator
97 template< typename ContextT >
98 typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const
100 typedef typename result< const this_type(ContextT const&) >::type result_type;
101 result_type strm = phoenix::eval(m_left, ctx);
102 if (phoenix::eval(m_cond, ctx))
103 phoenix::eval(m_then, ctx);
107 BOOST_DELETED_FUNCTION(if_output_terminal())
110 template< typename LeftT, typename CondT, typename ThenT, typename ElseT >
111 class if_else_output_terminal
115 typedef if_else_output_terminal this_type;
118 #ifndef BOOST_LOG_DOXYGEN_PASS
119 //! Internal typedef for type categorization
120 typedef void _is_boost_log_terminal;
123 //! Result type definition
127 template< typename ThisT, typename ContextT >
128 struct result< ThisT(ContextT) >
130 typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
131 typedef typename phoenix::evaluator::impl<
132 typename LeftT::proto_base_expr&,
139 //! Left argument actor
141 //! Condition expression
149 //! Initializing constructor
150 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_)
154 //! Invokation operator
155 template< typename ContextT >
156 typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx)
158 typedef typename result< this_type(ContextT const&) >::type result_type;
159 result_type strm = phoenix::eval(m_left, ctx);
160 if (phoenix::eval(m_cond, ctx))
161 phoenix::eval(m_then, ctx);
163 phoenix::eval(m_else, ctx);
167 //! Invokation operator
168 template< typename ContextT >
169 typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const
171 typedef typename result< const this_type(ContextT const&) >::type result_type;
172 result_type strm = phoenix::eval(m_left, ctx);
173 if (phoenix::eval(m_cond, ctx))
174 phoenix::eval(m_then, ctx);
176 phoenix::eval(m_else, ctx);
180 BOOST_DELETED_FUNCTION(if_else_output_terminal())
184 template< typename CondT, typename ThenT, typename ElseT >
185 struct if_then_else_gen
191 if_then_else_gen(CondT const& cond, ThenT const& then_, ElseT const& else_) : m_cond(cond), m_then(then_), m_else(else_)
196 #ifndef BOOST_LOG_DOXYGEN_PASS
198 #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
199 template< typename LeftExprT, typename CondT, typename ThenT, typename ElseT >\
200 BOOST_FORCEINLINE phoenix::actor< if_else_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT, ElseT > >\
201 operator<< (phoenix::actor< LeftExprT > left_ref left, if_then_else_gen< CondT, ThenT, ElseT > right_ref right)\
203 typedef if_else_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT, ElseT > terminal_type;\
204 phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.m_cond, right.m_then, right.m_else) }};\
208 #include <boost/log/detail/generate_overloads.hpp>
210 #undef BOOST_LOG_AUX_OVERLOAD
212 #endif // BOOST_LOG_DOXYGEN_PASS
214 template< typename CondT, typename ThenT >
222 else_gen(CondT const& cond, ThenT const& then_) : m_cond(cond), m_then(then_)
226 template< typename ElseT >
227 BOOST_FORCEINLINE if_then_else_gen< CondT, ThenT, ElseT > operator[] (ElseT const& el)
229 return if_then_else_gen< CondT, ThenT, ElseT >(m_cond, m_then, el);
234 if_then_gen(CondT const& cond, ThenT const& then_) : else_(cond, then_) {}
237 #ifndef BOOST_LOG_DOXYGEN_PASS
239 #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
240 template< typename LeftExprT, typename CondT, typename ThenT >\
241 BOOST_FORCEINLINE phoenix::actor< if_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT > >\
242 operator<< (phoenix::actor< LeftExprT > left_ref left, if_then_gen< CondT, ThenT > right_ref right)\
244 typedef if_output_terminal< phoenix::actor< LeftExprT >, CondT, ThenT > terminal_type;\
245 phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.else_.m_cond, right.else_.m_then) }};\
249 #include <boost/log/detail/generate_overloads.hpp>
251 #undef BOOST_LOG_AUX_OVERLOAD
253 #endif // BOOST_LOG_DOXYGEN_PASS
255 template< typename CondT >
262 explicit if_gen(CondT const& cond) : m_cond(cond)
266 template< typename ThenT >
267 BOOST_FORCEINLINE if_then_gen< CondT, ThenT > operator[] (ThenT const& then_) const
269 return if_then_gen< CondT, ThenT >(m_cond, then_);
276 * The function returns a conditional formatter generator object. The generator provides <tt>operator[]</tt> that can be used
277 * to construct the actual formatter. The formatter must participate in a streaming expression.
279 * \param cond A filter expression that will be used as the condition
281 template< typename CondT >
282 BOOST_FORCEINLINE aux::if_gen< CondT > if_(CondT const& cond)
284 return aux::if_gen< CondT >(cond);
287 } // namespace expressions
289 BOOST_LOG_CLOSE_NAMESPACE // namespace log
291 #ifndef BOOST_LOG_DOXYGEN_PASS
295 namespace result_of {
297 template< typename LeftT, typename CondT, typename ThenT >
298 struct is_nullary< custom_terminal< boost::log::expressions::aux::if_output_terminal< LeftT, CondT, ThenT > > > :
303 template< typename LeftT, typename CondT, typename ThenT, typename ElseT >
304 struct is_nullary< custom_terminal< boost::log::expressions::aux::if_else_output_terminal< LeftT, CondT, ThenT, ElseT > > > :
309 } // namespace result_of
311 } // namespace phoenix
317 #include <boost/log/detail/footer.hpp>
319 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_IF_HPP_INCLUDED_