keyword.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 keyword.hpp
  9. * \author Andrey Semashev
  10. * \date 29.01.2012
  11. *
  12. * The header contains attribute keyword declaration.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_KEYWORD_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_KEYWORD_HPP_INCLUDED_
  16. #include <boost/ref.hpp>
  17. #include <boost/proto/extends.hpp>
  18. #include <boost/proto/make_expr.hpp>
  19. #include <boost/phoenix/core/actor.hpp>
  20. #include <boost/phoenix/core/domain.hpp>
  21. #include <boost/phoenix/core/environment.hpp>
  22. #include <boost/fusion/sequence/intrinsic/at.hpp>
  23. #include <boost/preprocessor/cat.hpp>
  24. #include <boost/log/detail/config.hpp>
  25. #include <boost/log/detail/custom_terminal_spec.hpp>
  26. #include <boost/log/expressions/keyword_fwd.hpp>
  27. #include <boost/log/expressions/is_keyword_descriptor.hpp>
  28. #include <boost/log/expressions/attr.hpp>
  29. #include <boost/log/attributes/attribute_name.hpp>
  30. #include <boost/log/attributes/value_extraction.hpp>
  31. #include <boost/log/attributes/fallback_policy.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. * This class implements an expression template keyword. It is used to start template expressions involving attribute values.
  41. */
  42. template< typename DescriptorT, template< typename > class ActorT >
  43. struct attribute_keyword
  44. {
  45. //! Self type
  46. typedef attribute_keyword this_type;
  47. //! Attribute descriptor type
  48. typedef DescriptorT descriptor_type;
  49. BOOST_PROTO_BASIC_EXTENDS(typename proto::terminal< descriptor_type >::type, this_type, phoenix::phoenix_domain)
  50. //! Attribute value type
  51. typedef typename descriptor_type::value_type value_type;
  52. //! Returns attribute name
  53. static attribute_name get_name() { return descriptor_type::get_name(); }
  54. //! Expression with cached attribute name
  55. typedef attribute_actor<
  56. value_type,
  57. fallback_to_none,
  58. descriptor_type,
  59. ActorT
  60. > or_none_result_type;
  61. //! Generates an expression that extracts the attribute value or a default value
  62. static or_none_result_type or_none()
  63. {
  64. typedef typename or_none_result_type::terminal_type result_terminal;
  65. typename or_none_result_type::base_type act = {{ result_terminal(get_name()) }};
  66. return or_none_result_type(act);
  67. }
  68. //! Expression with cached attribute name
  69. typedef attribute_actor<
  70. value_type,
  71. fallback_to_throw,
  72. descriptor_type,
  73. ActorT
  74. > or_throw_result_type;
  75. //! Generates an expression that extracts the attribute value or throws an exception
  76. static or_throw_result_type or_throw()
  77. {
  78. typedef typename or_throw_result_type::terminal_type result_terminal;
  79. typename or_throw_result_type::base_type act = {{ result_terminal(get_name()) }};
  80. return or_throw_result_type(act);
  81. }
  82. //! Generates an expression that extracts the attribute value or a default value
  83. template< typename DefaultT >
  84. static attribute_actor<
  85. value_type,
  86. fallback_to_default< DefaultT >,
  87. descriptor_type,
  88. ActorT
  89. > or_default(DefaultT const& def_val)
  90. {
  91. typedef attribute_actor<
  92. value_type,
  93. fallback_to_default< DefaultT >,
  94. descriptor_type,
  95. ActorT
  96. > or_default_result_type;
  97. typedef typename or_default_result_type::terminal_type result_terminal;
  98. typename or_default_result_type::base_type act = {{ result_terminal(get_name(), def_val) }};
  99. return or_default_result_type(act);
  100. }
  101. };
  102. } // namespace expressions
  103. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  104. #ifndef BOOST_LOG_DOXYGEN_PASS
  105. namespace proto {
  106. namespace detail {
  107. // This hack is needed in order to cache attribute name into the expression terminal when the template
  108. // expression is constructed. The standard way through a custom domain doesn't work because phoenix::actor
  109. // is bound to phoenix_domain.
  110. template< typename DescriptorT, template< typename > class ActorT, typename DomainT >
  111. struct protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT >
  112. {
  113. typedef boost::log::expressions::attribute_keyword< DescriptorT, ActorT > keyword_type;
  114. typedef typename keyword_type::or_none_result_type result_type;
  115. result_type operator() (keyword_type const& keyword) const
  116. {
  117. return keyword.or_none();
  118. }
  119. };
  120. template< typename DescriptorT, template< typename > class ActorT, typename DomainT >
  121. struct protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >&, DomainT > :
  122. public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT >
  123. {
  124. };
  125. template< typename DescriptorT, template< typename > class ActorT, typename DomainT >
  126. struct protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT > const&, DomainT > :
  127. public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT >
  128. {
  129. };
  130. template< typename DescriptorT, template< typename > class ActorT, typename DomainT >
  131. struct protoify< boost::reference_wrapper< boost::log::expressions::attribute_keyword< DescriptorT, ActorT > >, DomainT > :
  132. public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT >
  133. {
  134. };
  135. template< typename DescriptorT, template< typename > class ActorT, typename DomainT >
  136. struct protoify< boost::reference_wrapper< boost::log::expressions::attribute_keyword< DescriptorT, ActorT > > const, DomainT > :
  137. public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT >
  138. {
  139. };
  140. template< typename DescriptorT, template< typename > class ActorT, typename DomainT >
  141. struct protoify< boost::reference_wrapper< const boost::log::expressions::attribute_keyword< DescriptorT, ActorT > >, DomainT > :
  142. public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT >
  143. {
  144. };
  145. template< typename DescriptorT, template< typename > class ActorT, typename DomainT >
  146. struct protoify< boost::reference_wrapper< const boost::log::expressions::attribute_keyword< DescriptorT, ActorT > > const, DomainT > :
  147. public protoify< boost::log::expressions::attribute_keyword< DescriptorT, ActorT >, DomainT >
  148. {
  149. };
  150. } // namespace detail
  151. } // namespace proto
  152. #endif // !defined(BOOST_LOG_DOXYGEN_PASS)
  153. } // namespace boost
  154. #ifndef BOOST_LOG_DOXYGEN_PASS
  155. #define BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE_IMPL(keyword_, name_, value_type_, tag_ns_)\
  156. namespace tag_ns_\
  157. {\
  158. struct keyword_ :\
  159. public ::boost::log::expressions::keyword_descriptor\
  160. {\
  161. typedef value_type_ value_type;\
  162. static ::boost::log::attribute_name get_name() { return ::boost::log::attribute_name(name_); }\
  163. };\
  164. }\
  165. typedef ::boost::log::expressions::attribute_keyword< tag_ns_::keyword_ > BOOST_PP_CAT(keyword_, _type);
  166. #define BOOST_LOG_ATTRIBUTE_KEYWORD_IMPL(keyword_, name_, value_type_, tag_ns_)\
  167. BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE_IMPL(keyword_, name_, value_type_, tag_ns_)\
  168. const BOOST_PP_CAT(keyword_, _type) keyword_ = {};
  169. #endif // BOOST_LOG_DOXYGEN_PASS
  170. /*!
  171. * \brief The macro declares an attribute keyword type
  172. *
  173. * The macro should be used at a namespace scope. It expands into an attribute keyword type definition, including the
  174. * \c tag namespace and the keyword tag type within which has the following layout:
  175. *
  176. * \code
  177. * namespace tag
  178. * {
  179. * struct keyword_ :
  180. * public boost::log::expressions::keyword_descriptor
  181. * {
  182. * typedef value_type_ value_type;
  183. * static boost::log::attribute_name get_name();
  184. * };
  185. * }
  186. *
  187. * typedef boost::log::expressions::attribute_keyword< tag::keyword_ > keyword_type;
  188. * \endcode
  189. *
  190. * The \c get_name method returns the attribute name.
  191. *
  192. * \note This macro only defines the type of the keyword. To also define the keyword object, use
  193. * the \c BOOST_LOG_ATTRIBUTE_KEYWORD macro instead.
  194. *
  195. * \param keyword_ Keyword name
  196. * \param name_ Attribute name string
  197. * \param value_type_ Attribute value type
  198. */
  199. #define BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(keyword_, name_, value_type_)\
  200. BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE_IMPL(keyword_, name_, value_type_, tag)
  201. /*!
  202. * \brief The macro declares an attribute keyword
  203. *
  204. * The macro provides definitions similar to \c BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE and additionally
  205. * defines the keyword object.
  206. *
  207. * \param keyword_ Keyword name
  208. * \param name_ Attribute name string
  209. * \param value_type_ Attribute value type
  210. */
  211. #define BOOST_LOG_ATTRIBUTE_KEYWORD(keyword_, name_, value_type_)\
  212. BOOST_LOG_ATTRIBUTE_KEYWORD_IMPL(keyword_, name_, value_type_, tag)
  213. #include <boost/log/detail/footer.hpp>
  214. #if defined(BOOST_LOG_TRIVIAL_HPP_INCLUDED_)
  215. #include <boost/log/detail/trivial_keyword.hpp>
  216. #endif
  217. #endif // BOOST_LOG_EXPRESSIONS_KEYWORD_HPP_INCLUDED_