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/char_decorator.hpp
9 * \author Andrey Semashev
12 * The header contains implementation of a character decorator.
15 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_
21 #include <boost/assert.hpp>
22 #include <boost/static_assert.hpp>
23 #include <boost/mpl/bool.hpp>
24 #include <boost/range/begin.hpp>
25 #include <boost/range/end.hpp>
26 #include <boost/range/size.hpp>
27 #include <boost/range/const_iterator.hpp>
28 #include <boost/range/value_type.hpp>
29 #include <boost/move/core.hpp>
30 #include <boost/move/utility_core.hpp>
31 #include <boost/core/addressof.hpp>
32 #include <boost/phoenix/core/actor.hpp>
33 #include <boost/phoenix/core/meta_grammar.hpp>
34 #include <boost/phoenix/core/terminal_fwd.hpp>
35 #include <boost/phoenix/core/is_nullary.hpp>
36 #include <boost/phoenix/core/environment.hpp>
37 #include <boost/phoenix/support/vector.hpp>
38 #include <boost/fusion/sequence/intrinsic/at_c.hpp>
39 #include <boost/type_traits/is_same.hpp>
40 #include <boost/type_traits/remove_cv.hpp>
41 #include <boost/type_traits/remove_reference.hpp>
42 #include <boost/log/detail/config.hpp>
43 #include <boost/log/detail/custom_terminal_spec.hpp>
44 #include <boost/log/detail/deduce_char_type.hpp>
45 #include <boost/log/detail/sfinae_tools.hpp>
46 #include <boost/log/utility/formatting_ostream.hpp>
47 #include <boost/log/detail/header.hpp>
49 #ifdef BOOST_HAS_PRAGMA_ONCE
55 BOOST_LOG_OPEN_NAMESPACE
57 namespace expressions {
61 template< typename RangeT >
62 struct string_const_iterator : range_const_iterator< RangeT > {};
64 struct string_const_iterator< char* > { typedef char* type; };
66 struct string_const_iterator< const char* > { typedef const char* type; };
68 struct string_const_iterator< wchar_t* > { typedef wchar_t* type; };
70 struct string_const_iterator< const wchar_t* > { typedef const wchar_t* type; };
75 * A simple character decorator implementation. This implementation replaces string patterns in the source string with
76 * the fixed replacements. Source patterns and replacements can be specified at the object construction.
78 template< typename CharT >
79 class pattern_replacer
83 typedef void result_type;
86 typedef CharT char_type;
88 typedef std::basic_string< char_type > string_type;
91 //! Lengths of source pattern and replacement
94 unsigned int from_len, to_len;
97 //! List of the decorations to apply
98 typedef std::vector< string_lengths > string_lengths_list;
101 //! Characters of the interleaved source patterns and replacements
102 string_type m_decoration_chars;
103 //! List of the decorations to apply
104 string_lengths_list m_string_lengths;
108 * Initializing constructor. Creates a pattern replacer with the specified \a decorations.
109 * The provided decorations must be a sequence of \c std::pair of strings. The first element
110 * of each pair is the source pattern, and the second one is the corresponding replacement.
112 template< typename RangeT >
113 explicit pattern_replacer(RangeT const& decorations
114 #ifndef BOOST_LOG_DOXYGEN_PASS
115 // This is needed for a workaround against an MSVC-10 and older bug in constructor overload resolution
116 , typename boost::enable_if_has_type< typename range_const_iterator< RangeT >::type, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()
120 typedef typename range_const_iterator< RangeT >::type iterator;
121 for (iterator it = boost::begin(decorations), end_ = boost::end(decorations); it != end_; ++it)
125 typedef typename aux::string_const_iterator< typename range_value< RangeT >::type::first_type >::type first_iterator;
126 first_iterator b = string_begin(it->first), e = string_end(it->first);
127 lens.from_len = static_cast< unsigned int >(std::distance(b, e));
128 m_decoration_chars.append(b, e);
131 typedef typename aux::string_const_iterator< typename range_value< RangeT >::type::second_type >::type second_iterator;
132 second_iterator b = string_begin(it->second), e = string_end(it->second);
133 lens.to_len = static_cast< unsigned int >(std::distance(b, e));
134 m_decoration_chars.append(b, e);
136 m_string_lengths.push_back(lens);
140 * Initializing constructor. Creates a pattern replacer with decorations specified
141 * in form of two same-sized string sequences. Each <tt>i</tt>'th decoration will be
142 * <tt>from[i]</tt> -> <tt>to[i]</tt>.
144 template< typename FromRangeT, typename ToRangeT >
145 pattern_replacer(FromRangeT const& from, ToRangeT const& to)
147 typedef typename range_const_iterator< FromRangeT >::type iterator1;
148 typedef typename range_const_iterator< ToRangeT >::type iterator2;
149 iterator1 it1 = boost::begin(from), end1 = boost::end(from);
150 iterator2 it2 = boost::begin(to), end2 = boost::end(to);
151 for (; it1 != end1 && it2 != end2; ++it1, ++it2)
155 typedef typename aux::string_const_iterator< typename range_value< FromRangeT >::type >::type from_iterator;
156 from_iterator b = string_begin(*it1), e = string_end(*it1);
157 lens.from_len = static_cast< unsigned int >(std::distance(b, e));
158 m_decoration_chars.append(b, e);
161 typedef typename aux::string_const_iterator< typename range_value< ToRangeT >::type >::type to_iterator;
162 to_iterator b = string_begin(*it2), e = string_end(*it2);
163 lens.to_len = static_cast< unsigned int >(std::distance(b, e));
164 m_decoration_chars.append(b, e);
166 m_string_lengths.push_back(lens);
169 // Both sequences should be of the same size
170 BOOST_ASSERT(it1 == end1);
171 BOOST_ASSERT(it2 == end2);
174 pattern_replacer(pattern_replacer const& that) : m_decoration_chars(that.m_decoration_chars), m_string_lengths(that.m_string_lengths)
178 //! Applies string replacements starting from the specified position
179 result_type operator() (string_type& str, typename string_type::size_type start_pos = 0) const
181 typedef typename string_type::size_type size_type;
183 const char_type* from_chars = m_decoration_chars.c_str();
184 for (typename string_lengths_list::const_iterator it = m_string_lengths.begin(), end = m_string_lengths.end(); it != end; ++it)
186 const unsigned int from_len = it->from_len, to_len = it->to_len;
187 const char_type* const to_chars = from_chars + from_len;
188 for (size_type pos = str.find(from_chars, start_pos, from_len); pos != string_type::npos; pos = str.find(from_chars, pos, from_len))
190 str.replace(pos, from_len, to_chars, to_len);
193 from_chars = to_chars + to_len;
198 static char_type* string_begin(char_type* p)
202 static const char_type* string_begin(const char_type* p)
206 template< typename RangeT >
207 static typename range_const_iterator< RangeT >::type string_begin(RangeT const& r)
209 return boost::begin(r);
212 static char_type* string_end(char_type* p)
214 return p + std::char_traits< char_type >::length(p);
216 static const char_type* string_end(const char_type* p)
218 return p + std::char_traits< char_type >::length(p);
220 template< typename RangeT >
221 static typename range_const_iterator< RangeT >::type string_end(RangeT const& r)
223 return boost::end(r);
229 //! Character decorator stream output terminal
230 template< typename LeftT, typename SubactorT, typename ImplT >
231 class char_decorator_output_terminal
235 typedef char_decorator_output_terminal< LeftT, SubactorT, ImplT > this_type;
238 #ifndef BOOST_LOG_DOXYGEN_PASS
239 //! Internal typedef for type categorization
240 typedef void _is_boost_log_terminal;
243 //! Implementation type
244 typedef ImplT impl_type;
247 typedef typename impl_type::char_type char_type;
249 typedef typename impl_type::string_type string_type;
250 //! Adopted actor type
251 typedef SubactorT subactor_type;
253 //! Result type definition
257 template< typename ThisT, typename ContextT >
258 struct result< ThisT(ContextT) >
260 typedef typename remove_cv< typename remove_reference< ContextT >::type >::type context_type;
261 typedef typename phoenix::evaluator::impl<
262 typename LeftT::proto_base_expr&,
269 //! Left argument actor
271 //! Adopted formatter actor
272 subactor_type m_subactor;
273 //! Implementation type
278 * Initializing constructor. Creates decorator of the \a fmt formatter with the specified \a decorations.
280 char_decorator_output_terminal(LeftT const& left, subactor_type const& sub, impl_type const& impl) :
281 m_left(left), m_subactor(sub), m_impl(impl)
287 char_decorator_output_terminal(char_decorator_output_terminal const& that) :
288 m_left(that.m_left), m_subactor(that.m_subactor), m_impl(that.m_impl)
293 * Invokation operator
295 template< typename ContextT >
296 typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx)
298 // Flush the stream and keep the current write position in the target string
299 typedef typename result< this_type(ContextT const&) >::type result_type;
300 result_type strm = phoenix::eval(m_left, ctx);
302 typename string_type::size_type const start_pos = strm.rdbuf()->storage()->size();
304 // Invoke the adopted formatter
305 phoenix::eval(m_subactor, ctx);
307 // Flush the buffered characters and apply decorations
309 m_impl(*strm.rdbuf()->storage(), start_pos);
310 strm.rdbuf()->ensure_max_size();
316 * Invokation operator
318 template< typename ContextT >
319 typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const
321 // Flush the stream and keep the current write position in the target string
322 typedef typename result< const this_type(ContextT const&) >::type result_type;
323 result_type strm = phoenix::eval(m_left, ctx);
325 typename string_type::size_type const start_pos = strm.rdbuf()->storage()->size();
327 // Invoke the adopted formatter
328 phoenix::eval(m_subactor, ctx);
330 // Flush the buffered characters and apply decorations
332 m_impl(*strm.rdbuf()->storage(), start_pos);
333 strm.rdbuf()->ensure_max_size();
338 BOOST_DELETED_FUNCTION(char_decorator_output_terminal())
344 * Character decorator terminal class. This formatter allows to modify strings generated by other
345 * formatters on character level. The most obvious application of decorators is replacing
346 * a certain set of characters with decorated equivalents to satisfy requirements of
349 * The \c char_decorator_terminal class aggregates the formatter being decorated, and a set
350 * of string pairs that are used as decorations. All decorations are applied sequentially.
351 * The \c char_decorator_terminal class is a formatter itself, so it can be used to construct
352 * more complex formatters, including nesting decorators.
354 template< typename SubactorT, typename ImplT >
355 class char_decorator_terminal
359 typedef char_decorator_terminal< SubactorT, ImplT > this_type;
362 #ifndef BOOST_LOG_DOXYGEN_PASS
363 //! Internal typedef for type categorization
364 typedef void _is_boost_log_terminal;
367 //! Implementation type
368 typedef ImplT impl_type;
370 typedef typename impl_type::char_type char_type;
372 typedef typename impl_type::string_type string_type;
374 typedef basic_formatting_ostream< char_type > stream_type;
375 //! Adopted actor type
376 typedef SubactorT subactor_type;
378 //! Result type definition
379 typedef string_type result_type;
382 //! Adopted formatter actor
383 subactor_type m_subactor;
389 * Initializing constructor.
391 char_decorator_terminal(subactor_type const& sub, impl_type const& impl) : m_subactor(sub), m_impl(impl)
397 char_decorator_terminal(char_decorator_terminal const& that) : m_subactor(that.m_subactor), m_impl(that.m_impl)
402 * \returns Adopted subactor
404 subactor_type const& get_subactor() const
410 * \returns Implementation
412 impl_type const& get_impl() const
418 * Invokation operator
420 template< typename ContextT >
421 result_type operator() (ContextT const& ctx)
424 stream_type strm(str);
426 // Invoke the adopted formatter
427 typedef phoenix::vector3<
429 typename fusion::result_of::at_c<
431 typename remove_reference<
432 typename phoenix::result_of::env< ContextT const& >::type
439 env_type env = { boost::addressof(m_subactor), fusion::at_c< 0 >(phoenix::env(ctx).args()), strm };
440 phoenix::eval(m_subactor, phoenix::make_context(env, phoenix::actions(ctx)));
442 // Flush the buffered characters and apply decorations
444 m_impl(*strm.rdbuf()->storage());
446 return BOOST_LOG_NRVO_RESULT(str);
450 * Invokation operator
452 template< typename ContextT >
453 result_type operator() (ContextT const& ctx) const
456 stream_type strm(str);
458 // Invoke the adopted formatter
459 typedef phoenix::vector3<
460 const subactor_type*,
461 typename fusion::result_of::at_c<
463 typename remove_reference<
464 typename phoenix::result_of::env< ContextT const& >::type
471 env_type env = { boost::addressof(m_subactor), fusion::at_c< 0 >(phoenix::env(ctx).args()), strm };
472 phoenix::eval(m_subactor, phoenix::make_context(env, phoenix::actions(ctx)));
474 // Flush the buffered characters and apply decorations
476 m_impl(*strm.rdbuf()->storage());
478 return BOOST_LOG_NRVO_RESULT(str);
481 BOOST_DELETED_FUNCTION(char_decorator_terminal())
485 * Character decorator actor
487 template< typename SubactorT, typename ImplT, template< typename > class ActorT = phoenix::actor >
488 class char_decorator_actor :
489 public ActorT< char_decorator_terminal< SubactorT, ImplT > >
492 //! Base terminal type
493 typedef char_decorator_terminal< SubactorT, ImplT > terminal_type;
495 typedef typename terminal_type::char_type char_type;
498 typedef ActorT< terminal_type > base_type;
501 //! Initializing constructor
502 explicit char_decorator_actor(base_type const& act) : base_type(act)
506 //! Returns reference to the terminal
507 terminal_type const& get_terminal() const
509 return this->proto_expr_.child0;
513 #ifndef BOOST_LOG_DOXYGEN_PASS
515 #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
516 template< typename LeftExprT, typename SubactorT, typename ImplT, template< typename > class ActorT >\
517 BOOST_FORCEINLINE phoenix::actor< aux::char_decorator_output_terminal< phoenix::actor< LeftExprT >, SubactorT, ImplT > >\
518 operator<< (phoenix::actor< LeftExprT > left_ref left, char_decorator_actor< SubactorT, ImplT, ActorT > right_ref right)\
520 typedef aux::char_decorator_output_terminal< phoenix::actor< LeftExprT >, SubactorT, ImplT > terminal_type;\
521 phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_terminal().get_subactor(), right.get_terminal().get_impl()) }};\
525 #include <boost/log/detail/generate_overloads.hpp>
527 #undef BOOST_LOG_AUX_OVERLOAD
529 #endif // BOOST_LOG_DOXYGEN_PASS
533 template< typename RangeT >
534 class char_decorator_gen1
536 RangeT const& m_decorations;
538 typedef typename boost::log::aux::deduce_char_type< typename range_value< RangeT >::type::first_type >::type char_type;
541 explicit char_decorator_gen1(RangeT const& decorations) : m_decorations(decorations)
545 template< typename SubactorT >
546 BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const
548 typedef pattern_replacer< char_type > replacer_type;
549 typedef char_decorator_actor< SubactorT, replacer_type > result_type;
550 typedef typename result_type::terminal_type terminal_type;
551 typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(m_decorations)) }};
552 return result_type(act);
556 template< typename FromRangeT, typename ToRangeT >
557 class char_decorator_gen2
559 FromRangeT const& m_from;
560 ToRangeT const& m_to;
562 typedef typename boost::log::aux::deduce_char_type< typename range_value< FromRangeT >::type >::type from_char_type;
563 typedef typename boost::log::aux::deduce_char_type< typename range_value< ToRangeT >::type >::type to_char_type;
564 BOOST_STATIC_ASSERT_MSG((is_same< from_char_type, to_char_type >::value), "Boost.Log: character decorator cannot be instantiated with different character types for source and replacement strings");
567 char_decorator_gen2(FromRangeT const& from, ToRangeT const& to) : m_from(from), m_to(to)
571 template< typename SubactorT >
572 BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< from_char_type > > operator[] (SubactorT const& subactor) const
574 typedef pattern_replacer< from_char_type > replacer_type;
575 typedef char_decorator_actor< SubactorT, replacer_type > result_type;
576 typedef typename result_type::terminal_type terminal_type;
577 typename result_type::base_type act = {{ terminal_type(subactor, replacer_type(m_from, m_to)) }};
578 return result_type(act);
585 * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
586 * to construct the actual decorator.
588 * \param decorations A sequence of string pairs that will be used as decorations. Every <tt>decorations[i].first</tt>
589 * substring occurrence in the output will be replaced with <tt>decorations[i].second</tt>.
591 template< typename RangeT >
592 BOOST_FORCEINLINE aux::char_decorator_gen1< RangeT > char_decor(RangeT const& decorations)
594 return aux::char_decorator_gen1< RangeT >(decorations);
598 * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
599 * to construct the actual decorator.
601 * \param from A sequence of strings that will be sought in the output.
602 * \param to A sequence of strings that will be used as replacements.
604 * \note The \a from and \a to sequences mush be of the same size. Every <tt>from[i]</tt>
605 * substring occurrence in the output will be replaced with <tt>to[i]</tt>.
607 template< typename FromRangeT, typename ToRangeT >
608 BOOST_FORCEINLINE aux::char_decorator_gen2< FromRangeT, ToRangeT > char_decor(FromRangeT const& from, ToRangeT const& to)
610 return aux::char_decorator_gen2< FromRangeT, ToRangeT >(from, to);
613 } // namespace expressions
615 BOOST_LOG_CLOSE_NAMESPACE // namespace log
617 #ifndef BOOST_LOG_DOXYGEN_PASS
621 namespace result_of {
623 template< typename SubactorT, typename ImplT >
624 struct is_nullary< custom_terminal< boost::log::expressions::char_decorator_terminal< SubactorT, ImplT > > > :
629 template< typename LeftT, typename SubactorT, typename ImplT >
630 struct is_nullary< custom_terminal< boost::log::expressions::aux::char_decorator_output_terminal< LeftT, SubactorT, ImplT > > > :
635 } // namespace result_of
637 } // namespace phoenix
643 #include <boost/log/detail/footer.hpp>
645 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_