Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75281 - in trunk/boost/chrono/io: . utility
From: vicente.botet_at_[hidden]
Date: 2011-11-02 17:46:49


Author: viboes
Date: 2011-11-02 17:46:47 EDT (Wed, 02 Nov 2011)
New Revision: 75281
URL: http://svn.boost.org/trac/boost/changeset/75281

Log:
Chrono: Added internal manip utility + adaptation, rename internam time_info by ios_base_data
Added:
   trunk/boost/chrono/io/utility/
   trunk/boost/chrono/io/utility/manip_base.hpp (contents, props changed)
Text files modified:
   trunk/boost/chrono/io/duration_io.hpp | 48 +++++++++++++++++++++++++-
   trunk/boost/chrono/io/ios_base_state.hpp | 50 +++++++++++++++------------
   trunk/boost/chrono/io/time_point_io.hpp | 72 +++++++++++++++++++++++++++++++++++++--
   3 files changed, 142 insertions(+), 28 deletions(-)

Modified: trunk/boost/chrono/io/duration_io.hpp
==============================================================================
--- trunk/boost/chrono/io/duration_io.hpp (original)
+++ trunk/boost/chrono/io/duration_io.hpp 2011-11-02 17:46:47 EDT (Wed, 02 Nov 2011)
@@ -23,6 +23,7 @@
 #include <locale>
 #include <boost/chrono/io/duration_put.hpp>
 #include <boost/chrono/io/duration_get.hpp>
+#include <boost/chrono/io/utility/manip_base.hpp>
 namespace boost
 {
   namespace chrono
@@ -290,6 +291,47 @@
     }
 #endif
 
+#if 1
+ /**
+ * duration parameterized manipulator.
+ */
+
+ class duration_fmt : public manip<duration_fmt>
+ {
+ duration_style::type style_;
+ public:
+
+ /**
+ * explicit manipulator constructor from a @c duration_style
+ */
+ explicit duration_fmt(duration_style::type style) BOOST_NOEXCEPT
+ : style_(style)
+ {}
+
+ /**
+ * Change the duration_style ios state;
+ */
+ template <typename out_stream>
+ void operator()(out_stream &ios) const
+ //void operator()(std::ios_base &ios) const
+ {
+#if defined BOOST_CHRONO_IO_USE_XALLOC
+ set_duration_style(ios, style_);
+#else
+ typedef duration_punct<typename out_stream::char_type > Facet;
+ std::locale loc = ios.getloc();
+ if (std::has_facet<Facet>(loc))
+ {
+ const Facet& f = std::use_facet<Facet>(loc);
+ if (f.get_duration_style()!=style_)
+ ios.imbue(std::locale(loc, new Facet(style_, f)));
+ }
+ else
+ ios.imbue(std::locale(loc, new Facet(style_)));
+#endif
+ }
+ };
+#else
     /**
      * duration parameterized manipulator.
      */
@@ -301,7 +343,7 @@
       /**
        * explicit manipulator constructor from a @c duration_style
        */
- explicit duration_fmt(duration_style::type style)BOOST_NOEXCEPT
+ explicit duration_fmt(duration_style::type style) BOOST_NOEXCEPT
       : style_(style)
       {}
 
@@ -344,7 +386,6 @@
 #endif
       return os;
     }
-
     /**
      * Change the duration_punc facet associated to the input stream depending on the duration_format style parameter.
      */
@@ -369,6 +410,9 @@
       return is;
     }
 
+#endif
+
+
     /**
      * duration_style i/o saver.
      *

Modified: trunk/boost/chrono/io/ios_base_state.hpp
==============================================================================
--- trunk/boost/chrono/io/ios_base_state.hpp (original)
+++ trunk/boost/chrono/io/ios_base_state.hpp 2011-11-02 17:46:47 EDT (Wed, 02 Nov 2011)
@@ -72,37 +72,42 @@
     {
 
       template<typename CharT>
- class time_info
+ class ios_base_data
       {
       public:
 
- time_info(std::basic_string<CharT> fmt) :
- fmt_(fmt)
+ ios_base_data() :
+ time_fmt_(""),
+ duration_fmt_("")
         {
         }
 
- static inline std::basic_string<CharT> get_time_fmt(std::ios_base & ios)
+ static inline ios_base_data<CharT>& instance(std::ios_base & ios)
         {
           register_once(index(), ios);
           void* &pw = ios.pword(index());
           if (pw == 0)
           {
- return "";
+ pw = new ios_base_data<CharT>();
           }
- return static_cast<const time_info<CharT>*> (pw)->fmt_;
+ return *static_cast<ios_base_data<CharT>*> (pw);
         }
- static inline void set_time_fmt(std::ios_base& ios, std::basic_string<
- CharT> fmt)
- {
-
- register_once(index(), ios);
- void*& pw = ios.pword(index());
- if (pw != 0)
- {
- delete static_cast<time_info<CharT>*> (pw);
- }
- pw = new time_info(fmt);
 
+ inline std::basic_string<CharT> get_time_fmt()
+ {
+ return time_fmt_;
+ }
+ inline void set_time_fmt(std::basic_string<CharT> fmt)
+ {
+ time_fmt_=fmt;
+ }
+ inline std::basic_string<CharT> get_duration_fmt()
+ {
+ return duration_fmt_;
+ }
+ inline void set_duration_fmt(std::basic_string<CharT> fmt)
+ {
+ duration_fmt_=fmt;
         }
       private:
         static inline void callback(std::ios_base::event evt, std::ios_base& ios, int index)
@@ -114,7 +119,7 @@
             void*& pw = ios.pword(index);
             if (pw != 0)
             {
- time_info* tmi = static_cast<time_info<CharT>*> (pw);
+ ios_base_data* tmi = static_cast<ios_base_data<CharT>*> (pw);
               delete tmi;
               pw = 0;
             }
@@ -125,7 +130,7 @@
             void*& pw = ios.pword(index);
             if (pw != 0)
             {
- pw = new time_info(static_cast<time_info<CharT>*> (pw)->fmt_);
+ pw = new ios_base_data(*static_cast<ios_base_data<CharT>*> (pw));
             }
             break;
           }
@@ -149,7 +154,8 @@
           return v_;
         }
 
- std::basic_string<CharT> fmt_;
+ std::basic_string<CharT> time_fmt_;
+ std::basic_string<CharT> duration_fmt_;
 
       };
 
@@ -158,14 +164,14 @@
     template<typename CharT>
     static inline std::basic_string<CharT> get_time_fmt(std::ios_base & ios)
     {
- return detail::time_info<CharT>::get_time_fmt(ios);
+ return detail::ios_base_data<CharT>::instance(ios).get_time_fmt();
     }
     template<typename CharT>
     static inline void set_time_fmt(std::ios_base& ios, std::basic_string<
         CharT> fmt)
     {
 
- detail::time_info<CharT>::set_time_fmt(ios, fmt);
+ detail::ios_base_data<CharT>::instance(ios).set_time_fmt(fmt);
 
     }
   } // chrono

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-11-02 17:46:47 EDT (Wed, 02 Nov 2011)
@@ -22,15 +22,13 @@
 #include <boost/chrono/io/ios_base_state.hpp>
 #include <cstring>
 #include <string.h>
+#include <boost/chrono/io/utility/manip_base.hpp>
 
 namespace boost
 {
-
   namespace chrono
   {
 
-
-
 #if ! defined BOOST_CHRONO_IO_USE_XALLOC
 
     template<class CharT>
@@ -72,6 +70,71 @@
     std::locale::id time_punct<CharT>::id;
 #endif
 
+#if 1
+ namespace detail
+ {
+
+ template<class CharT>
+ class time_manip : public manip<time_manip<CharT> >
+ {
+ std::basic_string<CharT> fmt_;
+ timezone_type tz_;
+ public:
+
+ time_manip(timezone_type tz, std::basic_string<CharT> fmt)
+ // todo move semantics
+ : fmt_(fmt), tz_(tz)
+ {
+ }
+
+ /**
+ * Change the timezone_type and time format ios state;
+ */
+ template <typename out_stream>
+ void operator()(out_stream &ios) const
+ //void operator()(std::ios_base &ios) const
+ {
+ #if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ ios.imbue(std::locale(ios.getloc(), new time_punct<CharT> (tz_, fmt_)));
+ #else
+ set_time_fmt<CharT>(ios, fmt_);
+ set_timezone(ios, tz_);
+ #endif
+ }
+ };
+
+ class time_man : public manip<time_man>
+ {
+ timezone_type tz_;
+ public:
+
+ time_man(timezone_type tz)
+ // todo move semantics
+ : tz_(tz)
+ {
+ }
+
+ /**
+ * Change the timezone_type and time format ios state;
+ */
+ template <typename out_stream>
+ void operator()(out_stream &ios) const
+ //void operator()(std::ios_base &ios) const
+ {
+ #if ! defined BOOST_CHRONO_IO_USE_XALLOC
+ //ios.imbue(std::locale(ios.getloc(), new time_punct<typename out_stream::char_type> (tz_, std::basic_string<typename out_stream::char_type>())));
+ ios.imbue(std::locale(ios.getloc(), new time_punct<typename out_stream::char_type> (tz_, "")));
+ #else
+ //set_time_fmt<CharT>(ios, std::basic_string<typename out_stream::char_type>());
+ set_time_fmt<typename out_stream::char_type>(ios, "");
+ set_timezone(ios, tz_);
+ #endif
+ }
+ };
+
+ }
+
+#else
     namespace detail
     {
       template<class CharT>
@@ -140,7 +203,7 @@
         os.imbue(std::locale(os.getloc(), new time_punct<CharT> (static_cast<timezone_type> (m), std::basic_string<
                     CharT>())));
 #else
- set_time_fmt<CharT>(os, "");
+ set_time_fmt<CharT>(os, std::basic_string<CharT>());
         set_timezone(os, static_cast<timezone_type> (m));
 #endif
         return os;
@@ -161,6 +224,7 @@
       }
 
     }
+#endif
 
     template<class CharT>
     inline detail::time_manip<CharT> time_fmt(timezone_type tz, const CharT* fmt)

Added: trunk/boost/chrono/io/utility/manip_base.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/io/utility/manip_base.hpp 2011-11-02 17:46:47 EDT (Wed, 02 Nov 2011)
@@ -0,0 +1,85 @@
+// boost/chrono/utility/locale_facet_ptr.hpp ------------------------------------------------------------//
+
+// Copyright 2011 Vicente J. Botet Escriba
+
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+// See http://www.boost.org/libs/chrono for documentation.
+
+#ifndef BOOST_CHRONO_UTILITY_MANIP_BASE_PTR_HPP
+#define BOOST_CHRONO_UTILITY_MANIP_BASE_PTR_HPP
+
+#include <locale>
+#include <boost/assert.hpp>
+
+/**
+ *
+ * @Example
+ * @code
+
+ class mendl: public manip<mendl>
+ {
+ public:
+ explicit mendl(size_t how_many) :
+ count(how_many) {}
+ template <typename out_stream>
+ void operator()(out_stream &out) const
+ {
+ for (size_t line = 0; line < count; ++line)
+ {
+ out.put(out.widen('\n'));
+ }
+ ÊÊÊÊÊ out.flush();
+ }
+ private:
+ size_t count;
+ };
+
+ * @codeend
+ */
+
+namespace boost
+{
+ namespace chrono
+ {
+
+
+ /**
+ *
+ * @param os
+ * @param m
+ * @return
+ */
+
+ template <typename Final>
+ class manip
+ {
+ public:
+ template <typename out_stream>
+ void operator()(out_stream &out) const
+ {
+ (*static_cast<const Final *> (this))(out);
+ }
+ };
+
+ template <typename out_stream, typename manip_type>
+ out_stream &operator<<(out_stream &out, const manip<manip_type> &op)
+ {
+ if (out.good())
+ op(out);
+ return out;
+ }
+
+ template <typename in_stream, typename manip_type>
+ in_stream &operator>>(in_stream &in, const manip<manip_type> &op)
+ {
+ if (in.good())
+ op(in);
+ return in;
+ }
+
+ } // namespace chrono
+} // namespace boost
+
+#endif // header


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