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