attr.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 attr.hpp
  9. * \author Andrey Semashev
  10. * \date 21.07.2012
  11. *
  12. * The header contains implementation of a generic attribute placeholder in template expressions.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_
  16. #include <boost/mpl/bool.hpp>
  17. #include <boost/utility/result_of.hpp>
  18. #include <boost/phoenix/core/actor.hpp>
  19. #include <boost/phoenix/core/terminal_fwd.hpp>
  20. #include <boost/phoenix/core/is_nullary.hpp>
  21. #include <boost/phoenix/core/environment.hpp>
  22. #include <boost/fusion/sequence/intrinsic/at_c.hpp>
  23. #include <boost/type_traits/remove_cv.hpp>
  24. #include <boost/type_traits/remove_reference.hpp>
  25. #include <boost/log/detail/config.hpp>
  26. #include <boost/log/detail/copy_cv.hpp>
  27. #include <boost/log/detail/custom_terminal_spec.hpp>
  28. #include <boost/log/attributes/attribute_name.hpp>
  29. #include <boost/log/attributes/value_extraction.hpp>
  30. #include <boost/log/attributes/fallback_policy.hpp>
  31. #include <boost/log/expressions/attr_fwd.hpp>
  32. #include <boost/log/detail/header.hpp>
  33. #ifdef BOOST_HAS_PRAGMA_ONCE
  34. #pragma once
  35. #endif
  36. namespace boost {
  37. BOOST_LOG_OPEN_NAMESPACE
  38. namespace expressions {
  39. /*!
  40. * An attribute value extraction terminal
  41. */
  42. template< typename T, typename FallbackPolicyT, typename TagT >
  43. class attribute_terminal
  44. {
  45. private:
  46. //! Value extractor type
  47. typedef value_extractor< T, FallbackPolicyT, TagT > value_extractor_type;
  48. //! Self type
  49. typedef attribute_terminal< T, FallbackPolicyT, TagT > this_type;
  50. public:
  51. #ifndef BOOST_LOG_DOXYGEN_PASS
  52. //! Internal typedef for type categorization
  53. typedef void _is_boost_log_terminal;
  54. #endif
  55. //! Attribute tag type
  56. typedef TagT tag_type;
  57. //! Attribute value type
  58. typedef typename value_extractor_type::value_type value_type;
  59. //! Fallback policy type
  60. typedef typename value_extractor_type::fallback_policy fallback_policy;
  61. //! Function result type
  62. template< typename >
  63. struct result;
  64. template< typename ThisT, typename ContextT >
  65. struct result< ThisT(ContextT) >
  66. {
  67. typedef typename remove_cv<
  68. typename remove_reference< typename phoenix::result_of::env< ContextT >::type >::type
  69. >::type env_type;
  70. typedef typename env_type::args_type args_type;
  71. typedef typename boost::log::aux::copy_cv< ThisT, value_extractor_type >::type cv_value_extractor_type;
  72. typedef typename boost::result_of< cv_value_extractor_type(attribute_name const&, typename fusion::result_of::at_c< args_type, 0 >::type) >::type type;
  73. };
  74. private:
  75. //! Attribute value name
  76. const attribute_name m_name;
  77. //! Attribute value extractor
  78. value_extractor_type m_value_extractor;
  79. public:
  80. /*!
  81. * Initializing constructor
  82. */
  83. explicit attribute_terminal(attribute_name const& name) : m_name(name)
  84. {
  85. }
  86. /*!
  87. * Initializing constructor
  88. */
  89. template< typename U >
  90. attribute_terminal(attribute_name const& name, U const& arg) : m_name(name), m_value_extractor(arg)
  91. {
  92. }
  93. /*!
  94. * \returns Attribute value name
  95. */
  96. attribute_name get_name() const
  97. {
  98. return m_name;
  99. }
  100. /*!
  101. * \returns Fallback policy
  102. */
  103. fallback_policy const& get_fallback_policy() const
  104. {
  105. return m_value_extractor.get_fallback_policy();
  106. }
  107. /*!
  108. * The operator extracts attribute value
  109. */
  110. template< typename ContextT >
  111. typename result< this_type(ContextT const&) >::type
  112. operator() (ContextT const& ctx)
  113. {
  114. return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()));
  115. }
  116. /*!
  117. * The operator extracts attribute value
  118. */
  119. template< typename ContextT >
  120. typename result< const this_type(ContextT const&) >::type
  121. operator() (ContextT const& ctx) const
  122. {
  123. return m_value_extractor(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()));
  124. }
  125. BOOST_DELETED_FUNCTION(attribute_terminal())
  126. };
  127. /*!
  128. * An attribute value extraction terminal actor
  129. */
  130. template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT >
  131. class attribute_actor :
  132. public ActorT< attribute_terminal< T, FallbackPolicyT, TagT > >
  133. {
  134. public:
  135. //! Attribute tag type
  136. typedef TagT tag_type;
  137. //! Fallback policy
  138. typedef FallbackPolicyT fallback_policy;
  139. //! Base terminal type
  140. typedef attribute_terminal< T, fallback_policy, tag_type > terminal_type;
  141. //! Attribute value type
  142. typedef typename terminal_type::value_type value_type;
  143. //! Base actor type
  144. typedef ActorT< terminal_type > base_type;
  145. public:
  146. //! Initializing constructor
  147. explicit attribute_actor(base_type const& act) : base_type(act)
  148. {
  149. }
  150. /*!
  151. * \returns The attribute name
  152. */
  153. attribute_name get_name() const
  154. {
  155. return this->proto_expr_.child0.get_name();
  156. }
  157. /*!
  158. * \returns Fallback policy
  159. */
  160. fallback_policy const& get_fallback_policy() const
  161. {
  162. return this->proto_expr_.child0.get_fallback_policy();
  163. }
  164. //! Expression with cached attribute name
  165. typedef attribute_actor< value_type, fallback_to_none, tag_type, ActorT > or_none_result_type;
  166. //! Generates an expression that extracts the attribute value or a default value
  167. or_none_result_type or_none() const
  168. {
  169. typedef typename or_none_result_type::terminal_type result_terminal;
  170. typename or_none_result_type::base_type act = {{ result_terminal(get_name()) }};
  171. return or_none_result_type(act);
  172. }
  173. //! Expression with cached attribute name
  174. typedef attribute_actor< value_type, fallback_to_throw, tag_type, ActorT > or_throw_result_type;
  175. //! Generates an expression that extracts the attribute value or throws an exception
  176. or_throw_result_type or_throw() const
  177. {
  178. typedef typename or_throw_result_type::terminal_type result_terminal;
  179. typename or_throw_result_type::base_type act = {{ result_terminal(get_name()) }};
  180. return or_throw_result_type(act);
  181. }
  182. //! Generates an expression that extracts the attribute value or a default value
  183. template< typename DefaultT >
  184. attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default(DefaultT const& def_val) const
  185. {
  186. typedef attribute_actor< value_type, fallback_to_default< DefaultT >, tag_type, ActorT > or_default_result_type;
  187. typedef typename or_default_result_type::terminal_type result_terminal;
  188. typename or_default_result_type::base_type act = {{ result_terminal(get_name(), def_val) }};
  189. return or_default_result_type(act);
  190. }
  191. };
  192. /*!
  193. * The function generates a terminal node in a template expression. The node will extract the value of the attribute
  194. * with the specified name and type.
  195. */
  196. template< typename AttributeValueT >
  197. BOOST_FORCEINLINE attribute_actor< AttributeValueT > attr(attribute_name const& name)
  198. {
  199. typedef attribute_actor< AttributeValueT > result_type;
  200. typedef typename result_type::terminal_type result_terminal;
  201. typename result_type::base_type act = {{ result_terminal(name) }};
  202. return result_type(act);
  203. }
  204. /*!
  205. * The function generates a terminal node in a template expression. The node will extract the value of the attribute
  206. * with the specified name and type.
  207. */
  208. template< typename AttributeValueT, typename TagT >
  209. BOOST_FORCEINLINE attribute_actor< AttributeValueT, fallback_to_none, TagT > attr(attribute_name const& name)
  210. {
  211. typedef attribute_actor< AttributeValueT, fallback_to_none, TagT > result_type;
  212. typedef typename result_type::terminal_type result_terminal;
  213. typename result_type::base_type act = {{ result_terminal(name) }};
  214. return result_type(act);
  215. }
  216. } // namespace expressions
  217. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  218. #ifndef BOOST_LOG_DOXYGEN_PASS
  219. namespace phoenix {
  220. namespace result_of {
  221. template< typename T, typename FallbackPolicyT, typename TagT >
  222. struct is_nullary< custom_terminal< boost::log::expressions::attribute_terminal< T, FallbackPolicyT, TagT > > > :
  223. public mpl::false_
  224. {
  225. };
  226. } // namespace result_of
  227. } // namespace phoenix
  228. #endif
  229. } // namespace boost
  230. #include <boost/log/detail/footer.hpp>
  231. #if defined(BOOST_LOG_EXPRESSIONS_FORMATTERS_STREAM_HPP_INCLUDED_)
  232. #include <boost/log/detail/attr_output_impl.hpp>
  233. #endif
  234. #endif // BOOST_LOG_EXPRESSIONS_ATTR_HPP_INCLUDED_