• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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)
6  */
7 /*!
8  * \file   fallback_policy.hpp
9  * \author Andrey Semashev
10  * \date   18.08.2012
11  *
12  * The header contains definition of fallback policies when attribute value visitation or extraction fails.
13  */
14 
15 #ifndef BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
16 #define BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
17 
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>
25 
26 #ifdef BOOST_HAS_PRAGMA_ONCE
27 #pragma once
28 #endif
29 
30 namespace boost {
31 
32 BOOST_LOG_OPEN_NAMESPACE
33 
34 /*!
35  * The \c fallback_to_none policy results in returning an empty value reference if the attribute value cannot be extracted.
36  */
37 struct fallback_to_none
38 {
39     enum { guaranteed_result = false };
40 
41     /*!
42      * The method is called in order to apply a function object to the default value.
43      */
44     template< typename FunT >
apply_defaultboost::fallback_to_none45     static bool apply_default(FunT&)
46     {
47         return false;
48     }
49 
50     /*!
51      * The method is called in order to apply a function object to the default value.
52      */
53     template< typename FunT >
apply_defaultboost::fallback_to_none54     static bool apply_default(FunT const&)
55     {
56         return false;
57     }
58 
59     /*!
60      * The method is called when value extraction failed because the attribute value has different type than requested.
61      */
on_invalid_typeboost::fallback_to_none62     static void on_invalid_type(typeindex::type_index const&)
63     {
64     }
65 
66     /*!
67      * The method is called when value extraction failed because the attribute value was not found.
68      */
on_missing_valueboost::fallback_to_none69     static void on_missing_value()
70     {
71     }
72 };
73 
74 /*!
75  * The \c fallback_to_throw policy results in throwing an exception if the attribute value cannot be extracted.
76  */
77 struct fallback_to_throw
78 {
79     enum { guaranteed_result = true };
80 
81     /*!
82      * The method is called in order to apply a function object to the default value.
83      */
84     template< typename FunT >
apply_defaultboost::fallback_to_throw85     static bool apply_default(FunT&)
86     {
87         return false;
88     }
89 
90     /*!
91      * The method is called in order to apply a function object to the default value.
92      */
93     template< typename FunT >
apply_defaultboost::fallback_to_throw94     static bool apply_default(FunT const&)
95     {
96         return false;
97     }
98 
99     /*!
100      * The method is called when value extraction failed because the attribute value has different type than requested.
101      */
on_invalid_typeboost::fallback_to_throw102     static void on_invalid_type(typeindex::type_index const& t)
103     {
104         BOOST_LOG_THROW_DESCR_PARAMS(invalid_type, "Attribute value has incompatible type", (t));
105     }
106 
107     /*!
108      * The method is called when value extraction failed because the attribute value was not found.
109      */
on_missing_valueboost::fallback_to_throw110     static void on_missing_value()
111     {
112         BOOST_LOG_THROW_DESCR(missing_value, "Attribute value not found");
113     }
114 };
115 
116 /*!
117  * The \c fallback_to_default policy results in a default value if the attribute value cannot be extracted.
118  */
119 template< typename DefaultT >
120 struct fallback_to_default
121 {
122     enum { guaranteed_result = true };
123 
124     //! Default value type
125     typedef typename remove_cv< typename remove_reference< DefaultT >::type >::type default_type;
126 
127     /*!
128      * Default constructor.
129      */
fallback_to_defaultboost::fallback_to_default130     fallback_to_default() : m_default()
131     {
132     }
133 
134     /*!
135      * Initializing constructor.
136      */
fallback_to_defaultboost::fallback_to_default137     explicit fallback_to_default(default_type const& def_val) : m_default(def_val)
138     {
139     }
140 
141     /*!
142      * The method is called in order to apply a function object to the default value.
143      */
144     template< typename FunT >
apply_defaultboost::fallback_to_default145     bool apply_default(FunT& fun) const
146     {
147         fun(m_default);
148         return true;
149     }
150 
151     /*!
152      * The method is called in order to apply a function object to the default value.
153      */
154     template< typename FunT >
apply_defaultboost::fallback_to_default155     bool apply_default(FunT const& fun) const
156     {
157         fun(m_default);
158         return true;
159     }
160 
161     /*!
162      * The method is called when value extraction failed because the attribute value has different type than requested.
163      */
on_invalid_typeboost::fallback_to_default164     static void on_invalid_type(typeindex::type_index const&)
165     {
166     }
167 
168     /*!
169      * The method is called when value extraction failed because the attribute value was not found.
170      */
on_missing_valueboost::fallback_to_default171     static void on_missing_value()
172     {
173     }
174 
175 private:
176     //! Default value
177     DefaultT m_default;
178 };
179 
180 BOOST_LOG_CLOSE_NAMESPACE // namespace log
181 
182 } // namespace boost
183 
184 #include <boost/log/detail/footer.hpp>
185 
186 #endif // BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
187