Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75291 - in trunk/boost/chrono/io: . utility
From: vicente.botet_at_[hidden]
Date: 2011-11-03 15:40:28


Author: viboes
Date: 2011-11-03 15:40:26 EDT (Thu, 03 Nov 2011)
New Revision: 75291
URL: http://svn.boost.org/trac/boost/changeset/75291

Log:
Chrono: Added ios_base flasgs and state interal utilities and adapt the ios_base_state.hpp to them.
Added:
   trunk/boost/chrono/io/utility/ios_base_state_ptr.hpp (contents, props changed)
Text files modified:
   trunk/boost/chrono/io/ios_base_state.hpp | 108 ++++++++++++++++++++++++++++++++++-----
   trunk/boost/chrono/io/utility/manip_base.hpp | 55 +++++++++++++-------
   2 files changed, 128 insertions(+), 35 deletions(-)

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-03 15:40:26 EDT (Thu, 03 Nov 2011)
@@ -15,11 +15,52 @@
 #include <locale>
 #include <boost/chrono/io/duration_style.hpp>
 #include <boost/chrono/io/timezone.hpp>
+#include <boost/chrono/io/utility/ios_base_state_ptr.hpp>
 
 namespace boost
 {
   namespace chrono
   {
+
+ class fmt_masks : ios_base_flags<fmt_masks>
+ {
+ typedef ios_base_flags<fmt_masks> base_type;
+
+ public:
+ fmt_masks(std::ios_base& ios): base_type(ios) {}
+ enum type
+ {
+ uses_symbol = 1 << 0,
+ uses_local = 1 << 1
+ };
+
+ inline duration_style::type get_duration_style()
+ {
+ return (flags() & uses_symbol) ? duration_style::symbol : duration_style::prefix;
+ }
+ inline void set_duration_style(duration_style::type style)
+ {
+ if (style == duration_style::symbol)
+ setf(uses_symbol);
+ else
+ unsetf(uses_symbol);
+ }
+
+ inline timezone_type get_timezone()
+ {
+ return (flags() & uses_local) ? timezone::local : timezone::utc;
+ }
+ inline void set_timezone(timezone_type tz)
+ {
+ if (tz == timezone::local)
+ setf(uses_local);
+ else
+ unsetf(uses_local);
+ }
+ };
+
+
+#if 0
     namespace detail
     {
 
@@ -27,6 +68,7 @@
       {
         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();
@@ -43,34 +85,39 @@
         iw |= registerd_callback_mask;
       }
     }// detail
+#endif
 
     inline duration_style::type get_duration_style(std::ios_base & ios)
     {
- long iw = ios.iword(detail::chrono_io_masks_index());
- return (iw & detail::duration_style_mask) ? duration_style::symbol : duration_style::prefix;
+ return fmt_masks(ios).get_duration_style();
+ //long iw = ios.iword(detail::chrono_io_masks_index());
+ //return (iw & detail::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(detail::chrono_io_masks_index());
- iw &= ~detail::duration_style_mask;
- iw |= (style ? detail::duration_style_mask : 0);
+ fmt_masks(ios).set_duration_style(style);
+ //long& iw = ios.iword(detail::chrono_io_masks_index());
+ //iw &= ~detail::duration_style_mask;
+ //iw |= (style ? detail::duration_style_mask : 0);
     }
 
     inline timezone_type get_timezone(std::ios_base & ios)
     {
- long iw = ios.iword(detail::chrono_io_masks_index());
- return (iw & detail::timezone_mask) ? timezone::local : timezone::utc;
- }
- inline void set_timezone(std::ios_base& ios, timezone_type style)
- {
- long& iw = ios.iword(detail::chrono_io_masks_index());
- iw &= ~detail::timezone_mask;
- iw |= (style ? detail::timezone_mask : 0);
+ return fmt_masks(ios).get_timezone();
+ //long iw = ios.iword(detail::chrono_io_masks_index());
+ //return (iw & detail::timezone_mask) ? timezone::local : timezone::utc;
+ }
+ inline void set_timezone(std::ios_base& ios, timezone_type tz)
+ {
+ fmt_masks(ios).set_timezone(tz);
+ //long& iw = ios.iword(detail::chrono_io_masks_index());
+ //iw &= ~detail::timezone_mask;
+ //iw |= (style ? detail::timezone_mask : 0);
     }
 
     namespace detail
     {
-
+#if 0
       template<typename CharT>
       class ios_base_data
       {
@@ -158,9 +205,25 @@
         std::basic_string<CharT> duration_fmt_;
 
       };
+#else
+ template<typename CharT>
+ struct ios_base_data_aux
+ {
+ std::basic_string<CharT> time_fmt;
+ std::basic_string<CharT> duration_fmt;
+ public:
+
+ ios_base_data_aux() :
+ time_fmt(""),
+ duration_fmt("")
+ {
+ }
+ };
 
+#endif
     } // detail
 
+#if 0
     template<typename CharT>
     static inline std::basic_string<CharT> get_time_fmt(std::ios_base & ios)
     {
@@ -170,10 +233,23 @@
     static inline void set_time_fmt(std::ios_base& ios, std::basic_string<
         CharT> fmt)
     {
-
       detail::ios_base_data<CharT>::instance(ios).set_time_fmt(fmt);
-
     }
+#else
+ template<typename CharT>
+ static inline std::basic_string<CharT> get_time_fmt(std::ios_base & ios)
+ {
+ ios_base_state<detail::ios_base_data_aux<CharT> > ptr(ios);
+ return ptr->time_fmt;
+ }
+ template<typename CharT>
+ static inline void set_time_fmt(std::ios_base& ios, std::basic_string<
+ CharT> const& fmt)
+ {
+ ios_base_state<detail::ios_base_data_aux<CharT> > ptr(ios);
+ ptr->time_fmt = fmt;
+ }
+#endif
   } // chrono
 } // boost
 

Added: trunk/boost/chrono/io/utility/ios_base_state_ptr.hpp
==============================================================================
--- (empty file)
+++ trunk/boost/chrono/io/utility/ios_base_state_ptr.hpp 2011-11-03 15:40:26 EDT (Thu, 03 Nov 2011)
@@ -0,0 +1,289 @@
+// boost/chrono/utility/ios_base_pword_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_IOS_BASE_STATE_PTR_HPP
+#define BOOST_CHRONO_UTILITY_IOS_BASE_STATE_PTR_HPP
+
+#include <ios>
+#include <boost/assert.hpp>
+
+
+/**
+ *
+
+
+ */
+namespace boost
+{
+ namespace chrono
+ {
+
+ template<typename T>
+ class ios_base_state_ptr
+ {
+ public:
+ explicit ios_base_state_ptr(std::ios_base& ios) :
+ ios_(ios)
+ {
+ }
+ //ios_base_state_ptr(std::ios_base ios, void (*cleanup_function)(T*));
+ ~ios_base_state_ptr()
+ {
+ }
+
+ T const* get() const BOOST_NOEXCEPT
+ {
+ register_once(index(), ios_);
+ void* &pw = ios_.pword(index());
+ if (pw == 0)
+ {
+ return 0;
+ }
+ return static_cast<const T*> (pw);
+ }
+ T * get() BOOST_NOEXCEPT
+ {
+ register_once(index(), ios_);
+ void* &pw = ios_.pword(index());
+ if (pw == 0)
+ {
+ return 0;
+ }
+ return static_cast<T*> (pw);
+ }
+ T * operator->() BOOST_NOEXCEPT
+ {
+ return get();
+ }
+ T const * operator->() const BOOST_NOEXCEPT
+ {
+ return get();
+ }
+
+ T & operator*() BOOST_NOEXCEPT
+ {
+ return *get();
+ }
+ T const & operator *() const BOOST_NOEXCEPT
+ {
+ return *get();
+ }
+
+ T * release()BOOST_NOEXCEPT
+ {
+ T const* f = get();
+ reset();
+ return f;
+ }
+
+ void reset(T* new_value=0) BOOST_NOEXCEPT
+ {
+ register_once(index(), ios_);
+ void*& pw = ios_.pword(index());
+ if (pw != 0)
+ {
+ delete static_cast<T*> (pw);
+ }
+ pw = new_value;
+ }
+
+ //explicit
+ operator bool() const BOOST_NOEXCEPT
+ {
+ return get()!=0;
+ }
+
+ std::ios_base& getios() BOOST_NOEXCEPT
+ {
+ return ios_;
+ }
+ std::ios_base& getios() const BOOST_NOEXCEPT
+ {
+ return ios_;
+ }
+ operator std::ios_base&() BOOST_NOEXCEPT
+ {
+ return ios_;
+ }
+ operator std::ios_base&() const BOOST_NOEXCEPT
+ {
+ return ios_;
+ }
+ private:
+ static inline bool is_registerd(std::ios_base& ios)
+ {
+ long iw = ios.iword(index());
+ return (iw == 1);
+ }
+ static inline void set_registered(std::ios_base& ios)
+ {
+ long& iw = ios.iword(index());
+ iw = 1;
+ }
+ static 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)
+ {
+ T* ptr = static_cast<T*> (pw);
+ delete ptr;
+ pw = 0;
+ }
+ break;
+ }
+ case std::ios_base::copyfmt_event:
+ {
+ void*& pw = ios.pword(index);
+ if (pw != 0)
+ {
+ pw = new T(*static_cast<T*> (pw));
+ }
+ break;
+ }
+ default:
+ break;
+ }
+ }
+
+ static inline int index()
+ {
+ static const int v_ = std::ios_base::xalloc();
+ return v_;
+ }
+
+ static inline void register_once(int indx, std::ios_base& ios)
+ {
+ // needs a mask registered
+ if (!is_registerd(ios))
+ {
+ set_registered(ios);
+ ios.register_callback(callback, indx);
+ }
+ }
+
+
+ protected:
+ std::ios_base& ios_;
+ };
+
+ /**
+ *
+
+
+ */
+ template<typename T>
+ class ios_base_state : public ios_base_state_ptr<T>
+ {
+ typedef ios_base_state_ptr<T> base_type;
+ public:
+ explicit ios_base_state(std::ios_base& ios) :
+ ios_base_state_ptr<T>(ios)
+ {
+ if (this->get()==0)
+ {
+ this->base_type::reset(new T());
+ }
+ }
+ //ios_base_state(std::ios_base ios, void (*cleanup_function)(T*));
+ ~ios_base_state()
+ {
+ }
+
+ void reset(T* new_value)BOOST_NOEXCEPT
+ {
+ BOOST_ASSERT(new_value!=0);
+ this->base_type::reset(new_value);
+ }
+
+ };
+
+
+/**
+ *
+ *
+ *
+ */
+ template<typename Base>
+ class ios_base_flags
+ {
+ public:
+ explicit ios_base_flags(std::ios_base& ios) :
+ ios_(ios)
+ {
+ }
+ //ios_base_state_ptr(std::ios_base ios, void (*cleanup_function)(T*));
+ ~ios_base_flags()
+ {
+ }
+
+ long flags() const BOOST_NOEXCEPT
+ {
+ return get();
+ }
+ long flags(long v) BOOST_NOEXCEPT
+ {
+ long tmp = flags();
+ ref() = v;
+ return tmp;
+ }
+
+ long setf(long v)
+ {
+ long tmp = get();
+ ref() |= v;
+ return tmp;
+ }
+
+ void unsetf(long mask)
+ {
+ ref() &= ~mask;
+ }
+
+ long setf(long v, long mask)
+ {
+ long tmp = get();
+ unsetf(mask);
+ ref() |= v & mask;
+ return tmp;
+ }
+
+ operator std::ios_base&() BOOST_NOEXCEPT
+ {
+ return ios_;
+ }
+ operator std::ios_base const&() const BOOST_NOEXCEPT
+ {
+ return ios_;
+ }
+ private:
+ long get() const BOOST_NOEXCEPT
+ {
+ return ios_.iword(index());
+ }
+ long& ref() BOOST_NOEXCEPT
+ {
+ return ios_.iword(index());
+ }
+ static inline int index()
+ {
+ static const int v_ = std::ios_base::xalloc();
+ return v_;
+ }
+
+ std::ios_base& ios_;
+ };
+
+ } // namespace chrono
+} // namespace boost
+
+#endif // header

Modified: trunk/boost/chrono/io/utility/manip_base.hpp
==============================================================================
--- trunk/boost/chrono/io/utility/manip_base.hpp (original)
+++ trunk/boost/chrono/io/utility/manip_base.hpp 2011-11-03 15:40:26 EDT (Thu, 03 Nov 2011)
@@ -15,8 +15,20 @@
 
 /**
  *
- * @Example
- * @code
+
+ */
+
+namespace boost
+{
+ namespace chrono
+ {
+
+ /**
+ * manip is a manipulator mixin class following the CRTP.
+ * @tparam Final the derived from manip and final type
+ *
+ * @Example
+ * @code
 
     class mendl: public manip<mendl>
     {
@@ -36,33 +48,31 @@
       size_t count;
     };
 
- * @codeend
- */
-
-namespace boost
-{
- namespace chrono
- {
-
-
- /**
- *
- * @param os
- * @param m
- * @return
+ * @codeend
      */
-
     template <typename Final>
     class manip
     {
     public:
+ /**
+ *
+ * @param ios the io stream or ios_base.
+ * @Effects calls to the manipulator final functor.
+ */
       template <typename out_stream>
- void operator()(out_stream &out) const
+ void operator()(out_stream &ios) const
       {
- (*static_cast<const Final *> (this))(out);
+ (*static_cast<const Final *> (this))(ios);
       }
     };
 
+ /**
+ * @c manip stream inserter
+ * @param out the io stream or ios_base.
+ * @param op the manipulator instance.
+ * @Effects if @c out is good calls to the manipulator functor @op.
+ * @return @c out
+ */
     template <typename out_stream, typename manip_type>
     out_stream &operator<<(out_stream &out, const manip<manip_type> &op)
     {
@@ -71,6 +81,13 @@
       return out;
     }
 
+ /**
+ * @c manip stream extractor
+ * @param in the io stream or ios_base.
+ * @param op the manipulator instance.
+ * @Effects if @c in is good calls to the manipulator functor @op.
+ * @return @c in
+ */
     template <typename in_stream, typename manip_type>
     in_stream &operator>>(in_stream &in, const manip<manip_type> &op)
     {


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