Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file fallback_policy.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 18.08.2012 Chris@16: * Chris@16: * The header contains definition of fallback policies when attribute value visitation or extraction fails. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: /*! Chris@16: * The \c fallback_to_none policy results in returning an empty value reference if the attribute value cannot be extracted. Chris@16: */ Chris@16: struct fallback_to_none Chris@16: { Chris@16: enum { guaranteed_result = false }; Chris@16: Chris@16: /*! Chris@16: * The method is called in order to apply a function object to the default value. Chris@16: */ Chris@16: template< typename FunT > Chris@16: static bool apply_default(FunT&) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The method is called in order to apply a function object to the default value. Chris@16: */ Chris@16: template< typename FunT > Chris@16: static bool apply_default(FunT const&) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The method is called when value extraction failed because the attribute value has different type than requested. Chris@16: */ Chris@16: static void on_invalid_type(type_info_wrapper const&) Chris@16: { Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The method is called when value extraction failed because the attribute value was not found. Chris@16: */ Chris@16: static void on_missing_value() Chris@16: { Chris@16: } Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * The \c fallback_to_throw policy results in throwing an exception if the attribute value cannot be extracted. Chris@16: */ Chris@16: struct fallback_to_throw Chris@16: { Chris@16: enum { guaranteed_result = true }; Chris@16: Chris@16: /*! Chris@16: * The method is called in order to apply a function object to the default value. Chris@16: */ Chris@16: template< typename FunT > Chris@16: static bool apply_default(FunT&) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The method is called in order to apply a function object to the default value. Chris@16: */ Chris@16: template< typename FunT > Chris@16: static bool apply_default(FunT const&) Chris@16: { Chris@16: return false; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The method is called when value extraction failed because the attribute value has different type than requested. Chris@16: */ Chris@16: static void on_invalid_type(type_info_wrapper const& t) Chris@16: { Chris@16: BOOST_LOG_THROW_DESCR_PARAMS(invalid_type, "Attribute value has incompatible type", (t)); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The method is called when value extraction failed because the attribute value was not found. Chris@16: */ Chris@16: static void on_missing_value() Chris@16: { Chris@16: BOOST_LOG_THROW_DESCR(missing_value, "Attribute value not found"); Chris@16: } Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * The \c fallback_to_default policy results in a default value if the attribute value cannot be extracted. Chris@16: */ Chris@16: template< typename DefaultT > Chris@16: struct fallback_to_default Chris@16: { Chris@16: enum { guaranteed_result = true }; Chris@16: Chris@16: //! Default value type Chris@16: typedef typename remove_cv< typename remove_reference< DefaultT >::type >::type default_type; Chris@16: Chris@16: /*! Chris@16: * Default constructor. Chris@16: */ Chris@16: fallback_to_default() : m_default() Chris@16: { Chris@16: } Chris@16: Chris@16: /*! Chris@16: * Initializing constructor. Chris@16: */ Chris@16: explicit fallback_to_default(default_type const& def_val) : m_default(def_val) Chris@16: { Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The method is called in order to apply a function object to the default value. Chris@16: */ Chris@16: template< typename FunT > Chris@16: bool apply_default(FunT& fun) const Chris@16: { Chris@16: fun(m_default); Chris@16: return true; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The method is called in order to apply a function object to the default value. Chris@16: */ Chris@16: template< typename FunT > Chris@16: bool apply_default(FunT const& fun) const Chris@16: { Chris@16: fun(m_default); Chris@16: return true; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The method is called when value extraction failed because the attribute value has different type than requested. Chris@16: */ Chris@16: static void on_invalid_type(type_info_wrapper const&) Chris@16: { Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The method is called when value extraction failed because the attribute value was not found. Chris@16: */ Chris@16: static void on_missing_value() Chris@16: { Chris@16: } Chris@16: Chris@16: private: Chris@16: //! Default value Chris@16: DefaultT m_default; Chris@16: }; Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_