filter.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 filter.hpp
  9. * \author Andrey Semashev
  10. * \date 13.07.2012
  11. *
  12. * The header contains a filter function object definition.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_FILTER_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_FILTER_HPP_INCLUDED_
  16. #include <boost/move/core.hpp>
  17. #include <boost/move/utility_core.hpp>
  18. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  19. #include <boost/type_traits/is_same.hpp>
  20. #include <boost/type_traits/remove_cv.hpp>
  21. #include <boost/log/detail/sfinae_tools.hpp>
  22. #endif
  23. #include <boost/log/detail/config.hpp>
  24. #include <boost/log/attributes/attribute_value_set.hpp>
  25. #include <boost/log/detail/light_function.hpp>
  26. #include <boost/log/detail/header.hpp>
  27. #ifdef BOOST_HAS_PRAGMA_ONCE
  28. #pragma once
  29. #endif
  30. namespace boost {
  31. BOOST_LOG_OPEN_NAMESPACE
  32. /*!
  33. * Log record filter function wrapper.
  34. */
  35. class filter
  36. {
  37. BOOST_COPYABLE_AND_MOVABLE(filter)
  38. public:
  39. //! Result type
  40. typedef bool result_type;
  41. private:
  42. //! Filter function type
  43. typedef boost::log::aux::light_function< bool (attribute_value_set const&) > filter_type;
  44. //! Default filter, always returns \c true
  45. struct default_filter
  46. {
  47. typedef bool result_type;
  48. result_type operator() (attribute_value_set const&) const { return true; }
  49. };
  50. private:
  51. //! Filter function
  52. filter_type m_Filter;
  53. public:
  54. /*!
  55. * Default constructor. Creates a filter that always returns \c true.
  56. */
  57. filter() : m_Filter(default_filter())
  58. {
  59. }
  60. /*!
  61. * Copy constructor
  62. */
  63. filter(filter const& that) : m_Filter(that.m_Filter)
  64. {
  65. }
  66. /*!
  67. * Move constructor. The moved-from filter is left in an unspecified state.
  68. */
  69. filter(BOOST_RV_REF(filter) that) BOOST_NOEXCEPT : m_Filter(boost::move(that.m_Filter))
  70. {
  71. }
  72. /*!
  73. * Initializing constructor. Creates a filter which will invoke the specified function object.
  74. */
  75. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  76. template< typename FunT >
  77. filter(FunT&& fun) : m_Filter(boost::forward< FunT >(fun))
  78. {
  79. }
  80. #elif !defined(BOOST_MSVC) || BOOST_MSVC >= 1600
  81. template< typename FunT >
  82. filter(FunT const& fun, typename boost::disable_if_c< move_detail::is_rv< FunT >::value, boost::log::aux::sfinae_dummy >::type = boost::log::aux::sfinae_dummy()) : m_Filter(fun)
  83. {
  84. }
  85. #else
  86. // MSVC 9 and older blows up in unexpected ways if we use SFINAE to disable constructor instantiation
  87. template< typename FunT >
  88. filter(FunT const& fun) : m_Filter(fun)
  89. {
  90. }
  91. template< typename FunT >
  92. filter(rv< FunT >& fun) : m_Filter(fun)
  93. {
  94. }
  95. template< typename FunT >
  96. filter(rv< FunT > const& fun) : m_Filter(static_cast< FunT const& >(fun))
  97. {
  98. }
  99. filter(rv< filter > const& that) : m_Filter(that.m_Filter)
  100. {
  101. }
  102. #endif
  103. /*!
  104. * Move assignment. The moved-from filter is left in an unspecified state.
  105. */
  106. filter& operator= (BOOST_RV_REF(filter) that) BOOST_NOEXCEPT
  107. {
  108. m_Filter.swap(that.m_Filter);
  109. return *this;
  110. }
  111. /*!
  112. * Copy assignment.
  113. */
  114. filter& operator= (BOOST_COPY_ASSIGN_REF(filter) that)
  115. {
  116. m_Filter = that.m_Filter;
  117. return *this;
  118. }
  119. /*!
  120. * Initializing assignment. Sets the specified function object to the filter.
  121. */
  122. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  123. template< typename FunT >
  124. filter& operator= (FunT const& fun)
  125. #else
  126. template< typename FunT >
  127. typename boost::disable_if_c< is_same< typename remove_cv< FunT >::type, filter >::value, filter& >::type
  128. operator= (FunT const& fun)
  129. #endif
  130. {
  131. filter(fun).swap(*this);
  132. return *this;
  133. }
  134. /*!
  135. * Filtering operator.
  136. *
  137. * \param values Attribute values of the log record.
  138. * \return \c true if the log record passes the filter, \c false otherwise.
  139. */
  140. result_type operator() (attribute_value_set const& values) const
  141. {
  142. return m_Filter(values);
  143. }
  144. /*!
  145. * Resets the filter to the default. The default filter always returns \c true.
  146. */
  147. void reset()
  148. {
  149. m_Filter = default_filter();
  150. }
  151. /*!
  152. * Swaps two filters
  153. */
  154. void swap(filter& that) BOOST_NOEXCEPT
  155. {
  156. m_Filter.swap(that.m_Filter);
  157. }
  158. };
  159. inline void swap(filter& left, filter& right) BOOST_NOEXCEPT
  160. {
  161. left.swap(right);
  162. }
  163. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  164. } // namespace boost
  165. #include <boost/log/detail/footer.hpp>
  166. #endif // BOOST_LOG_EXPRESSIONS_FILTER_HPP_INCLUDED_