Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75155 - in trunk/boost/chrono: . io
From: vicente.botet_at_[hidden]
Date: 2011-10-28 10:48:31


Author: viboes
Date: 2011-10-28 10:48:30 EDT (Fri, 28 Oct 2011)
New Revision: 75155
URL: http://svn.boost.org/trac/boost/changeset/75155

Log:
Chrono: Towards the replacement of facet by xalloc
Text files modified:
   trunk/boost/chrono/config.hpp | 20 -----
   trunk/boost/chrono/io/duration_io.hpp | 135 +++++++++++++++++++++++++++++++++++----
   trunk/boost/chrono/io/time_point_io.hpp | 113 ++++++++++++++++++++++++++++++++
   3 files changed, 233 insertions(+), 35 deletions(-)

Modified: trunk/boost/chrono/config.hpp
==============================================================================
--- trunk/boost/chrono/config.hpp (original)
+++ trunk/boost/chrono/config.hpp 2011-10-28 10:48:30 EDT (Fri, 28 Oct 2011)
@@ -88,6 +88,9 @@
 #undef BOOST_CHRONO_THREAD_CLOCK_IS_STEADY
 #endif
 
+// deprecated i/o
+#define BOOST_CHRONO_IO_V1_DONT_PROVIDE_DEPRECATED
+#define BOOST_CHRONO_IO_USE_XALLOC
 
 // unicode support ------------------------------//
 
@@ -97,18 +100,6 @@
 #define BOOST_CHRONO_HAS_UNICODE_SUPPORT 1
 #endif
 
-//// define constexpr related macros ------------------------------//
-//
-//#if defined(BOOST_NO_CONSTEXPR)
-//#define BOOST_CHRONO_CONSTEXPR
-//#define BOOST_CHRONO_CONSTEXPR_OR_CONST const
-//#define BOOST_CHRONO_CONST_REF const&
-//#else
-//#define BOOST_CHRONO_CONSTEXPR constexpr
-//#define BOOST_CHRONO_CONSTEXPR_OR_CONST constexpr
-//#define BOOST_CHRONO_CONST_REF
-//#endif
-
 
 #if ! defined BOOST_NOEXCEPT
 #if defined(BOOST_NO_NOEXCEPT)
@@ -117,11 +108,6 @@
 #define BOOST_NOEXCEPT noexcept
 #endif
 #endif
-//
-//#undef BOOST_STATIC_CONSTEXPR
-//#define BOOST_STATIC_CONSTEXPR static BOOST_CHRONO_CONSTEXPR_OR_CONST
-
-//#define BOOST_CHRONO_STATIC_CONSTEXPR static BOOST_CHRONO_CONSTEXPR_OR_CONST
 
 
 #ifdef BOOST_CHRONO_HEADER_ONLY

Modified: trunk/boost/chrono/io/duration_io.hpp
==============================================================================
--- trunk/boost/chrono/io/duration_io.hpp (original)
+++ trunk/boost/chrono/io/duration_io.hpp 2011-10-28 10:48:30 EDT (Fri, 28 Oct 2011)
@@ -27,7 +27,6 @@
 {
   namespace chrono
   {
-
     /**
      * Scoped enumeration emulation stating whether the duration I/O style is long or short.
      * prefix_text means duration::rep with whatever stream/locale settings are set for it followed by a long name representing the unit
@@ -40,6 +39,34 @@
         prefix, symbol
       };
     };
+ namespace detail {
+
+ enum chrono_fmt_masks
+ {
+ duration_style_mask=1<<0, timezone_mask = 1<<1, registerd_callback_mask = 1<<2
+ };
+ inline int chrono_io_masks_index() {
+ static const int v_ = std::ios_base::xalloc();
+ return v_;
+ }
+ inline duration_style::type get_duration_style(std::ios_base & ios) {
+ long iw = ios.iword(chrono_io_masks_index());
+ return (iw & duration_style_mask) ? duration_style::symbol : duration_style::prefix;
+ }
+ inline void set_duration_style(std::ios_base& ios, duration_style::type style) {
+ long& iw = ios.iword(chrono_io_masks_index());
+ iw &= ~duration_style_mask;
+ iw |= (style?duration_style_mask:0) ;
+ }
+ inline bool is_registerd(std::ios_base & ios) {
+ long iw = ios.iword(chrono_io_masks_index());
+ return (iw & registerd_callback_mask) ;
+ }
+ inline void set_registered(std::ios_base& ios) {
+ long& iw = ios.iword(chrono_io_masks_index());
+ iw |= registerd_callback_mask ;
+ }
+ }
 
     template<class CharT>
     class duration_punct: public std::locale::facet
@@ -54,8 +81,9 @@
 #endif
 
     private:
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
       duration_style::type style_;
-
+#endif
 #if !defined BOOST_CHRONO_IO_V1_DONT_PROVIDE_DEPRECATED
       string_type long_seconds_;
       string_type long_minutes_;
@@ -111,31 +139,60 @@
       static std::locale::id id;
 
 #if !defined BOOST_CHRONO_IO_V1_DONT_PROVIDE_DEPRECATED
- explicit duration_punct(int use = duration_style::prefix, size_t refs = 0) :
- std::locale::facet(refs), style_(duration_style::type(use))
+ explicit duration_punct(
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ int use = duration_style::prefix,
+#endif
+ size_t refs = 0) :
+ std::locale::facet(refs)
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ , style_(duration_style::type(use))
+#endif
       {
         init_C();
       }
 
- duration_punct(int use, const string_type& long_seconds, const string_type& long_minutes, const string_type& long_hours, const string_type& short_seconds, const string_type& short_minutes, const string_type& short_hours, size_t refs =
+ duration_punct(
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ int use,
+#endif
+ const string_type& long_seconds, const string_type& long_minutes, const string_type& long_hours, const string_type& short_seconds, const string_type& short_minutes, const string_type& short_hours, size_t refs =
               0);
 
- duration_punct(int use, const duration_punct& d, size_t refs = 0);
+ duration_punct(
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ int use,
+#endif
+ const duration_punct& d, size_t refs = 0);
 
 #else
- explicit duration_punct(duration_style::type style= duration_style::prefix, size_t refs = 0) :
- std::locale::facet(refs), style_(style)
+ explicit duration_punct(
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ duration_style::type style= duration_style::prefix,
+#endif
+ size_t refs = 0) :
+ std::locale::facet(refs)
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ , style_(style)
+#endif
       {
       }
- duration_punct(duration_style::type style, const duration_punct&, size_t refs = 0) :
- std::locale::facet(refs), style_(style)
+ duration_punct(
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ duration_style::type style,
+#endif
+ const duration_punct&, size_t refs = 0) :
+ std::locale::facet(refs)
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ , style_(style)
+#endif
       {
       }
 #endif
 
 
 
-
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
       bool is_symbol() const BOOST_NOEXCEPT
       {
         return (style_==duration_style::symbol);
@@ -148,6 +205,7 @@
       {
         return style_;
       }
+#endif
 #if !defined BOOST_CHRONO_IO_V1_DONT_PROVIDE_DEPRECATED
 
       template<class Period>
@@ -215,8 +273,15 @@
 
 
     template<class CharT>
- duration_punct<CharT>::duration_punct(int use, const string_type& long_seconds, const string_type& long_minutes, const string_type& long_hours, const string_type& short_seconds, const string_type& short_minutes, const string_type& short_hours, size_t refs) :
- std::locale::facet(refs), style_(duration_style::type(use)),
+ duration_punct<CharT>::duration_punct(
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ int use,
+#endif
+ const string_type& long_seconds, const string_type& long_minutes, const string_type& long_hours, const string_type& short_seconds, const string_type& short_minutes, const string_type& short_hours, size_t refs) :
+ std::locale::facet(refs),
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ style_(duration_style::type(use)),
+#endif
           long_seconds_(long_seconds), long_minutes_(long_minutes),
           long_hours_(long_hours), short_seconds_(short_seconds),
           short_minutes_(short_minutes), short_hours_(short_hours)
@@ -224,8 +289,15 @@
     }
 
     template<class CharT>
- duration_punct<CharT>::duration_punct(int use, const duration_punct& d, size_t refs) :
- std::locale::facet(refs), style_(duration_style::type(use)),
+ duration_punct<CharT>::duration_punct(
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ int use,
+#endif
+ const duration_punct& d, size_t refs) :
+ std::locale::facet(refs),
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ style_(duration_style::type(use)),
+#endif
           long_seconds_(d.long_seconds_), long_minutes_(d.long_minutes_),
           long_hours_(d.long_hours_), short_seconds_(d.short_seconds_),
           short_minutes_(d.short_minutes_), short_hours_(d.short_hours_)
@@ -302,6 +374,9 @@
     std::basic_ostream<CharT, Traits>&
     operator <<(std::basic_ostream<CharT, Traits>& os, duration_fmt d)
     {
+#if defined BOOST_CHRONO_IO_USE_XALLOC
+ detail::set_duration_style(os, d.get_duration_style());
+#else
       typedef duration_punct<CharT> Facet;
       std::locale loc = os.getloc();
       if (std::has_facet<Facet>(loc))
@@ -311,6 +386,7 @@
           os.imbue(std::locale(loc, new Facet(d.get_duration_style(), f)));
       } else
         os.imbue(std::locale(loc, new Facet(d.get_duration_style())));
+#endif
       return os;
     }
 
@@ -321,6 +397,9 @@
     std::basic_istream<CharT, Traits>&
     operator >>(std::basic_istream<CharT, Traits>& is, duration_fmt d)
     {
+#if defined BOOST_CHRONO_IO_USE_XALLOC
+ detail::set_duration_style(is, d.get_duration_style());
+#else
       typedef duration_punct<CharT> Facet;
       std::locale loc = is.getloc();
       if (std::has_facet<Facet>(loc))
@@ -330,6 +409,7 @@
           is.imbue(std::locale(loc, new Facet(d.get_duration_style(), f)));
       } else
         is.imbue(std::locale(loc, new Facet(d.get_duration_style())));
+#endif
       return is;
     }
 
@@ -355,6 +435,10 @@
       explicit duration_style_io_saver(state_type &s) :
       s_save_(s)
       {
+#if defined BOOST_CHRONO_IO_USE_XALLOC
+ a_save_ = detail::get_duration_style(s_save_);
+
+#else
         typedef duration_punct<CharT> Facet;
         std::locale loc = s_save_.getloc();
         if (!std::has_facet<Facet>(loc))
@@ -362,6 +446,7 @@
 
         const Facet& f = std::use_facet<Facet>(s_save_.getloc());
         a_save_ = f.get_duration_style();
+#endif
       }
 
       /**
@@ -389,7 +474,11 @@
        */
       void restore()
       {
+#if defined BOOST_CHRONO_IO_USE_XALLOC
+ detail::set_duration_style(s_save_, a_save_);
+#else
         s_save_ << duration_fmt(a_save_);
+#endif
       }
     private:
       state_type& s_save_;
@@ -409,6 +498,19 @@
         bool failed = false;
         try
         {
+#if defined BOOST_CHRONO_IO_USE_XALLOC
+
+#if !defined BOOST_CHRONO_IO_V1_DONT_PROVIDE_DEPRECATED
+ std::locale loc = os.getloc();
+
+ if (!std::has_facet<Facet>(loc))
+ os.imbue(std::locale(loc, new Facet));
+ const Facet& f = std::use_facet<Facet>(os.getloc());
+ return os << d.count() << ' ' << f.template name<Rep,Period>(d);
+#else
+ return os << d.count() << ' ' << duration_unit<CharT>(detail::get_duration_style(os)==duration_style::prefix, d);
+#endif
+#else
           std::locale loc = os.getloc();
 
           if (!std::has_facet<Facet>(loc))
@@ -420,6 +522,7 @@
 #else
           return os << d.count() << ' ' << duration_unit<CharT>(f.is_prefix(), d);
 #endif
+#endif
         }
         catch (...)
         {
@@ -462,12 +565,14 @@
     std::basic_istream<CharT, Traits>&
     operator>>(std::basic_istream<CharT, Traits>& is, duration<Rep, Period>& d)
     {
+#if !defined BOOST_CHRONO_IO_V1_DONT_PROVIDE_DEPRECATED
       typedef duration_punct<CharT> Facet;
       std::locale loc = is.getloc();
       if (!std::has_facet<Facet>(loc))
         is.imbue(std::locale(loc, new Facet));
       loc = is.getloc();
       const Facet& f = std::use_facet<Facet>(loc);
+#endif
       typedef typename chrono_detail::duration_io_intermediate<Rep>::type intermediate_type;
       intermediate_type r;
       // read value into r

Modified: trunk/boost/chrono/io/time_point_io.hpp
==============================================================================
--- trunk/boost/chrono/io/time_point_io.hpp (original)
+++ trunk/boost/chrono/io/time_point_io.hpp 2011-10-28 10:48:30 EDT (Fri, 28 Oct 2011)
@@ -19,6 +19,7 @@
 #include <boost/chrono/round.hpp>
 #include <boost/chrono/io/translate.hpp>
 #include <boost/chrono/clock_string.hpp>
+#include <cstring>
 
 namespace boost
 {
@@ -33,7 +34,78 @@
         utc, local
       };
     };
+
     typedef timezone::type timezone_type;
+ namespace detail {
+
+ inline timezone_type get_timezone(std::ios_base & ios) {
+ long iw = ios.iword(chrono_io_masks_index());
+ return (iw & timezone_mask) ? timezone::local : timezone::utc;
+ }
+ inline void set_timezone(std::ios_base& ios, timezone_type style) {
+ long& iw = ios.iword(chrono_io_masks_index());
+ iw &= ~timezone_mask;
+ iw |= (style?timezone_mask:0);
+ }
+
+ inline void callback(std::ios_base::event evt, std::ios_base& ios, int index)
+ {
+ switch (evt) {
+ case std::ios_base::erase_event:
+ {
+ void*& pw = ios.pword(index);
+ if (pw!=0) {
+ free(pw);
+ pw = 0;
+ }
+ break;
+ }
+ case std::ios_base::copyfmt_event:
+ {
+ void*& pw = ios.pword(index);
+ if (pw!=0) {
+ pw = strdup(static_cast<const char*>(pw));
+ }
+ break;
+ }
+ default:
+ break;
+ }
+ }
+
+
+ inline void register_once(int index, std::ios_base& ios)
+ {
+ if (! detail::is_registerd(ios) )
+ {
+ detail::set_registered(ios);
+ ios.register_callback(detail::callback, index);
+ }
+ }
+
+ inline int chrono_io_time_fmt_index() {
+ static const int v_ = std::ios_base::xalloc();
+ return v_;
+ }
+ template <typename CharT>
+ inline const CharT* get_time_fmt(std::ios_base & ios) {
+ register_once(chrono_io_time_fmt_index(),ios);
+ void* &pw = ios.pword(chrono_io_time_fmt_index());
+ if(pw==0)
+ pw = strdup("");
+
+ return static_cast<const CharT*>(pw);
+ }
+ template <typename CharT>
+ inline void set_time_fmt(std::ios_base& ios, const CharT* fmt) {
+ register_once(chrono_io_time_fmt_index(),ios);
+ void*& pw = ios.pword(chrono_io_time_fmt_index());
+ if (pw!=0) free(pw);
+ pw = strdup(fmt);
+ }
+
+ }
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
 
     template<class CharT>
     class time_punct: public std::locale::facet
@@ -72,6 +144,7 @@
 
     template<class CharT>
     std::locale::id time_punct<CharT>::id;
+#endif
 
     namespace detail
     {
@@ -79,9 +152,9 @@
       struct time_manip
       {
         std::basic_string<CharT> fmt_;
- timezone tz_;
+ timezone_type tz_;
 
- time_manip(timezone tz, std::basic_string<CharT> fmt)
+ time_manip(timezone_type tz, std::basic_string<CharT> fmt)
         // todo move semantics
         :
           fmt_(fmt), tz_(tz)
@@ -95,7 +168,12 @@
       operator <<(std::basic_ostream<CharT, Traits>& os, time_manip<CharT> m)
       {
         // todo move semantics
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
         os.imbue(std::locale(os.getloc(), new time_punct<CharT> (m.tz_, m.fmt_)));
+#else
+ detail::set_time_fmt(os, m.fmt_.c_str());
+ detail::set_timezone(os, m.tz_);
+#endif
         return os;
       }
 
@@ -104,7 +182,12 @@
       operator >>(std::basic_istream<CharT, Traits>& is, time_manip<CharT> m)
       {
         // todo move semantics
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
         is.imbue(std::locale(is.getloc(), new time_punct<CharT> (m.tz_, m.fmt_)));
+#else
+ detail::set_time_fmt(is, m.fmt_.c_str());
+ detail::set_timezone(is, m.tz_);
+#endif
         return is;
       }
 
@@ -127,8 +210,13 @@
       std::basic_ostream<CharT, Traits>&
       operator <<(std::basic_ostream<CharT, Traits>& os, time_man m)
       {
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
         os.imbue(std::locale(os.getloc(), new time_punct<CharT> (static_cast<timezone_type> (m), std::basic_string<
             CharT>())));
+#else
+ detail::set_time_fmt(os, "");
+ detail::set_timezone(os, static_cast<timezone_type> (m));
+#endif
         return os;
       }
 
@@ -136,8 +224,13 @@
       std::basic_istream<CharT, Traits>&
       operator >>(std::basic_istream<CharT, Traits>& is, time_man m)
       {
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
         is.imbue(std::locale(is.getloc(), new time_punct<CharT> (static_cast<timezone_type> (m), std::basic_string<
             CharT>())));
+#else
+ detail::set_time_fmt(is, "");
+ detail::set_timezone(is, static_cast<timezone_type> (m));
+#endif
         return is;
       }
 
@@ -154,7 +247,7 @@
     template<class CharT>
     inline
     detail::time_manip<CharT>
- time_fmt(timezone tz, std::basic_string<CharT> fmt)
+ time_fmt(timezone_type tz, std::basic_string<CharT> fmt)
     {
       // todo move semantics
       return detail::time_manip<CharT>(tz, fmt);
@@ -404,6 +497,7 @@
         bool failed = false;
         try
         {
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
           const CharT* pb = 0; //nullptr;
           const CharT* pe = pb;
           timezone_type tz = timezone::utc;
@@ -416,6 +510,12 @@
             pe = pb + f.fmt().size();
             tz = f.get_timezone();
           }
+#else
+ const CharT* pb = detail::get_time_fmt<CharT>(os);
+ const CharT* pe = pb+strlen(pb);
+ timezone_type tz = detail::get_timezone(os);
+ std::locale loc = os.getloc();
+#endif
           time_t t = system_clock::to_time_t(tp);
           tm tm;
           if (tz == timezone::local)
@@ -561,6 +661,7 @@
         std::ios_base::iostate err = std::ios_base::goodbit;
         try
         {
+#if ! defined BOOST_CHRONO_IO_USE_XALLOC
           const CharT* pb = 0;//nullptr;
           const CharT* pe = pb;
           typedef time_punct<CharT> F;
@@ -573,6 +674,12 @@
             pe = pb + f.fmt().size();
             tz = f.timezone_type();
           }
+#else
+ const CharT* pb = detail::get_time_fmt<CharT>(is);
+ const CharT* pe = pb+strlen(pb);
+ timezone_type tz = detail::get_timezone(is);
+ std::locale loc = is.getloc();
+#endif
           const std::time_get<CharT>& tg = std::use_facet<
               std::time_get<CharT> >(loc);
           const std::ctype<CharT>& ct =


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk