Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file formatters/date_time.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 16.09.2012 Chris@16: * Chris@16: * The header contains a formatter function for date and time attribute values. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace expressions { Chris@16: Chris@16: /*! Chris@16: * Date and time formatter terminal. Chris@16: */ Chris@16: template< typename T, typename FallbackPolicyT, typename CharT > Chris@16: class format_date_time_terminal Chris@16: { Chris@16: public: Chris@16: //! Internal typedef for type categorization Chris@16: typedef void _is_boost_log_terminal; Chris@16: Chris@16: //! Attribute value type Chris@16: typedef T value_type; Chris@16: //! Fallback policy Chris@16: typedef FallbackPolicyT fallback_policy; Chris@16: //! Character type Chris@16: typedef CharT char_type; Chris@16: //! String type Chris@16: typedef std::basic_string< char_type > string_type; Chris@16: //! Formatting stream type Chris@16: typedef basic_formatting_ostream< char_type > stream_type; Chris@16: Chris@16: //! Formatter function Chris@16: typedef boost::log::aux::light_function< void (stream_type&, value_type const&) > formatter_function_type; Chris@16: Chris@16: //! Function result type Chris@16: typedef string_type result_type; Chris@16: Chris@16: private: Chris@16: //! Formatter generator traits Chris@16: typedef aux::date_time_formatter_generator_traits< value_type, char_type > formatter_generator; Chris@16: //! Attribute value visitor invoker Chris@16: typedef value_visitor_invoker< value_type, fallback_policy > visitor_invoker_type; Chris@16: Chris@16: private: Chris@16: //! Attribute name Chris@16: attribute_name m_name; Chris@16: //! Formattr function Chris@16: formatter_function_type m_formatter; Chris@16: //! Attribute value visitor invoker Chris@16: visitor_invoker_type m_visitor_invoker; Chris@16: Chris@16: public: Chris@16: //! Initializing constructor Chris@16: format_date_time_terminal(attribute_name const& name, fallback_policy const& fallback, string_type const& format) : Chris@16: m_name(name), m_formatter(formatter_generator::parse(format)), m_visitor_invoker(fallback) Chris@16: { Chris@16: } Chris@16: //! Copy constructor Chris@16: format_date_time_terminal(format_date_time_terminal const& that) : Chris@16: m_name(that.m_name), m_formatter(that.m_formatter), m_visitor_invoker(that.m_visitor_invoker) Chris@16: { Chris@16: } Chris@16: Chris@16: //! Returns attribute name Chris@16: attribute_name get_name() const Chris@16: { Chris@16: return m_name; Chris@16: } Chris@16: Chris@16: //! Returns fallback policy Chris@16: fallback_policy const& get_fallback_policy() const Chris@16: { Chris@16: return m_visitor_invoker.get_fallback_policy(); Chris@16: } Chris@16: Chris@16: //! Retruns formatter function Chris@16: formatter_function_type const& get_formatter_function() const Chris@16: { Chris@16: return m_formatter; Chris@16: } Chris@16: Chris@16: //! Invokation operator Chris@16: template< typename ContextT > Chris@16: result_type operator() (ContextT const& ctx) Chris@16: { Chris@16: string_type str; Chris@16: stream_type strm(str); Chris@16: m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type&, stream_type& >(m_formatter, strm)); Chris@16: strm.flush(); Chris@16: return boost::move(str); Chris@16: } Chris@16: Chris@16: //! Invokation operator Chris@16: template< typename ContextT > Chris@16: result_type operator() (ContextT const& ctx) const Chris@16: { Chris@16: string_type str; Chris@16: stream_type strm(str); Chris@16: m_visitor_invoker(m_name, fusion::at_c< 0 >(phoenix::env(ctx).args()), binder1st< formatter_function_type const&, stream_type& >(m_formatter, strm)); Chris@16: strm.flush(); Chris@16: return boost::move(str); Chris@16: } Chris@16: Chris@16: BOOST_DELETED_FUNCTION(format_date_time_terminal()) Chris@16: }; Chris@16: Chris@16: /*! Chris@16: * Date and time formatter actor. Chris@16: */ Chris@16: template< typename T, typename FallbackPolicyT, typename CharT, template< typename > class ActorT = phoenix::actor > Chris@16: class format_date_time_actor : Chris@16: public ActorT< format_date_time_terminal< T, FallbackPolicyT, CharT > > Chris@16: { Chris@16: public: Chris@16: //! Attribute value type Chris@16: typedef T value_type; Chris@16: //! Character type Chris@16: typedef CharT char_type; Chris@16: //! Fallback policy Chris@16: typedef FallbackPolicyT fallback_policy; Chris@16: //! Base terminal type Chris@16: typedef format_date_time_terminal< value_type, fallback_policy, char_type > terminal_type; Chris@16: //! Formatter function Chris@16: typedef typename terminal_type::formatter_function_type formatter_function_type; Chris@16: Chris@16: //! Base actor type Chris@16: typedef ActorT< terminal_type > base_type; Chris@16: Chris@16: public: Chris@16: //! Initializing constructor Chris@16: explicit format_date_time_actor(base_type const& act) : base_type(act) Chris@16: { Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \returns The attribute name Chris@16: */ Chris@16: attribute_name get_name() const Chris@16: { Chris@16: return this->proto_expr_.child0.get_name(); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \returns Fallback policy Chris@16: */ Chris@16: fallback_policy const& get_fallback_policy() const Chris@16: { Chris@16: return this->proto_expr_.child0.get_fallback_policy(); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * \returns Formatter function Chris@16: */ Chris@16: formatter_function_type const& get_formatter_function() const Chris@16: { Chris@16: return this->proto_expr_.child0.get_formatter_function(); Chris@16: } Chris@16: }; Chris@16: Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: #define BOOST_LOG_AUX_OVERLOAD(left_ref, right_ref)\ Chris@16: template< typename LeftExprT, typename T, typename FallbackPolicyT, typename CharT >\ Chris@16: BOOST_FORCEINLINE phoenix::actor< aux::attribute_output_terminal< phoenix::actor< LeftExprT >, T, FallbackPolicyT, typename format_date_time_actor< T, FallbackPolicyT, CharT >::formatter_function_type > >\ Chris@16: operator<< (phoenix::actor< LeftExprT > left_ref left, format_date_time_actor< T, FallbackPolicyT, CharT > right_ref right)\ Chris@16: {\ Chris@16: typedef aux::attribute_output_terminal< phoenix::actor< LeftExprT >, T, FallbackPolicyT, typename format_date_time_actor< T, FallbackPolicyT, CharT >::formatter_function_type > terminal_type;\ Chris@16: phoenix::actor< terminal_type > actor = {{ terminal_type(left, right.get_name(), right.get_formatter_function(), right.get_fallback_policy()) }};\ Chris@16: return actor;\ Chris@16: } Chris@16: Chris@16: #include Chris@16: Chris@16: #undef BOOST_LOG_AUX_OVERLOAD Chris@16: Chris@16: #endif // BOOST_LOG_DOXYGEN_PASS Chris@16: Chris@16: /*! Chris@16: * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting Chris@16: * expression (stream output or \c format placeholder filler). Chris@16: * Chris@16: * \param name Attribute name Chris@16: * \param format Format string Chris@16: */ Chris@16: template< typename AttributeValueT, typename CharT > Chris@16: BOOST_FORCEINLINE format_date_time_actor< AttributeValueT, fallback_to_none, CharT > format_date_time(attribute_name const& name, const CharT* format) Chris@16: { Chris@16: typedef format_date_time_actor< AttributeValueT, fallback_to_none, CharT > actor_type; Chris@16: typedef typename actor_type::terminal_type terminal_type; Chris@16: typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), format) }}; Chris@16: return actor_type(act); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting Chris@16: * expression (stream output or \c format placeholder filler). Chris@16: * Chris@16: * \param name Attribute name Chris@16: * \param format Format string Chris@16: */ Chris@16: template< typename AttributeValueT, typename CharT > Chris@16: BOOST_FORCEINLINE format_date_time_actor< AttributeValueT, fallback_to_none, CharT > format_date_time(attribute_name const& name, std::basic_string< CharT > const& format) Chris@16: { Chris@16: typedef format_date_time_actor< AttributeValueT, fallback_to_none, CharT > actor_type; Chris@16: typedef typename actor_type::terminal_type terminal_type; Chris@16: typename actor_type::base_type act = {{ terminal_type(name, fallback_to_none(), format) }}; Chris@16: return actor_type(act); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting Chris@16: * expression (stream output or \c format placeholder filler). Chris@16: * Chris@16: * \param keyword Attribute keyword Chris@16: * \param format Format string Chris@16: */ Chris@16: template< typename DescriptorT, template< typename > class ActorT, typename CharT > Chris@16: BOOST_FORCEINLINE format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > Chris@16: format_date_time(attribute_keyword< DescriptorT, ActorT > const& keyword, const CharT* format) Chris@16: { Chris@16: typedef format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > actor_type; Chris@16: typedef typename actor_type::terminal_type terminal_type; Chris@16: typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), format) }}; Chris@16: return actor_type(act); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting Chris@16: * expression (stream output or \c format placeholder filler). Chris@16: * Chris@16: * \param keyword Attribute keyword Chris@16: * \param format Format string Chris@16: */ Chris@16: template< typename DescriptorT, template< typename > class ActorT, typename CharT > Chris@16: BOOST_FORCEINLINE format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > Chris@16: format_date_time(attribute_keyword< DescriptorT, ActorT > const& keyword, std::basic_string< CharT > const& format) Chris@16: { Chris@16: typedef format_date_time_actor< typename DescriptorT::value_type, fallback_to_none, CharT, ActorT > actor_type; Chris@16: typedef typename actor_type::terminal_type terminal_type; Chris@16: typename actor_type::base_type act = {{ terminal_type(keyword.get_name(), fallback_to_none(), format) }}; Chris@16: return actor_type(act); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting Chris@16: * expression (stream output or \c format placeholder filler). Chris@16: * Chris@16: * \param placeholder Attribute placeholder Chris@16: * \param format Format string Chris@16: */ Chris@16: template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT > Chris@16: BOOST_FORCEINLINE format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > Chris@16: format_date_time(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, const CharT* format) Chris@16: { Chris@16: typedef format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > actor_type; Chris@16: typedef typename actor_type::terminal_type terminal_type; Chris@16: typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), format) }}; Chris@16: return actor_type(act); Chris@16: } Chris@16: Chris@16: /*! Chris@16: * The function generates a manipulator node in a template expression. The manipulator must participate in a formatting Chris@16: * expression (stream output or \c format placeholder filler). Chris@16: * Chris@16: * \param placeholder Attribute placeholder Chris@16: * \param format Format string Chris@16: */ Chris@16: template< typename T, typename FallbackPolicyT, typename TagT, template< typename > class ActorT, typename CharT > Chris@16: BOOST_FORCEINLINE format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > Chris@16: format_date_time(attribute_actor< T, FallbackPolicyT, TagT, ActorT > const& placeholder, std::basic_string< CharT > const& format) Chris@16: { Chris@16: typedef format_date_time_actor< T, FallbackPolicyT, CharT, ActorT > actor_type; Chris@16: typedef typename actor_type::terminal_type terminal_type; Chris@16: typename actor_type::base_type act = {{ terminal_type(placeholder.get_name(), placeholder.get_fallback_policy(), format) }}; Chris@16: return actor_type(act); Chris@16: } Chris@16: Chris@16: } // namespace expressions Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@101: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@101: Chris@101: namespace phoenix { Chris@101: Chris@101: namespace result_of { Chris@101: Chris@101: template< typename T, typename FallbackPolicyT, typename CharT > Chris@101: struct is_nullary< custom_terminal< boost::log::expressions::format_date_time_terminal< T, FallbackPolicyT, CharT > > > : Chris@101: public mpl::false_ Chris@101: { Chris@101: }; Chris@101: Chris@101: } // namespace result_of Chris@101: Chris@101: } // namespace phoenix Chris@101: Chris@101: #endif // BOOST_LOG_DOXYGEN_PASS Chris@101: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_EXPRESSIONS_FORMATTERS_DATE_TIME_HPP_INCLUDED_