has_attr.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 has_attr.hpp
  9. * \author Andrey Semashev
  10. * \date 23.07.2012
  11. *
  12. * The header contains implementation of a generic attribute presence checker in template expressions.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_HAS_ATTR_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_PREDICATES_HAS_ATTR_HPP_INCLUDED_
  16. #include <boost/phoenix/core/actor.hpp>
  17. #include <boost/log/detail/config.hpp>
  18. #include <boost/log/core/record_view.hpp>
  19. #include <boost/log/attributes/attribute_name.hpp>
  20. #include <boost/log/attributes/attribute_value_set.hpp>
  21. #include <boost/log/attributes/value_visitation.hpp>
  22. #include <boost/log/expressions/keyword_fwd.hpp>
  23. #include <boost/log/detail/unary_function_terminal.hpp>
  24. #include <boost/log/utility/functional/nop.hpp>
  25. #include <boost/log/detail/header.hpp>
  26. #ifdef BOOST_HAS_PRAGMA_ONCE
  27. #pragma once
  28. #endif
  29. namespace boost {
  30. BOOST_LOG_OPEN_NAMESPACE
  31. namespace expressions {
  32. /*!
  33. * An attribute value presence checker.
  34. */
  35. template< typename T >
  36. class has_attribute
  37. {
  38. public:
  39. //! Function result_type
  40. typedef bool result_type;
  41. //! Expected attribute value type
  42. typedef T value_type;
  43. private:
  44. //! Attribute value name
  45. const attribute_name m_name;
  46. //! Visitor invoker
  47. value_visitor_invoker< value_type > m_visitor_invoker;
  48. public:
  49. /*!
  50. * Initializing constructor
  51. *
  52. * \param name Attribute name
  53. */
  54. explicit has_attribute(attribute_name const& name) : m_name(name)
  55. {
  56. }
  57. /*!
  58. * Checking operator
  59. *
  60. * \param arg A set of attribute values or a log record
  61. * \return \c true if the log record contains the sought attribute value, \c false otherwise
  62. */
  63. template< typename ArgT >
  64. result_type operator() (ArgT const& arg) const
  65. {
  66. return m_visitor_invoker(m_name, arg, nop()).code() == visitation_result::ok;
  67. }
  68. };
  69. /*!
  70. * An attribute value presence checker. This specialization does not check the type of the attribute value.
  71. */
  72. template< >
  73. class has_attribute< void >
  74. {
  75. public:
  76. //! Function result_type
  77. typedef bool result_type;
  78. //! Expected attribute value type
  79. typedef void value_type;
  80. private:
  81. //! Attribute name
  82. const attribute_name m_name;
  83. public:
  84. /*!
  85. * Initializing constructor
  86. *
  87. * \param name Attribute name
  88. */
  89. explicit has_attribute(attribute_name const& name) : m_name(name)
  90. {
  91. }
  92. /*!
  93. * Checking operator
  94. *
  95. * \param attrs A set of attribute values
  96. * \return \c true if the log record contains the sought attribute value, \c false otherwise
  97. */
  98. result_type operator() (attribute_value_set const& attrs) const
  99. {
  100. return attrs.find(m_name) != attrs.end();
  101. }
  102. /*!
  103. * Checking operator
  104. *
  105. * \param rec A log record
  106. * \return \c true if the log record contains the sought attribute value, \c false otherwise
  107. */
  108. result_type operator() (boost::log::record_view const& rec) const
  109. {
  110. return operator()(rec.attribute_values());
  111. }
  112. };
  113. /*!
  114. * The function generates a terminal node in a template expression. The node will check for the attribute value
  115. * presence in a log record. The node will also check that the attribute value has the specified type, if present.
  116. */
  117. template< typename AttributeValueT >
  118. BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< has_attribute< AttributeValueT > > > has_attr(attribute_name const& name)
  119. {
  120. typedef aux::unary_function_terminal< has_attribute< AttributeValueT > > terminal_type;
  121. phoenix::actor< terminal_type > act = {{ terminal_type(name) }};
  122. return act;
  123. }
  124. /*!
  125. * The function generates a terminal node in a template expression. The node will check for the attribute value
  126. * presence in a log record.
  127. */
  128. BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< has_attribute< void > > > has_attr(attribute_name const& name)
  129. {
  130. typedef aux::unary_function_terminal< has_attribute< void > > terminal_type;
  131. phoenix::actor< terminal_type > act = {{ terminal_type(name) }};
  132. return act;
  133. }
  134. /*!
  135. * The function generates a terminal node in a template expression. The node will check for the attribute value
  136. * presence in a log record. The node will also check that the attribute value has the specified type, if present.
  137. */
  138. template< typename DescriptorT, template< typename > class ActorT >
  139. BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< has_attribute< typename DescriptorT::value_type > > > has_attr(attribute_keyword< DescriptorT, ActorT > const&)
  140. {
  141. typedef aux::unary_function_terminal< has_attribute< typename DescriptorT::value_type > > terminal_type;
  142. ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name()) }};
  143. return act;
  144. }
  145. } // namespace expressions
  146. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  147. } // namespace boost
  148. #include <boost/log/detail/footer.hpp>
  149. #endif // BOOST_LOG_EXPRESSIONS_PREDICATES_HAS_ATTR_HPP_INCLUDED_