2 * Copyright Andrey Semashev 2007 - 2015.
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)
8 * \file fallback_policy.hpp
9 * \author Andrey Semashev
12 * The header contains definition of fallback policies when attribute value visitation or extraction fails.
15 #ifndef BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
16 #define BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
18 #include <boost/type_index.hpp>
19 #include <boost/type_traits/remove_cv.hpp>
20 #include <boost/type_traits/remove_reference.hpp>
21 #include <boost/log/detail/config.hpp>
22 #include <boost/log/exceptions.hpp>
23 #include <boost/log/attributes/fallback_policy_fwd.hpp>
24 #include <boost/log/detail/header.hpp>
26 #ifdef BOOST_HAS_PRAGMA_ONCE
32 BOOST_LOG_OPEN_NAMESPACE
35 * The \c fallback_to_none policy results in returning an empty value reference if the attribute value cannot be extracted.
37 struct fallback_to_none
39 enum { guaranteed_result = false };
42 * The method is called in order to apply a function object to the default value.
44 template< typename FunT >
45 static bool apply_default(FunT&)
51 * The method is called in order to apply a function object to the default value.
53 template< typename FunT >
54 static bool apply_default(FunT const&)
60 * The method is called when value extraction failed because the attribute value has different type than requested.
62 static void on_invalid_type(typeindex::type_index const&)
67 * The method is called when value extraction failed because the attribute value was not found.
69 static void on_missing_value()
75 * The \c fallback_to_throw policy results in throwing an exception if the attribute value cannot be extracted.
77 struct fallback_to_throw
79 enum { guaranteed_result = true };
82 * The method is called in order to apply a function object to the default value.
84 template< typename FunT >
85 static bool apply_default(FunT&)
91 * The method is called in order to apply a function object to the default value.
93 template< typename FunT >
94 static bool apply_default(FunT const&)
100 * The method is called when value extraction failed because the attribute value has different type than requested.
102 static void on_invalid_type(typeindex::type_index const& t)
104 BOOST_LOG_THROW_DESCR_PARAMS(invalid_type, "Attribute value has incompatible type", (t));
108 * The method is called when value extraction failed because the attribute value was not found.
110 static void on_missing_value()
112 BOOST_LOG_THROW_DESCR(missing_value, "Attribute value not found");
117 * The \c fallback_to_default policy results in a default value if the attribute value cannot be extracted.
119 template< typename DefaultT >
120 struct fallback_to_default
122 enum { guaranteed_result = true };
124 //! Default value type
125 typedef typename remove_cv< typename remove_reference< DefaultT >::type >::type default_type;
128 * Default constructor.
130 fallback_to_default() : m_default()
135 * Initializing constructor.
137 explicit fallback_to_default(default_type const& def_val) : m_default(def_val)
142 * The method is called in order to apply a function object to the default value.
144 template< typename FunT >
145 bool apply_default(FunT& fun) const
152 * The method is called in order to apply a function object to the default value.
154 template< typename FunT >
155 bool apply_default(FunT const& fun) const
162 * The method is called when value extraction failed because the attribute value has different type than requested.
164 static void on_invalid_type(typeindex::type_index const&)
169 * The method is called when value extraction failed because the attribute value was not found.
171 static void on_missing_value()
180 BOOST_LOG_CLOSE_NAMESPACE // namespace log
184 #include <boost/log/detail/footer.hpp>
186 #endif // BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_