Back to home page

EIC code displayed by LXR

 
 

    


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

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   fallback_policy.hpp
0009  * \author Andrey Semashev
0010  * \date   18.08.2012
0011  *
0012  * The header contains definition of fallback policies when attribute value visitation or extraction fails.
0013  */
0014 
0015 #ifndef BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
0016 #define BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
0017 
0018 #include <boost/type_index.hpp>
0019 #include <boost/type_traits/remove_cv.hpp>
0020 #include <boost/type_traits/remove_reference.hpp>
0021 #include <boost/log/detail/config.hpp>
0022 #include <boost/log/exceptions.hpp>
0023 #include <boost/log/attributes/fallback_policy_fwd.hpp>
0024 #include <boost/log/detail/header.hpp>
0025 
0026 #ifdef BOOST_HAS_PRAGMA_ONCE
0027 #pragma once
0028 #endif
0029 
0030 namespace boost {
0031 
0032 BOOST_LOG_OPEN_NAMESPACE
0033 
0034 /*!
0035  * The \c fallback_to_none policy results in returning an empty value reference if the attribute value cannot be extracted.
0036  */
0037 struct fallback_to_none
0038 {
0039     enum { guaranteed_result = false };
0040 
0041     /*!
0042      * The method is called in order to apply a function object to the default value.
0043      */
0044     template< typename FunT >
0045     static bool apply_default(FunT&)
0046     {
0047         return false;
0048     }
0049 
0050     /*!
0051      * The method is called in order to apply a function object to the default value.
0052      */
0053     template< typename FunT >
0054     static bool apply_default(FunT const&)
0055     {
0056         return false;
0057     }
0058 
0059     /*!
0060      * The method is called when value extraction failed because the attribute value has different type than requested.
0061      */
0062     static void on_invalid_type(typeindex::type_index const&)
0063     {
0064     }
0065 
0066     /*!
0067      * The method is called when value extraction failed because the attribute value was not found.
0068      */
0069     static void on_missing_value()
0070     {
0071     }
0072 };
0073 
0074 /*!
0075  * The \c fallback_to_throw policy results in throwing an exception if the attribute value cannot be extracted.
0076  */
0077 struct fallback_to_throw
0078 {
0079     enum { guaranteed_result = true };
0080 
0081     /*!
0082      * The method is called in order to apply a function object to the default value.
0083      */
0084     template< typename FunT >
0085     static bool apply_default(FunT&)
0086     {
0087         return false;
0088     }
0089 
0090     /*!
0091      * The method is called in order to apply a function object to the default value.
0092      */
0093     template< typename FunT >
0094     static bool apply_default(FunT const&)
0095     {
0096         return false;
0097     }
0098 
0099     /*!
0100      * The method is called when value extraction failed because the attribute value has different type than requested.
0101      */
0102     static void on_invalid_type(typeindex::type_index const& t)
0103     {
0104         BOOST_LOG_THROW_DESCR_PARAMS(invalid_type, "Attribute value has incompatible type", (t));
0105     }
0106 
0107     /*!
0108      * The method is called when value extraction failed because the attribute value was not found.
0109      */
0110     static void on_missing_value()
0111     {
0112         BOOST_LOG_THROW_DESCR(missing_value, "Attribute value not found");
0113     }
0114 };
0115 
0116 /*!
0117  * The \c fallback_to_default policy results in a default value if the attribute value cannot be extracted.
0118  */
0119 template< typename DefaultT >
0120 struct fallback_to_default
0121 {
0122     enum { guaranteed_result = true };
0123 
0124     //! Default value type
0125     typedef typename remove_cv< typename remove_reference< DefaultT >::type >::type default_type;
0126 
0127     /*!
0128      * Default constructor.
0129      */
0130     fallback_to_default() : m_default()
0131     {
0132     }
0133 
0134     /*!
0135      * Initializing constructor.
0136      */
0137     explicit fallback_to_default(default_type const& def_val) : m_default(def_val)
0138     {
0139     }
0140 
0141     /*!
0142      * The method is called in order to apply a function object to the default value.
0143      */
0144     template< typename FunT >
0145     bool apply_default(FunT& fun) const
0146     {
0147         fun(m_default);
0148         return true;
0149     }
0150 
0151     /*!
0152      * The method is called in order to apply a function object to the default value.
0153      */
0154     template< typename FunT >
0155     bool apply_default(FunT const& fun) const
0156     {
0157         fun(m_default);
0158         return true;
0159     }
0160 
0161     /*!
0162      * The method is called when value extraction failed because the attribute value has different type than requested.
0163      */
0164     static void on_invalid_type(typeindex::type_index const&)
0165     {
0166     }
0167 
0168     /*!
0169      * The method is called when value extraction failed because the attribute value was not found.
0170      */
0171     static void on_missing_value()
0172     {
0173     }
0174 
0175 private:
0176     //! Default value
0177     DefaultT m_default;
0178 };
0179 
0180 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0181 
0182 } // namespace boost
0183 
0184 #include <boost/log/detail/footer.hpp>
0185 
0186 #endif // BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_