Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:22

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   formatters/date_time.hpp
0009  * \author Andrey Semashev
0010  * \date   16.09.2012
0011  *
0012  * The header contains a formatter function for date and time attribute values.
0013  */
0014 
0015 #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_
0016 #define BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_
0017 
0018 #include <string>
0019 #include <boost/move/core.hpp>
0020 #include <boost/move/utility_core.hpp>
0021 #include <boost/phoenix/core/actor.hpp>
0022 #include <boost/phoenix/core/terminal_fwd.hpp>
0023 #include <boost/phoenix/core/is_nullary.hpp>
0024 #include <boost/phoenix/core/environment.hpp>
0025 #include <boost/fusion/sequence/intrinsic/at_c.hpp>
0026 #include <boost/log/detail/config.hpp>
0027 #include <boost/log/attributes/attribute_name.hpp>
0028 #include <boost/log/attributes/fallback_policy.hpp>
0029 #include <boost/log/attributes/value_visitation.hpp>
0030 #include <boost/log/detail/light_function.hpp>
0031 #include <boost/log/detail/date_time_fmt_gen_traits_fwd.hpp>
0032 #include <boost/log/detail/custom_terminal_spec.hpp>
0033 #include <boost/log/detail/attr_output_terminal.hpp>
0034 #include <boost/log/expressions/attr_fwd.hpp>
0035 #include <boost/log/expressions/keyword_fwd.hpp>
0036 #include <boost/log/utility/formatting_ostream.hpp>
0037 #include <boost/log/utility/functional/bind.hpp>
0038 #include <boost/log/detail/header.hpp>
0039 
0040 #ifdef BOOST_HAS_PRAGMA_ONCE
0041 #pragma once
0042 #endif
0043 
0044 namespace boost {
0045 
0046 BOOST_LOG_OPEN_NAMESPACE
0047 
0048 namespace expressions {
0049 
0050 /*!
0051  * Date and time formatter terminal.
0052  */
0053 template< typename T, typename FallbackPolicyT, typename CharT >
0054 class format_date_time_terminal
0055 {
0056 public:
0057 #ifndef BOOST_LOG_DOXYGEN_PASS
0058     //! Internal typedef for type categorization
0059     typedef void _is_boost_log_terminal;
0060 #endif
0061 
0062     //! Attribute value type
0063     typedef T value_type;
0064     //! Fallback policy
0065     typedef FallbackPolicyT fallback_policy;
0066     //! Character type
0067     typedef CharT char_type;
0068     //! String type
0069     typedef std::basic_string< char_type > string_type;
0070     //! Formatting stream type
0071     typedef basic_formatting_ostream< char_type > stream_type;
0072 
0073     //! Formatter function
0074     typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type;
0075 
0076     //! Function result type
0077     typedef string_type result_type;
0078 
0079 private:
0080     //! Formatter generator traits
0081     typedef aux::date_time_formatter_generator_traits< value_type, char_type > formatter_generator;
0082     //! Attribute value visitor invoker
0083     typedef value_visitor_invoker< value_type, fallback_policy > visitor_invoker_type;
0084 
0085 private:
0086     //! Attribute name
0087     attribute_name m_name;
0088     //! Formattr function
0089     formatter_function_type m_formatter;
0090     //! Attribute value visitor invoker
0091     visitor_invoker_type m_visitor_invoker;
0092 
0093 public:
0094     //! Initializing constructor
0095     format_date_time_terminal(attribute_name const& name, fallback_policy const& fallback, string_type const& format) :
0096         m_name(name), m_formatter(formatter_generator::parse(format)), m_visitor_invoker(fallback)
0097     {
0098     }
0099     //! Copy constructor
0100     format_date_time_terminal(format_date_time_terminal const& that) :
0101         m_name(that.m_name), m_formatter(that.m_formatter), m_visitor_invoker(that.m_visitor_invoker)
0102     {
0103     }
0104 
0105     //! Returns attribute name
0106     attribute_name get_name() const
0107     {
0108         return m_name;
0109     }
0110 
0111     //! Returns fallback policy
0112     fallback_policy const& get_fallback_policy() const
0113     {
0114         return m_visitor_invoker.get_fallback_policy();
0115     }
0116 
0117     //! Retruns formatter function
0118     formatter_function_type const& get_formatter_function() const
0119     {
0120         return m_formatter;
0121     }
0122 
0123     //! Invokation operator
0124     template< typename ContextT >
0125     result_type operator() (ContextT const& ctx)
0126     {
0127         string_type str;
0128         stream_type strm(str);
0129         m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type&, stream_type& >(m_formatter, strm));
0130         strm.flush();
0131         return BOOST_LOG_NRVO_RESULT(str);
0132     }
0133 
0134     //! Invokation operator
0135     template< typename ContextT >
0136     result_type operator() (ContextT const& ctx) const
0137     {
0138         string_type str;
0139         stream_type strm(str);
0140         m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type const&, stream_type& >(m_formatter, strm));
0141         strm.flush();
0142         return BOOST_LOG_NRVO_RESULT(str);
0143     }
0144 
0145     BOOST_DELETED_FUNCTION(format_date_time_terminal())
0146 };
0147 
0148 /*!
0149  * Date and time formatter actor.
0150  */
0151 template< typename T, typename FallbackPolicyT, typename CharT, template< typename > class ActorT = phoenix::actor >
0152 class format_date_time_actor :
0153     public ActorT< format_date_time_terminal< T, FallbackPolicyT, CharT > >
0154 {
0155 public:
0156     //! Attribute value type
0157     typedef T value_type;
0158     //! Character type
0159     typedef CharT char_type;
0160     //! Fallback policy
0161     typedef FallbackPolicyT fallback_policy;
0162     //! Base terminal type
0163     typedef format_date_time_terminal< value_type, fallback_policy, char_type > terminal_type;
0164     //! Formatter function
0165     typedef typename terminal_type::formatter_function_type formatter_function_type;
0166 
0167     //! Base actor type
0168     typedef ActorT< terminal_type > base_type;
0169 
0170 public:
0171     //! Initializing constructor
0172     explicit format_date_time_actor(base_type const& act) : base_type(act)
0173     {
0174     }
0175 
0176     /*!
0177      * \returns The attribute name
0178      */
0179     attribute_name get_name() const
0180     {
0181         return this->proto_expr_.child0.get_name();
0182     }
0183 
0184     /*!
0185      * \returns Fallback policy
0186      */
0187     fallback_policy const& get_fallback_policy() const
0188     {
0189         return this->proto_expr_.child0.get_fallback_policy();
0190     }
0191 
0192     /*!
0193      * \returns Formatter function
0194      */
0195     formatter_function_type const& get_formatter_function() const
0196     {
0197         return this->proto_expr_.child0.get_formatter_function();
0198     }
0199 };
0200 
0201 #ifndef BOOST_LOG_DOXYGEN_PASS
0202 
0203 #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\
0204     template< typename LeftExprT, typename T, typename FallbackPolicyT, typename CharT >\
0205     BOOST_FORCEINLINE phoenix::actor< aux::attribute_output_terminal< phoenix::actor< LeftExprT >, T, FallbackPolicyT, typename format_date_time_actor< T, FallbackPolicyT, CharT >::formatter_function_type > >\
0206     operator<< (phoenix::actor< LeftExprT > left_ref left, format_date_time_actor< T, FallbackPolicyT, CharT > right_ref right)\
0207     {\
0208         typedef aux::attribute_output_terminal< phoenix::actor< LeftExprT >, T, FallbackPolicyT, typename format_date_time_actor< T, FallbackPolicyT, CharT >::formatter_function_type > terminal_type;\
0209         phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_name(), right.get_formatter_function(), right.get_fallback_policy()) }};\
0210         return actor;\
0211     }
0212 
0213 #include <boost/log/detail/generate_overloads.hpp>
0214 
0215 #undef BOOST_LOG_AUX_OVERLOAD
0216 
0217 #endif // BOOST_LOG_DOXYGEN_PASS
0218 
0219 /*!
0220  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
0221  * expression (stream output or \c format placeholder filler).
0222  *
0223  * \param name Attribute name
0224  * \param format Format string
0225  */
0226 template< typename AttributeValueT, typename CharT >
0227 BOOST_FORCEINLINE format_date_time_actor< AttributeValueT, fallback_to_none, CharT > format_date_time(attribute_name const& name, const CharT* format)
0228 {
0229     typedef format_date_time_actor< AttributeValueT, fallback_to_none, CharT > actor_type;
0230     typedef typename actor_type::terminal_type terminal_type;
0231     typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), format) }};
0232     return actor_type(act);
0233 }
0234 
0235 /*!
0236  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
0237  * expression (stream output or \c format placeholder filler).
0238  *
0239  * \param name Attribute name
0240  * \param format Format string
0241  */
0242 template< typename AttributeValueT, typename CharT >
0243 BOOST_FORCEINLINE format_date_time_actor< AttributeValueT, fallback_to_none, CharT > format_date_time(attribute_name const& name, std::basic_string< CharT > const& format)
0244 {
0245     typedef format_date_time_actor< AttributeValueT, fallback_to_none, CharT > actor_type;
0246     typedef typename actor_type::terminal_type terminal_type;
0247     typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), format) }};
0248     return actor_type(act);
0249 }
0250 
0251 /*!
0252  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
0253  * expression (stream output or \c format placeholder filler).
0254  *
0255  * \param keyword Attribute keyword
0256  * \param format Format string
0257  */
0258 template< typename DescriptorT, template< typename > class ActorT, typename CharT >
0259 BOOST_FORCEINLINE format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT >
0260 format_date_time(attribute_keyword< DescriptorT, ActorT > const& keyword, const CharT* format)
0261 {
0262     typedef format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > actor_type;
0263     typedef typename actor_type::terminal_type terminal_type;
0264     typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), format) }};
0265     return actor_type(act);
0266 }
0267 
0268 /*!
0269  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
0270  * expression (stream output or \c format placeholder filler).
0271  *
0272  * \param keyword Attribute keyword
0273  * \param format Format string
0274  */
0275 template< typename DescriptorT, template< typename > class ActorT, typename CharT >
0276 BOOST_FORCEINLINE format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT >
0277 format_date_time(attribute_keyword< DescriptorT, ActorT > const& keyword, std::basic_string< CharT > const& format)
0278 {
0279     typedef format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > actor_type;
0280     typedef typename actor_type::terminal_type terminal_type;
0281     typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), format) }};
0282     return actor_type(act);
0283 }
0284 
0285 /*!
0286  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
0287  * expression (stream output or \c format placeholder filler).
0288  *
0289  * \param placeholder Attribute placeholder
0290  * \param format Format string
0291  */
0292 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT >
0293 BOOST_FORCEINLINE format_date_time_actor< T, FallbackPolicyT, CharT, ActorT >
0294 format_date_time(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, const CharT* format)
0295 {
0296     typedef format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > actor_type;
0297     typedef typename actor_type::terminal_type terminal_type;
0298     typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), format) }};
0299     return actor_type(act);
0300 }
0301 
0302 /*!
0303  * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting
0304  * expression (stream output or \c format placeholder filler).
0305  *
0306  * \param placeholder Attribute placeholder
0307  * \param format Format string
0308  */
0309 template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT >
0310 BOOST_FORCEINLINE format_date_time_actor< T, FallbackPolicyT, CharT, ActorT >
0311 format_date_time(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, std::basic_string< CharT > const& format)
0312 {
0313     typedef format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > actor_type;
0314     typedef typename actor_type::terminal_type terminal_type;
0315     typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), format) }};
0316     return actor_type(act);
0317 }
0318 
0319 } // namespace expressions
0320 
0321 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0322 
0323 #ifndef BOOST_LOG_DOXYGEN_PASS
0324 
0325 namespace phoenix {
0326 
0327 namespace result_of {
0328 
0329 template< typename T, typename FallbackPolicyT, typename CharT >
0330 struct is_nullary< custom_terminal< boost::log::expressions::format_date_time_terminal< T, FallbackPolicyT, CharT > > > :
0331     public mpl::false_
0332 {
0333 };
0334 
0335 } // namespace result_of
0336 
0337 } // namespace phoenix
0338 
0339 #endif // BOOST_LOG_DOXYGEN_PASS
0340 
0341 } // namespace boost
0342 
0343 #include <boost/log/detail/footer.hpp>
0344 
0345 #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_