attr.hpp 8.7 KB

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