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)
8 * \file record_ordering.hpp
9 * \author Andrey Semashev
12 * This header contains ordering predicates for logging records.
15 #ifndef BOOST_LOG_UTILITY_RECORD_ORDERING_HPP_INCLUDED_
16 #define BOOST_LOG_UTILITY_RECORD_ORDERING_HPP_INCLUDED_
18 #include <boost/core/enable_if.hpp>
19 #include <boost/type_traits/is_same.hpp>
20 #include <boost/log/detail/config.hpp>
21 #include <boost/log/detail/function_traits.hpp>
22 #include <boost/log/core/record_view.hpp>
23 #include <boost/log/attributes/attribute_name.hpp>
24 #include <boost/log/attributes/attribute_value.hpp>
25 #include <boost/log/attributes/value_visitation.hpp>
26 #include <boost/log/utility/functional/logical.hpp>
27 #include <boost/log/utility/functional/nop.hpp>
28 #include <boost/log/detail/header.hpp>
30 #ifdef BOOST_HAS_PRAGMA_ONCE
36 BOOST_LOG_OPEN_NAMESPACE
39 * \brief Ordering predicate, based on opaque pointers to the record view implementation data
41 * Since record views only refer to a shared implementation data, this predicate is able to order the views
42 * by comparing the pointers to the data. Therefore two views are considered to be equivalent if they
43 * refer to the same implementation data. Otherwise it is not specified whether one record is ordered before
44 * the other until the predicate is applied. Note that the ordering may change every time the application runs.
46 * This kind of ordering may be useful if log records are to be stored in an associative
47 * container with as least performance overhead as possible, when the particular order is not important.
49 * The \c FunT template argument is the predicate that is used to actually compare pointers. It should be
50 * able to compare <tt>const void*</tt> pointers. The compared pointers may refer to distinct memory regions,
51 * the pointers must not be interpreted in any way.
53 template< typename FunT = less >
54 class abstract_ordering :
59 typedef bool result_type;
63 * Default constructor. Requires \c FunT to be default constructible.
65 abstract_ordering() : FunT()
69 * Initializing constructor. Constructs \c FunT instance as a copy of the \a fun argument.
71 explicit abstract_ordering(FunT const& fun) : FunT(fun)
78 result_type operator() (record_view const& left, record_view const& right) const
80 // We rely on the fact that the attribute_values() method returns a reference to the object in the record implementation,
81 // so we can compare pointers.
82 return FunT::operator() (static_cast< const void* >(&left.attribute_values()), static_cast< const void* >(&right.attribute_values()));
87 * \brief Ordering predicate, based on attribute values associated with records
89 * This predicate allows to order log records based on values of a specifically named attribute
90 * associated with them. Two given log records being compared should both have the specified
91 * attribute value of the specified type to be able to be ordered properly. As a special case,
92 * if neither of the records have the value, these records are considered equivalent. Otherwise,
93 * the ordering results are unspecified.
95 template< typename ValueT, typename FunT = less >
96 class attribute_value_ordering :
101 typedef bool result_type;
102 //! Compared attribute value type
103 typedef ValueT value_type;
106 template< typename LeftT >
109 typedef void result_type;
111 l2_visitor(FunT const& fun, LeftT const& left, bool& result) :
112 m_fun(fun), m_left(left), m_result(result)
116 template< typename RightT >
117 result_type operator() (RightT const& right) const
119 m_result = m_fun(m_left, right);
129 friend struct l1_visitor;
132 typedef void result_type;
134 l1_visitor(attribute_value_ordering const& owner, record_view const& right, bool& result) :
135 m_owner(owner), m_right(right), m_result(result)
139 template< typename LeftT >
140 result_type operator() (LeftT const& left) const
142 boost::log::visit< value_type >(m_owner.m_name, m_right, l2_visitor< LeftT >(static_cast< FunT const& >(m_owner), left, m_result));
146 attribute_value_ordering const& m_owner;
147 record_view const& m_right;
152 //! Attribute value name
153 const attribute_name m_name;
157 * Initializing constructor.
159 * \param name The attribute value name to be compared
160 * \param fun The ordering functor
162 explicit attribute_value_ordering(attribute_name const& name, FunT const& fun = FunT()) :
171 result_type operator() (record_view const& left, record_view const& right) const
174 if (!boost::log::visit< value_type >(m_name, left, l1_visitor(*this, right, result)))
176 return !boost::log::visit< value_type >(m_name, right, nop());
183 * The function constructs a log record ordering predicate
185 template< typename ValueT, typename FunT >
186 inline attribute_value_ordering< ValueT, FunT > make_attr_ordering(attribute_name const& name, FunT const& fun)
188 typedef attribute_value_ordering< ValueT, FunT > ordering_t;
189 return ordering_t(name, fun);
192 #if !defined(BOOST_LOG_NO_FUNCTION_TRAITS)
196 //! An ordering predicate constructor that uses SFINAE to disable invalid instantiations
199 typename ArityCheckT = typename boost::enable_if_c< aux::arity_of< FunT >::value == 2 >::type,
200 typename Arg1T = typename aux::first_argument_type_of< FunT >::type,
201 typename Arg2T = typename aux::second_argument_type_of< FunT >::type,
202 typename ArgsCheckT = typename boost::enable_if_c< is_same< Arg1T, Arg2T >::value >::type
204 struct make_attr_ordering_type
206 typedef attribute_value_ordering< Arg1T, FunT > type;
212 * The function constructs a log record ordering predicate
214 template< typename FunT >
215 inline typename aux::make_attr_ordering_type< FunT >::type make_attr_ordering(attribute_name const& name, FunT const& fun)
217 typedef typename aux::make_attr_ordering_type< FunT >::type ordering_t;
218 return ordering_t(name, fun);
221 #endif // BOOST_LOG_NO_FUNCTION_TRAITS
223 BOOST_LOG_CLOSE_NAMESPACE // namespace log
227 #include <boost/log/detail/footer.hpp>
229 #endif // BOOST_LOG_UTILITY_RECORD_ORDERING_HPP_INCLUDED_