attribute_predicate.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 attribute_predicate.hpp
  9. * \author Andrey Semashev
  10. * \date 02.09.2012
  11. *
  12. * The header contains implementation of a generic predicate in template expressions.
  13. */
  14. #ifndef BOOST_LOG_DETAIL_ATTRIBUTE_PREDICATE_HPP_INCLUDED_
  15. #define BOOST_LOG_DETAIL_ATTRIBUTE_PREDICATE_HPP_INCLUDED_
  16. #include <boost/phoenix/core/actor.hpp>
  17. #include <boost/utility/result_of.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/attributes/attribute_name.hpp>
  20. #include <boost/log/attributes/value_visitation.hpp>
  21. #include <boost/log/attributes/fallback_policy.hpp>
  22. #include <boost/log/utility/functional/bind.hpp>
  23. #include <boost/log/utility/functional/save_result.hpp>
  24. #include <boost/log/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. BOOST_LOG_OPEN_NAMESPACE
  30. namespace expressions {
  31. namespace aux {
  32. /*!
  33. * The predicate checks if the attribute value satisfies a predicate.
  34. */
  35. template< typename T, typename ArgT, typename PredicateT, typename FallbackPolicyT = fallback_to_none >
  36. class attribute_predicate
  37. {
  38. public:
  39. //! Function result_type
  40. typedef bool result_type;
  41. //! Expected attribute value type
  42. typedef T value_type;
  43. //! Predicate type
  44. typedef PredicateT predicate_type;
  45. //! Argument type for the predicate
  46. typedef ArgT argument_type;
  47. //! Fallback policy
  48. typedef FallbackPolicyT fallback_policy;
  49. private:
  50. //! Argument for the predicate
  51. const argument_type m_arg;
  52. //! Attribute value name
  53. const attribute_name m_name;
  54. //! Visitor invoker
  55. value_visitor_invoker< value_type, fallback_policy > m_visitor_invoker;
  56. public:
  57. /*!
  58. * Initializing constructor
  59. *
  60. * \param name Attribute name
  61. * \param pred_arg The predicate argument
  62. */
  63. attribute_predicate(attribute_name const& name, argument_type const& pred_arg) : m_arg(pred_arg), m_name(name)
  64. {
  65. }
  66. /*!
  67. * Initializing constructor
  68. *
  69. * \param name Attribute name
  70. * \param pred_arg The predicate argument
  71. * \param arg Additional parameter for the fallback policy
  72. */
  73. template< typename U >
  74. attribute_predicate(attribute_name const& name, argument_type const& pred_arg, U const& arg) : m_arg(pred_arg), m_name(name), m_visitor_invoker(arg)
  75. {
  76. }
  77. /*!
  78. * Checking operator
  79. *
  80. * \param arg A set of attribute values or a log record
  81. * \return \c true if the log record contains the sought attribute value, \c false otherwise
  82. */
  83. template< typename ArgumentT >
  84. result_type operator() (ArgumentT const& arg) const
  85. {
  86. typedef binder2nd< predicate_type, argument_type const& > visitor_type;
  87. bool res = false;
  88. m_visitor_invoker(m_name, arg, boost::log::save_result(visitor_type(predicate_type(), m_arg), res));
  89. return res;
  90. }
  91. };
  92. } // namespace aux
  93. } // namespace expressions
  94. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  95. } // namespace boost
  96. #include <boost/log/detail/footer.hpp>
  97. #endif // BOOST_LOG_DETAIL_ATTRIBUTE_PREDICATE_HPP_INCLUDED_