Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:48

0001 //  (C) Copyright Howard Hinnant
0002 //  (C) Copyright 2011 Vicente J. Botet Escriba
0003 //  Copyright (c) Microsoft Corporation 2014
0004 //  Use, modification and distribution are subject to the Boost Software License,
0005 //  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0006 //  http://www.boost.org/LICENSE_1_0.txt).
0007 //
0008 
0009 #ifndef BOOST_CHRONO_IO_TIME_POINT_UNITS_HPP
0010 #define BOOST_CHRONO_IO_TIME_POINT_UNITS_HPP
0011 
0012 #include <boost/chrono/config.hpp>
0013 #include <boost/chrono/process_cpu_clocks.hpp>
0014 #include <boost/chrono/system_clocks.hpp>
0015 #include <boost/chrono/thread_clock.hpp>
0016 #include <boost/chrono/io/ios_base_state.hpp>
0017 #include <string>
0018 #include <iosfwd>
0019 #include <ios>
0020 #include <locale>
0021 #include <algorithm>
0022 
0023 namespace boost
0024 {
0025   namespace chrono
0026   {
0027     /**
0028      * customization point to the epoch associated to the clock @c Clock
0029      * The default calls @c f.do_get_epoch(Clock()). The user can overload this function.
0030      * @return the string epoch associated to the @c Clock
0031      */
0032     template <typename CharT, typename Clock, typename TPUFacet>
0033     std::basic_string<CharT> get_epoch_custom(Clock, TPUFacet& f)
0034     {
0035       return f.do_get_epoch(Clock());
0036     }
0037 
0038     /**
0039      * @c time_point_units facet gives useful information about the time_point pattern,
0040      * the text associated to a time_point's epoch,
0041      */
0042     template <typename CharT=char>
0043     class time_point_units: public std::locale::facet
0044     {
0045     public:
0046       /**
0047        * Type of character the facet is instantiated on.
0048        */
0049       typedef CharT char_type;
0050       /**
0051        * Type of character string used by member functions.
0052        */
0053       typedef std::basic_string<char_type> string_type;
0054 
0055       /**
0056        * Unique identifier for this type of facet.
0057        */
0058       static std::locale::id id;
0059 
0060       /**
0061        * Construct a @c time_point_units facet.
0062        * @param refs
0063        * @Effects Construct a @c time_point_units facet.
0064        * If the @c refs argument is @c 0 then destruction of the object is
0065        * delegated to the @c locale, or locales, containing it. This allows
0066        * the user to ignore lifetime management issues. On the other had,
0067        * if @c refs is @c 1 then the object must be explicitly deleted;
0068        * the @c locale will not do so. In this case, the object can be
0069        * maintained across the lifetime of multiple locales.
0070        */
0071       explicit time_point_units(size_t refs = 0) :
0072         std::locale::facet(refs)
0073       {
0074       }
0075 
0076       /**
0077        * @return the pattern to be used by default.
0078        */
0079       virtual string_type get_pattern() const =0;
0080 
0081       /**
0082        * @return the epoch associated to the clock @c Clock calling @c do_get_epoch(Clock())
0083        */
0084       template <typename Clock>
0085       string_type get_epoch() const
0086       {
0087         return get_epoch_custom<CharT>(Clock(), *this);
0088       }
0089 
0090     protected:
0091       /**
0092        * Destroy the facet.
0093        */
0094       virtual ~time_point_units() {}
0095 
0096     public:
0097 
0098       /**
0099        *
0100        * @param c a dummy instance of @c system_clock.
0101        * @return The epoch string associated to the @c system_clock.
0102        */
0103       virtual string_type do_get_epoch(system_clock) const=0;
0104 
0105       /**
0106        *
0107        * @param c a dummy instance of @c steady_clock.
0108        * @return The epoch string associated to the @c steady_clock.
0109        */
0110       virtual string_type do_get_epoch(steady_clock) const=0;
0111 
0112 #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
0113       /**
0114        *
0115        * @param c a dummy instance of @c process_real_cpu_clock.
0116        * @return The epoch string associated to the @c process_real_cpu_clock.
0117        */
0118       virtual string_type do_get_epoch(process_real_cpu_clock) const=0;
0119 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
0120       /**
0121        *
0122        * @param c a dummy instance of @c process_user_cpu_clock.
0123        * @return The epoch string associated to the @c process_user_cpu_clock.
0124        */
0125       virtual string_type do_get_epoch(process_user_cpu_clock) const=0;
0126       /**
0127        *
0128        * @param c a dummy instance of @c process_system_cpu_clock.
0129        * @return The epoch string associated to the @c process_system_cpu_clock.
0130        */
0131       virtual string_type do_get_epoch(process_system_cpu_clock) const=0;
0132       /**
0133        *
0134        * @param c a dummy instance of @c process_cpu_clock.
0135        * @return The epoch string associated to the @c process_cpu_clock.
0136        */
0137       virtual string_type do_get_epoch(process_cpu_clock) const=0;
0138 #endif
0139 #endif
0140 #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
0141       /**
0142        *
0143        * @param c a dummy instance of @c thread_clock.
0144        * @return The epoch string associated to the @c thread_clock.
0145        */
0146       virtual string_type do_get_epoch(thread_clock) const=0;
0147 #endif
0148 
0149     };
0150 
0151     template <typename CharT>
0152     std::locale::id time_point_units<CharT>::id;
0153 
0154 
0155     // This class is used to define the strings for the default English
0156     template <typename CharT=char>
0157     class time_point_units_default: public time_point_units<CharT>
0158     {
0159     public:
0160       /**
0161        * Type of character the facet is instantiated on.
0162        */
0163       typedef CharT char_type;
0164       /**
0165        * Type of character string returned by member functions.
0166        */
0167       typedef std::basic_string<char_type> string_type;
0168 
0169       explicit time_point_units_default(size_t refs = 0) :
0170         time_point_units<CharT> (refs)
0171       {
0172       }
0173       ~time_point_units_default() {}
0174 
0175       /**
0176        * @return the default pattern "%d%e".
0177        */
0178       string_type get_pattern() const
0179       {
0180         static const CharT t[] =
0181         { '%', 'd', '%', 'e' };
0182         static const string_type pattern(t, t + sizeof (t) / sizeof (t[0]));
0183 
0184         return pattern;
0185       }
0186 
0187     //protected:
0188       /**
0189        * @param c a dummy instance of @c system_clock.
0190        * @return The epoch string returned by @c clock_string<system_clock,CharT>::since().
0191        */
0192       string_type do_get_epoch(system_clock ) const
0193       {
0194         return clock_string<system_clock,CharT>::since();
0195       }
0196       /**
0197        * @param c a dummy instance of @c steady_clock.
0198        * @return The epoch string returned by @c clock_string<steady_clock,CharT>::since().
0199        */
0200       string_type do_get_epoch(steady_clock ) const
0201       {
0202         return clock_string<steady_clock,CharT>::since();
0203       }
0204 
0205 #if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS)
0206       /**
0207        * @param c a dummy instance of @c process_real_cpu_clock.
0208        * @return The epoch string returned by @c clock_string<process_real_cpu_clock,CharT>::since().
0209        */
0210       string_type do_get_epoch(process_real_cpu_clock ) const
0211       {
0212         return clock_string<process_real_cpu_clock,CharT>::since();
0213       }
0214 #if ! BOOST_OS_WINDOWS || BOOST_PLAT_WINDOWS_DESKTOP
0215       /**
0216        * @param c a dummy instance of @c process_user_cpu_clock.
0217        * @return The epoch string returned by @c clock_string<process_user_cpu_clock,CharT>::since().
0218        */
0219       string_type do_get_epoch(process_user_cpu_clock ) const
0220       {
0221         return clock_string<process_user_cpu_clock,CharT>::since();
0222       }
0223       /**
0224        * @param c a dummy instance of @c process_system_cpu_clock.
0225        * @return The epoch string returned by @c clock_string<process_system_cpu_clock,CharT>::since().
0226        */
0227       string_type do_get_epoch(process_system_cpu_clock ) const
0228       {
0229         return clock_string<process_system_cpu_clock,CharT>::since();
0230       }
0231       /**
0232        * @param c a dummy instance of @c process_cpu_clock.
0233        * @return The epoch string returned by @c clock_string<process_cpu_clock,CharT>::since().
0234        */
0235       string_type do_get_epoch(process_cpu_clock ) const
0236       {
0237         return clock_string<process_cpu_clock,CharT>::since();
0238       }
0239 
0240 #endif
0241 #endif
0242 #if defined(BOOST_CHRONO_HAS_THREAD_CLOCK)
0243       /**
0244        * @param c a dummy instance of @c thread_clock.
0245        * @return The epoch string returned by @c clock_string<thread_clock,CharT>::since().
0246        */
0247       string_type do_get_epoch(thread_clock ) const
0248       {
0249         return clock_string<thread_clock,CharT>::since();
0250       }
0251 #endif
0252 
0253     };
0254 
0255 
0256   } // chrono
0257 
0258 } // boost
0259 
0260 #endif  // header