Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r52432 - sandbox/synchro/boost/conversion
From: vicente.botet_at_[hidden]
Date: 2009-04-16 16:07:47


Author: viboes
Date: 2009-04-16 16:07:45 EDT (Thu, 16 Apr 2009)
New Revision: 52432
URL: http://svn.boost.org/trac/boost/changeset/52432

Log:
0.3.1 : Add test and doc to conversion

Added:
   sandbox/synchro/boost/conversion/
   sandbox/synchro/boost/conversion/chrono_duration_to_posix_time_duration.hpp (contents, props changed)
   sandbox/synchro/boost/conversion/chrono_time_point_to_posix_time_ptime.hpp (contents, props changed)
   sandbox/synchro/boost/conversion/convert_to.hpp (contents, props changed)

Added: sandbox/synchro/boost/conversion/chrono_duration_to_posix_time_duration.hpp
==============================================================================
--- (empty file)
+++ sandbox/synchro/boost/conversion/chrono_duration_to_posix_time_duration.hpp 2009-04-16 16:07:45 EDT (Thu, 16 Apr 2009)
@@ -0,0 +1,67 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2009. 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/synchro for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_CONVERT_TO_CHRONO_DURATION_TO_POSIX_TIME_DURATION__HPP
+#define BOOST_CONVERT_TO_CHRONO_DURATION_TO_POSIX_TIME_DURATION__HPP
+
+#include <boost/chrono/chrono.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
+#include <boost/conversion/convert_to.hpp>
+
+namespace boost {
+
+ namespace partial_specialization_workaround {
+ template < class Rep, class Period>
+ struct convert_to<posix_time::time_duration, chrono::duration<Rep, Period> > {
+ inline static posix_time::time_duration apply(chrono::duration<Rep, Period> const & from)
+ {
+ typedef chrono::duration<Rep, Period> src_duration_t;
+ typedef chrono::nanoseconds duration_t;
+ typedef duration_t::rep rep_t;
+ rep_t d = chrono::duration_cast<duration_t>(from).count();
+ rep_t sec = d/1000000000;
+ rep_t nsec = d%1000000000;
+ return boost::posix_time::seconds(static_cast<long>(sec))+
+#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
+ boost::posix_time::nanoseconds(nsec);
+#else
+ boost::posix_time::microseconds((nsec+500)/1000);
+#endif
+ }
+ };
+ template < class Rep, class Period>
+ struct assign_to<posix_time::time_duration, chrono::duration<Rep, Period> > {
+ inline static posix_time::time_duration& apply(const chrono::duration<Rep, Period>& from, posix_time::time_duration& to)
+ {
+ to = boost::convert_to<posix_time::time_duration>(from);
+ return to;
+ }
+ };
+
+ template < class Rep, class Period>
+ struct convert_to<chrono::duration<Rep, Period>, posix_time::time_duration> {
+ inline static chrono::duration<Rep, Period> apply(posix_time::time_duration const & from)
+ {
+ return chrono::nanoseconds(from.total_nanoseconds());
+ }
+ };
+ template < class Rep, class Period>
+ struct assign_to<chrono::duration<Rep, Period>, posix_time::time_duration> {
+ inline static chrono::duration<Rep, Period> & apply(const posix_time::time_duration& from, chrono::duration<Rep, Period> & to)
+ {
+ to = boost::convert_to<chrono::duration<Rep, Period> >(from);
+ return to;
+ }
+ };
+ }
+}
+
+#endif
+

Added: sandbox/synchro/boost/conversion/chrono_time_point_to_posix_time_ptime.hpp
==============================================================================
--- (empty file)
+++ sandbox/synchro/boost/conversion/chrono_time_point_to_posix_time_ptime.hpp 2009-04-16 16:07:45 EDT (Thu, 16 Apr 2009)
@@ -0,0 +1,73 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2009. 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/synchro for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+//[CHRONO_TIME_POINT_TO_POSIX_TIME_PTIME__HPP
+#ifndef BOOST_CONVERT_TO_CHRONO_TIME_POINT_TO_POSIX_TIME_PTIME__HPP
+#define BOOST_CONVERT_TO_CHRONO_TIME_POINT_TO_POSIX_TIME_PTIME__HPP
+
+#include <boost/chrono/chrono.hpp>
+#include <boost/date_time/posix_time/posix_time_types.hpp>
+#include <boost/conversion/convert_to.hpp>
+
+namespace boost {
+
+ namespace partial_specialization_workaround {
+ template < class Clock, class Duration>
+ struct convert_to<posix_time::ptime, chrono::time_point<Clock, Duration> > {
+ inline static posix_time::ptime apply(const chrono::time_point<Clock, Duration>& from)
+ {
+ typedef chrono::time_point<Clock, Duration> time_point_t;
+ typedef chrono::nanoseconds duration_t;
+ typedef duration_t::rep rep_t;
+ rep_t d = chrono::duration_cast<duration_t>(from.time_since_epoch()).count();
+ rep_t sec = d/1000000000;
+ rep_t nsec = d%1000000000;
+ return boost::posix_time::from_time_t(0)+
+ boost::posix_time::seconds(static_cast<long>(sec))+
+#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
+ boost::posix_time::nanoseconds(nsec);
+#else
+ boost::posix_time::microseconds((nsec+500)/1000);
+#endif
+ }
+ };
+ template < class Clock, class Duration>
+ struct assign_to<posix_time::ptime, chrono::time_point<Clock, Duration> > {
+ inline static posix_time::ptime& apply(const chrono::time_point<Clock, Duration>& from, posix_time::ptime& to)
+ {
+ to = boost::convert_to<posix_time::ptime>(from);
+ return to;
+ }
+ };
+
+ template < class Clock, class Duration>
+ struct convert_to<chrono::time_point<Clock, Duration>, posix_time::ptime> {
+ inline static chrono::time_point<Clock, Duration> apply(const posix_time::ptime& from)
+ {
+ boost::posix_time::time_duration const time_since_epoch=from-boost::posix_time::from_time_t(0);
+ chrono::time_point<Clock, Duration> t=chrono::system_clock::from_time_t(time_since_epoch.total_seconds());
+ long nsec=time_since_epoch.fractional_seconds()*(1000000000/time_since_epoch.ticks_per_second());
+ return t+chrono::nanoseconds(nsec);
+
+ }
+ };
+ template < class Clock, class Duration>
+ struct assign_to<chrono::time_point<Clock, Duration>, posix_time::ptime> {
+ inline static chrono::time_point<Clock, Duration>& apply(const posix_time::ptime& from, chrono::time_point<Clock, Duration>& to)
+ {
+ to = boost::convert_to<chrono::time_point<Clock, Duration> >(from);
+ return to;
+ }
+ };
+ }
+}
+
+#endif
+//]

Added: sandbox/synchro/boost/conversion/convert_to.hpp
==============================================================================
--- (empty file)
+++ sandbox/synchro/boost/conversion/convert_to.hpp 2009-04-16 16:07:45 EDT (Thu, 16 Apr 2009)
@@ -0,0 +1,78 @@
+//////////////////////////////////////////////////////////////////////////////
+//
+// (C) Copyright Vicente J. Botet Escriba 2009. 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/synchro for documentation.
+//
+//////////////////////////////////////////////////////////////////////////////
+
+#ifndef BOOST_CONVERT_TO__HPP
+#define BOOST_CONVERT_TO__HPP
+
+#include <cstddef> //for std::size_t
+
+namespace boost {
+
+ template < typename To, typename From >
+ To convert_to(const From& val);
+
+ namespace partial_specialization_workaround {
+ template < typename To, typename From >
+ struct convert_to {
+ inline static To apply(const From& val)
+ {
+ return To(val);
+ }
+ };
+ template < typename To, typename From >
+ struct assign_to {
+ inline static To& apply(const From& from, To& to)
+ {
+ to = from;
+ return to;
+ }
+ };
+ template < typename To, typename From, std::size_t N >
+ struct assign_to<To[N],From[N]> {
+ inline static To*& apply(const From(& from)[N], To (&to)[N])
+ {
+ for (std::size_t i = 0; i < N; ++i)
+ {
+ to[i] = boost::convert_to(from[i]);
+ }
+ return to;
+ }
+ };
+#if 0
+ template < typename To, typename From, std::size_t N >
+ struct convert_to<To[N],From[N]> {
+ inline static To[N] apply(const From (& val)[N])
+ {
+ To[N] to;
+ for (std::size_t i = 0; i < N; ++i)
+ {
+ to[i] = boost::convert(from[i]);
+ }
+ return to;
+ }
+ };
+#endif
+ }
+
+ template < typename To, typename From >
+ To convert_to(const From& val) {
+ return partial_specialization_workaround::convert_to<To,From>::apply(val);
+ }
+
+ template < typename To, typename From >
+ To& assign_to(const From& from, To& to) {
+ return partial_specialization_workaround::assign_to<To,From>::apply(from, to);
+ }
+
+
+}
+
+#endif
+


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