attr.hpp 8.4 KB

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