Chris@16
|
1 /*
|
Chris@101
|
2 * Copyright Andrey Semashev 2007 - 2015.
|
Chris@16
|
3 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7 /*!
|
Chris@16
|
8 * \file fallback_policy.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 18.08.2012
|
Chris@16
|
11 *
|
Chris@16
|
12 * The header contains definition of fallback policies when attribute value visitation or extraction fails.
|
Chris@16
|
13 */
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
|
Chris@16
|
16 #define BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/type_traits/remove_cv.hpp>
|
Chris@16
|
19 #include <boost/type_traits/remove_reference.hpp>
|
Chris@16
|
20 #include <boost/log/detail/config.hpp>
|
Chris@16
|
21 #include <boost/log/exceptions.hpp>
|
Chris@16
|
22 #include <boost/log/utility/type_info_wrapper.hpp>
|
Chris@16
|
23 #include <boost/log/attributes/fallback_policy_fwd.hpp>
|
Chris@16
|
24 #include <boost/log/detail/header.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
27 #pragma once
|
Chris@16
|
28 #endif
|
Chris@16
|
29
|
Chris@16
|
30 namespace boost {
|
Chris@16
|
31
|
Chris@16
|
32 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
33
|
Chris@16
|
34 /*!
|
Chris@16
|
35 * The \c fallback_to_none policy results in returning an empty value reference if the attribute value cannot be extracted.
|
Chris@16
|
36 */
|
Chris@16
|
37 struct fallback_to_none
|
Chris@16
|
38 {
|
Chris@16
|
39 enum { guaranteed_result = false };
|
Chris@16
|
40
|
Chris@16
|
41 /*!
|
Chris@16
|
42 * The method is called in order to apply a function object to the default value.
|
Chris@16
|
43 */
|
Chris@16
|
44 template< typename FunT >
|
Chris@16
|
45 static bool apply_default(FunT&)
|
Chris@16
|
46 {
|
Chris@16
|
47 return false;
|
Chris@16
|
48 }
|
Chris@16
|
49
|
Chris@16
|
50 /*!
|
Chris@16
|
51 * The method is called in order to apply a function object to the default value.
|
Chris@16
|
52 */
|
Chris@16
|
53 template< typename FunT >
|
Chris@16
|
54 static bool apply_default(FunT const&)
|
Chris@16
|
55 {
|
Chris@16
|
56 return false;
|
Chris@16
|
57 }
|
Chris@16
|
58
|
Chris@16
|
59 /*!
|
Chris@16
|
60 * The method is called when value extraction failed because the attribute value has different type than requested.
|
Chris@16
|
61 */
|
Chris@16
|
62 static void on_invalid_type(type_info_wrapper const&)
|
Chris@16
|
63 {
|
Chris@16
|
64 }
|
Chris@16
|
65
|
Chris@16
|
66 /*!
|
Chris@16
|
67 * The method is called when value extraction failed because the attribute value was not found.
|
Chris@16
|
68 */
|
Chris@16
|
69 static void on_missing_value()
|
Chris@16
|
70 {
|
Chris@16
|
71 }
|
Chris@16
|
72 };
|
Chris@16
|
73
|
Chris@16
|
74 /*!
|
Chris@16
|
75 * The \c fallback_to_throw policy results in throwing an exception if the attribute value cannot be extracted.
|
Chris@16
|
76 */
|
Chris@16
|
77 struct fallback_to_throw
|
Chris@16
|
78 {
|
Chris@16
|
79 enum { guaranteed_result = true };
|
Chris@16
|
80
|
Chris@16
|
81 /*!
|
Chris@16
|
82 * The method is called in order to apply a function object to the default value.
|
Chris@16
|
83 */
|
Chris@16
|
84 template< typename FunT >
|
Chris@16
|
85 static bool apply_default(FunT&)
|
Chris@16
|
86 {
|
Chris@16
|
87 return false;
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 /*!
|
Chris@16
|
91 * The method is called in order to apply a function object to the default value.
|
Chris@16
|
92 */
|
Chris@16
|
93 template< typename FunT >
|
Chris@16
|
94 static bool apply_default(FunT const&)
|
Chris@16
|
95 {
|
Chris@16
|
96 return false;
|
Chris@16
|
97 }
|
Chris@16
|
98
|
Chris@16
|
99 /*!
|
Chris@16
|
100 * The method is called when value extraction failed because the attribute value has different type than requested.
|
Chris@16
|
101 */
|
Chris@16
|
102 static void on_invalid_type(type_info_wrapper const& t)
|
Chris@16
|
103 {
|
Chris@16
|
104 BOOST_LOG_THROW_DESCR_PARAMS(invalid_type, "Attribute value has incompatible type", (t));
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 /*!
|
Chris@16
|
108 * The method is called when value extraction failed because the attribute value was not found.
|
Chris@16
|
109 */
|
Chris@16
|
110 static void on_missing_value()
|
Chris@16
|
111 {
|
Chris@16
|
112 BOOST_LOG_THROW_DESCR(missing_value, "Attribute value not found");
|
Chris@16
|
113 }
|
Chris@16
|
114 };
|
Chris@16
|
115
|
Chris@16
|
116 /*!
|
Chris@16
|
117 * The \c fallback_to_default policy results in a default value if the attribute value cannot be extracted.
|
Chris@16
|
118 */
|
Chris@16
|
119 template< typename DefaultT >
|
Chris@16
|
120 struct fallback_to_default
|
Chris@16
|
121 {
|
Chris@16
|
122 enum { guaranteed_result = true };
|
Chris@16
|
123
|
Chris@16
|
124 //! Default value type
|
Chris@16
|
125 typedef typename remove_cv< typename remove_reference< DefaultT >::type >::type default_type;
|
Chris@16
|
126
|
Chris@16
|
127 /*!
|
Chris@16
|
128 * Default constructor.
|
Chris@16
|
129 */
|
Chris@16
|
130 fallback_to_default() : m_default()
|
Chris@16
|
131 {
|
Chris@16
|
132 }
|
Chris@16
|
133
|
Chris@16
|
134 /*!
|
Chris@16
|
135 * Initializing constructor.
|
Chris@16
|
136 */
|
Chris@16
|
137 explicit fallback_to_default(default_type const& def_val) : m_default(def_val)
|
Chris@16
|
138 {
|
Chris@16
|
139 }
|
Chris@16
|
140
|
Chris@16
|
141 /*!
|
Chris@16
|
142 * The method is called in order to apply a function object to the default value.
|
Chris@16
|
143 */
|
Chris@16
|
144 template< typename FunT >
|
Chris@16
|
145 bool apply_default(FunT& fun) const
|
Chris@16
|
146 {
|
Chris@16
|
147 fun(m_default);
|
Chris@16
|
148 return true;
|
Chris@16
|
149 }
|
Chris@16
|
150
|
Chris@16
|
151 /*!
|
Chris@16
|
152 * The method is called in order to apply a function object to the default value.
|
Chris@16
|
153 */
|
Chris@16
|
154 template< typename FunT >
|
Chris@16
|
155 bool apply_default(FunT const& fun) const
|
Chris@16
|
156 {
|
Chris@16
|
157 fun(m_default);
|
Chris@16
|
158 return true;
|
Chris@16
|
159 }
|
Chris@16
|
160
|
Chris@16
|
161 /*!
|
Chris@16
|
162 * The method is called when value extraction failed because the attribute value has different type than requested.
|
Chris@16
|
163 */
|
Chris@16
|
164 static void on_invalid_type(type_info_wrapper const&)
|
Chris@16
|
165 {
|
Chris@16
|
166 }
|
Chris@16
|
167
|
Chris@16
|
168 /*!
|
Chris@16
|
169 * The method is called when value extraction failed because the attribute value was not found.
|
Chris@16
|
170 */
|
Chris@16
|
171 static void on_missing_value()
|
Chris@16
|
172 {
|
Chris@16
|
173 }
|
Chris@16
|
174
|
Chris@16
|
175 private:
|
Chris@16
|
176 //! Default value
|
Chris@16
|
177 DefaultT m_default;
|
Chris@16
|
178 };
|
Chris@16
|
179
|
Chris@16
|
180 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
181
|
Chris@16
|
182 } // namespace boost
|
Chris@16
|
183
|
Chris@16
|
184 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
185
|
Chris@16
|
186 #endif // BOOST_LOG_ATTRIBUTES_FALLBACK_POLICY_HPP_INCLUDED_
|