zhangmeng
2021-07-02 056f71f24cefaf88f2a93714c6678c03ed5f1e0e
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
/*
 *             Copyright Andrey Semashev 2020.
 * Distributed under the Boost Software License, Version 1.0.
 *    (See accompanying file LICENSE_1_0.txt or copy at
 *          https://www.boost.org/LICENSE_1_0.txt)
 */
/*!
 * \file   utility/manipulators/range.hpp
 * \author Andrey Semashev
 * \date   11.05.2020
 *
 * The header contains implementation of a stream manipulator for inserting a range of elements, optionally separated with a delimiter.
 */
 
#ifndef BOOST_LOG_UTILITY_MANIPULATORS_RANGE_HPP_INCLUDED_
#define BOOST_LOG_UTILITY_MANIPULATORS_RANGE_HPP_INCLUDED_
 
#include <cstddef>
#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <boost/range/const_iterator.hpp>
#include <boost/core/enable_if.hpp>
#include <boost/type_traits/is_scalar.hpp>
#include <boost/type_traits/conditional.hpp>
#include <boost/log/detail/config.hpp>
#include <boost/log/detail/is_ostream.hpp>
#include <boost/log/detail/header.hpp>
 
#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif
 
namespace boost {
 
BOOST_LOG_OPEN_NAMESPACE
 
/*!
 * Stream manipulator for inserting a range of elements, optionally separated with a delimiter.
 */
template< typename RangeT, typename DelimiterT >
class range_manipulator
{
private:
    typedef typename conditional<
        is_scalar< DelimiterT >::value,
        DelimiterT,
        DelimiterT const&
    >::type stored_delimiter_type;
 
private:
    RangeT const& m_range;
    stored_delimiter_type m_delimiter;
 
public:
    //! Initializing constructor
    range_manipulator(RangeT const& range, stored_delimiter_type delimiter) BOOST_NOEXCEPT :
        m_range(range),
        m_delimiter(delimiter)
    {
    }
 
    //! The method outputs elements of the range separated with delimiter
    template< typename StreamT >
    void output(StreamT& stream) const
    {
        typedef typename boost::range_const_iterator< RangeT >::type const_iterator;
        const_iterator it = boost::begin(m_range);
        const const_iterator end = boost::end(m_range);
        if (BOOST_LIKELY(it != end))
        {
            stream << *it;
 
            for (++it; it != end; ++it)
            {
                stream << m_delimiter;
                stream << *it;
            }
        }
    }
};
 
/*!
 * Stream manipulator for inserting a range of elements. Specialization for when there is no delimiter.
 */
template< typename RangeT >
class range_manipulator< RangeT, void >
{
private:
    RangeT const& m_range;
 
public:
    //! Initializing constructor
    explicit range_manipulator(RangeT const& range) BOOST_NOEXCEPT :
        m_range(range)
    {
    }
 
    //! The method outputs elements of the range
    template< typename StreamT >
    void output(StreamT& stream) const
    {
        typedef typename boost::range_const_iterator< RangeT >::type const_iterator;
        const_iterator it = boost::begin(m_range);
        const const_iterator end = boost::end(m_range);
        for (; it != end; ++it)
        {
            stream << *it;
        }
    }
};
 
/*!
 * Stream output operator for \c range_manipulator. Outputs every element of the range, separated with a delimiter, if one was specified on manipulator construction.
 */
template< typename StreamT, typename RangeT, typename DelimiterT >
inline typename boost::enable_if_c< log::aux::is_ostream< StreamT >::value, StreamT& >::type operator<< (StreamT& strm, range_manipulator< RangeT, DelimiterT > const& manip)
{
    if (BOOST_LIKELY(strm.good()))
        manip.output(strm);
 
    return strm;
}
 
/*!
 * Range manipulator generator function.
 *
 * \param range Range of elements to output. The range must support begin and end iterators, and its elements must support stream output.
 * \param delimiter Delimiter to separate elements in the output. Optional. If not specified, elements are output without separation.
 * \returns Manipulator to be inserted into the stream.
 *
 * \note Both \a range and \a delimiter objects must outlive the created manipulator object.
 */
template< typename RangeT, typename DelimiterT >
inline typename boost::enable_if_c<
    is_scalar< DelimiterT >::value,
    range_manipulator< RangeT, DelimiterT >
>::type range_manip(RangeT const& range, DelimiterT delimiter) BOOST_NOEXCEPT
{
    return range_manipulator< RangeT, DelimiterT >(range, delimiter);
}
 
/*!
 * Range manipulator generator function.
 *
 * \param range Range of elements to output. The range must support begin and end iterators, and its elements must support stream output.
 * \param delimiter Delimiter to separate elements in the output. Optional. If not specified, elements are output without separation.
 * \returns Manipulator to be inserted into the stream.
 *
 * \note Both \a range and \a delimiter objects must outlive the created manipulator object.
 */
template< typename RangeT, typename DelimiterT >
inline typename boost::disable_if_c<
    is_scalar< DelimiterT >::value,
    range_manipulator< RangeT, DelimiterT >
>::type range_manip(RangeT const& range, DelimiterT const& delimiter) BOOST_NOEXCEPT
{
    return range_manipulator< RangeT, DelimiterT >(range, delimiter);
}
 
/*!
 * Range manipulator generator function.
 *
 * \param range Range of elements to output. The range must support begin and end iterators, and its elements must support stream output.
 * \param delimiter Delimiter to separate elements in the output. Optional. If not specified, elements are output without separation.
 * \returns Manipulator to be inserted into the stream.
 *
 * \note Both \a range and \a delimiter objects must outlive the created manipulator object.
 */
template< typename RangeT, typename DelimiterElementT, std::size_t N >
inline range_manipulator< RangeT, DelimiterElementT* > range_manip(RangeT const& range, DelimiterElementT (&delimiter)[N]) BOOST_NOEXCEPT
{
    return range_manipulator< RangeT, DelimiterElementT* >(range, delimiter);
}
 
/*!
 * Range manipulator generator function.
 *
 * \param range Range of elements to output. The range must support begin and end iterators, and its elements must support stream output.
 * \returns Manipulator to be inserted into the stream.
 *
 * \note \a delimiter object must outlive the created manipulator object.
 */
template< typename RangeT >
inline range_manipulator< RangeT, void > range_manip(RangeT const& range) BOOST_NOEXCEPT
{
    return range_manipulator< RangeT, void >(range);
}
 
BOOST_LOG_CLOSE_NAMESPACE // namespace log
 
} // namespace boost
 
#include <boost/log/detail/footer.hpp>
 
#endif // BOOST_LOG_UTILITY_MANIPULATORS_RANGE_HPP_INCLUDED_