]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/log/include/boost/log/expressions/formatters/char_decorator.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / log / include / boost / log / expressions / formatters / char_decorator.hpp
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/char_decorator.hpp
9 * \author Andrey Semashev
10 * \date 17.11.2012
11 *
12 * The header contains implementation of a character decorator.
13 */
14
15 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_
16 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_
17
18 #include <vector>
19 #include <string>
20 #include <iterator>
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>
48
49 #ifdef BOOST_HAS_PRAGMA_ONCE
50 #pragma once
51 #endif
52
53 namespace boost {
54
55 BOOST_LOG_OPEN_NAMESPACE
56
57 namespace expressions {
58
59 namespace aux {
60
61 template< typename RangeT >
62 struct string_const_iterator : range_const_iterator< RangeT > {};
63 template< >
64 struct string_const_iterator< char* > { typedef char* type; };
65 template< >
66 struct string_const_iterator< const char* > { typedef const char* type; };
67 template< >
68 struct string_const_iterator< wchar_t* > { typedef wchar_t* type; };
69 template< >
70 struct string_const_iterator< const wchar_t* > { typedef const wchar_t* type; };
71
72 } // namespace aux
73
74 /*!
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.
77 */
78 template< typename CharT >
79 class pattern_replacer
80 {
81 public:
82 //! Result type
83 typedef void result_type;
84
85 //! Character type
86 typedef CharT char_type;
87 //! String type
88 typedef std::basic_string< char_type > string_type;
89
90 private:
91 //! Lengths of source pattern and replacement
92 struct string_lengths
93 {
94 unsigned int from_len, to_len;
95 };
96
97 //! List of the decorations to apply
98 typedef std::vector< string_lengths > string_lengths_list;
99
100 private:
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;
105
106 public:
107 /*!
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.
111 */
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()
117 #endif
118 )
119 {
120 typedef typename range_const_iterator< RangeT >::type iterator;
121 for (iterator it = boost::begin(decorations), end_ = boost::end(decorations); it != end_; ++it)
122 {
123 string_lengths lens;
124 {
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);
129 }
130 {
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);
135 }
136 m_string_lengths.push_back(lens);
137 }
138 }
139 /*!
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>.
143 */
144 template< typename FromRangeT, typename ToRangeT >
145 pattern_replacer(FromRangeT const& from, ToRangeT const& to)
146 {
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)
152 {
153 string_lengths lens;
154 {
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);
159 }
160 {
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);
165 }
166 m_string_lengths.push_back(lens);
167 }
168
169 // Both sequences should be of the same size
170 BOOST_ASSERT(it1 == end1);
171 BOOST_ASSERT(it2 == end2);
172 }
173 //! Copy constructor
174 pattern_replacer(pattern_replacer const& that) : m_decoration_chars(that.m_decoration_chars), m_string_lengths(that.m_string_lengths)
175 {
176 }
177
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
180 {
181 typedef typename string_type::size_type size_type;
182
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)
185 {
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))
189 {
190 str.replace(pos, from_len, to_chars, to_len);
191 pos += to_len;
192 }
193 from_chars = to_chars + to_len;
194 }
195 }
196
197 private:
198 static char_type* string_begin(char_type* p)
199 {
200 return p;
201 }
202 static const char_type* string_begin(const char_type* p)
203 {
204 return p;
205 }
206 template< typename RangeT >
207 static typename range_const_iterator< RangeT >::type string_begin(RangeT const& r)
208 {
209 return boost::begin(r);
210 }
211
212 static char_type* string_end(char_type* p)
213 {
214 return p + std::char_traits< char_type >::length(p);
215 }
216 static const char_type* string_end(const char_type* p)
217 {
218 return p + std::char_traits< char_type >::length(p);
219 }
220 template< typename RangeT >
221 static typename range_const_iterator< RangeT >::type string_end(RangeT const& r)
222 {
223 return boost::end(r);
224 }
225 };
226
227 namespace aux {
228
229 //! Character decorator stream output terminal
230 template< typename LeftT, typename SubactorT, typename ImplT >
231 class char_decorator_output_terminal
232 {
233 private:
234 //! Self type
235 typedef char_decorator_output_terminal< LeftT, SubactorT, ImplT > this_type;
236
237 public:
238 #ifndef BOOST_LOG_DOXYGEN_PASS
239 //! Internal typedef for type categorization
240 typedef void _is_boost_log_terminal;
241 #endif
242
243 //! Implementation type
244 typedef ImplT impl_type;
245
246 //! Character type
247 typedef typename impl_type::char_type char_type;
248 //! String type
249 typedef typename impl_type::string_type string_type;
250 //! Adopted actor type
251 typedef SubactorT subactor_type;
252
253 //! Result type definition
254 template< typename >
255 struct result;
256
257 template< typename ThisT, typename ContextT >
258 struct result< ThisT(ContextT) >
259 {
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&,
263 context_type,
264 phoenix::unused
265 >::result_type type;
266 };
267
268 private:
269 //! Left argument actor
270 LeftT m_left;
271 //! Adopted formatter actor
272 subactor_type m_subactor;
273 //! Implementation type
274 impl_type m_impl;
275
276 public:
277 /*!
278 * Initializing constructor. Creates decorator of the \a fmt formatter with the specified \a decorations.
279 */
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)
282 {
283 }
284 /*!
285 * Copy constructor
286 */
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)
289 {
290 }
291
292 /*!
293 * Invokation operator
294 */
295 template< typename ContextT >
296 typename result< this_type(ContextT const&) >::type operator() (ContextT const& ctx)
297 {
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);
301 strm.flush();
302 typename string_type::size_type const start_pos = strm.rdbuf()->storage()->size();
303
304 // Invoke the adopted formatter
305 phoenix::eval(m_subactor, ctx);
306
307 // Flush the buffered characters and apply decorations
308 strm.flush();
309 m_impl(*strm.rdbuf()->storage(), start_pos);
310 strm.rdbuf()->ensure_max_size();
311
312 return strm;
313 }
314
315 /*!
316 * Invokation operator
317 */
318 template< typename ContextT >
319 typename result< const this_type(ContextT const&) >::type operator() (ContextT const& ctx) const
320 {
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);
324 strm.flush();
325 typename string_type::size_type const start_pos = strm.rdbuf()->storage()->size();
326
327 // Invoke the adopted formatter
328 phoenix::eval(m_subactor, ctx);
329
330 // Flush the buffered characters and apply decorations
331 strm.flush();
332 m_impl(*strm.rdbuf()->storage(), start_pos);
333 strm.rdbuf()->ensure_max_size();
334
335 return strm;
336 }
337
338 BOOST_DELETED_FUNCTION(char_decorator_output_terminal())
339 };
340
341 } // namespace aux
342
343 /*!
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
347 * text-based sinks.
348 *
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.
353 */
354 template< typename SubactorT, typename ImplT >
355 class char_decorator_terminal
356 {
357 private:
358 //! Self type
359 typedef char_decorator_terminal< SubactorT, ImplT > this_type;
360
361 public:
362 #ifndef BOOST_LOG_DOXYGEN_PASS
363 //! Internal typedef for type categorization
364 typedef void _is_boost_log_terminal;
365 #endif
366
367 //! Implementation type
368 typedef ImplT impl_type;
369 //! Character type
370 typedef typename impl_type::char_type char_type;
371 //! String type
372 typedef typename impl_type::string_type string_type;
373 //! Stream type
374 typedef basic_formatting_ostream< char_type > stream_type;
375 //! Adopted actor type
376 typedef SubactorT subactor_type;
377
378 //! Result type definition
379 typedef string_type result_type;
380
381 private:
382 //! Adopted formatter actor
383 subactor_type m_subactor;
384 //! Implementation
385 impl_type m_impl;
386
387 public:
388 /*!
389 * Initializing constructor.
390 */
391 char_decorator_terminal(subactor_type const& sub, impl_type const& impl) : m_subactor(sub), m_impl(impl)
392 {
393 }
394 /*!
395 * Copy constructor
396 */
397 char_decorator_terminal(char_decorator_terminal const& that) : m_subactor(that.m_subactor), m_impl(that.m_impl)
398 {
399 }
400
401 /*!
402 * \returns Adopted subactor
403 */
404 subactor_type const& get_subactor() const
405 {
406 return m_subactor;
407 }
408
409 /*!
410 * \returns Implementation
411 */
412 impl_type const& get_impl() const
413 {
414 return m_impl;
415 }
416
417 /*!
418 * Invokation operator
419 */
420 template< typename ContextT >
421 result_type operator() (ContextT const& ctx)
422 {
423 string_type str;
424 stream_type strm(str);
425
426 // Invoke the adopted formatter
427 typedef phoenix::vector3<
428 subactor_type*,
429 typename fusion::result_of::at_c<
430 typename remove_cv<
431 typename remove_reference<
432 typename phoenix::result_of::env< ContextT const& >::type
433 >::type
434 >::type::args_type,
435 0
436 >::type,
437 stream_type&
438 > env_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)));
441
442 // Flush the buffered characters and apply decorations
443 strm.flush();
444 m_impl(*strm.rdbuf()->storage());
445
446 return BOOST_LOG_NRVO_RESULT(str);
447 }
448
449 /*!
450 * Invokation operator
451 */
452 template< typename ContextT >
453 result_type operator() (ContextT const& ctx) const
454 {
455 string_type str;
456 stream_type strm(str);
457
458 // Invoke the adopted formatter
459 typedef phoenix::vector3<
460 const subactor_type*,
461 typename fusion::result_of::at_c<
462 typename remove_cv<
463 typename remove_reference<
464 typename phoenix::result_of::env< ContextT const& >::type
465 >::type
466 >::type::args_type,
467 0
468 >::type,
469 stream_type&
470 > env_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)));
473
474 // Flush the buffered characters and apply decorations
475 strm.flush();
476 m_impl(*strm.rdbuf()->storage());
477
478 return BOOST_LOG_NRVO_RESULT(str);
479 }
480
481 BOOST_DELETED_FUNCTION(char_decorator_terminal())
482 };
483
484 /*!
485 * Character decorator actor
486 */
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 > >
490 {
491 public:
492 //! Base terminal type
493 typedef char_decorator_terminal< SubactorT, ImplT > terminal_type;
494 //! Character type
495 typedef typename terminal_type::char_type char_type;
496
497 //! Base actor type
498 typedef ActorT< terminal_type > base_type;
499
500 public:
501 //! Initializing constructor
502 explicit char_decorator_actor(base_type const& act) : base_type(act)
503 {
504 }
505
506 //! Returns reference to the terminal
507 terminal_type const& get_terminal() const
508 {
509 return this->proto_expr_.child0;
510 }
511 };
512
513 #ifndef BOOST_LOG_DOXYGEN_PASS
514
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)\
519 {\
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()) }};\
522 return actor;\
523 }
524
525 #include <boost/log/detail/generate_overloads.hpp>
526
527 #undef BOOST_LOG_AUX_OVERLOAD
528
529 #endif // BOOST_LOG_DOXYGEN_PASS
530
531 namespace aux {
532
533 template< typename RangeT >
534 class char_decorator_gen1
535 {
536 RangeT const& m_decorations;
537
538 typedef typename boost::log::aux::deduce_char_type< typename range_value< RangeT >::type::first_type >::type char_type;
539
540 public:
541 explicit char_decorator_gen1(RangeT const& decorations) : m_decorations(decorations)
542 {
543 }
544
545 template< typename SubactorT >
546 BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< char_type > > operator[] (SubactorT const& subactor) const
547 {
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);
553 }
554 };
555
556 template< typename FromRangeT, typename ToRangeT >
557 class char_decorator_gen2
558 {
559 FromRangeT const& m_from;
560 ToRangeT const& m_to;
561
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");
565
566 public:
567 char_decorator_gen2(FromRangeT const& from, ToRangeT const& to) : m_from(from), m_to(to)
568 {
569 }
570
571 template< typename SubactorT >
572 BOOST_FORCEINLINE char_decorator_actor< SubactorT, pattern_replacer< from_char_type > > operator[] (SubactorT const& subactor) const
573 {
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);
579 }
580 };
581
582 } // namespace aux
583
584 /*!
585 * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
586 * to construct the actual decorator.
587 *
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>.
590 */
591 template< typename RangeT >
592 BOOST_FORCEINLINE aux::char_decorator_gen1< RangeT > char_decor(RangeT const& decorations)
593 {
594 return aux::char_decorator_gen1< RangeT >(decorations);
595 }
596
597 /*!
598 * The function returns a decorator generator object. The generator provides <tt>operator[]</tt> that can be used
599 * to construct the actual decorator.
600 *
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.
603 *
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>.
606 */
607 template< typename FromRangeT, typename ToRangeT >
608 BOOST_FORCEINLINE aux::char_decorator_gen2< FromRangeT, ToRangeT > char_decor(FromRangeT const& from, ToRangeT const& to)
609 {
610 return aux::char_decorator_gen2< FromRangeT, ToRangeT >(from, to);
611 }
612
613 } // namespace expressions
614
615 BOOST_LOG_CLOSE_NAMESPACE // namespace log
616
617 #ifndef BOOST_LOG_DOXYGEN_PASS
618
619 namespace phoenix {
620
621 namespace result_of {
622
623 template< typename SubactorT, typename ImplT >
624 struct is_nullary< custom_terminal< boost::log::expressions::char_decorator_terminal< SubactorT, ImplT > > > :
625 public mpl::false_
626 {
627 };
628
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 > > > :
631 public mpl::false_
632 {
633 };
634
635 } // namespace result_of
636
637 } // namespace phoenix
638
639 #endif
640
641 } // namespace boost
642
643 #include <boost/log/detail/footer.hpp>
644
645 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_CHAR_DECORATOR_HPP_INCLUDED_