Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:28

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   formatter_parser.hpp
0009  * \author Andrey Semashev
0010  * \date   07.04.2008
0011  *
0012  * The header contains definition of a formatter parser function, along with facilities to
0013  * add support for custom formatters.
0014  */
0015 
0016 #ifndef BOOST_LOG_UTILITY_SETUP_FORMATTER_PARSER_HPP_INCLUDED_
0017 #define BOOST_LOG_UTILITY_SETUP_FORMATTER_PARSER_HPP_INCLUDED_
0018 
0019 #include <iosfwd>
0020 #include <map>
0021 #include <string>
0022 #include <boost/smart_ptr/shared_ptr.hpp>
0023 #include <boost/smart_ptr/make_shared_object.hpp>
0024 #include <boost/core/enable_if.hpp>
0025 #include <boost/type_traits/is_base_and_derived.hpp>
0026 #include <boost/log/detail/setup_config.hpp>
0027 #include <boost/log/attributes/attribute_name.hpp>
0028 #include <boost/log/core/record.hpp>
0029 #include <boost/log/expressions/formatter.hpp>
0030 #include <boost/log/expressions/attr.hpp>
0031 #include <boost/log/expressions/formatters/stream.hpp>
0032 #include <boost/log/detail/header.hpp>
0033 
0034 #ifdef BOOST_HAS_PRAGMA_ONCE
0035 #pragma once
0036 #endif
0037 
0038 namespace boost {
0039 
0040 BOOST_LOG_OPEN_NAMESPACE
0041 
0042 /*!
0043  * Formatter factory base interface.
0044  */
0045 template< typename CharT >
0046 struct formatter_factory
0047 {
0048     //! Character type
0049     typedef CharT char_type;
0050     //! String type
0051     typedef std::basic_string< char_type > string_type;
0052     //! The formatter function object
0053     typedef basic_formatter< char_type > formatter_type;
0054     /*!
0055      * Type of the map of formatter factory arguments [argument name -> argument value].
0056      * This type of maps will be passed to formatter factories on attempt to create a formatter.
0057      */
0058     typedef std::map< string_type, string_type > args_map;
0059 
0060     /*!
0061      * Default constructor
0062      */
0063     BOOST_DEFAULTED_FUNCTION(formatter_factory(), {})
0064 
0065     /*!
0066      * Virtual destructor
0067      */
0068     virtual ~formatter_factory() {}
0069 
0070     /*!
0071      * The function creates a formatter for the specified attribute.
0072      *
0073      * \param name Attribute name
0074      * \param args Formatter arguments
0075      */
0076     virtual formatter_type create_formatter(attribute_name const& name, args_map const& args) = 0;
0077 
0078     BOOST_DELETED_FUNCTION(formatter_factory(formatter_factory const&))
0079     BOOST_DELETED_FUNCTION(formatter_factory& operator= (formatter_factory const&))
0080 };
0081 
0082 /*!
0083  * Base class for formatter factories. This class provides default implementation of formatter expressions for
0084  * types supporting stream output. The factory does not take into account any additional parameters that may be specified.
0085  */
0086 template< typename CharT, typename AttributeValueT >
0087 class basic_formatter_factory :
0088     public formatter_factory< CharT >
0089 {
0090 private:
0091     typedef formatter_factory< CharT > base_type;
0092 
0093 public:
0094     //! Attribute value type
0095     typedef AttributeValueT value_type;
0096     //  Type imports from the base class
0097     typedef typename base_type::formatter_type formatter_type;
0098     typedef typename base_type::args_map args_map;
0099 
0100     /*!
0101      * The function creates a formatter for the specified attribute.
0102      *
0103      * \param name Attribute name
0104      * \param args Formatter arguments
0105      */
0106     formatter_type create_formatter(attribute_name const& name, args_map const& args)
0107     {
0108         return formatter_type(expressions::stream << expressions::attr< value_type >(name));
0109     }
0110 };
0111 
0112 /*!
0113  * \brief The function registers a user-defined formatter factory
0114  *
0115  * The function registers a user-defined formatter factory. The registered factory function will be
0116  * called when the formatter parser detects the specified attribute name in the formatter string.
0117  *
0118  * \pre <tt>!!attr_name && !!factory</tt>.
0119  *
0120  * \param attr_name Attribute name
0121  * \param factory Pointer to the formatter factory
0122  */
0123 template< typename CharT >
0124 BOOST_LOG_SETUP_API void register_formatter_factory(
0125     attribute_name const& attr_name, shared_ptr< formatter_factory< CharT > > const& factory);
0126 
0127 /*!
0128  * \brief The function registers a user-defined formatter factory
0129  *
0130  * The function registers a user-defined formatter factory. The registered factory function will be
0131  * called when the formatter parser detects the specified attribute name in the formatter string.
0132  *
0133  * \pre <tt>!!attr_name && !!factory</tt>.
0134  *
0135  * \param attr_name Attribute name
0136  * \param factory Pointer to the formatter factory
0137  */
0138 template< typename FactoryT >
0139 inline typename boost::enable_if_c<
0140     is_base_and_derived< formatter_factory< typename FactoryT::char_type >, FactoryT >::value
0141 >::type register_formatter_factory(attribute_name const& attr_name, shared_ptr< FactoryT > const& factory)
0142 {
0143     typedef formatter_factory< typename FactoryT::char_type > factory_base;
0144     register_formatter_factory(attr_name, boost::static_pointer_cast< factory_base >(factory));
0145 }
0146 
0147 /*!
0148  * \brief The function registers a simple formatter factory
0149  *
0150  * The function registers a simple formatter factory. The registered factory will generate formatters
0151  * that will be equivalent to the <tt>log::expressions::attr</tt> formatter (i.e. that will use the
0152  * native \c operator<< to format the attribute value). The factory does not use any arguments from the format string,
0153  * if specified.
0154  *
0155  * \pre <tt>!!attr_name</tt>.
0156  *
0157  * \param attr_name Attribute name
0158  */
0159 template< typename AttributeValueT, typename CharT >
0160 inline void register_simple_formatter_factory(attribute_name const& attr_name)
0161 {
0162     shared_ptr< formatter_factory< CharT > > factory =
0163         boost::make_shared< basic_formatter_factory< CharT, AttributeValueT > >();
0164     register_formatter_factory(attr_name, factory);
0165 }
0166 
0167 /*!
0168  * The function parses a formatter from the sequence of characters
0169  *
0170  * \pre <tt>begin <= end</tt>, both pointers must not be NULL
0171  * \param begin Pointer to the first character of the sequence
0172  * \param end Pointer to the after-the-last character of the sequence
0173  * \return The parsed formatter.
0174  *
0175  * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
0176  */
0177 template< typename CharT >
0178 BOOST_LOG_SETUP_API basic_formatter< CharT > parse_formatter(const CharT* begin, const CharT* end);
0179 
0180 /*!
0181  * The function parses a formatter from the string
0182  *
0183  * \param str A string that contains format description
0184  * \return The parsed formatter.
0185  *
0186  * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
0187  */
0188 template< typename CharT, typename TraitsT, typename AllocatorT >
0189 inline basic_formatter< CharT > parse_formatter(std::basic_string< CharT, TraitsT, AllocatorT > const& str)
0190 {
0191     const CharT* p = str.c_str();
0192     return parse_formatter(p, p + str.size());
0193 }
0194 
0195 /*!
0196  * The function parses a formatter from the string
0197  *
0198  * \pre <tt>str != NULL</tt>, <tt>str</tt> points to a zero-terminated string
0199  * \param str A string that contains format description.
0200  * \return The parsed formatter.
0201  *
0202  * \b Throws: An <tt>std::exception</tt>-based exception, if a formatter cannot be recognized in the character sequence.
0203  */
0204 template< typename CharT >
0205 inline basic_formatter< CharT > parse_formatter(const CharT* str)
0206 {
0207     return parse_formatter(str, str + std::char_traits< CharT >::length(str));
0208 }
0209 
0210 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0211 
0212 } // namespace boost
0213 
0214 #include <boost/log/detail/footer.hpp>
0215 
0216 #endif // BOOST_LOG_UTILITY_SETUP_FORMATTER_PARSER_HPP_INCLUDED_