value_visitation.hpp 13 KB

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