]> git.sesse.net Git - casparcg/blob - dependencies/boost/boost/log/utility/init/formatter_parser.hpp
Manually merged pull request #222
[casparcg] / dependencies / boost / boost / log / utility / init / formatter_parser.hpp
1 /*
2  *          Copyright Andrey Semashev 2007 - 2010.
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   formatter_parser.hpp
9  * \author Andrey Semashev
10  * \date   07.04.2008
11  *
12  * The header contains definition of a formatter parser function, along with facilities to
13  * add support for custom formatters.
14  */
15
16 #if (defined(_MSC_VER) && _MSC_VER > 1000)
17 #pragma once
18 #endif // _MSC_VER > 1000
19
20 #ifndef BOOST_LOG_UTILITY_INIT_FORMATTER_PARSER_HPP_INCLUDED_
21 #define BOOST_LOG_UTILITY_INIT_FORMATTER_PARSER_HPP_INCLUDED_
22
23 #include <new> // std::nothrow
24 #include <iosfwd>
25 #include <map>
26 #include <string>
27 #include <boost/function/function2.hpp>
28 #include <boost/log/detail/setup_prologue.hpp>
29 #include <boost/log/core/record.hpp>
30 #include <boost/log/formatters/attr.hpp>
31
32 #ifdef _MSC_VER
33 #pragma warning(push)
34 // 'm_A' : class 'A' needs to have dll-interface to be used by clients of class 'B'
35 #pragma warning(disable: 4251)
36 // non dll-interface class 'A' used as base for dll-interface class 'B'
37 #pragma warning(disable: 4275)
38 #endif // _MSC_VER
39
40 namespace boost {
41
42 namespace BOOST_LOG_NAMESPACE {
43
44 /*!
45  * \brief Auxiliary formatter traits
46  *
47  * The structure generates commonly used types related to formatters and formatter factories.
48  */
49 template< typename CharT >
50 struct formatter_types
51 {
52     //! Character type
53     typedef CharT char_type;
54     //! String type
55     typedef std::basic_string< char_type > string_type;
56     //! Output stream type
57     typedef std::basic_ostream< char_type > ostream_type;
58     //! Log record type
59     typedef basic_record< char_type > record_type;
60     //! The formatter function object
61     typedef function2< void, ostream_type&, record_type const& > formatter_type;
62
63     /*!
64      * Type of the map of formatter factory arguments [argument name -> argument value].
65      * This type of maps will be passed to formatter factories on attempt to create a formatter.
66      */
67     typedef std::map< string_type, string_type > formatter_factory_args;
68     /*!
69      * \brief The type of a function object that constructs formatter instance
70      * \param name Attribute name
71      * \param args Formatter arguments
72      * \return The constructed formatter. The formatter must not be empty.
73      *
74      * \b Throws: An <tt>std::exception</tt>-based If an exception is thrown from the method,
75      *        the exception is propagated to the parse_formatter caller
76      */
77     typedef function2< formatter_type, string_type const&, formatter_factory_args const& > formatter_factory;
78     //! Map of formatter factory function objects
79     typedef std::map< string_type, formatter_factory > factories_map;
80 };
81
82 namespace aux {
83
84     /*!
85      * A simple formatter factory function. Provides an easy way to add user-defined types support to
86      * the formatter parser. The factory does not consider any formatter arguments, if specified,
87      * but produces the formatter that uses the native \c operator<< to format the attribute value.
88      *
89      * \param attr_name Attribute name to create formatter for.
90      */
91     template< typename CharT, typename AttributeValueT >
92     typename formatter_types< CharT >::formatter_type make_simple_formatter(
93         typename formatter_types< CharT >::string_type const& attr_name,
94         typename formatter_types< CharT >::formatter_factory_args const&)
95     {
96         typedef typename formatter_types< CharT >::formatter_type formatter_type;
97         return formatter_type(boost::log::formatters::attr< AttributeValueT >(attr_name, std::nothrow));
98     }
99
100 } // namespace aux
101
102 /*!
103  * \brief The function registers a user-defined formatter factory
104  *
105  * The function registers a user-defined formatter factory. The registered factory function will be
106  * called when the formatter parser detects the specified attribute name in the formatter string.
107  *
108  * \pre <tt>attr_name != NULL && !factory.empty()</tt>, \c attr_name must point to a zero-terminated sequence of characters.
109  *
110  * \param attr_name Attribute name
111  * \param factory Formatter factory function
112  */
113 template< typename CharT >
114 BOOST_LOG_SETUP_EXPORT void register_formatter_factory(
115     const CharT* attr_name,
116 #ifndef BOOST_LOG_BROKEN_TEMPLATE_DEFINITION_MATCHING
117     typename formatter_types< CharT >::formatter_factory const& factory
118 #else
119     function2<
120         function2< void, std::basic_ostream< CharT >&, basic_record< CharT > const& >,
121         std::basic_string< CharT > const&,
122         std::map< std::basic_string< CharT >, std::basic_string< CharT > > const&
123     > const& factory
124 #endif // BOOST_LOG_BROKEN_TEMPLATE_DEFINITION_MATCHING
125     );
126
127 /*!
128  * \brief The function registers a user-defined formatter factory
129  *
130  * The function registers a user-defined formatter factory. The registered factory function will be
131  * called when the formatter parser detects the specified attribute name in the formatter string.
132  *
133  * \pre <tt>!factory.empty()</tt>
134  *
135  * \param attr_name Attribute name
136  * \param factory Formatter factory function
137  */
138 template< typename CharT, typename TraitsT, typename AllocatorT >
139 inline void register_formatter_factory(
140     std::basic_string< CharT, TraitsT, AllocatorT > const& attr_name,
141     typename formatter_types< CharT >::formatter_factory const& factory)
142 {
143     register_formatter_factory(attr_name.c_str(), factory);
144 }
145
146 /*!
147  * \brief The function registers a simple formatter factory
148  *
149  * The function registers a simple formatter factory. The registered factory will generate formatters
150  * that will be equivalent to the <tt>log::formatters::attr</tt> formatter (i.e. that will use the
151  * native \c operator<< to format the attribute value). The factory does not use any arguments,
152  * if specified.
153  *
154  * \pre <tt>attr_name != NULL</tt>, \c attr_name must point to a zero-terminated sequence of characters.
155  *
156  * \param attr_name Attribute name
157  */
158 template< typename AttributeValueT, typename CharT >
159 inline void register_simple_formatter_factory(const CharT* attr_name)
160 {
161     register_formatter_factory(attr_name, &boost::log::aux::make_simple_formatter< CharT, AttributeValueT >);
162 }
163
164 /*!
165  * \brief The function registers a simple formatter factory
166  *
167  * The function registers a simple formatter factory. The registered factory will generate formatters
168  * that will be equivalent to the <tt>log::formatters::attr</tt> formatter (i.e. that will use the
169  * native \c operator<< to format the attribute value). The factory does not use any arguments,
170  * if specified.
171  *
172  * \param attr_name Attribute name
173  */
174 template< typename AttributeValueT, typename CharT, typename TraitsT, typename AllocatorT >
175 inline void register_simple_formatter_factory(std::basic_string< CharT, TraitsT, AllocatorT > const& attr_name)
176 {
177     register_formatter_factory(attr_name.c_str(), &boost::log::aux::make_simple_formatter< CharT, AttributeValueT >);
178 }
179
180 /*!
181  * The function parses a formatter from the sequence of characters
182  *
183  * \pre <tt>begin <= end</tt>, both pointers must not be NULL
184  * \param begin Pointer to the first character of the sequence
185  * \param end Pointer to the after-the-last character of the sequence
186  * \return A function object that can be used as a formatter.
187  *
188  * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
189  */
190 template< typename CharT >
191 BOOST_LOG_SETUP_EXPORT
192 #ifndef BOOST_LOG_BROKEN_TEMPLATE_DEFINITION_MATCHING
193 typename formatter_types< CharT >::formatter_type
194 #else
195 function2< void, std::basic_ostream< CharT >&, basic_record< CharT > const& >
196 #endif
197 parse_formatter(const CharT* begin, const CharT* end);
198
199 /*!
200  * The function parses a formatter from the string
201  *
202  * \param str A string that contains format description
203  * \return A function object that can be used as a formatter.
204  *
205  * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
206  */
207 template< typename CharT, typename TraitsT, typename AllocatorT >
208 inline typename formatter_types< CharT >::formatter_type
209 parse_formatter(std::basic_string< CharT, TraitsT, AllocatorT > const& str)
210 {
211     const CharT* p = str.c_str();
212     return parse_formatter(p, p + str.size());
213 }
214
215 /*!
216  * The function parses a formatter from the string
217  *
218  * \pre <tt>str != NULL</tt>, <tt>str</tt> points to a zero-terminated string
219  * \param str A string that contains format description.
220  * \return A function object that can be used as a formatter.
221  *
222  * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
223  */
224 template< typename CharT >
225 inline typename formatter_types< CharT >::formatter_type
226 parse_formatter(const CharT* str)
227 {
228     return parse_formatter(str, str + std::char_traits< CharT >::length(str));
229 }
230
231 } // namespace log
232
233 } // namespace boost
234
235 #ifdef _MSC_VER
236 #pragma warning(pop)
237 #endif // _MSC_VER
238
239 #endif // BOOST_LOG_UTILITY_INIT_FORMATTER_PARSER_HPP_INCLUDED_