is_in_range.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 is_in_range.hpp
  9. * \author Andrey Semashev
  10. * \date 02.09.2012
  11. *
  12. * The header contains implementation of an \c is_in_range predicate in template expressions.
  13. */
  14. #ifndef BOOST_LOG_EXPRESSIONS_PREDICATES_IS_IN_RANGE_HPP_INCLUDED_
  15. #define BOOST_LOG_EXPRESSIONS_PREDICATES_IS_IN_RANGE_HPP_INCLUDED_
  16. #include <utility>
  17. #include <boost/phoenix/core/actor.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/detail/embedded_string_type.hpp>
  20. #include <boost/log/detail/unary_function_terminal.hpp>
  21. #include <boost/log/detail/attribute_predicate.hpp>
  22. #include <boost/log/expressions/attr_fwd.hpp>
  23. #include <boost/log/expressions/keyword_fwd.hpp>
  24. #include <boost/log/attributes/attribute_name.hpp>
  25. #include <boost/log/attributes/fallback_policy.hpp>
  26. #include <boost/log/utility/functional/in_range.hpp>
  27. #include <boost/log/detail/header.hpp>
  28. #ifdef BOOST_HAS_PRAGMA_ONCE
  29. #pragma once
  30. #endif
  31. namespace boost {
  32. BOOST_LOG_OPEN_NAMESPACE
  33. namespace expressions {
  34. /*!
  35. * The predicate checks if the attribute value contains a substring. The attribute value is assumed to be of a string type.
  36. */
  37. #if !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  38. template< typename T, typename BoundaryT, typename FallbackPolicyT = fallback_to_none >
  39. using attribute_is_in_range = aux::attribute_predicate< T, std::pair< BoundaryT, BoundaryT >, in_range_fun, FallbackPolicyT >;
  40. #else // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  41. template< typename T, typename BoundaryT, typename FallbackPolicyT = fallback_to_none >
  42. class attribute_is_in_range :
  43. public aux::attribute_predicate< T, std::pair< BoundaryT, BoundaryT >, in_range_fun, FallbackPolicyT >
  44. {
  45. typedef aux::attribute_predicate< T, std::pair< BoundaryT, BoundaryT >, in_range_fun, FallbackPolicyT > base_type;
  46. public:
  47. /*!
  48. * Initializing constructor
  49. *
  50. * \param name Attribute name
  51. * \param boundaries The expected attribute value boundaries
  52. */
  53. attribute_is_in_range(attribute_name const& name, std::pair< BoundaryT, BoundaryT > const& boundaries) : base_type(name, boundaries)
  54. {
  55. }
  56. /*!
  57. * Initializing constructor
  58. *
  59. * \param name Attribute name
  60. * \param boundaries The expected attribute value boundaries
  61. * \param arg Additional parameter for the fallback policy
  62. */
  63. template< typename U >
  64. attribute_is_in_range(attribute_name const& name, std::pair< BoundaryT, BoundaryT > const& boundaries, U const& arg) : base_type(name, boundaries, arg)
  65. {
  66. }
  67. };
  68. #endif // !defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
  69. /*!
  70. * The function generates a terminal node in a template expression. The node will check if the attribute value
  71. * is in the specified range. The range must be half-open, that is the predicate will be equivalent to <tt>least <= attr < most</tt>.
  72. */
  73. template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename BoundaryT >
  74. BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_is_in_range< T, typename boost::log::aux::make_embedded_string_type< BoundaryT >::type, FallbackPolicyT > > >
  75. is_in_range(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& attr, BoundaryT const& least, BoundaryT const& most)
  76. {
  77. typedef typename boost::log::aux::make_embedded_string_type< BoundaryT >::type boundary_type;
  78. typedef aux::unary_function_terminal< attribute_is_in_range< T, boundary_type, FallbackPolicyT > > terminal_type;
  79. ActorT< terminal_type > act = {{ terminal_type(attr.get_name(), std::pair< boundary_type, boundary_type >(least, most), attr.get_fallback_policy()) }};
  80. return act;
  81. }
  82. /*!
  83. * The function generates a terminal node in a template expression. The node will check if the attribute value
  84. * is in the specified range. The range must be half-open, that is the predicate will be equivalent to <tt>least <= attr < most</tt>.
  85. */
  86. template< typename DescriptorT, template< typename > class ActorT, typename BoundaryT >
  87. BOOST_FORCEINLINE ActorT< aux::unary_function_terminal< attribute_is_in_range< typename DescriptorT::value_type, typename boost::log::aux::make_embedded_string_type< BoundaryT >::type > > >
  88. is_in_range(attribute_keyword< DescriptorT, ActorT > const&, BoundaryT const& least, BoundaryT const& most)
  89. {
  90. typedef typename boost::log::aux::make_embedded_string_type< BoundaryT >::type boundary_type;
  91. typedef aux::unary_function_terminal< attribute_is_in_range< typename DescriptorT::value_type, boundary_type > > terminal_type;
  92. ActorT< terminal_type > act = {{ terminal_type(DescriptorT::get_name(), std::pair< boundary_type, boundary_type >(least, most)) }};
  93. return act;
  94. }
  95. /*!
  96. * The function generates a terminal node in a template expression. The node will check if the attribute value
  97. * is in the specified range. The range must be half-open, that is the predicate will be equivalent to <tt>least <= attr < most</tt>.
  98. */
  99. template< typename T, typename BoundaryT >
  100. BOOST_FORCEINLINE phoenix::actor< aux::unary_function_terminal< attribute_is_in_range< T, typename boost::log::aux::make_embedded_string_type< BoundaryT >::type > > >
  101. is_in_range(attribute_name const& name, BoundaryT const& least, BoundaryT const& most)
  102. {
  103. typedef typename boost::log::aux::make_embedded_string_type< BoundaryT >::type boundary_type;
  104. typedef aux::unary_function_terminal< attribute_is_in_range< T, boundary_type > > terminal_type;
  105. phoenix::actor< terminal_type > act = {{ terminal_type(name, std::pair< boundary_type, boundary_type >(least, most)) }};
  106. return act;
  107. }
  108. } // namespace expressions
  109. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  110. } // namespace boost
  111. #include <boost/log/detail/footer.hpp>
  112. #endif // BOOST_LOG_EXPRESSIONS_PREDICATES_IS_IN_RANGE_HPP_INCLUDED_