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 value_visitation.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 01.03.2008
|
Chris@16
|
11 *
|
Chris@16
|
12 * The header contains implementation of convenience tools to apply visitors to an attribute value
|
Chris@16
|
13 * in the view.
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_LOG_ATTRIBUTES_VALUE_VISITATION_HPP_INCLUDED_
|
Chris@16
|
17 #define BOOST_LOG_ATTRIBUTES_VALUE_VISITATION_HPP_INCLUDED_
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/log/detail/config.hpp>
|
Chris@16
|
20 #include <boost/log/exceptions.hpp>
|
Chris@16
|
21 #include <boost/log/core/record.hpp>
|
Chris@16
|
22 #include <boost/log/attributes/attribute_name.hpp>
|
Chris@16
|
23 #include <boost/log/attributes/attribute_value.hpp>
|
Chris@16
|
24 #include <boost/log/attributes/attribute.hpp>
|
Chris@16
|
25 #include <boost/log/attributes/attribute_value_set.hpp>
|
Chris@16
|
26 #include <boost/log/attributes/value_visitation_fwd.hpp>
|
Chris@16
|
27 #include <boost/log/attributes/fallback_policy.hpp>
|
Chris@16
|
28 #include <boost/log/expressions/keyword_fwd.hpp>
|
Chris@16
|
29 #include <boost/utility/explicit_operator_bool.hpp>
|
Chris@16
|
30 #include <boost/log/utility/type_dispatch/static_type_dispatcher.hpp>
|
Chris@16
|
31 #include <boost/log/detail/header.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
34 #pragma once
|
Chris@16
|
35 #endif
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost {
|
Chris@16
|
38
|
Chris@16
|
39 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
40
|
Chris@16
|
41 /*!
|
Chris@16
|
42 * \brief The class represents attribute value visitation result
|
Chris@16
|
43 *
|
Chris@16
|
44 * The main purpose of this class is to provide a convenient interface for checking
|
Chris@16
|
45 * whether the attribute value visitation succeeded or not. It also allows to discover
|
Chris@16
|
46 * the actual cause of failure, should the operation fail.
|
Chris@16
|
47 */
|
Chris@16
|
48 class visitation_result
|
Chris@16
|
49 {
|
Chris@16
|
50 public:
|
Chris@16
|
51 //! Error codes for attribute value visitation
|
Chris@16
|
52 enum error_code
|
Chris@16
|
53 {
|
Chris@16
|
54 ok, //!< The attribute value has been visited successfully
|
Chris@16
|
55 value_not_found, //!< The attribute value is not present in the view
|
Chris@16
|
56 value_has_invalid_type //!< The attribute value is present in the view, but has an unexpected type
|
Chris@16
|
57 };
|
Chris@16
|
58
|
Chris@16
|
59 private:
|
Chris@16
|
60 error_code m_code;
|
Chris@16
|
61
|
Chris@16
|
62 public:
|
Chris@16
|
63 /*!
|
Chris@16
|
64 * Initializing constructor. Creates the result that is equivalent to the
|
Chris@16
|
65 * specified error code.
|
Chris@16
|
66 */
|
Chris@16
|
67 BOOST_CONSTEXPR visitation_result(error_code code = ok) BOOST_NOEXCEPT : m_code(code) {}
|
Chris@16
|
68
|
Chris@16
|
69 /*!
|
Chris@16
|
70 * Checks if the visitation was successful.
|
Chris@16
|
71 *
|
Chris@16
|
72 * \return \c true if the value was visited successfully, \c false otherwise.
|
Chris@16
|
73 */
|
Chris@101
|
74 BOOST_EXPLICIT_OPERATOR_BOOL_NOEXCEPT()
|
Chris@16
|
75 /*!
|
Chris@16
|
76 * Checks if the visitation was unsuccessful.
|
Chris@16
|
77 *
|
Chris@16
|
78 * \return \c false if the value was visited successfully, \c true otherwise.
|
Chris@16
|
79 */
|
Chris@16
|
80 bool operator! () const BOOST_NOEXCEPT { return (m_code != ok); }
|
Chris@16
|
81
|
Chris@16
|
82 /*!
|
Chris@16
|
83 * \return The actual result code of value visitation
|
Chris@16
|
84 */
|
Chris@16
|
85 error_code code() const BOOST_NOEXCEPT { return m_code; }
|
Chris@16
|
86 };
|
Chris@16
|
87
|
Chris@16
|
88 /*!
|
Chris@16
|
89 * \brief Generic attribute value visitor invoker
|
Chris@16
|
90 *
|
Chris@16
|
91 * Attribute value invoker is a functional object that attempts to find and extract the stored
|
Chris@16
|
92 * attribute value from the attribute value view or a log record. The extracted value is passed to
|
Chris@16
|
93 * a unary function object (the visitor) provided by user.
|
Chris@16
|
94 *
|
Chris@16
|
95 * The invoker can be specialized on one or several attribute value types that should be
|
Chris@16
|
96 * specified in the second template argument.
|
Chris@16
|
97 */
|
Chris@16
|
98 template< typename T, typename FallbackPolicyT >
|
Chris@16
|
99 class value_visitor_invoker :
|
Chris@16
|
100 private FallbackPolicyT
|
Chris@16
|
101 {
|
Chris@16
|
102 typedef value_visitor_invoker< T, FallbackPolicyT > this_type;
|
Chris@16
|
103
|
Chris@16
|
104 public:
|
Chris@16
|
105 //! Attribute value types
|
Chris@16
|
106 typedef T value_type;
|
Chris@16
|
107
|
Chris@16
|
108 //! Fallback policy
|
Chris@16
|
109 typedef FallbackPolicyT fallback_policy;
|
Chris@16
|
110
|
Chris@16
|
111 //! Function object result type
|
Chris@16
|
112 typedef visitation_result result_type;
|
Chris@16
|
113
|
Chris@16
|
114 public:
|
Chris@16
|
115 /*!
|
Chris@16
|
116 * Default constructor
|
Chris@16
|
117 */
|
Chris@16
|
118 BOOST_DEFAULTED_FUNCTION(value_visitor_invoker(), {})
|
Chris@16
|
119
|
Chris@16
|
120 /*!
|
Chris@16
|
121 * Copy constructor
|
Chris@16
|
122 */
|
Chris@16
|
123 value_visitor_invoker(value_visitor_invoker const& that) : fallback_policy(static_cast< fallback_policy const& >(that))
|
Chris@16
|
124 {
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 /*!
|
Chris@16
|
128 * Initializing constructor
|
Chris@16
|
129 *
|
Chris@16
|
130 * \param arg Fallback policy argument
|
Chris@16
|
131 */
|
Chris@16
|
132 template< typename U >
|
Chris@16
|
133 explicit value_visitor_invoker(U const& arg) : fallback_policy(arg) {}
|
Chris@16
|
134
|
Chris@16
|
135 /*!
|
Chris@16
|
136 * Visitation operator. Attempts to acquire the stored value of one of the supported types. If acquisition succeeds,
|
Chris@16
|
137 * the value is passed to \a visitor.
|
Chris@16
|
138 *
|
Chris@16
|
139 * \param attr An attribute value to apply the visitor to.
|
Chris@16
|
140 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
141 * \return The result of visitation.
|
Chris@16
|
142 */
|
Chris@16
|
143 template< typename VisitorT >
|
Chris@16
|
144 result_type operator() (attribute_value const& attr, VisitorT visitor) const
|
Chris@16
|
145 {
|
Chris@16
|
146 if (!!attr)
|
Chris@16
|
147 {
|
Chris@16
|
148 static_type_dispatcher< value_type > disp(visitor);
|
Chris@16
|
149 if (attr.dispatch(disp) || fallback_policy::apply_default(visitor))
|
Chris@16
|
150 {
|
Chris@16
|
151 return visitation_result::ok;
|
Chris@16
|
152 }
|
Chris@16
|
153 else
|
Chris@16
|
154 {
|
Chris@16
|
155 fallback_policy::on_invalid_type(attr.get_type());
|
Chris@16
|
156 return visitation_result::value_has_invalid_type;
|
Chris@16
|
157 }
|
Chris@16
|
158 }
|
Chris@16
|
159
|
Chris@16
|
160 if (fallback_policy::apply_default(visitor))
|
Chris@16
|
161 return visitation_result::ok;
|
Chris@16
|
162
|
Chris@16
|
163 fallback_policy::on_missing_value();
|
Chris@16
|
164 return visitation_result::value_not_found;
|
Chris@16
|
165 }
|
Chris@16
|
166
|
Chris@16
|
167 /*!
|
Chris@16
|
168 * Visitation operator. Looks for an attribute value with the specified name
|
Chris@16
|
169 * and tries to acquire the stored value of one of the supported types. If acquisition succeeds,
|
Chris@16
|
170 * the value is passed to \a visitor.
|
Chris@16
|
171 *
|
Chris@16
|
172 * \param name Attribute value name.
|
Chris@16
|
173 * \param attrs A set of attribute values in which to look for the specified attribute value.
|
Chris@16
|
174 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
175 * \return The result of visitation.
|
Chris@16
|
176 */
|
Chris@16
|
177 template< typename VisitorT >
|
Chris@16
|
178 result_type operator() (attribute_name const& name, attribute_value_set const& attrs, VisitorT visitor) const
|
Chris@16
|
179 {
|
Chris@16
|
180 try
|
Chris@16
|
181 {
|
Chris@16
|
182 attribute_value_set::const_iterator it = attrs.find(name);
|
Chris@16
|
183 if (it != attrs.end())
|
Chris@16
|
184 return operator() (it->second, visitor);
|
Chris@16
|
185 else
|
Chris@16
|
186 return operator() (attribute_value(), visitor);
|
Chris@16
|
187 }
|
Chris@16
|
188 catch (exception& e)
|
Chris@16
|
189 {
|
Chris@16
|
190 // Attach the attribute name to the exception
|
Chris@16
|
191 boost::log::aux::attach_attribute_name_info(e, name);
|
Chris@16
|
192 throw;
|
Chris@16
|
193 }
|
Chris@16
|
194 }
|
Chris@16
|
195
|
Chris@16
|
196 /*!
|
Chris@16
|
197 * Visitation operator. Looks for an attribute value with the specified name
|
Chris@16
|
198 * and tries to acquire the stored value of one of the supported types. If acquisition succeeds,
|
Chris@16
|
199 * the value is passed to \a visitor.
|
Chris@16
|
200 *
|
Chris@16
|
201 * \param name Attribute value name.
|
Chris@16
|
202 * \param rec A log record. The attribute value will be sought among those associated with the record.
|
Chris@16
|
203 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
204 * \return The result of visitation.
|
Chris@16
|
205 */
|
Chris@16
|
206 template< typename VisitorT >
|
Chris@16
|
207 result_type operator() (attribute_name const& name, record const& rec, VisitorT visitor) const
|
Chris@16
|
208 {
|
Chris@16
|
209 return operator() (name, rec.attribute_values(), visitor);
|
Chris@16
|
210 }
|
Chris@16
|
211
|
Chris@16
|
212 /*!
|
Chris@16
|
213 * Visitation operator. Looks for an attribute value with the specified name
|
Chris@16
|
214 * and tries to acquire the stored value of one of the supported types. If acquisition succeeds,
|
Chris@16
|
215 * the value is passed to \a visitor.
|
Chris@16
|
216 *
|
Chris@16
|
217 * \param name Attribute value name.
|
Chris@16
|
218 * \param rec A log record view. The attribute value will be sought among those associated with the record.
|
Chris@16
|
219 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
220 * \return The result of visitation.
|
Chris@16
|
221 */
|
Chris@16
|
222 template< typename VisitorT >
|
Chris@16
|
223 result_type operator() (attribute_name const& name, record_view const& rec, VisitorT visitor) const
|
Chris@16
|
224 {
|
Chris@16
|
225 return operator() (name, rec.attribute_values(), visitor);
|
Chris@16
|
226 }
|
Chris@16
|
227
|
Chris@16
|
228 /*!
|
Chris@16
|
229 * \returns Fallback policy
|
Chris@16
|
230 */
|
Chris@16
|
231 fallback_policy const& get_fallback_policy() const
|
Chris@16
|
232 {
|
Chris@16
|
233 return *static_cast< fallback_policy const* >(this);
|
Chris@16
|
234 }
|
Chris@16
|
235 };
|
Chris@16
|
236
|
Chris@16
|
237 /*!
|
Chris@16
|
238 * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the
|
Chris@16
|
239 * type or set of possible types of the attribute value to be visited.
|
Chris@16
|
240 *
|
Chris@16
|
241 * \param name The name of the attribute value to visit.
|
Chris@16
|
242 * \param attrs A set of attribute values in which to look for the specified attribute value.
|
Chris@16
|
243 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
244 * \return The result of visitation.
|
Chris@16
|
245 */
|
Chris@16
|
246 template< typename T, typename VisitorT >
|
Chris@16
|
247 inline visitation_result
|
Chris@16
|
248 visit(attribute_name const& name, attribute_value_set const& attrs, VisitorT visitor)
|
Chris@16
|
249 {
|
Chris@16
|
250 value_visitor_invoker< T > invoker;
|
Chris@16
|
251 return invoker(name, attrs, visitor);
|
Chris@16
|
252 }
|
Chris@16
|
253
|
Chris@16
|
254 /*!
|
Chris@16
|
255 * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the
|
Chris@16
|
256 * type or set of possible types of the attribute value to be visited.
|
Chris@16
|
257 *
|
Chris@16
|
258 * \param name The name of the attribute value to visit.
|
Chris@16
|
259 * \param rec A log record. The attribute value will be sought among those associated with the record.
|
Chris@16
|
260 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
261 * \return The result of visitation.
|
Chris@16
|
262 */
|
Chris@16
|
263 template< typename T, typename VisitorT >
|
Chris@16
|
264 inline visitation_result
|
Chris@16
|
265 visit(attribute_name const& name, record const& rec, VisitorT visitor)
|
Chris@16
|
266 {
|
Chris@16
|
267 value_visitor_invoker< T > invoker;
|
Chris@16
|
268 return invoker(name, rec, visitor);
|
Chris@16
|
269 }
|
Chris@16
|
270
|
Chris@16
|
271 /*!
|
Chris@16
|
272 * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the
|
Chris@16
|
273 * type or set of possible types of the attribute value to be visited.
|
Chris@16
|
274 *
|
Chris@16
|
275 * \param name The name of the attribute value to visit.
|
Chris@16
|
276 * \param rec A log record view. The attribute value will be sought among those associated with the record.
|
Chris@16
|
277 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
278 * \return The result of visitation.
|
Chris@16
|
279 */
|
Chris@16
|
280 template< typename T, typename VisitorT >
|
Chris@16
|
281 inline visitation_result
|
Chris@16
|
282 visit(attribute_name const& name, record_view const& rec, VisitorT visitor)
|
Chris@16
|
283 {
|
Chris@16
|
284 value_visitor_invoker< T > invoker;
|
Chris@16
|
285 return invoker(name, rec, visitor);
|
Chris@16
|
286 }
|
Chris@16
|
287
|
Chris@16
|
288 /*!
|
Chris@16
|
289 * The function applies a visitor to an attribute value. The user has to explicitly specify the
|
Chris@16
|
290 * type or set of possible types of the attribute value to be visited.
|
Chris@16
|
291 *
|
Chris@16
|
292 * \param value The attribute value to visit.
|
Chris@16
|
293 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
294 * \return The result of visitation.
|
Chris@16
|
295 */
|
Chris@16
|
296 template< typename T, typename VisitorT >
|
Chris@16
|
297 inline visitation_result
|
Chris@16
|
298 visit(attribute_value const& value, VisitorT visitor)
|
Chris@16
|
299 {
|
Chris@16
|
300 value_visitor_invoker< T > invoker;
|
Chris@16
|
301 return invoker(value, visitor);
|
Chris@16
|
302 }
|
Chris@16
|
303
|
Chris@16
|
304 /*!
|
Chris@16
|
305 * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the
|
Chris@16
|
306 * type or set of possible types of the attribute value to be visited.
|
Chris@16
|
307 *
|
Chris@16
|
308 * \param keyword The keyword of the attribute value to visit.
|
Chris@16
|
309 * \param attrs A set of attribute values in which to look for the specified attribute value.
|
Chris@16
|
310 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
311 * \return The result of visitation.
|
Chris@16
|
312 */
|
Chris@16
|
313 template< typename DescriptorT, template< typename > class ActorT, typename VisitorT >
|
Chris@16
|
314 inline visitation_result
|
Chris@16
|
315 visit(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, attribute_value_set const& attrs, VisitorT visitor)
|
Chris@16
|
316 {
|
Chris@16
|
317 value_visitor_invoker< typename DescriptorT::value_type > invoker;
|
Chris@16
|
318 return invoker(keyword.get_name(), attrs, visitor);
|
Chris@16
|
319 }
|
Chris@16
|
320
|
Chris@16
|
321 /*!
|
Chris@16
|
322 * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the
|
Chris@16
|
323 * type or set of possible types of the attribute value to be visited.
|
Chris@16
|
324 *
|
Chris@16
|
325 * \param keyword The keyword of the attribute value to visit.
|
Chris@16
|
326 * \param rec A log record. The attribute value will be sought among those associated with the record.
|
Chris@16
|
327 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
328 * \return The result of visitation.
|
Chris@16
|
329 */
|
Chris@16
|
330 template< typename DescriptorT, template< typename > class ActorT, typename VisitorT >
|
Chris@16
|
331 inline visitation_result
|
Chris@16
|
332 visit(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, record const& rec, VisitorT visitor)
|
Chris@16
|
333 {
|
Chris@16
|
334 value_visitor_invoker< typename DescriptorT::value_type > invoker;
|
Chris@16
|
335 return invoker(keyword.get_name(), rec, visitor);
|
Chris@16
|
336 }
|
Chris@16
|
337
|
Chris@16
|
338 /*!
|
Chris@16
|
339 * The function applies a visitor to an attribute value from the view. The user has to explicitly specify the
|
Chris@16
|
340 * type or set of possible types of the attribute value to be visited.
|
Chris@16
|
341 *
|
Chris@16
|
342 * \param keyword The keyword of the attribute value to visit.
|
Chris@16
|
343 * \param rec A log record view. The attribute value will be sought among those associated with the record.
|
Chris@16
|
344 * \param visitor A receiving function object to pass the attribute value to.
|
Chris@16
|
345 * \return The result of visitation.
|
Chris@16
|
346 */
|
Chris@16
|
347 template< typename DescriptorT, template< typename > class ActorT, typename VisitorT >
|
Chris@16
|
348 inline visitation_result
|
Chris@16
|
349 visit(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, record_view const& rec, VisitorT visitor)
|
Chris@16
|
350 {
|
Chris@16
|
351 value_visitor_invoker< typename DescriptorT::value_type > invoker;
|
Chris@16
|
352 return invoker(keyword.get_name(), rec, visitor);
|
Chris@16
|
353 }
|
Chris@16
|
354
|
Chris@16
|
355
|
Chris@16
|
356 #if !defined(BOOST_LOG_DOXYGEN_PASS)
|
Chris@16
|
357
|
Chris@16
|
358 template< typename T, typename VisitorT >
|
Chris@16
|
359 inline visitation_result attribute_value::visit(VisitorT visitor) const
|
Chris@16
|
360 {
|
Chris@16
|
361 return boost::log::visit< T >(*this, visitor);
|
Chris@16
|
362 }
|
Chris@16
|
363
|
Chris@16
|
364 #endif // !defined(BOOST_LOG_DOXYGEN_PASS)
|
Chris@16
|
365
|
Chris@16
|
366 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
367
|
Chris@16
|
368 } // namespace boost
|
Chris@16
|
369
|
Chris@16
|
370 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
371
|
Chris@16
|
372 #endif // BOOST_LOG_ATTRIBUTES_VALUE_VISITATION_HPP_INCLUDED_
|