Back to home page

EIC code displayed by LXR

 
 

    


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

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   attribute_mapping.hpp
0009  * \author Andrey Semashev
0010  * \date   07.11.2008
0011  *
0012  * The header contains facilities that are used in different sinks to map attribute values
0013  * used throughout the application to values used with the specific native logging API.
0014  * These tools are mostly needed to map application severity levels on native levels,
0015  * required by OS-specific sink backends.
0016  */
0017 
0018 #ifndef BOOST_LOG_SINKS_ATTRIBUTE_MAPPING_HPP_INCLUDED_
0019 #define BOOST_LOG_SINKS_ATTRIBUTE_MAPPING_HPP_INCLUDED_
0020 
0021 #include <map>
0022 #include <boost/log/detail/config.hpp>
0023 #include <boost/log/detail/tagged_integer.hpp>
0024 #include <boost/log/core/record_view.hpp>
0025 #include <boost/log/attributes/attribute_name.hpp>
0026 #include <boost/log/attributes/attribute_value_set.hpp>
0027 #include <boost/log/attributes/value_visitation.hpp>
0028 #include <boost/log/detail/header.hpp>
0029 
0030 #ifdef BOOST_HAS_PRAGMA_ONCE
0031 #pragma once
0032 #endif
0033 
0034 namespace boost {
0035 
0036 BOOST_LOG_OPEN_NAMESPACE
0037 
0038 namespace sinks {
0039 
0040 //! Base class for attribute mapping function objects
0041 template< typename MappedT >
0042 struct basic_mapping
0043 {
0044     //! Mapped value type
0045     typedef MappedT mapped_type;
0046     //! Result type
0047     typedef mapped_type result_type;
0048 };
0049 
0050 namespace aux {
0051 
0052     //! Attribute value visitor
0053     template< typename MappedT >
0054     struct direct_mapping_visitor
0055     {
0056         typedef void result_type;
0057         typedef MappedT mapped_type;
0058 
0059         explicit direct_mapping_visitor(mapped_type& extracted) :
0060             m_Extracted(extracted)
0061         {
0062         }
0063         template< typename T >
0064         void operator() (T const& val) const
0065         {
0066             m_Extracted = mapped_type(val);
0067         }
0068 
0069     private:
0070         mapped_type& m_Extracted;
0071     };
0072     //  Specialization for the tagged integer
0073     template< typename IntT, typename TagT >
0074     struct direct_mapping_visitor< boost::log::aux::tagged_integer< IntT, TagT > >
0075     {
0076         typedef void result_type;
0077         typedef boost::log::aux::tagged_integer< IntT, TagT > mapped_type;
0078 
0079         explicit direct_mapping_visitor(mapped_type& extracted) :
0080             m_Extracted(extracted)
0081         {
0082         }
0083         template< typename T >
0084         void operator() (T const& val) const
0085         {
0086             mapped_type v = { static_cast< IntT >(val) };
0087             m_Extracted = v;
0088         }
0089 
0090     private:
0091         mapped_type& m_Extracted;
0092     };
0093 
0094 } // namespace aux
0095 
0096 /*!
0097  * \brief Straightforward mapping
0098  *
0099  * This type of mapping assumes that attribute with a particular name always
0100  * provides values that map directly onto the native values. The mapping
0101  * simply returns the extracted attribute value converted to the native value.
0102  */
0103 template< typename MappedT, typename AttributeValueT = int >
0104 class basic_direct_mapping :
0105     public basic_mapping< MappedT >
0106 {
0107     //! Base type
0108     typedef basic_direct_mapping< MappedT > base_type;
0109 
0110 public:
0111     //! Attribute contained value type
0112     typedef AttributeValueT attribute_value_type;
0113     //! Mapped value type
0114     typedef typename base_type::mapped_type mapped_type;
0115 
0116 private:
0117     //! Attribute name
0118     const attribute_name m_Name;
0119     //! Visitor invoker for the attribute value
0120     value_visitor_invoker< attribute_value_type > m_Invoker;
0121     //! Default native value
0122     mapped_type m_DefaultValue;
0123 
0124 public:
0125     /*!
0126      * Constructor
0127      *
0128      * \param name Attribute name
0129      * \param default_value The default native value that is returned if the attribute value is not found
0130      */
0131     explicit basic_direct_mapping(attribute_name const& name, mapped_type const& default_value) :
0132         m_Name(name),
0133         m_DefaultValue(default_value)
0134     {
0135     }
0136 
0137     /*!
0138      * Extraction operator
0139      *
0140      * \param rec A log record to extract value from
0141      * \return An extracted attribute value
0142      */
0143     mapped_type operator() (record_view const& rec) const
0144     {
0145         mapped_type res = m_DefaultValue;
0146         aux::direct_mapping_visitor< mapped_type > vis(res);
0147         m_Invoker(m_Name, rec.attribute_values(), vis);
0148         return res;
0149     }
0150 };
0151 
0152 /*!
0153  * \brief Customizable mapping
0154  *
0155  * The class allows to setup a custom mapping between an attribute and native values.
0156  * The mapping should be initialized similarly to the standard \c map container, by using
0157  * indexing operator and assignment.
0158  *
0159  * \note Unlike many other components of the library, exact type of the attribute value
0160  *       must be specified in the template parameter \c AttributeValueT. Type sequences
0161  *       are not supported.
0162  */
0163 template< typename MappedT, typename AttributeValueT = int >
0164 class basic_custom_mapping :
0165     public basic_mapping< MappedT >
0166 {
0167     //! Base type
0168     typedef basic_mapping< MappedT > base_type;
0169 
0170 public:
0171     //! Attribute contained value type
0172     typedef AttributeValueT attribute_value_type;
0173     //! Mapped value type
0174     typedef typename base_type::mapped_type mapped_type;
0175 
0176 private:
0177     //! \cond
0178 
0179     //! Mapping type
0180     typedef std::map< attribute_value_type, mapped_type > mapping_type;
0181     //! Smart reference class for implementing insertion into the map
0182     class reference_proxy;
0183     friend class reference_proxy;
0184     class reference_proxy
0185     {
0186         mapping_type& m_Mapping;
0187         attribute_value_type m_Key;
0188 
0189     public:
0190         //! Constructor
0191         reference_proxy(mapping_type& mapping, attribute_value_type const& key) : m_Mapping(mapping), m_Key(key) {}
0192         //! Insertion
0193         reference_proxy const& operator= (mapped_type const& val) const
0194         {
0195             m_Mapping[m_Key] = val;
0196             return *this;
0197         }
0198     };
0199 
0200     //! Attribute value visitor
0201     struct visitor;
0202     friend struct visitor;
0203     struct visitor
0204     {
0205         typedef void result_type;
0206 
0207         visitor(mapping_type const& mapping, mapped_type& extracted) :
0208             m_Mapping(mapping),
0209             m_Extracted(extracted)
0210         {
0211         }
0212         template< typename T >
0213         void operator() (T const& val) const
0214         {
0215             typename mapping_type::const_iterator it = m_Mapping.find(val);
0216             if (it != m_Mapping.end())
0217                 m_Extracted = it->second;
0218         }
0219 
0220     private:
0221         mapping_type const& m_Mapping;
0222         mapped_type& m_Extracted;
0223     };
0224 
0225     //! \endcond
0226 
0227 private:
0228     //! Attribute name
0229     const attribute_name m_Name;
0230     //! Visitor invoker for the attribute value
0231     value_visitor_invoker< attribute_value_type > m_Invoker;
0232     //! Default native value
0233     mapped_type m_DefaultValue;
0234     //! Conversion mapping
0235     mapping_type m_Mapping;
0236 
0237 public:
0238     /*!
0239      * Constructor
0240      *
0241      * \param name Attribute name
0242      * \param default_value The default native value that is returned if the conversion cannot be performed
0243      */
0244     explicit basic_custom_mapping(attribute_name const& name, mapped_type const& default_value) :
0245         m_Name(name),
0246         m_DefaultValue(default_value)
0247     {
0248     }
0249     /*!
0250      * Extraction operator. Extracts the attribute value and attempts to map it onto
0251      * the native value.
0252      *
0253      * \param rec A log record to extract value from
0254      * \return A mapped value, if mapping was successful, or the default value if
0255      *         mapping did not succeed.
0256      */
0257     mapped_type operator() (record_view const& rec) const
0258     {
0259         mapped_type res = m_DefaultValue;
0260         visitor vis(m_Mapping, res);
0261         m_Invoker(m_Name, rec.attribute_values(), vis);
0262         return res;
0263     }
0264     /*!
0265      * Insertion operator
0266      *
0267      * \param key Attribute value to be mapped
0268      * \return An object of unspecified type that allows to insert a new mapping through assignment.
0269      *         The \a key argument becomes the key attribute value, and the assigned value becomes the
0270      *         mapped native value.
0271      */
0272 #ifndef BOOST_LOG_DOXYGEN_PASS
0273     reference_proxy operator[] (attribute_value_type const& key)
0274 #else
0275     implementation_defined operator[] (attribute_value_type const& key)
0276 #endif
0277     {
0278         return reference_proxy(m_Mapping, key);
0279     }
0280 };
0281 
0282 } // namespace sinks
0283 
0284 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0285 
0286 } // namespace boost
0287 
0288 #include <boost/log/detail/footer.hpp>
0289 
0290 #endif // BOOST_LOG_SINKS_ATTRIBUTE_MAPPING_HPP_INCLUDED_