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