value_extraction.hpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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_extraction.hpp
  9. * \author Andrey Semashev
  10. * \date 01.03.2008
  11. *
  12. * The header contains implementation of tools for extracting an attribute value
  13. * from the view.
  14. */
  15. #ifndef BOOST_LOG_ATTRIBUTES_VALUE_EXTRACTION_HPP_INCLUDED_
  16. #define BOOST_LOG_ATTRIBUTES_VALUE_EXTRACTION_HPP_INCLUDED_
  17. #include <boost/mpl/vector.hpp>
  18. #include <boost/mpl/joint_view.hpp>
  19. #include <boost/mpl/if.hpp>
  20. #include <boost/mpl/eval_if.hpp>
  21. #include <boost/mpl/identity.hpp>
  22. #include <boost/mpl/is_sequence.hpp>
  23. #include <boost/mpl/contains.hpp>
  24. #include <boost/mpl/push_back.hpp>
  25. #include <boost/type_traits/is_same.hpp>
  26. #include <boost/core/enable_if.hpp>
  27. #include <boost/log/detail/config.hpp>
  28. #include <boost/log/exceptions.hpp>
  29. #include <boost/log/core/record.hpp>
  30. #include <boost/log/attributes/attribute_name.hpp>
  31. #include <boost/log/attributes/attribute_value.hpp>
  32. #include <boost/log/attributes/attribute.hpp>
  33. #include <boost/log/attributes/attribute_value_set.hpp>
  34. #include <boost/log/attributes/value_extraction_fwd.hpp>
  35. #include <boost/log/attributes/fallback_policy.hpp>
  36. #include <boost/log/expressions/keyword_fwd.hpp>
  37. #include <boost/log/utility/value_ref.hpp>
  38. #include <boost/log/utility/type_dispatch/static_type_dispatcher.hpp>
  39. #include <boost/log/detail/header.hpp>
  40. #ifdef BOOST_HAS_PRAGMA_ONCE
  41. #pragma once
  42. #endif
  43. namespace boost {
  44. BOOST_LOG_OPEN_NAMESPACE
  45. namespace result_of {
  46. /*!
  47. * \brief A metafunction that allows to acquire the result of the value extraction
  48. *
  49. * The metafunction results in a type that is in form of <tt>T const&</tt>, if \c T is
  50. * not an MPL type sequence and <tt>DefaultT</tt> is the same as <tt>T</tt>,
  51. * or <tt>value_ref< TypesT, TagT ></tt> otherwise, with
  52. * \c TypesT being a type sequence comprising the types from sequence \c T and \c DefaultT,
  53. * if it is not present in \c T already.
  54. */
  55. template< typename T, typename DefaultT, typename TagT >
  56. struct extract_or_default
  57. {
  58. typedef typename mpl::eval_if<
  59. mpl::is_sequence< T >,
  60. mpl::eval_if<
  61. mpl::contains< T, DefaultT >,
  62. mpl::identity< T >,
  63. mpl::push_back< T, DefaultT >
  64. >,
  65. mpl::if_<
  66. is_same< T, DefaultT >,
  67. T,
  68. mpl::vector2< T, DefaultT >
  69. >
  70. >::type extracted_type;
  71. typedef typename mpl::if_<
  72. mpl::is_sequence< extracted_type >,
  73. value_ref< extracted_type, TagT >,
  74. extracted_type const&
  75. >::type type;
  76. };
  77. /*!
  78. * \brief A metafunction that allows to acquire the result of the value extraction
  79. *
  80. * The metafunction results in a type that is in form of <tt>T const&</tt>, if \c T is
  81. * not an MPL type sequence, or <tt>value_ref< T, TagT ></tt> otherwise. In the latter
  82. * case the value reference shall never be empty.
  83. */
  84. template< typename T, typename TagT >
  85. struct extract_or_throw
  86. {
  87. typedef typename mpl::if_<
  88. mpl::is_sequence< T >,
  89. value_ref< T, TagT >,
  90. T const&
  91. >::type type;
  92. };
  93. /*!
  94. * \brief A metafunction that allows to acquire the result of the value extraction
  95. *
  96. * The metafunction results in a type that is in form of <tt>value_ref< T, TagT ></tt>.
  97. */
  98. template< typename T, typename TagT >
  99. struct extract
  100. {
  101. typedef value_ref< T, TagT > type;
  102. };
  103. } // namespace result_of
  104. namespace aux {
  105. //! The function object initializes the value reference
  106. template< typename RefT >
  107. struct value_ref_initializer
  108. {
  109. typedef void result_type;
  110. value_ref_initializer(RefT& ref) : m_ref(ref)
  111. {
  112. }
  113. template< typename ArgT >
  114. result_type operator() (ArgT const& arg) const
  115. {
  116. m_ref = RefT(arg);
  117. }
  118. private:
  119. RefT& m_ref;
  120. };
  121. //! The function unwraps \c value_ref, if possible
  122. template< typename T, typename TagT >
  123. BOOST_FORCEINLINE typename boost::enable_if_c< mpl::is_sequence< T >::value, value_ref< T, TagT > >::type
  124. unwrap_value_ref(value_ref< T, TagT > const& r)
  125. {
  126. return r;
  127. }
  128. template< typename T, typename TagT >
  129. BOOST_FORCEINLINE typename boost::disable_if_c< mpl::is_sequence< T >::value, T const& >::type
  130. unwrap_value_ref(value_ref< T, TagT > const& r)
  131. {
  132. return r.get();
  133. }
  134. } // namespace aux
  135. /*!
  136. * \brief Generic attribute value extractor
  137. *
  138. * Attribute value extractor is a functional object that attempts to find and extract the stored
  139. * attribute value from the attribute values view or a log record. The extracted value is returned
  140. * from the extractor.
  141. */
  142. template< typename T, typename FallbackPolicyT, typename TagT >
  143. class value_extractor :
  144. private FallbackPolicyT
  145. {
  146. public:
  147. //! Fallback policy
  148. typedef FallbackPolicyT fallback_policy;
  149. //! Attribute value types
  150. typedef T value_type;
  151. //! Function object result type
  152. typedef value_ref< value_type, TagT > result_type;
  153. public:
  154. /*!
  155. * Default constructor
  156. */
  157. BOOST_DEFAULTED_FUNCTION(value_extractor(), {})
  158. /*!
  159. * Copy constructor
  160. */
  161. value_extractor(value_extractor const& that) : fallback_policy(static_cast< fallback_policy const& >(that))
  162. {
  163. }
  164. /*!
  165. * Constructor
  166. *
  167. * \param arg Fallback policy constructor argument
  168. */
  169. template< typename U >
  170. explicit value_extractor(U const& arg) : fallback_policy(arg) {}
  171. /*!
  172. * Extraction operator. Attempts to acquire the stored value of one of the supported types. If extraction succeeds,
  173. * the extracted value is returned.
  174. *
  175. * \param attr The attribute value to extract from.
  176. * \return The extracted value, if extraction succeeded, an empty value otherwise.
  177. */
  178. result_type operator() (attribute_value const& attr) const
  179. {
  180. result_type res;
  181. aux::value_ref_initializer< result_type > initializer(res);
  182. if (!!attr)
  183. {
  184. static_type_dispatcher< value_type > disp(initializer);
  185. if (!attr.dispatch(disp) && !fallback_policy::apply_default(initializer))
  186. fallback_policy::on_invalid_type(attr.get_type());
  187. }
  188. else if (!fallback_policy::apply_default(initializer))
  189. {
  190. fallback_policy::on_missing_value();
  191. }
  192. return res;
  193. }
  194. /*!
  195. * Extraction operator. Looks for an attribute value with the specified name
  196. * and tries to acquire the stored value of one of the supported types. If extraction succeeds,
  197. * the extracted value is returned.
  198. *
  199. * \param name Attribute value name.
  200. * \param attrs A set of attribute values in which to look for the specified attribute value.
  201. * \return The extracted value, if extraction succeeded, an empty value otherwise.
  202. */
  203. result_type operator() (attribute_name const& name, attribute_value_set const& attrs) const
  204. {
  205. try
  206. {
  207. attribute_value_set::const_iterator it = attrs.find(name);
  208. if (it != attrs.end())
  209. return operator() (it->second);
  210. else
  211. return operator() (attribute_value());
  212. }
  213. catch (exception& e)
  214. {
  215. // Attach the attribute name to the exception
  216. boost::log::aux::attach_attribute_name_info(e, name);
  217. throw;
  218. }
  219. }
  220. /*!
  221. * Extraction operator. Looks for an attribute value with the specified name
  222. * and tries to acquire the stored value of one of the supported types. If extraction succeeds,
  223. * the extracted value is returned.
  224. *
  225. * \param name Attribute value name.
  226. * \param rec A log record. The attribute value will be sought among those associated with the record.
  227. * \return The extracted value, if extraction succeeded, an empty value otherwise.
  228. */
  229. result_type operator() (attribute_name const& name, record const& rec) const
  230. {
  231. return operator() (name, rec.attribute_values());
  232. }
  233. /*!
  234. * Extraction operator. Looks for an attribute value with the specified name
  235. * and tries to acquire the stored value of one of the supported types. If extraction succeeds,
  236. * the extracted value is returned.
  237. *
  238. * \param name Attribute value name.
  239. * \param rec A log record view. The attribute value will be sought among those associated with the record.
  240. * \return The extracted value, if extraction succeeded, an empty value otherwise.
  241. */
  242. result_type operator() (attribute_name const& name, record_view const& rec) const
  243. {
  244. return operator() (name, rec.attribute_values());
  245. }
  246. /*!
  247. * \returns Fallback policy
  248. */
  249. fallback_policy const& get_fallback_policy() const
  250. {
  251. return *static_cast< fallback_policy const* >(this);
  252. }
  253. };
  254. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  255. #if !defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS)
  256. #define BOOST_LOG_AUX_VOID_DEFAULT = void
  257. #else
  258. #define BOOST_LOG_AUX_VOID_DEFAULT
  259. #endif
  260. #endif // !defined(BOOST_LOG_DOXYGEN_PASS)
  261. /*!
  262. * The function extracts an attribute value from the view. The user has to explicitly specify the
  263. * type or set of possible types of the attribute value to be extracted.
  264. *
  265. * \param name The name of the attribute value to extract.
  266. * \param attrs A set of attribute values in which to look for the specified attribute value.
  267. * \return A \c value_ref that refers to the extracted value, if found. An empty value otherwise.
  268. */
  269. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT >
  270. inline typename result_of::extract< T, TagT >::type extract(attribute_name const& name, attribute_value_set const& attrs)
  271. {
  272. value_extractor< T, fallback_to_none, TagT > extractor;
  273. return extractor(name, attrs);
  274. }
  275. /*!
  276. * The function extracts 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 extracted.
  278. *
  279. * \param name The name of the attribute value to extract.
  280. * \param rec A log record. The attribute value will be sought among those associated with the record.
  281. * \return A \c value_ref that refers to the extracted value, if found. An empty value otherwise.
  282. */
  283. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT >
  284. inline typename result_of::extract< T, TagT >::type extract(attribute_name const& name, record const& rec)
  285. {
  286. value_extractor< T, fallback_to_none, TagT > extractor;
  287. return extractor(name, rec);
  288. }
  289. /*!
  290. * The function extracts an attribute value from the view. The user has to explicitly specify the
  291. * type or set of possible types of the attribute value to be extracted.
  292. *
  293. * \param name The name of the attribute value to extract.
  294. * \param rec A log record view. The attribute value will be sought among those associated with the record.
  295. * \return A \c value_ref that refers to the extracted value, if found. An empty value otherwise.
  296. */
  297. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT >
  298. inline typename result_of::extract< T, TagT >::type extract(attribute_name const& name, record_view const& rec)
  299. {
  300. value_extractor< T, fallback_to_none, TagT > extractor;
  301. return extractor(name, rec);
  302. }
  303. /*!
  304. * The function extracts an attribute value from the view. The user has to explicitly specify the
  305. * type or set of possible types of the attribute value to be extracted.
  306. *
  307. * \param value Attribute value.
  308. * \return A \c value_ref that refers to the extracted value, if found. An empty value otherwise.
  309. */
  310. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT >
  311. inline typename result_of::extract< T, TagT >::type extract(attribute_value const& value)
  312. {
  313. value_extractor< T, fallback_to_none, TagT > extractor;
  314. return extractor(value);
  315. }
  316. /*!
  317. * The function extracts an attribute value from the view. The user has to explicitly specify the
  318. * type or set of possible types of the attribute value to be extracted.
  319. *
  320. * \param name The name of the attribute value to extract.
  321. * \param attrs A set of attribute values in which to look for the specified attribute value.
  322. * \return The extracted value or a non-empty \c value_ref that refers to the value.
  323. * \throws An exception is thrown if the requested value cannot be extracted.
  324. */
  325. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT >
  326. inline typename result_of::extract_or_throw< T, TagT >::type extract_or_throw(attribute_name const& name, attribute_value_set const& attrs)
  327. {
  328. value_extractor< T, fallback_to_throw, TagT > extractor;
  329. return aux::unwrap_value_ref(extractor(name, attrs));
  330. }
  331. /*!
  332. * The function extracts an attribute value from the view. The user has to explicitly specify the
  333. * type or set of possible types of the attribute value to be extracted.
  334. *
  335. * \param name The name of the attribute value to extract.
  336. * \param rec A log record. The attribute value will be sought among those associated with the record.
  337. * \return The extracted value or a non-empty \c value_ref that refers to the value.
  338. * \throws An exception is thrown if the requested value cannot be extracted.
  339. */
  340. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT >
  341. inline typename result_of::extract_or_throw< T, TagT >::type extract_or_throw(attribute_name const& name, record const& rec)
  342. {
  343. value_extractor< T, fallback_to_throw, TagT > extractor;
  344. return aux::unwrap_value_ref(extractor(name, rec));
  345. }
  346. /*!
  347. * The function extracts an attribute value from the view. The user has to explicitly specify the
  348. * type or set of possible types of the attribute value to be extracted.
  349. *
  350. * \param name The name of the attribute value to extract.
  351. * \param rec A log record view. The attribute value will be sought among those associated with the record.
  352. * \return The extracted value or a non-empty \c value_ref that refers to the value.
  353. * \throws An exception is thrown if the requested value cannot be extracted.
  354. */
  355. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT >
  356. inline typename result_of::extract_or_throw< T, TagT >::type extract_or_throw(attribute_name const& name, record_view const& rec)
  357. {
  358. value_extractor< T, fallback_to_throw, TagT > extractor;
  359. return aux::unwrap_value_ref(extractor(name, rec));
  360. }
  361. /*!
  362. * The function extracts an attribute value from the view. The user has to explicitly specify the
  363. * type or set of possible types of the attribute value to be extracted.
  364. *
  365. * \param value Attribute value.
  366. * \return The extracted value or a non-empty \c value_ref that refers to the value.
  367. * \throws An exception is thrown if the requested value cannot be extracted.
  368. */
  369. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT >
  370. inline typename result_of::extract_or_throw< T, TagT >::type extract_or_throw(attribute_value const& value)
  371. {
  372. value_extractor< T, fallback_to_throw, TagT > extractor;
  373. return aux::unwrap_value_ref(extractor(value));
  374. }
  375. /*!
  376. * The function extracts an attribute value from the view. The user has to explicitly specify the
  377. * type or set of possible types of the attribute value to be extracted.
  378. *
  379. * \note Caution must be exercised if the default value is a temporary object. Because the function returns
  380. * a reference, if the temporary object is destroyed, the reference may become dangling.
  381. *
  382. * \param name The name of the attribute value to extract.
  383. * \param attrs A set of attribute values in which to look for the specified attribute value.
  384. * \param def_val The default value
  385. * \return The extracted value, if found. The default value otherwise.
  386. */
  387. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT, typename DefaultT >
  388. inline typename result_of::extract_or_default< T, DefaultT, TagT >::type
  389. extract_or_default(attribute_name const& name, attribute_value_set const& attrs, DefaultT const& def_val)
  390. {
  391. typedef typename result_of::extract_or_default< T, DefaultT, TagT >::extracted_type extracted_type;
  392. value_extractor< extracted_type, fallback_to_default< DefaultT const& >, TagT > extractor(def_val);
  393. return aux::unwrap_value_ref(extractor(name, attrs));
  394. }
  395. /*!
  396. * The function extracts an attribute value from the view. The user has to explicitly specify the
  397. * type or set of possible types of the attribute value to be visited.
  398. *
  399. * \note Caution must be exercised if the default value is a temporary object. Because the function returns
  400. * a reference, if the temporary object is destroyed, the reference may become dangling.
  401. *
  402. * \param name The name of the attribute value to extract.
  403. * \param rec A log record. The attribute value will be sought among those associated with the record.
  404. * \param def_val The default value
  405. * \return The extracted value, if found. The default value otherwise.
  406. */
  407. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT, typename DefaultT >
  408. inline typename result_of::extract_or_default< T, DefaultT, TagT >::type
  409. extract_or_default(attribute_name const& name, record const& rec, DefaultT const& def_val)
  410. {
  411. typedef typename result_of::extract_or_default< T, DefaultT, TagT >::extracted_type extracted_type;
  412. value_extractor< extracted_type, fallback_to_default< DefaultT const& >, TagT > extractor(def_val);
  413. return aux::unwrap_value_ref(extractor(name, rec));
  414. }
  415. /*!
  416. * The function extracts an attribute value from the view. The user has to explicitly specify the
  417. * type or set of possible types of the attribute value to be visited.
  418. *
  419. * \note Caution must be exercised if the default value is a temporary object. Because the function returns
  420. * a reference, if the temporary object is destroyed, the reference may become dangling.
  421. *
  422. * \param name The name of the attribute value to extract.
  423. * \param rec A log record view. The attribute value will be sought among those associated with the record.
  424. * \param def_val The default value
  425. * \return The extracted value, if found. The default value otherwise.
  426. */
  427. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT, typename DefaultT >
  428. inline typename result_of::extract_or_default< T, DefaultT, TagT >::type
  429. extract_or_default(attribute_name const& name, record_view const& rec, DefaultT const& def_val)
  430. {
  431. typedef typename result_of::extract_or_default< T, DefaultT, TagT >::extracted_type extracted_type;
  432. value_extractor< extracted_type, fallback_to_default< DefaultT const& >, TagT > extractor(def_val);
  433. return aux::unwrap_value_ref(extractor(name, rec));
  434. }
  435. /*!
  436. * The function extracts an attribute value from the view. The user has to explicitly specify the
  437. * type or set of possible types of the attribute value to be visited.
  438. *
  439. * \note Caution must be exercised if the default value is a temporary object. Because the function returns
  440. * a reference, if the temporary object is destroyed, the reference may become dangling.
  441. *
  442. * \param value Attribute value.
  443. * \param def_val The default value
  444. * \return The extracted value, if found. The default value otherwise.
  445. */
  446. template< typename T, typename TagT BOOST_LOG_AUX_VOID_DEFAULT, typename DefaultT >
  447. inline typename result_of::extract_or_default< T, DefaultT, TagT >::type extract_or_default(attribute_value const& value, DefaultT const& def_val)
  448. {
  449. typedef typename result_of::extract_or_default< T, DefaultT, TagT >::extracted_type extracted_type;
  450. value_extractor< extracted_type, fallback_to_default< DefaultT const& >, TagT > extractor(def_val);
  451. return aux::unwrap_value_ref(extractor(value));
  452. }
  453. #if defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS)
  454. template< typename T >
  455. inline typename result_of::extract< T >::type extract(attribute_name const& name, attribute_value_set const& attrs)
  456. {
  457. value_extractor< T, fallback_to_none > extractor;
  458. return extractor(name, attrs);
  459. }
  460. template< typename T >
  461. inline typename result_of::extract< T >::type extract(attribute_name const& name, record const& rec)
  462. {
  463. value_extractor< T, fallback_to_none > extractor;
  464. return extractor(name, rec);
  465. }
  466. template< typename T >
  467. inline typename result_of::extract< T >::type extract(attribute_name const& name, record_view const& rec)
  468. {
  469. value_extractor< T, fallback_to_none > extractor;
  470. return extractor(name, rec);
  471. }
  472. template< typename T >
  473. inline typename result_of::extract< T >::type extract(attribute_value const& value)
  474. {
  475. value_extractor< T, fallback_to_none > extractor;
  476. return extractor(value);
  477. }
  478. template< typename T >
  479. inline typename result_of::extract_or_throw< T >::type extract_or_throw(attribute_name const& name, attribute_value_set const& attrs)
  480. {
  481. value_extractor< T, fallback_to_throw > extractor;
  482. return aux::unwrap_value_ref(extractor(name, attrs));
  483. }
  484. template< typename T >
  485. inline typename result_of::extract_or_throw< T >::type extract_or_throw(attribute_name const& name, record const& rec)
  486. {
  487. value_extractor< T, fallback_to_throw > extractor;
  488. return aux::unwrap_value_ref(extractor(name, rec));
  489. }
  490. template< typename T >
  491. inline typename result_of::extract_or_throw< T >::type extract_or_throw(attribute_name const& name, record_view const& rec)
  492. {
  493. value_extractor< T, fallback_to_throw > extractor;
  494. return aux::unwrap_value_ref(extractor(name, rec));
  495. }
  496. template< typename T >
  497. inline typename result_of::extract_or_throw< T >::type extract_or_throw(attribute_value const& value)
  498. {
  499. value_extractor< T, fallback_to_throw > extractor;
  500. return aux::unwrap_value_ref(extractor(value));
  501. }
  502. template< typename T, typename DefaultT >
  503. inline typename result_of::extract_or_default< T, DefaultT >::type extract_or_default(
  504. attribute_name const& name, attribute_value_set const& attrs, DefaultT const& def_val)
  505. {
  506. typedef typename result_of::extract_or_default< T, DefaultT >::extracted_type extracted_type;
  507. value_extractor< extracted_type, fallback_to_default< DefaultT const& > > extractor(def_val);
  508. return aux::unwrap_value_ref(extractor(name, attrs));
  509. }
  510. template< typename T, typename DefaultT >
  511. inline typename result_of::extract_or_default< T, DefaultT >::type extract_or_default(
  512. attribute_name const& name, record const& rec, DefaultT const& def_val)
  513. {
  514. typedef typename result_of::extract_or_default< T, DefaultT >::extracted_type extracted_type;
  515. value_extractor< extracted_type, fallback_to_default< DefaultT const& > > extractor(def_val);
  516. return aux::unwrap_value_ref(extractor(name, rec));
  517. }
  518. template< typename T, typename DefaultT >
  519. inline typename result_of::extract_or_default< T, DefaultT >::type extract_or_default(
  520. attribute_name const& name, record_view const& rec, DefaultT const& def_val)
  521. {
  522. typedef typename result_of::extract_or_default< T, DefaultT >::extracted_type extracted_type;
  523. value_extractor< extracted_type, fallback_to_default< DefaultT const& > > extractor(def_val);
  524. return aux::unwrap_value_ref(extractor(name, rec));
  525. }
  526. template< typename T, typename DefaultT >
  527. inline typename result_of::extract_or_default< T, DefaultT >::type extract_or_default(attribute_value const& value, DefaultT const& def_val)
  528. {
  529. typedef typename result_of::extract_or_default< T, DefaultT >::extracted_type extracted_type;
  530. value_extractor< extracted_type, fallback_to_default< DefaultT const& > > extractor(def_val);
  531. return aux::unwrap_value_ref(extractor(value));
  532. }
  533. #endif // defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS)
  534. /*!
  535. * The function extracts an attribute value from the view. The user has to explicitly specify the
  536. * type or set of possible types of the attribute value to be extracted.
  537. *
  538. * \param keyword The keyword of the attribute value to extract.
  539. * \param attrs A set of attribute values in which to look for the specified attribute value.
  540. * \return A \c value_ref that refers to the extracted value, if found. An empty value otherwise.
  541. */
  542. template< typename DescriptorT, template< typename > class ActorT >
  543. inline typename result_of::extract< typename DescriptorT::value_type, DescriptorT >::type
  544. extract(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, attribute_value_set const& attrs)
  545. {
  546. value_extractor< typename DescriptorT::value_type, fallback_to_none, DescriptorT > extractor;
  547. return extractor(keyword.get_name(), attrs);
  548. }
  549. /*!
  550. * The function extracts an attribute value from the view. The user has to explicitly specify the
  551. * type or set of possible types of the attribute value to be extracted.
  552. *
  553. * \param keyword The keyword of the attribute value to extract.
  554. * \param rec A log record. The attribute value will be sought among those associated with the record.
  555. * \return A \c value_ref that refers to the extracted value, if found. An empty value otherwise.
  556. */
  557. template< typename DescriptorT, template< typename > class ActorT >
  558. inline typename result_of::extract< typename DescriptorT::value_type, DescriptorT >::type
  559. extract(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, record const& rec)
  560. {
  561. value_extractor< typename DescriptorT::value_type, fallback_to_none, DescriptorT > extractor;
  562. return extractor(keyword.get_name(), rec);
  563. }
  564. /*!
  565. * The function extracts an attribute value from the view. The user has to explicitly specify the
  566. * type or set of possible types of the attribute value to be extracted.
  567. *
  568. * \param keyword The keyword of the attribute value to extract.
  569. * \param rec A log record view. The attribute value will be sought among those associated with the record.
  570. * \return A \c value_ref that refers to the extracted value, if found. An empty value otherwise.
  571. */
  572. template< typename DescriptorT, template< typename > class ActorT >
  573. inline typename result_of::extract< typename DescriptorT::value_type, DescriptorT >::type
  574. extract(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, record_view const& rec)
  575. {
  576. value_extractor< typename DescriptorT::value_type, fallback_to_none, DescriptorT > extractor;
  577. return extractor(keyword.get_name(), rec);
  578. }
  579. /*!
  580. * The function extracts an attribute value from the view. The user has to explicitly specify the
  581. * type or set of possible types of the attribute value to be extracted.
  582. *
  583. * \param keyword The keyword of the attribute value to extract.
  584. * \param attrs A set of attribute values in which to look for the specified attribute value.
  585. * \return The extracted value or a non-empty \c value_ref that refers to the value.
  586. * \throws An exception is thrown if the requested value cannot be extracted.
  587. */
  588. template< typename DescriptorT, template< typename > class ActorT >
  589. inline typename result_of::extract_or_throw< typename DescriptorT::value_type, DescriptorT >::type
  590. extract_or_throw(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, attribute_value_set const& attrs)
  591. {
  592. value_extractor< typename DescriptorT::value_type, fallback_to_throw, DescriptorT > extractor;
  593. return aux::unwrap_value_ref(extractor(keyword.get_name(), attrs));
  594. }
  595. /*!
  596. * The function extracts an attribute value from the view. The user has to explicitly specify the
  597. * type or set of possible types of the attribute value to be extracted.
  598. *
  599. * \param keyword The keyword of the attribute value to extract.
  600. * \param rec A log record. The attribute value will be sought among those associated with the record.
  601. * \return The extracted value or a non-empty \c value_ref that refers to the value.
  602. * \throws An exception is thrown if the requested value cannot be extracted.
  603. */
  604. template< typename DescriptorT, template< typename > class ActorT >
  605. inline typename result_of::extract_or_throw< typename DescriptorT::value_type, DescriptorT >::type
  606. extract_or_throw(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, record const& rec)
  607. {
  608. value_extractor< typename DescriptorT::value_type, fallback_to_throw, DescriptorT > extractor;
  609. return aux::unwrap_value_ref(extractor(keyword.get_name(), rec));
  610. }
  611. /*!
  612. * The function extracts an attribute value from the view. The user has to explicitly specify the
  613. * type or set of possible types of the attribute value to be extracted.
  614. *
  615. * \param keyword The keyword of the attribute value to extract.
  616. * \param rec A log record view. The attribute value will be sought among those associated with the record.
  617. * \return The extracted value or a non-empty \c value_ref that refers to the value.
  618. * \throws An exception is thrown if the requested value cannot be extracted.
  619. */
  620. template< typename DescriptorT, template< typename > class ActorT >
  621. inline typename result_of::extract_or_throw< typename DescriptorT::value_type, DescriptorT >::type
  622. extract_or_throw(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, record_view const& rec)
  623. {
  624. value_extractor< typename DescriptorT::value_type, fallback_to_throw, DescriptorT > extractor;
  625. return aux::unwrap_value_ref(extractor(keyword.get_name(), rec));
  626. }
  627. /*!
  628. * The function extracts an attribute value from the view. The user has to explicitly specify the
  629. * type or set of possible types of the attribute value to be extracted.
  630. *
  631. * \note Caution must be exercised if the default value is a temporary object. Because the function returns
  632. * a reference, if the temporary object is destroyed, the reference may become dangling.
  633. *
  634. * \param keyword The keyword of the attribute value to extract.
  635. * \param attrs A set of attribute values in which to look for the specified attribute value.
  636. * \param def_val The default value
  637. * \return The extracted value, if found. The default value otherwise.
  638. */
  639. template< typename DescriptorT, template< typename > class ActorT, typename DefaultT >
  640. inline typename result_of::extract_or_default< typename DescriptorT::value_type, DefaultT, DescriptorT >::type
  641. extract_or_default(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, attribute_value_set const& attrs, DefaultT const& def_val)
  642. {
  643. typedef typename result_of::extract_or_default< typename DescriptorT::value_type, DefaultT, DescriptorT >::extracted_type extracted_type;
  644. value_extractor< extracted_type, fallback_to_default< DefaultT const& >, DescriptorT > extractor(def_val);
  645. return aux::unwrap_value_ref(extractor(keyword.get_name(), attrs));
  646. }
  647. /*!
  648. * The function extracts an attribute value from the view. The user has to explicitly specify the
  649. * type or set of possible types of the attribute value to be visited.
  650. *
  651. * \note Caution must be exercised if the default value is a temporary object. Because the function returns
  652. * a reference, if the temporary object is destroyed, the reference may become dangling.
  653. *
  654. * \param keyword The keyword of the attribute value to extract.
  655. * \param rec A log record. The attribute value will be sought among those associated with the record.
  656. * \param def_val The default value
  657. * \return The extracted value, if found. The default value otherwise.
  658. */
  659. template< typename DescriptorT, template< typename > class ActorT, typename DefaultT >
  660. inline typename result_of::extract_or_default< typename DescriptorT::value_type, DefaultT, DescriptorT >::type
  661. extract_or_default(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, record const& rec, DefaultT const& def_val)
  662. {
  663. typedef typename result_of::extract_or_default< typename DescriptorT::value_type, DefaultT, DescriptorT >::extracted_type extracted_type;
  664. value_extractor< extracted_type, fallback_to_default< DefaultT const& >, DescriptorT > extractor(def_val);
  665. return aux::unwrap_value_ref(extractor(keyword.get_name(), rec));
  666. }
  667. /*!
  668. * The function extracts an attribute value from the view. The user has to explicitly specify the
  669. * type or set of possible types of the attribute value to be visited.
  670. *
  671. * \note Caution must be exercised if the default value is a temporary object. Because the function returns
  672. * a reference, if the temporary object is destroyed, the reference may become dangling.
  673. *
  674. * \param keyword The keyword of the attribute value to extract.
  675. * \param rec A log record view. The attribute value will be sought among those associated with the record.
  676. * \param def_val The default value
  677. * \return The extracted value, if found. The default value otherwise.
  678. */
  679. template< typename DescriptorT, template< typename > class ActorT, typename DefaultT >
  680. inline typename result_of::extract_or_default< typename DescriptorT::value_type, DefaultT, DescriptorT >::type
  681. extract_or_default(expressions::attribute_keyword< DescriptorT, ActorT > const& keyword, record_view const& rec, DefaultT const& def_val)
  682. {
  683. typedef typename result_of::extract_or_default< typename DescriptorT::value_type, DefaultT, DescriptorT >::extracted_type extracted_type;
  684. value_extractor< extracted_type, fallback_to_default< DefaultT const& >, DescriptorT > extractor(def_val);
  685. return aux::unwrap_value_ref(extractor(keyword.get_name(), rec));
  686. }
  687. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  688. template< typename T, typename TagT >
  689. inline typename result_of::extract< T, TagT >::type attribute_value::extract() const
  690. {
  691. return boost::log::extract< T, TagT >(*this);
  692. }
  693. template< typename T, typename TagT >
  694. inline typename result_of::extract_or_throw< T, TagT >::type attribute_value::extract_or_throw() const
  695. {
  696. return boost::log::extract_or_throw< T, TagT >(*this);
  697. }
  698. template< typename T, typename TagT >
  699. inline typename result_of::extract_or_default< T, T, TagT >::type attribute_value::extract_or_default(T const& def_value) const
  700. {
  701. return boost::log::extract_or_default< T, TagT >(*this, def_value);
  702. }
  703. template< typename T, typename TagT, typename DefaultT >
  704. inline typename result_of::extract_or_default< T, DefaultT, TagT >::type attribute_value::extract_or_default(DefaultT const& def_value) const
  705. {
  706. return boost::log::extract_or_default< T, TagT >(*this, def_value);
  707. }
  708. #if defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS)
  709. template< typename T >
  710. inline typename result_of::extract< T >::type attribute_value::extract() const
  711. {
  712. return boost::log::extract< T >(*this);
  713. }
  714. template< typename T >
  715. inline typename result_of::extract_or_throw< T >::type attribute_value::extract_or_throw() const
  716. {
  717. return boost::log::extract_or_throw< T >(*this);
  718. }
  719. template< typename T >
  720. inline typename result_of::extract_or_default< T, T >::type attribute_value::extract_or_default(T const& def_value) const
  721. {
  722. return boost::log::extract_or_default< T >(*this, def_value);
  723. }
  724. template< typename T, typename DefaultT >
  725. inline typename result_of::extract_or_default< T, DefaultT >::type attribute_value::extract_or_default(DefaultT const& def_value) const
  726. {
  727. return boost::log::extract_or_default< T >(*this, def_value);
  728. }
  729. #endif // defined(BOOST_NO_CXX11_FUNCTION_TEMPLATE_DEFAULT_ARGS)
  730. #endif // !defined(BOOST_LOG_DOXYGEN_PASS)
  731. #undef BOOST_LOG_AUX_VOID_DEFAULT
  732. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  733. } // namespace boost
  734. #include <boost/log/detail/footer.hpp>
  735. #endif // BOOST_LOG_ATTRIBUTES_VALUE_EXTRACTION_HPP_INCLUDED_