diff options
Diffstat (limited to 'src/boost/libs/chrono/example')
33 files changed, 5204 insertions, 0 deletions
diff --git a/src/boost/libs/chrono/example/await_keystroke.cpp b/src/boost/libs/chrono/example/await_keystroke.cpp new file mode 100644 index 00000000..6e506243 --- /dev/null +++ b/src/boost/libs/chrono/example/await_keystroke.cpp @@ -0,0 +1,73 @@ +// await_keystroke.cpp -----------------------------------------------------// + +// Copyright Beman Dawes 2008 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +#define _CRT_SECURE_NO_WARNINGS + +#include <boost/chrono/chrono.hpp> +#include <iostream> +#include <iomanip> + +using namespace boost::chrono; + +template< class Clock > +class timer +{ + typename Clock::time_point start; +public: + + timer() : start( Clock::now() ) {} + + typename Clock::duration elapsed() const + { + return Clock::now() - start; + } + + double seconds() const + { + return elapsed().count() * ((double)Clock::period::num/Clock::period::den); + } +}; + +int main() +{ + timer<system_clock> t1; + timer<steady_clock> t2; + timer<high_resolution_clock> t3; + + std::cout << "Strike any key: "; + std::cin.get(); + + std::cout << std::fixed << std::setprecision(9); + std::cout << "system_clock-----------: " + << t1.seconds() << " seconds\n"; + std::cout << "steady_clock--------: " + << t2.seconds() << " seconds\n"; + std::cout << "high_resolution_clock--: " + << t3.seconds() << " seconds\n"; + + system_clock::time_point d4 = system_clock::now(); + system_clock::time_point d5 = system_clock::now(); + + std::cout << "\nsystem_clock latency-----------: " << (d5 - d4).count() << std::endl; + + steady_clock::time_point d6 = steady_clock::now(); + steady_clock::time_point d7 = steady_clock::now(); + + std::cout << "steady_clock latency--------: " << (d7 - d6).count() << std::endl; + + high_resolution_clock::time_point d8 = high_resolution_clock::now(); + high_resolution_clock::time_point d9 = high_resolution_clock::now(); + + std::cout << "high_resolution_clock latency--: " << (d9 - d8).count() << std::endl; + + std::time_t now = system_clock::to_time_t(system_clock::now()); + + std::cout << "\nsystem_clock::now() reports UTC is " + << std::asctime(std::gmtime(&now)) << "\n"; + + return 0; +} diff --git a/src/boost/libs/chrono/example/chrono_unit_test.cpp b/src/boost/libs/chrono/example/chrono_unit_test.cpp new file mode 100644 index 00000000..f06fca19 --- /dev/null +++ b/src/boost/libs/chrono/example/chrono_unit_test.cpp @@ -0,0 +1,32 @@ +// chrono_unit_test.cpp ----------------------------------------------------// + +// Copyright 2008 Beman Dawes + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness + +#include <boost/chrono/chrono.hpp> +#include <iostream> + + +int main() +{ + std::time_t sys_time + = boost::chrono::system_clock::to_time_t(boost::chrono::system_clock::now()); + + #ifdef UNDER_CE + // Windows CE does not define asctime() + struct tm * t = std::gmtime(&sys_time); + std::cout + << "system_clock::to_time_t(system_clock::now()) is " + << t->tm_mon << "/" << t->tm_mday << "/" << (1900 + t->tm_year) << " " << t->tm_hour << ":" << t->tm_min << ":" << t->tm_sec << std::endl; + #else + std::cout + << "system_clock::to_time_t(system_clock::now()) is " + << std::asctime(std::gmtime(&sys_time)) << std::endl; + #endif + + return 0; +} diff --git a/src/boost/libs/chrono/example/clock_name.cpp b/src/boost/libs/chrono/example/clock_name.cpp new file mode 100644 index 00000000..e10117d6 --- /dev/null +++ b/src/boost/libs/chrono/example/clock_name.cpp @@ -0,0 +1,20 @@ +// stopclock_perf.cpp ---------------------------------------------------// + +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +// See http://www.boost.org/libs/chrono for documentation. + +#include "clock_name.hpp" +#include <iostream> + +int main() +{ + std::cout << name<boost::chrono::system_clock>::apply() << '\n'; +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + std::cout << name<boost::chrono::steady_clock>::apply() << '\n'; +#endif + std::cout << name<boost::chrono::high_resolution_clock>::apply() << '\n'; +} diff --git a/src/boost/libs/chrono/example/clock_name.hpp b/src/boost/libs/chrono/example/clock_name.hpp new file mode 100644 index 00000000..74e8ff7c --- /dev/null +++ b/src/boost/libs/chrono/example/clock_name.hpp @@ -0,0 +1,68 @@ +// stopclock_perf.cpp ---------------------------------------------------// + +// Copyright 2009 Vicente J. Botet Escriba +// Copyright 2009 Howard Hinnant + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +// See http://www.boost.org/libs/chrono for documentation. + +#ifndef BOOST_CHRONO_CLOCK_NAME_HPP +#define BOOST_CHRONO_CLOCK_NAME_HPP + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits/is_same.hpp> + +template <typename Clock, + bool = boost::is_same<Clock, boost::chrono::system_clock>::value, +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + bool = boost::is_same<Clock, boost::chrono::steady_clock>::value, +#else + bool = false, +#endif + bool = boost::is_same<Clock, boost::chrono::high_resolution_clock>::value + > +struct name; + +template <typename Clock> +struct name<Clock, false, false, false> { + static const char* apply() { return "unknown clock";} +}; + +template <typename Clock> +struct name<Clock, true, false, false> { + static const char* apply() { return "system_clock";} +}; + +template <typename Clock> +struct name<Clock, false, true, false> { + static const char* apply() { return "steady_clock";} +}; + +template <typename Clock> +struct name<Clock, false, false, true> { + static const char* apply() { return "high_resolution_clock";} +}; + +template <typename Clock> +struct name<Clock, false, true, true> { + static const char* apply() { return "steady_clock and high_resolution_clock";} +}; + +template <typename Clock> +struct name<Clock, true, false, true> { + static const char* apply() { return "system_clock and high_resolution_clock";} +}; + +template <typename Clock> +struct name<Clock, true, true, false> { + static const char* apply() { return "system_clock and steady_clock";} +}; + +template <typename Clock> +struct name<Clock, true, true, true> { + static const char* apply() { return "system_clock, steady_clock and high_resolution_clock";} +}; + +#endif diff --git a/src/boost/libs/chrono/example/cycle_count.cpp b/src/boost/libs/chrono/example/cycle_count.cpp new file mode 100644 index 00000000..abae9c25 --- /dev/null +++ b/src/boost/libs/chrono/example/cycle_count.cpp @@ -0,0 +1,140 @@ +// cycle_count.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +using namespace boost::chrono; + + +template <long long speed> +struct cycle_count +{ + typedef typename boost::ratio_multiply<boost::ratio<speed>, boost::mega>::type frequency; // Mhz + typedef typename boost::ratio_divide<boost::ratio<1>, frequency>::type period; + typedef long long rep; + typedef boost::chrono::duration<rep, period> duration; + typedef boost::chrono::time_point<cycle_count> time_point; + + static time_point now() + { + static long long tick = 0; + // return exact cycle count + return time_point(duration(++tick)); // fake access to clock cycle count + } +}; + +template <long long speed> +struct approx_cycle_count +{ + static const long long frequency = speed * 1000000; // MHz + typedef nanoseconds duration; + typedef duration::rep rep; + typedef duration::period period; + static const long long nanosec_per_sec = period::den; + typedef boost::chrono::time_point<approx_cycle_count> time_point; + + static time_point now() + { + static long long tick = 0; + // return cycle count as an approximate number of nanoseconds + // compute as if nanoseconds is only duration in the std::lib + return time_point(duration(++tick * nanosec_per_sec / frequency)); + } +}; + +void cycle_count_delay() +{ + { + typedef cycle_count<400> clock; + std::cout << "\nSimulated " << clock::frequency::num / boost::mega::num << "MHz clock which has a tick period of " + << duration<double, boost::nano>(clock::duration(1)).count() << " nanoseconds\n"; + nanoseconds delayns(500); + clock::duration delay = duration_cast<clock::duration>(delayns); + std::cout << "delay = " << delayns.count() << " nanoseconds which is " << delay.count() << " cycles\n"; + clock::time_point start = clock::now(); + clock::time_point stop = start + delay; + while (clock::now() < stop) // no multiplies or divides in this loop + ; + clock::time_point end = clock::now(); + clock::duration elapsed = end - start; + std::cout << "paused " << elapsed.count() << " cycles "; + std::cout << "which is " << duration_cast<nanoseconds>(elapsed).count() << " nanoseconds\n"; + } + { + typedef approx_cycle_count<400> clock; + std::cout << "\nSimulated " << clock::frequency / 1000000 << "MHz clock modeled with nanoseconds\n"; + clock::duration delay = nanoseconds(500); + std::cout << "delay = " << delay.count() << " nanoseconds\n"; + clock::time_point start = clock::now(); + clock::time_point stop = start + delay; + while (clock::now() < stop) // 1 multiplication and 1 division in this loop + ; + clock::time_point end = clock::now(); + clock::duration elapsed = end - start; + std::cout << "paused " << elapsed.count() << " nanoseconds\n"; + } + { + typedef cycle_count<1500> clock; + std::cout << "\nSimulated " << clock::frequency::num / boost::mega::num << "MHz clock which has a tick period of " + << duration<double, boost::nano>(clock::duration(1)).count() << " nanoseconds\n"; + nanoseconds delayns(500); + clock::duration delay = duration_cast<clock::duration>(delayns); + std::cout << "delay = " << delayns.count() << " nanoseconds which is " << delay.count() << " cycles\n"; + clock::time_point start = clock::now(); + clock::time_point stop = start + delay; + while (clock::now() < stop) // no multiplies or divides in this loop + ; + clock::time_point end = clock::now(); + clock::duration elapsed = end - start; + std::cout << "paused " << elapsed.count() << " cycles "; + std::cout << "which is " << duration_cast<nanoseconds>(elapsed).count() << " nanoseconds\n"; + } + { + typedef approx_cycle_count<1500> clock; + std::cout << "\nSimulated " << clock::frequency / 1000000 << "MHz clock modeled with nanoseconds\n"; + clock::duration delay = nanoseconds(500); + std::cout << "delay = " << delay.count() << " nanoseconds\n"; + clock::time_point start = clock::now(); + clock::time_point stop = start + delay; + while (clock::now() < stop) // 1 multiplication and 1 division in this loop + ; + clock::time_point end = clock::now(); + clock::duration elapsed = end - start; + std::cout << "paused " << elapsed.count() << " nanoseconds\n"; + } +} + +int main() +{ + cycle_count_delay(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/explore_limits.cpp b/src/boost/libs/chrono/example/explore_limits.cpp new file mode 100644 index 00000000..730b193c --- /dev/null +++ b/src/boost/libs/chrono/example/explore_limits.cpp @@ -0,0 +1,60 @@ +// explore_limits.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +using namespace boost::chrono; + + +void explore_limits() +{ + typedef duration<long long, boost::ratio_multiply<boost::ratio<24*3652425,10000>, + hours::period>::type> Years; +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + steady_clock::time_point t1( Years(250)); + steady_clock::time_point t2(-Years(250)); +#else + system_clock::time_point t1( Years(250)); + system_clock::time_point t2(-Years(250)); +#endif + // nanosecond resolution is likely to overflow. "up cast" to microseconds. + // The "up cast" trades precision for range. + microseconds d = time_point_cast<microseconds>(t1) - time_point_cast<microseconds>(t2); + std::cout << d.count() << " microseconds\n"; +} + + +int main() +{ + explore_limits(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/french.cpp b/src/boost/libs/chrono/example/french.cpp new file mode 100644 index 00000000..f885a601 --- /dev/null +++ b/src/boost/libs/chrono/example/french.cpp @@ -0,0 +1,130 @@ +// french.cpp ----------------------------------------------------------// + +// Copyright 2010 Howard Hinnant +// Copyright 2011 Vicente J. Botet Escriba +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +// Adapted to Boost from the original Hawards's code + + +#include <boost/chrono/config.hpp> +#include <boost/chrono/chrono_io.hpp> +#include <boost/chrono/process_cpu_clocks.hpp> +#include <boost/chrono/thread_clock.hpp> +#include <iostream> +#include <locale> + + +#if BOOST_CHRONO_VERSION==2 +#include <boost/chrono/io/duration_units.hpp> + + using namespace boost; + using namespace boost::chrono; + + template <typename CharT=char> + class duration_units_fr: public duration_units_default<CharT> + { + public: + typedef CharT char_type; + + explicit duration_units_fr(size_t refs = 0) : + duration_units_default<CharT>(refs) + { + } + protected: + + using duration_units_default<CharT>::do_get_unit; + std::size_t do_get_plural_form(boost::int_least64_t value) const + { + return (value == -1 || value == 0 || value == 1) ? 0 : 1; + } + + std::basic_string<CharT> do_get_unit(duration_style style, ratio<1> , std::size_t pf) const + { + static const CharT t[] = + { 's' }; + static const std::basic_string<CharT> symbol(t, t + sizeof (t) / sizeof (t[0])); + static const CharT u[] = + { 's', 'e', 'c', 'o', 'n', 'd', 'e' }; + static const std::basic_string<CharT> singular(u, u + sizeof (u) / sizeof (u[0])); + static const CharT v[] = + { 's', 'e', 'c', 'o', 'n', 'd', 'e', 's' }; + static const std::basic_string<CharT> plural(v, v + sizeof (v) / sizeof (v[0])); + + if (style == duration_style::symbol) return symbol; + if (pf == 0) return singular; + if (pf == 1) return plural; + // assert + //throw "exception"; + return ""; + } + + std::basic_string<CharT> do_get_unit(duration_style style, ratio<60> , std::size_t pf) const + { + static const CharT t[] = + { 'm', 'i', 'n' }; + static const std::basic_string<CharT> symbol(t, t + sizeof (t) / sizeof (t[0])); + + static const CharT u[] = + { 'm', 'i', 'n', 'u', 't', 'e' }; + static const std::basic_string<CharT> singular(u, u + sizeof (u) / sizeof (u[0])); + static const CharT v[] = + { 'm', 'i', 'n', 'u', 't', 'e', 's' }; + static const std::basic_string<CharT> plural(v, v + sizeof (v) / sizeof (v[0])); + + if (style == duration_style::symbol) return symbol; + if (pf == 0) return singular; + if (pf == 1) return plural; + // assert + //throw "exception"; + return ""; + } + + std::basic_string<CharT> do_get_unit(duration_style style, ratio<3600> , std::size_t pf) const + { + static const CharT t[] = + { 'h' }; + static const std::basic_string<CharT> symbol(t, t + sizeof (t) / sizeof (t[0])); + static const CharT u[] = + { 'h', 'e', 'u', 'r', 'e' }; + static const std::basic_string<CharT> singular(u, u + sizeof (u) / sizeof (u[0])); + static const CharT v[] = + { 'h', 'e', 'u', 'r', 'e', 's' }; + static const std::basic_string<CharT> plural(v, v + sizeof (v) / sizeof (v[0])); + + if (style == duration_style::symbol) return symbol; + if (pf == 0) return singular; + if (pf == 1) return plural; + // assert + //throw "exception"; + return ""; + } + }; +#endif + +int main() +{ + using std::cout; + using std::locale; + using namespace boost; + using namespace boost::chrono; + +#if BOOST_CHRONO_VERSION==2 + cout.imbue(locale(locale(), new duration_units_fr<>())); +#else + cout.imbue(locale(locale(), new duration_punct<char> + ( + duration_punct<char>::use_long, + "secondes", "minutes", "heures", + "s", "m", "h" + ))); +#endif + hours h(5); + minutes m(45); + seconds s(15); + milliseconds ms(763); + cout << h << ", " << m << ", " << s << " et " << ms << '\n'; + cout << hours(0) << ", " << minutes(0) << ", " << s << " et " << ms << '\n'; + return 0; +} diff --git a/src/boost/libs/chrono/example/i_dont_like_the_default_duration_behavior.cpp b/src/boost/libs/chrono/example/i_dont_like_the_default_duration_behavior.cpp new file mode 100644 index 00000000..3fb4a90d --- /dev/null +++ b/src/boost/libs/chrono/example/i_dont_like_the_default_duration_behavior.cpp @@ -0,0 +1,97 @@ +// i_dont_like_the_default_duration_behavior.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +namespace I_dont_like_the_default_duration_behavior +{ + +// Here's how you override the duration's default constructor to do anything you want (in this case zero) + +template <class R> +class zero_default +{ +public: + typedef R rep; + +private: + rep rep_; +public: + zero_default(rep i = 0) : rep_(i) {} + operator rep() const {return rep_;} + + zero_default& operator+=(zero_default x) {rep_ += x.rep_; return *this;} + zero_default& operator-=(zero_default x) {rep_ -= x.rep_; return *this;} + zero_default& operator*=(zero_default x) {rep_ *= x.rep_; return *this;} + zero_default& operator/=(zero_default x) {rep_ /= x.rep_; return *this;} + + zero_default operator+ () const {return *this;} + zero_default operator- () const {return zero_default(-rep_);} + zero_default& operator++() {++rep_; return *this;} + zero_default operator++(int) {return zero_default(rep_++);} + zero_default& operator--() {--rep_; return *this;} + zero_default operator--(int) {return zero_default(rep_--);} + + friend zero_default operator+(zero_default x, zero_default y) {return x += y;} + friend zero_default operator-(zero_default x, zero_default y) {return x -= y;} + friend zero_default operator*(zero_default x, zero_default y) {return x *= y;} + friend zero_default operator/(zero_default x, zero_default y) {return x /= y;} + + friend bool operator==(zero_default x, zero_default y) {return x.rep_ == y.rep_;} + friend bool operator!=(zero_default x, zero_default y) {return !(x == y);} + friend bool operator< (zero_default x, zero_default y) {return x.rep_ < y.rep_;} + friend bool operator<=(zero_default x, zero_default y) {return !(y < x);} + friend bool operator> (zero_default x, zero_default y) {return y < x;} + friend bool operator>=(zero_default x, zero_default y) {return !(x < y);} +}; + +typedef boost::chrono::duration<zero_default<long long>, boost::nano > nanoseconds; +typedef boost::chrono::duration<zero_default<long long>, boost::micro > microseconds; +typedef boost::chrono::duration<zero_default<long long>, boost::milli > milliseconds; +typedef boost::chrono::duration<zero_default<long long> > seconds; +typedef boost::chrono::duration<zero_default<long long>, boost::ratio<60> > minutes; +typedef boost::chrono::duration<zero_default<long long>, boost::ratio<3600> > hours; + +void test() +{ + milliseconds ms; + std::cout << ms.count() << '\n'; +} + +} // I_dont_like_the_default_duration_behavior + + +int main() +{ + I_dont_like_the_default_duration_behavior::test(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/io_ex1.cpp b/src/boost/libs/chrono/example/io_ex1.cpp new file mode 100644 index 00000000..2e021dd3 --- /dev/null +++ b/src/boost/libs/chrono/example/io_ex1.cpp @@ -0,0 +1,87 @@ +// io_ex1.cpp ----------------------------------------------------------// + +// Copyright 2010 Howard Hinnant +// Copyright 2010 Vicente J. Botet Escriba +// Copyright (c) Microsoft Corporation 2014 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation. +Many thanks to Howard for making his code available under the Boost license. + +*/ + +#include <iostream> +#include <boost/chrono/config.hpp> +#include <boost/chrono/chrono_io.hpp> +#include <boost/chrono/system_clocks.hpp> +#include <boost/chrono/thread_clock.hpp> +#include <boost/chrono/process_cpu_clocks.hpp> + +int main() +{ + using std::cout; + using namespace boost; + using namespace boost::chrono; + + cout << "milliseconds(1) = " + << milliseconds(1) << '\n'; + cout << "milliseconds(3) + microseconds(10) = " + << milliseconds(3) + microseconds(10) << '\n'; + + cout << "hours(3) + minutes(10) = " + << hours(3) + minutes(10) << '\n'; + + typedef duration<long long, ratio<1, 2500000000ULL> > ClockTick; + cout << "ClockTick(3) + nanoseconds(10) = " + << ClockTick(3) + nanoseconds(10) << '\n'; + + cout << "\nSet cout to use short names:\n"; +#if BOOST_CHRONO_VERSION==2 + cout << duration_fmt(duration_style::symbol); +#else + cout << duration_short; +#endif + cout << "milliseconds(3) + microseconds(10) = " + << milliseconds(3) + microseconds(10) << '\n'; + + cout << "hours(3) + minutes(10) = " + << hours(3) + minutes(10) << '\n'; + + cout << "ClockTick(3) + nanoseconds(10) = " + << ClockTick(3) + nanoseconds(10) << '\n'; + + cout << "\nsystem_clock::now() = " << system_clock::now() << '\n'; +#if defined _MSC_VER && _MSC_VER == 1700 +#else +#if BOOST_CHRONO_VERSION==2 + cout << "\nsystem_clock::now() = " << time_fmt(chrono::timezone::local) << system_clock::now() << '\n'; + cout << "\nsystem_clock::now() = " << time_fmt(chrono::timezone::local,"%Y/%m/%d") << system_clock::now() << '\n'; +#endif +#endif + +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + cout << "steady_clock::now() = " << steady_clock::now() << '\n'; +#endif +#if BOOST_CHRONO_VERSION==2 + cout << "\nSet cout to use long names:\n" << duration_fmt(duration_style::prefix) + << "high_resolution_clock::now() = " << high_resolution_clock::now() << '\n'; +#else + cout << "\nSet cout to use long names:\n" << duration_long + << "high_resolution_clock::now() = " << high_resolution_clock::now() << '\n'; +#endif +#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK) + cout << "\nthread_clock::now() = " << thread_clock::now() << '\n'; +#endif +#if defined(BOOST_CHRONO_HAS_PROCESS_CLOCKS) + cout << "\nprocess_real_cpu_clock::now() = " << process_real_cpu_clock::now() << '\n'; +#if BOOST_PLAT_WINDOWS_DESKTOP + cout << "\nprocess_user_cpu_clock::now() = " << process_user_cpu_clock::now() << '\n'; + cout << "\nprocess_system_cpu_clock::now() = " << process_system_cpu_clock::now() << '\n'; + cout << "\nprocess_cpu_clock::now() = " << process_cpu_clock::now() << '\n'; +#endif +#endif + return 0; +} diff --git a/src/boost/libs/chrono/example/io_ex2.cpp b/src/boost/libs/chrono/example/io_ex2.cpp new file mode 100644 index 00000000..b67b4e14 --- /dev/null +++ b/src/boost/libs/chrono/example/io_ex2.cpp @@ -0,0 +1,36 @@ +// io_ex2.cpp ----------------------------------------------------------// + +// Copyright 2010 Howard Hinnant +// Copyright 2010 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation. +Many thanks to Howard for making his code available under the Boost license. + +*/ + +#include <boost/chrono/chrono_io.hpp> +#include <sstream> +#include <boost/assert.hpp> + +int main() +{ + using namespace boost::chrono; + + std::istringstream in("5000 milliseconds 4000 ms 3001 ms"); + seconds d(0); + in >> d; + BOOST_ASSERT(in.good()); + BOOST_ASSERT(d == seconds(5)); + in >> d; + BOOST_ASSERT(in.good()); + BOOST_ASSERT(d == seconds(4)); + in >> d; + BOOST_ASSERT(in.fail()); + BOOST_ASSERT(d == seconds(4)); + return 0; +} + diff --git a/src/boost/libs/chrono/example/io_ex3.cpp b/src/boost/libs/chrono/example/io_ex3.cpp new file mode 100644 index 00000000..c972df42 --- /dev/null +++ b/src/boost/libs/chrono/example/io_ex3.cpp @@ -0,0 +1,48 @@ +// io_ex1.cpp ----------------------------------------------------------// + +// Copyright 2010 Howard Hinnant +// Copyright 2010 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation. +Many thanks to Howard for making his code available under the Boost license. + +*/ + +#include <boost/chrono/chrono_io.hpp> +#include <sstream> +#include <iostream> +#include <boost/assert.hpp> + +int main() +{ + using namespace boost::chrono; + using std::cout; + + high_resolution_clock::time_point t0 = high_resolution_clock::now(); + std::stringstream io; + io << t0; + BOOST_ASSERT(!io.fail()); + cout << io.str() << '\n'; + BOOST_ASSERT(!io.fail()); + high_resolution_clock::time_point t1; + io >> t1; + BOOST_ASSERT(!io.fail()); + cout << io.str() << '\n'; + cout << t0 << '\n'; + cout << t1 << '\n'; + high_resolution_clock::time_point t = high_resolution_clock::now(); + cout << t << '\n'; + + cout << "That took " << t - t0 << '\n'; + cout << "That took " << t - t1 << '\n'; + + return 0; +} + +//~ 50908679121461 nanoseconds since boot +//~ That took 649630 nanoseconds + diff --git a/src/boost/libs/chrono/example/io_ex4.cpp b/src/boost/libs/chrono/example/io_ex4.cpp new file mode 100644 index 00000000..04856ed7 --- /dev/null +++ b/src/boost/libs/chrono/example/io_ex4.cpp @@ -0,0 +1,32 @@ +// io_ex1.cpp ----------------------------------------------------------// + +// Copyright 2010 Howard Hinnant +// Copyright 2010 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation. +Many thanks to Howard for making his code available under the Boost license. + +*/ + +#include <boost/chrono/chrono_io.hpp> +#include <iostream> + +int main() +{ + using std::cout; + using namespace boost; + using namespace boost::chrono; + +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + typedef time_point<steady_clock, duration<double, ratio<3600> > > T; + T tp = steady_clock::now(); + std::cout << tp << '\n'; +#endif + return 0; +} + +//~ 17.8666 hours since boot diff --git a/src/boost/libs/chrono/example/io_ex5.cpp b/src/boost/libs/chrono/example/io_ex5.cpp new file mode 100644 index 00000000..4bd5cbb4 --- /dev/null +++ b/src/boost/libs/chrono/example/io_ex5.cpp @@ -0,0 +1,90 @@ +// io_ex1.cpp ----------------------------------------------------------// + +// Copyright 2010 Howard Hinnant +// Copyright 2010 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was adapted by Vicente J. Botet Escriba from Hinnant's html documentation. +Many thanks to Howard for making his code available under the Boost license. + +*/ + +#include <boost/chrono/chrono_io.hpp> +#include <ostream> +#include <iostream> + +// format duration as [-]d/hh::mm::ss.cc +template <class CharT, class Traits, class Rep, class Period> +std::basic_ostream<CharT, Traits>& +display(std::basic_ostream<CharT, Traits>& os, + boost::chrono::duration<Rep, Period> d) +{ + using std::cout; + using namespace boost; + using namespace boost::chrono; + + typedef duration<long long, ratio<86400> > days; + typedef duration<long long, centi> centiseconds; + + // if negative, print negative sign and negate + if (d < duration<Rep, Period>(0)) + { + d = -d; + os << '-'; + } + // round d to nearest centiseconds, to even on tie + centiseconds cs = duration_cast<centiseconds>(d); + if (d - cs > milliseconds(5) + || (d - cs == milliseconds(5) && cs.count() & 1)) + ++cs; + // separate seconds from centiseconds + seconds s = duration_cast<seconds>(cs); + cs -= s; + // separate minutes from seconds + minutes m = duration_cast<minutes>(s); + s -= m; + // separate hours from minutes + hours h = duration_cast<hours>(m); + m -= h; + // separate days from hours + days dy = duration_cast<days>(h); + h -= dy; + // print d/hh:mm:ss.cc + os << dy.count() << '/'; + if (h < hours(10)) + os << '0'; + os << h.count() << ':'; + if (m < minutes(10)) + os << '0'; + os << m.count() << ':'; + if (s < seconds(10)) + os << '0'; + os << s.count() << '.'; + if (cs < centiseconds(10)) + os << '0'; + os << cs.count(); + return os; +} + +int main() +{ + using std::cout; + using namespace boost; + using namespace boost::chrono; + +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + display(cout, steady_clock::now().time_since_epoch() + + duration<long, mega>(1)) << '\n'; +#endif + display(cout, -milliseconds(6)) << '\n'; + display(cout, duration<long, mega>(1)) << '\n'; + display(cout, -duration<long, mega>(1)) << '\n'; +} + +//~ 12/06:03:22.95 +//~ -0/00:00:00.01 +//~ 11/13:46:40.00 +//~ -11/13:46:40.00 diff --git a/src/boost/libs/chrono/example/manipulate_clock_object.cpp b/src/boost/libs/chrono/example/manipulate_clock_object.cpp new file mode 100644 index 00000000..96d89541 --- /dev/null +++ b/src/boost/libs/chrono/example/manipulate_clock_object.cpp @@ -0,0 +1,61 @@ +// manipulate_clock_object.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +using namespace boost::chrono; + +#if defined _MSC_VER +#pragma warning(push) +#pragma warning(disable:4100) +#endif +void manipulate_clock_object(system_clock clock) +#if defined _MSC_VER +#pragma warning(pop) +#endif +{ + system_clock::duration delay = milliseconds(5); + system_clock::time_point start = clock.now(); + + while ((clock.now() - start) <= delay) {} + + system_clock::time_point stop = clock.now(); + system_clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; +} + + +int main() +{ + manipulate_clock_object(system_clock()); + return 0; +} + diff --git a/src/boost/libs/chrono/example/min_time_point.cpp b/src/boost/libs/chrono/example/min_time_point.cpp new file mode 100644 index 00000000..91a4b72b --- /dev/null +++ b/src/boost/libs/chrono/example/min_time_point.cpp @@ -0,0 +1,79 @@ +// min_time_point.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/chrono/typeof/boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +using namespace boost::chrono; + +template <class Rep, class Period> +void print_duration(std::ostream& os, duration<Rep, Period> d) +{ + os << d.count() << " * " << Period::num << '/' << Period::den << " seconds\n"; +} + +namespace my_ns { +// Example min utility: returns the earliest time_point +// Being able to *easily* write this function is a major feature! +template <class Clock, class Duration1, class Duration2> +inline +typename boost::common_type<time_point<Clock, Duration1>, + time_point<Clock, Duration2> >::type +min BOOST_PREVENT_MACRO_SUBSTITUTION (time_point<Clock, Duration1> t1, time_point<Clock, Duration2> t2) +{ + return t2 < t1 ? t2 : t1; +} +} +void test_min() +{ +#if 1 + typedef time_point<system_clock, + boost::common_type<system_clock::duration, seconds>::type> T1; + typedef time_point<system_clock, + boost::common_type<system_clock::duration, nanoseconds>::type> T2; + typedef boost::common_type<T1, T2>::type T3; + /*auto*/ T1 t1 = system_clock::now() + seconds(3); + /*auto*/ T2 t2 = system_clock::now() + nanoseconds(3); + /*auto*/ T3 t3 = (my_ns::min)(t1, t2); +#else + BOOST_AUTO(t1, system_clock::now() + seconds(3)); + BOOST_AUTO(t2, system_clock::now() + nanoseconds(3)); + BOOST_AUTO(t3, (min)(t1, t2)); +#endif + print_duration(std::cout, t1 - t3); + print_duration(std::cout, t2 - t3); +} + +int main() +{ + test_min(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/miscellaneous.cpp b/src/boost/libs/chrono/example/miscellaneous.cpp new file mode 100644 index 00000000..414546f5 --- /dev/null +++ b/src/boost/libs/chrono/example/miscellaneous.cpp @@ -0,0 +1,156 @@ +// miscellaneous.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +// miscellaneous tests and demos: + +#include <cassert> +#include <iostream> + +using namespace boost::chrono; + +void physics_function(duration<double> d) +{ + std::cout << "d = " << d.count() << '\n'; +} + +void drive_physics_function() +{ + physics_function(nanoseconds(3)); + physics_function(hours(3)); + physics_function(duration<double>(2./3)); + std::cout.precision(16); + physics_function( hours(3) + nanoseconds(-3) ); +} + +void test_range() +{ + using namespace boost::chrono; + hours h1 = hours(24 * ( 365 * 292 + 292/4)); + nanoseconds n1 = h1 + nanoseconds(1); + nanoseconds delta = n1 - h1; + std::cout << "292 years of hours = " << h1.count() << "hr\n"; + std::cout << "Add a nanosecond = " << n1.count() << "ns\n"; + std::cout << "Find the difference = " << delta.count() << "ns\n"; +} + +void test_extended_range() +{ + using namespace boost::chrono; + hours h1 = hours(24 * ( 365 * 244000 + 244000/4)); + /*auto*/ microseconds u1 = h1 + microseconds(1); + /*auto*/ microseconds delta = u1 - h1; + std::cout << "244,000 years of hours = " << h1.count() << "hr\n"; + std::cout << "Add a microsecond = " << u1.count() << "us\n"; + std::cout << "Find the difference = " << delta.count() << "us\n"; +} + +template <class Rep, class Period> +void inspect_duration(boost::chrono::duration<Rep, Period> d, const std::string& name) +{ + typedef boost::chrono::duration<Rep, Period> Duration; + std::cout << "********* " << name << " *********\n"; + std::cout << "The period of " << name << " is " << (double)Period::num/Period::den << " seconds.\n"; + std::cout << "The frequency of " << name << " is " << (double)Period::den/Period::num << " Hz.\n"; + std::cout << "The representation is "; + if (boost::is_floating_point<Rep>::value) + { + std::cout << "floating point\n"; + std::cout << "The precision is the most significant "; + std::cout << std::numeric_limits<Rep>::digits10 << " decimal digits.\n"; + } + else if (boost::is_integral<Rep>::value) + { + std::cout << "integral\n"; + d = Duration(Rep(1)); + boost::chrono::duration<double> dsec = d; + std::cout << "The precision is " << dsec.count() << " seconds.\n"; + } + else + { + std::cout << "a class type\n"; + d = Duration(Rep(1)); + boost::chrono::duration<double> dsec = d; + std::cout << "The precision is " << dsec.count() << " seconds.\n"; + } + d = Duration((std::numeric_limits<Rep>::max)()); + using namespace boost::chrono; + typedef duration<double, boost::ratio_multiply<boost::ratio<24*3652425,10000>, hours::period>::type> Years; + Years years = d; + std::cout << "The range is +/- " << years.count() << " years.\n"; + std::cout << "sizeof(" << name << ") = " << sizeof(d) << '\n'; +} + +void inspect_all() +{ + using namespace boost::chrono; + std::cout.precision(6); + inspect_duration(nanoseconds(), "nanoseconds"); + inspect_duration(microseconds(), "microseconds"); + inspect_duration(milliseconds(), "milliseconds"); + inspect_duration(seconds(), "seconds"); + inspect_duration(minutes(), "minutes"); + inspect_duration(hours(), "hours"); + inspect_duration(duration<double>(), "duration<double>"); +} + +void test_milliseconds() +{ + using namespace boost::chrono; + milliseconds ms(250); + ms += milliseconds(1); + milliseconds ms2(150); + milliseconds msdiff = ms - ms2; + if (msdiff == milliseconds(101)) + std::cout << "success\n"; + else + std::cout << "failure: " << msdiff.count() << '\n'; +} + +int main() +{ + using namespace boost; + drive_physics_function(); + test_range(); + test_extended_range(); + inspect_all(); + test_milliseconds(); + inspect_duration(common_type<duration<double>, hours, microseconds>::type(), + "common_type<duration<double>, hours, microseconds>::type"); + duration<double, boost::milli> d = milliseconds(3) * 2.5; + inspect_duration(milliseconds(3) * 2.5, "milliseconds(3) * 2.5"); + std::cout << d.count() << '\n'; +// milliseconds ms(3.5); // doesn't compile +// std::cout << "milliseconds ms(3.5) doesn't compile\n"; + return 0; +} + diff --git a/src/boost/libs/chrono/example/rounding.cpp b/src/boost/libs/chrono/example/rounding.cpp new file mode 100644 index 00000000..31fa5956 --- /dev/null +++ b/src/boost/libs/chrono/example/rounding.cpp @@ -0,0 +1,44 @@ +// french.cpp ----------------------------------------------------------// + +// Copyright 2010 Howard Hinnant +// Copyright 2011 Vicente J. Botet Escriba +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +// Adapted to Boost from the original Hawards's code + +#include <iostream> +//#include <boost/chrono/chrono_io.hpp> +#include <boost/chrono/floor.hpp> +#include <boost/chrono/round.hpp> +#include <boost/chrono/ceil.hpp> + +int main() +{ + boost::chrono::milliseconds ms(2500); + std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; + std::cout << boost::chrono::floor<boost::chrono::seconds>(ms).count() + << " seconds\n"; + std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; + std::cout << boost::chrono::round<boost::chrono::seconds>(ms).count() + << " seconds\n"; + std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; + std::cout << boost::chrono::ceil<boost::chrono::seconds>(ms).count() + << " seconds\n"; + std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; + ms = boost::chrono::milliseconds(2516); + std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; + typedef boost::chrono::duration<long, boost::ratio<1, 30> > frame_rate; + std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; + std::cout << boost::chrono::floor<frame_rate>(ms).count() + << " [1/30] seconds\n"; + std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; + std::cout << boost::chrono::round<frame_rate>(ms).count() + << " [1/30] seconds\n"; + std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; + std::cout << boost::chrono::ceil<frame_rate>(ms).count() + << " [1/30] seconds\n"; + std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl; + + return 0; +} diff --git a/src/boost/libs/chrono/example/run_timer_example.cpp b/src/boost/libs/chrono/example/run_timer_example.cpp new file mode 100644 index 00000000..de1c318f --- /dev/null +++ b/src/boost/libs/chrono/example/run_timer_example.cpp @@ -0,0 +1,22 @@ +// run_timer_example.cpp ---------------------------------------------------// + +// Copyright Beman Dawes 2006, 2008 +// Copyright 2009/2010 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +// See http://www.boost.org/libs/chrono for documentation. + +#include <boost/chrono/process_times.hpp> +#include <cmath> + +int main() +{ + boost::chrono::run_timer t; + + for ( long i = 0; i < 10000; ++i ) + std::sqrt( 123.456L ); // burn some time + + return 0; +} diff --git a/src/boost/libs/chrono/example/run_timer_example2.cpp b/src/boost/libs/chrono/example/run_timer_example2.cpp new file mode 100644 index 00000000..52185971 --- /dev/null +++ b/src/boost/libs/chrono/example/run_timer_example2.cpp @@ -0,0 +1,24 @@ +// run_timer_example.cpp ---------------------------------------------------// + +// Copyright Beman Dawes 2006, 2008 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +// See http://www.boost.org/libs/chrono for documentation. + +#include <boost/chrono/process_times.hpp> +#include <cmath> + +int main( int argc, char * argv[] ) +{ + const char * format = argc > 1 ? argv[1] : "%t cpu seconds\n"; + int places = argc > 2 ? std::atoi( argv[2] ) : 2; + + boost::chrono::run_timer t( format, places ); + + for ( long i = 0; i < 10000; ++i ) + std::sqrt( 123.456L ); // burn some time + + return 0; +} diff --git a/src/boost/libs/chrono/example/runtime_resolution.cpp b/src/boost/libs/chrono/example/runtime_resolution.cpp new file mode 100644 index 00000000..29c41145 --- /dev/null +++ b/src/boost/libs/chrono/example/runtime_resolution.cpp @@ -0,0 +1,244 @@ +// runtime_resolution.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba +// Copyright (c) Microsoft Corporation 2014 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ +#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +#if defined(BOOST_CHRONO_MAC_API) +#include <sys/time.h> //for gettimeofday and timeval +#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t +#endif + +#if defined(BOOST_CHRONO_WINDOWS_API) +#include <windows.h> + +namespace +{ + #if defined UNDER_CE || BOOST_PLAT_WINDOWS_RUNTIME + // Windows CE and Windows store does not define timeval + struct timeval { + long tv_sec; /* seconds */ + long tv_usec; /* and microseconds */ + }; + #endif + + int gettimeofday(struct timeval * tp, void *) + { + FILETIME ft; + #if defined(UNDER_CE) + // Windows CE does not define GetSystemTimeAsFileTime so we do it in two steps. + SYSTEMTIME st; + ::GetSystemTime( &st ); + ::SystemTimeToFileTime( &st, &ft ); + #else + ::GetSystemTimeAsFileTime( &ft ); // never fails + #endif + long long t = (static_cast<long long>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + # if !defined( BOOST_MSVC ) || BOOST_MSVC > 1300 // > VC++ 7.0 + t -= 116444736000000000LL; + # else + t -= 116444736000000000; + # endif + t /= 10; // microseconds + tp->tv_sec = static_cast<long>( t / 1000000UL); + tp->tv_usec = static_cast<long>( t % 1000000UL); + return 0; + } +} // unnamed namespace + +#endif + +// Handle duration with resolution not known until run time +using namespace boost::chrono; + +namespace runtime_resolution +{ + +class duration +{ +public: + typedef long long rep; +private: + rep rep_; + + static const double ticks_per_nanosecond; + +public: + typedef boost::chrono::duration<double, boost::nano> tonanosec; + + duration() {} // = default; + explicit duration(const rep& r) : rep_(r) {} + + // conversions + explicit duration(const tonanosec& d) + : rep_(static_cast<rep>(d.count() * ticks_per_nanosecond)) {} + + // explicit + tonanosec convert_to_nanosec() const {return tonanosec(rep_/ticks_per_nanosecond);} + + // observer + + rep count() const {return rep_;} + + // arithmetic + + duration& operator+=(const duration& d) {rep_ += d.rep_; return *this;} + duration& operator-=(const duration& d) {rep_ += d.rep_; return *this;} + duration& operator*=(rep rhs) {rep_ *= rhs; return *this;} + duration& operator/=(rep rhs) {rep_ /= rhs; return *this;} + + duration operator+() const {return *this;} + duration operator-() const {return duration(-rep_);} + duration& operator++() {++rep_; return *this;} + duration operator++(int) {return duration(rep_++);} + duration& operator--() {--rep_; return *this;} + duration operator--(int) {return duration(rep_--);} + + friend duration operator+(duration x, duration y) {return x += y;} + friend duration operator-(duration x, duration y) {return x -= y;} + friend duration operator*(duration x, rep y) {return x *= y;} + friend duration operator*(rep x, duration y) {return y *= x;} + friend duration operator/(duration x, rep y) {return x /= y;} + + friend bool operator==(duration x, duration y) {return x.rep_ == y.rep_;} + friend bool operator!=(duration x, duration y) {return !(x == y);} + friend bool operator< (duration x, duration y) {return x.rep_ < y.rep_;} + friend bool operator<=(duration x, duration y) {return !(y < x);} + friend bool operator> (duration x, duration y) {return y < x;} + friend bool operator>=(duration x, duration y) {return !(x < y);} +}; + +static +double +init_duration() +{ +#if defined(BOOST_CHRONO_WINDOWS_API) + return static_cast<double>(1) / 1000; // Windows FILETIME is 1 per microsec +#elif defined(BOOST_CHRONO_MAC_API) + mach_timebase_info_data_t MachInfo; + mach_timebase_info(&MachInfo); + return static_cast<double>(MachInfo.denom) / MachInfo.numer; +#elif defined(BOOST_CHRONO_POSIX_API) + return static_cast<double>(1) / 1000; +#endif + +} + +const double duration::ticks_per_nanosecond = init_duration(); + +class clock; + +class time_point +{ +public: + typedef runtime_resolution::clock clock; + typedef long long rep; +private: + rep rep_; + + + rep count() const {return rep_;} +public: + + time_point() : rep_(0) {} + explicit time_point(const duration& d) + : rep_(d.count()) {} + + // arithmetic + + time_point& operator+=(const duration& d) {rep_ += d.count(); return *this;} + time_point& operator-=(const duration& d) {rep_ -= d.count(); return *this;} + + friend time_point operator+(time_point x, duration y) {return x += y;} + friend time_point operator+(duration x, time_point y) {return y += x;} + friend time_point operator-(time_point x, duration y) {return x -= y;} + friend duration operator-(time_point x, time_point y) {return duration(x.rep_ - y.rep_);} +}; + + +class clock +{ +public: + typedef runtime_resolution::duration::rep rep; + typedef runtime_resolution::duration duration; + typedef runtime_resolution::time_point time_point; + + static time_point now() + { + +#if defined(BOOST_CHRONO_WINDOWS_API) + timeval tv; + gettimeofday( &tv, 0 ); + return time_point(duration((static_cast<rep>(tv.tv_sec)<<32) | tv.tv_usec)); + +#elif defined(BOOST_CHRONO_MAC_API) + + timeval tv; + gettimeofday( &tv, 0 ); + return time_point(duration((static_cast<rep>(tv.tv_sec)<<32) | tv.tv_usec)); + +#elif defined(BOOST_CHRONO_POSIX_API) + timespec ts; + ::clock_gettime( CLOCK_REALTIME, &ts ); + + return time_point(duration((static_cast<rep>(ts.tv_sec)<<32) | ts.tv_nsec/1000)); + + +#endif // POSIX + + } +}; + +void test() +{ + std::cout << "runtime_resolution test\n"; + clock::duration delay(boost::chrono::milliseconds(5)); + clock::time_point start = clock::now(); + while (clock::now() - start <= delay) + ; + clock::time_point stop = clock::now(); + clock::duration elapsed = stop - start; + std::cout << "paused " << + boost::chrono::nanoseconds( + boost::chrono::duration_cast<boost::chrono::nanoseconds>( elapsed.convert_to_nanosec() )).count() + << " nanoseconds\n"; +} + +} // runtime_resolution + + +int main() +{ + runtime_resolution::test(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/saturating.cpp b/src/boost/libs/chrono/example/saturating.cpp new file mode 100644 index 00000000..b7c7ba1f --- /dev/null +++ b/src/boost/libs/chrono/example/saturating.cpp @@ -0,0 +1,505 @@ +// saturating.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +////////////////////////////////////////////////////////// +//////////////////// User2 Example /////////////////////// +////////////////////////////////////////////////////////// + +// Demonstrate User2: +// A "saturating" signed integral type is developed. This type has +/- infinity and a nan +// (like IEEE floating point) but otherwise obeys signed integral arithmetic. +// This class is subsequently used as the rep in boost::chrono::duration to demonstrate a +// duration class that does not silently ignore overflow. +#include <ostream> +#include <stdexcept> +#include <climits> + +namespace User2 +{ + +template <class I> +class saturate +{ +public: + typedef I int_type; + + static const int_type nan = int_type(int_type(1) << (sizeof(int_type) * CHAR_BIT - 1)); + static const int_type neg_inf = nan + 1; + static const int_type pos_inf = -neg_inf; +private: + int_type i_; + +// static_assert(std::is_integral<int_type>::value && std::is_signed<int_type>::value, +// "saturate only accepts signed integral types"); +// static_assert(nan == -nan && neg_inf < pos_inf, +// "saturate assumes two's complement hardware for signed integrals"); + +public: + saturate() : i_(nan) {} + explicit saturate(int_type i) : i_(i) {} + // explicit + operator int_type() const; + + saturate& operator+=(saturate x); + saturate& operator-=(saturate x) {return *this += -x;} + saturate& operator*=(saturate x); + saturate& operator/=(saturate x); + saturate& operator%=(saturate x); + + saturate operator- () const {return saturate(-i_);} + saturate& operator++() {*this += saturate(int_type(1)); return *this;} + saturate operator++(int) {saturate tmp(*this); ++(*this); return tmp;} + saturate& operator--() {*this -= saturate(int_type(1)); return *this;} + saturate operator--(int) {saturate tmp(*this); --(*this); return tmp;} + + friend saturate operator+(saturate x, saturate y) {return x += y;} + friend saturate operator-(saturate x, saturate y) {return x -= y;} + friend saturate operator*(saturate x, saturate y) {return x *= y;} + friend saturate operator/(saturate x, saturate y) {return x /= y;} + friend saturate operator%(saturate x, saturate y) {return x %= y;} + + friend bool operator==(saturate x, saturate y) + { + if (x.i_ == nan || y.i_ == nan) + return false; + return x.i_ == y.i_; + } + + friend bool operator!=(saturate x, saturate y) {return !(x == y);} + + friend bool operator<(saturate x, saturate y) + { + if (x.i_ == nan || y.i_ == nan) + return false; + return x.i_ < y.i_; + } + + friend bool operator<=(saturate x, saturate y) + { + if (x.i_ == nan || y.i_ == nan) + return false; + return x.i_ <= y.i_; + } + + friend bool operator>(saturate x, saturate y) + { + if (x.i_ == nan || y.i_ == nan) + return false; + return x.i_ > y.i_; + } + + friend bool operator>=(saturate x, saturate y) + { + if (x.i_ == nan || y.i_ == nan) + return false; + return x.i_ >= y.i_; + } + + friend std::ostream& operator<<(std::ostream& os, saturate s) + { + switch (s.i_) + { + case pos_inf: + return os << "inf"; + case nan: + return os << "nan"; + case neg_inf: + return os << "-inf"; + }; + return os << s.i_; + } +}; + +template <class I> +saturate<I>::operator I() const +{ + switch (i_) + { + case nan: + case neg_inf: + case pos_inf: + throw std::out_of_range("saturate special value can not convert to int_type"); + } + return i_; +} + +template <class I> +saturate<I>& +saturate<I>::operator+=(saturate x) +{ + switch (i_) + { + case pos_inf: + switch (x.i_) + { + case neg_inf: + case nan: + i_ = nan; + } + return *this; + case nan: + return *this; + case neg_inf: + switch (x.i_) + { + case pos_inf: + case nan: + i_ = nan; + } + return *this; + } + switch (x.i_) + { + case pos_inf: + case neg_inf: + case nan: + i_ = x.i_; + return *this; + } + if (x.i_ >= 0) + { + if (i_ < pos_inf - x.i_) + i_ += x.i_; + else + i_ = pos_inf; + return *this; + } + if (i_ > neg_inf - x.i_) + i_ += x.i_; + else + i_ = neg_inf; + return *this; +} + +template <class I> +saturate<I>& +saturate<I>::operator*=(saturate x) +{ + switch (i_) + { + case 0: + switch (x.i_) + { + case pos_inf: + case neg_inf: + case nan: + i_ = nan; + } + return *this; + case pos_inf: + switch (x.i_) + { + case nan: + case 0: + i_ = nan; + return *this; + } + if (x.i_ < 0) + i_ = neg_inf; + return *this; + case nan: + return *this; + case neg_inf: + switch (x.i_) + { + case nan: + case 0: + i_ = nan; + return *this; + } + if (x.i_ < 0) + i_ = pos_inf; + return *this; + } + switch (x.i_) + { + case 0: + i_ = 0; + return *this; + case nan: + i_ = nan; + return *this; + case pos_inf: + if (i_ < 0) + i_ = neg_inf; + else + i_ = pos_inf; + return *this; + case neg_inf: + if (i_ < 0) + i_ = pos_inf; + else + i_ = neg_inf; + return *this; + } + int s = (i_ < 0 ? -1 : 1) * (x.i_ < 0 ? -1 : 1); + i_ = i_ < 0 ? -i_ : i_; + int_type x_i_ = x.i_ < 0 ? -x.i_ : x.i_; + if (i_ <= pos_inf / x_i_) + i_ *= x_i_; + else + i_ = pos_inf; + i_ *= s; + return *this; +} + +template <class I> +saturate<I>& +saturate<I>::operator/=(saturate x) +{ + switch (x.i_) + { + case pos_inf: + case neg_inf: + switch (i_) + { + case pos_inf: + case neg_inf: + case nan: + i_ = nan; + break; + default: + i_ = 0; + break; + } + return *this; + case nan: + i_ = nan; + return *this; + case 0: + switch (i_) + { + case pos_inf: + case neg_inf: + case nan: + return *this; + case 0: + i_ = nan; + return *this; + } + if (i_ > 0) + i_ = pos_inf; + else + i_ = neg_inf; + return *this; + } + switch (i_) + { + case 0: + case nan: + return *this; + case pos_inf: + case neg_inf: + if (x.i_ < 0) + i_ = -i_; + return *this; + } + i_ /= x.i_; + return *this; +} + +template <class I> +saturate<I>& +saturate<I>::operator%=(saturate x) +{ +// *this -= *this / x * x; // definition + switch (x.i_) + { + case nan: + case neg_inf: + case 0: + case pos_inf: + i_ = nan; + return *this; + } + switch (i_) + { + case neg_inf: + case pos_inf: + i_ = nan; + case nan: + return *this; + } + i_ %= x.i_; + return *this; +} + +// Demo overflow-safe integral durations ranging from picoseconds resolution to millennium resolution +typedef boost::chrono::duration<saturate<long long>, boost::pico > picoseconds; +typedef boost::chrono::duration<saturate<long long>, boost::nano > nanoseconds; +typedef boost::chrono::duration<saturate<long long>, boost::micro > microseconds; +typedef boost::chrono::duration<saturate<long long>, boost::milli > milliseconds; +typedef boost::chrono::duration<saturate<long long> > seconds; +typedef boost::chrono::duration<saturate<long long>, boost::ratio< 60LL> > minutes; +typedef boost::chrono::duration<saturate<long long>, boost::ratio< 3600LL> > hours; +typedef boost::chrono::duration<saturate<long long>, boost::ratio< 86400LL> > days; +typedef boost::chrono::duration<saturate<long long>, boost::ratio< 31556952LL> > years; +typedef boost::chrono::duration<saturate<long long>, boost::ratio<31556952000LL> > millennium; + +} // User2 + +// Demonstrate custom promotion rules (needed only if there are no implicit conversions) +namespace User2 { namespace detail { + +template <class T1, class T2, bool = boost::is_integral<T1>::value> +struct promote_helper; + +template <class T1, class T2> +struct promote_helper<T1, saturate<T2>, true> // integral +{ + typedef typename boost::common_type<T1, T2>::type rep; + typedef User2::saturate<rep> type; +}; + +template <class T1, class T2> +struct promote_helper<T1, saturate<T2>, false> // floating +{ + typedef T1 type; +}; + +} } + +namespace boost +{ + +template <class T1, class T2> +struct common_type<User2::saturate<T1>, User2::saturate<T2> > +{ + typedef typename common_type<T1, T2>::type rep; + typedef User2::saturate<rep> type; +}; + +template <class T1, class T2> +struct common_type<T1, User2::saturate<T2> > + : User2::detail::promote_helper<T1, User2::saturate<T2> > {}; + +template <class T1, class T2> +struct common_type<User2::saturate<T1>, T2> + : User2::detail::promote_helper<T2, User2::saturate<T1> > {}; + + +// Demonstrate specialization of duration_values: + +namespace chrono { + +template <class I> +struct duration_values<User2::saturate<I> > +{ + typedef User2::saturate<I> Rep; +public: + static Rep zero() {return Rep(0);} + static Rep max BOOST_PREVENT_MACRO_SUBSTITUTION () {return Rep(Rep::pos_inf-1);} + static Rep min BOOST_PREVENT_MACRO_SUBSTITUTION () {return -(max)();} +}; + +} // namespace chrono + +} // namespace boost + +#include <iostream> + +void testUser2() +{ + std::cout << "*************\n"; + std::cout << "* testUser2 *\n"; + std::cout << "*************\n"; + using namespace User2; + typedef seconds::rep sat; + years yr(sat(100)); + std::cout << "100 years expressed as years = " << yr.count() << '\n'; + nanoseconds ns = yr; + std::cout << "100 years expressed as nanoseconds = " << ns.count() << '\n'; + ns += yr; + std::cout << "200 years expressed as nanoseconds = " << ns.count() << '\n'; + ns += yr; + std::cout << "300 years expressed as nanoseconds = " << ns.count() << '\n'; +// yr = ns; // does not compile + std::cout << "yr = ns; // does not compile\n"; +// picoseconds ps1 = yr; // does not compile, compile-time overflow in ratio arithmetic + std::cout << "ps = yr; // does not compile\n"; + ns = yr; + picoseconds ps = ns; + std::cout << "100 years expressed as picoseconds = " << ps.count() << '\n'; + ps = ns / sat(1000); + std::cout << "0.1 years expressed as picoseconds = " << ps.count() << '\n'; + yr = years(sat(-200000000)); + std::cout << "200 million years ago encoded in years: " << yr.count() << '\n'; + days d = boost::chrono::duration_cast<days>(yr); + std::cout << "200 million years ago encoded in days: " << d.count() << '\n'; + millennium c = boost::chrono::duration_cast<millennium>(yr); + std::cout << "200 million years ago encoded in millennium: " << c.count() << '\n'; + std::cout << "Demonstrate \"uninitialized protection\" behavior:\n"; + seconds sec; + for (++sec; sec < seconds(sat(10)); ++sec) + ; + std::cout << sec.count() << '\n'; + std::cout << "\n"; +} + +void testStdUser() +{ + std::cout << "***************\n"; + std::cout << "* testStdUser *\n"; + std::cout << "***************\n"; + using namespace boost::chrono; + hours hr = hours(100); + std::cout << "100 hours expressed as hours = " << hr.count() << '\n'; + nanoseconds ns = hr; + std::cout << "100 hours expressed as nanoseconds = " << ns.count() << '\n'; + ns += hr; + std::cout << "200 hours expressed as nanoseconds = " << ns.count() << '\n'; + ns += hr; + std::cout << "300 hours expressed as nanoseconds = " << ns.count() << '\n'; +// hr = ns; // does not compile + std::cout << "hr = ns; // does not compile\n"; +// hr * ns; // does not compile + std::cout << "hr * ns; // does not compile\n"; + duration<double> fs(2.5); + std::cout << "duration<double> has count() = " << fs.count() << '\n'; +// seconds sec = fs; // does not compile + std::cout << "seconds sec = duration<double> won't compile\n"; + seconds sec = duration_cast<seconds>(fs); + std::cout << "seconds has count() = " << sec.count() << '\n'; + std::cout << "\n"; +} + + +int main() +{ + testStdUser(); + testUser2(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/simulated_thread_interface_demo.cpp b/src/boost/libs/chrono/example/simulated_thread_interface_demo.cpp new file mode 100644 index 00000000..81f2e86f --- /dev/null +++ b/src/boost/libs/chrono/example/simulated_thread_interface_demo.cpp @@ -0,0 +1,198 @@ +// simulated_thread_interface_demo.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> +#include <ostream> +#include <stdexcept> +#include <climits> + +////////////////////////////////////////////////////////// +///////////// simulated thread interface ///////////////// +////////////////////////////////////////////////////////// + +namespace { +void print_time(boost::chrono::system_clock::time_point t) +{ + using namespace boost::chrono; + time_t c_time = system_clock::to_time_t(t); + std::tm* tmptr = std::localtime(&c_time); + system_clock::duration d = t.time_since_epoch(); + std::cout << tmptr->tm_hour << ':' << tmptr->tm_min << ':' << tmptr->tm_sec + << '.' << (d - duration_cast<seconds>(d)).count(); +} +} + +namespace boost { +namespace this_thread { + +template <class Rep, class Period> +void sleep_for(const boost::chrono::duration<Rep, Period>& d) +{ + boost::chrono::microseconds t = boost::chrono::duration_cast<boost::chrono::microseconds>(d); + if (t < d) + ++t; + if (t > boost::chrono::microseconds(0)) + std::cout << "sleep_for " << t.count() << " microseconds\n"; +} + +template <class Clock, class Duration> +void sleep_until(const boost::chrono::time_point<Clock, Duration>& t) +{ + using namespace boost::chrono; + typedef time_point<Clock, Duration> Time; + typedef system_clock::time_point SysTime; + if (t > Clock::now()) + { + typedef typename boost::common_type<typename Time::duration, + typename SysTime::duration>::type D; + /* auto */ D d = t - Clock::now(); + microseconds us = duration_cast<microseconds>(d); + if (us < d) + ++us; + SysTime st = system_clock::now() + us; + std::cout << "sleep_until "; + ::print_time(st); + std::cout << " which is " << (st - system_clock::now()).count() << " microseconds away\n"; + } +} + +} // this_thread + +struct mutex {}; + +struct timed_mutex +{ + bool try_lock() {std::cout << "timed_mutex::try_lock()\n"; return true;} + + template <class Rep, class Period> + bool try_lock_for(const boost::chrono::duration<Rep, Period>& d) + { + boost::chrono::microseconds t = boost::chrono::duration_cast<boost::chrono::microseconds>(d); + if (t <= boost::chrono::microseconds(0)) + return try_lock(); + std::cout << "try_lock_for " << t.count() << " microseconds\n"; + return true; + } + + template <class Clock, class Duration> + bool try_lock_until(const boost::chrono::time_point<Clock, Duration>& t) + { + using namespace boost::chrono; + typedef time_point<Clock, Duration> Time; + typedef system_clock::time_point SysTime; + if (t <= Clock::now()) + return try_lock(); + typedef typename boost::common_type<typename Time::duration, + typename Clock::duration>::type D; + /* auto */ D d = t - Clock::now(); + microseconds us = duration_cast<microseconds>(d); + SysTime st = system_clock::now() + us; + std::cout << "try_lock_until "; + ::print_time(st); + std::cout << " which is " << (st - system_clock::now()).count() + << " microseconds away\n"; + return true; + } +}; + +struct condition_variable +{ + template <class Rep, class Period> + bool wait_for(mutex&, const boost::chrono::duration<Rep, Period>& d) + { + boost::chrono::microseconds t = boost::chrono::duration_cast<boost::chrono::microseconds>(d); + std::cout << "wait_for " << t.count() << " microseconds\n"; + return true; + } + + template <class Clock, class Duration> + bool wait_until(mutex&, const boost::chrono::time_point<Clock, Duration>& t) + { + using namespace boost::chrono; + typedef time_point<Clock, Duration> Time; + typedef system_clock::time_point SysTime; + if (t <= Clock::now()) + return false; + typedef typename boost::common_type<typename Time::duration, + typename Clock::duration>::type D; + /* auto */ D d = t - Clock::now(); + microseconds us = duration_cast<microseconds>(d); + SysTime st = system_clock::now() + us; + std::cout << "wait_until "; + ::print_time(st); + std::cout << " which is " << (st - system_clock::now()).count() + << " microseconds away\n"; + return true; + } +}; + +} + +////////////////////////////////////////////////////////// +//////////// Simple sleep and wait examples ////////////// +////////////////////////////////////////////////////////// + +boost::mutex m; +boost::timed_mutex mut; +boost::condition_variable cv; + +void basic_examples() +{ + std::cout << "Running basic examples\n"; + using namespace boost; + using namespace boost::chrono; + system_clock::time_point time_limit = system_clock::now() + seconds(4) + milliseconds(500); + this_thread::sleep_for(seconds(3)); + this_thread::sleep_for(nanoseconds(300)); + this_thread::sleep_until(time_limit); +// this_thread::sleep_for(time_limit); // desired compile-time error +// this_thread::sleep_until(seconds(3)); // desired compile-time error + mut.try_lock_for(milliseconds(30)); + mut.try_lock_until(time_limit); +// mut.try_lock_for(time_limit); // desired compile-time error +// mut.try_lock_until(milliseconds(30)); // desired compile-time error + cv.wait_for(m, minutes(1)); // real code would put this in a loop + cv.wait_until(m, time_limit); // real code would put this in a loop + // For those who prefer floating point + this_thread::sleep_for(duration<double>(0.25)); + this_thread::sleep_until(system_clock::now() + duration<double>(1.5)); +} + + + +int main() +{ + basic_examples(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/test_clock.cpp b/src/boost/libs/chrono/example/test_clock.cpp new file mode 100644 index 00000000..ffb94586 --- /dev/null +++ b/src/boost/libs/chrono/example/test_clock.cpp @@ -0,0 +1,182 @@ +// test_system_clock.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +#include "clock_name.hpp" + +#if defined(BOOST_NO_CXX11_CONSTEXPR) + +using namespace boost::chrono; + +template <typename Clock> +void test_clock() +{ + std::cout << "\n"<< name<Clock>::apply() << " test" << std::endl; +{ + typename Clock::duration delay = milliseconds(5); + typename Clock::time_point start = Clock::now(); + while (Clock::now() - start <= delay) + ; + typename Clock::time_point stop = Clock::now(); + //typename Clock::duration elapsed = stop - start; + std::cout << "5 milliseconds paused " << nanoseconds(stop - start).count() << " nanoseconds\n"; +} +{ + typename Clock::time_point start = Clock::now(); + typename Clock::time_point stop; + std::size_t counter=1; + while ((stop=Clock::now()) == start) { + ++counter; + } + //typename Clock::duration elapsed = stop - start; + std::cout << "After " << counter << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n"; + + start = Clock::now(); + for (std::size_t c=counter; c>0; --c) { + stop=Clock::now();; + } + std::cout << "After " << counter << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n"; + + +} +{ + typename Clock::time_point start = Clock::now(); + typename Clock::time_point stop = Clock::now(); + std::cout << "Resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +} +} + +void test_system_clock() +{ + std::cout << "system_clock test" << std::endl; + system_clock::duration delay = milliseconds(5); + system_clock::time_point start = system_clock::now(); + while (system_clock::now() - start <= delay) + ; + system_clock::time_point stop = system_clock::now(); + system_clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; + start = system_clock::now(); + stop = system_clock::now(); + std::cout << "system_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +} + +void test_steady_clock() +{ +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + std::cout << "steady_clock test" << std::endl; + steady_clock::duration delay = milliseconds(5); + steady_clock::time_point start = steady_clock::now(); + while (steady_clock::now() - start <= delay) + ; + steady_clock::time_point stop = steady_clock::now(); + steady_clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; + start = steady_clock::now(); + stop = steady_clock::now(); + std::cout << "steady_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +#endif +} +void test_hi_resolution_clock() +{ + std::cout << "high_resolution_clock test" << std::endl; + high_resolution_clock::duration delay = milliseconds(5); + high_resolution_clock::time_point start = high_resolution_clock::now(); + while (high_resolution_clock::now() - start <= delay) + ; + high_resolution_clock::time_point stop = high_resolution_clock::now(); + high_resolution_clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; + start = high_resolution_clock::now(); + stop = high_resolution_clock::now(); + std::cout << "high_resolution_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +} + +//void test_mixed_clock() +//{ +// std::cout << "mixed clock test" << std::endl; +// high_resolution_clock::time_point hstart = high_resolution_clock::now(); +// std::cout << "Add 5 milliseconds to a high_resolution_clock::time_point\n"; +// steady_clock::time_point mend = hstart + milliseconds(5); +// bool b = hstart == mend; +// system_clock::time_point sstart = system_clock::now(); +// std::cout << "Subtracting system_clock::time_point from steady_clock::time_point doesn't compile\n"; +//// mend - sstart; // doesn't compile +// std::cout << "subtract high_resolution_clock::time_point from steady_clock::time_point" +// " and add that to a system_clock::time_point\n"; +// system_clock::time_point send = sstart + duration_cast<system_clock::duration>(mend - hstart); +// std::cout << "subtract two system_clock::time_point's and output that in microseconds:\n"; +// microseconds ms = send - sstart; +// std::cout << ms.count() << " microseconds\n"; +//} +// +//void test_c_mapping() +//{ +// std::cout << "C map test\n"; +// using namespace boost::chrono; +// system_clock::time_point t1 = system_clock::now(); +// std::time_t c_time = system_clock::to_time_t(t1); +// std::tm* tmptr = std::localtime(&c_time); +// std::cout << "It is now " << tmptr->tm_hour << ':' << tmptr->tm_min << ':' << tmptr->tm_sec << ' ' +// << tmptr->tm_year + 1900 << '-' << tmptr->tm_mon + 1 << '-' << tmptr->tm_mday << '\n'; +// c_time = std::mktime(tmptr); +// system_clock::time_point t2 = system_clock::from_time_t(c_time); +// microseconds ms = t1 - t2; +// std::cout << "Round-tripping through the C interface truncated the precision by " << ms.count() << " microseconds\n"; +//} + + +int main() +{ + test_system_clock(); + test_steady_clock(); + test_hi_resolution_clock(); + //test_mixed_clock(); + test_clock<system_clock>(); +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + test_clock<steady_clock>(); +#endif + test_clock<high_resolution_clock>(); + + + + return 0; +} + +#else +int main() +{ + + + return 0; +} +#endif diff --git a/src/boost/libs/chrono/example/test_clock2.cpp b/src/boost/libs/chrono/example/test_clock2.cpp new file mode 100644 index 00000000..a6558621 --- /dev/null +++ b/src/boost/libs/chrono/example/test_clock2.cpp @@ -0,0 +1,210 @@ +// test_system_clock.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +#include "clock_name.hpp" + +namespace boost { + namespace detail_chrono { + class steady_clock {}; + class system_clock {}; + } + namespace chrono { + namespace chrono_detail { + using namespace detail_chrono; + struct has_steady_clock { + template< class T > static char sfinae( typename T::rep ); + template< class > static int sfinae( ... ); + + enum { value = sizeof sfinae< steady_clock >( 0 ) == sizeof(char) }; + }; + struct has_system_clock { + template< class T > static char sfinae( typename T::rep ); + template< class > static int sfinae( ... ); + + enum { value = sizeof sfinae< system_clock >( 0 ) == sizeof(char) }; + }; + } + struct has_steady_clock + : integral_constant<bool, chrono_detail::has_steady_clock::value> {}; + struct has_system_clock + : integral_constant<bool, chrono_detail::has_system_clock::value> {}; + } + +} + +BOOST_STATIC_ASSERT(boost::chrono::has_system_clock::value); +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY +BOOST_STATIC_ASSERT(boost::chrono::has_steady_clock::value); +#else +BOOST_STATIC_ASSERT(!boost::chrono::has_steady_clock::value); +#endif + +using namespace boost::chrono; +using namespace boost; + +template <typename Clock> +void test_clock() +{ + std::cout << "\n"<< name<Clock>::apply() << " test" << std::endl; +{ + typename Clock::duration delay = milliseconds(5); + typename Clock::time_point start = Clock::now(); + while (Clock::now() - start <= delay) + ; + typename Clock::time_point stop = Clock::now(); + //typename Clock::duration elapsed = stop - start; + std::cout << "5 milliseconds paused " << nanoseconds(stop - start).count() << " nanoseconds\n"; +} +{ + typename Clock::time_point start = Clock::now(); + typename Clock::time_point stop; + std::size_t count=1; + while ((stop=Clock::now()) == start) { + ++count; + } + //typename Clock::duration elapsed = stop - start; + std::cout << "After " << count << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n"; + + start = Clock::now(); + for (std::size_t c=count; c>0; --c) { + stop=Clock::now();; + } + std::cout << "After " << count << " trials, elapsed time " << nanoseconds(stop - start).count() << " nanoseconds\n"; + + +} +{ + typename Clock::time_point start = Clock::now(); + typename Clock::time_point stop = Clock::now(); + std::cout << "Resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +} +} + +void test_system_clock() +{ + std::cout << "system_clock test" << std::endl; + //~ system_clock clk; + chrono::system_clock::duration delay = milliseconds(5); + chrono::system_clock::time_point start = system_clock::now(); + while (chrono::system_clock::now() - start <= delay) + ; + chrono::system_clock::time_point stop = system_clock::now(); + chrono::system_clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; + start = chrono::system_clock::now(); + stop = chrono::system_clock::now(); + std::cout << "system_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +} + +void test_steady_clock() +{ +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + std::cout << "steady_clock test" << std::endl; + steady_clock::duration delay = milliseconds(5); + steady_clock::time_point start = steady_clock::now(); + while (steady_clock::now() - start <= delay) + ; + steady_clock::time_point stop = steady_clock::now(); + steady_clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; + start = steady_clock::now(); + stop = steady_clock::now(); + std::cout << "steady_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +#endif +} +void test_hi_resolution_clock() +{ + std::cout << "high_resolution_clock test" << std::endl; + high_resolution_clock::duration delay = milliseconds(5); + high_resolution_clock::time_point start = high_resolution_clock::now(); + while (high_resolution_clock::now() - start <= delay) + ; + high_resolution_clock::time_point stop = high_resolution_clock::now(); + high_resolution_clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; + start = high_resolution_clock::now(); + stop = high_resolution_clock::now(); + std::cout << "high_resolution_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +} + +//void test_mixed_clock() +//{ +// std::cout << "mixed clock test" << std::endl; +// high_resolution_clock::time_point hstart = high_resolution_clock::now(); +// std::cout << "Add 5 milliseconds to a high_resolution_clock::time_point\n"; +// steady_clock::time_point mend = hstart + milliseconds(5); +// bool b = hstart == mend; +// system_clock::time_point sstart = system_clock::now(); +// std::cout << "Subtracting system_clock::time_point from steady_clock::time_point doesn't compile\n"; +//// mend - sstart; // doesn't compile +// std::cout << "subtract high_resolution_clock::time_point from steady_clock::time_point" +// " and add that to a system_clock::time_point\n"; +// system_clock::time_point send = sstart + duration_cast<system_clock::duration>(mend - hstart); +// std::cout << "subtract two system_clock::time_point's and output that in microseconds:\n"; +// microseconds ms = send - sstart; +// std::cout << ms.count() << " microseconds\n"; +//} +// +//void test_c_mapping() +//{ +// std::cout << "C map test\n"; +// using namespace boost::chrono; +// system_clock::time_point t1 = system_clock::now(); +// std::time_t c_time = system_clock::to_time_t(t1); +// std::tm* tmptr = std::localtime(&c_time); +// std::cout << "It is now " << tmptr->tm_hour << ':' << tmptr->tm_min << ':' << tmptr->tm_sec << ' ' +// << tmptr->tm_year + 1900 << '-' << tmptr->tm_mon + 1 << '-' << tmptr->tm_mday << '\n'; +// c_time = std::mktime(tmptr); +// system_clock::time_point t2 = system_clock::from_time_t(c_time); +// microseconds ms = t1 - t2; +// std::cout << "Round-tripping through the C interface truncated the precision by " << ms.count() << " microseconds\n"; +//} + + +int main() +{ + test_system_clock(); + test_steady_clock(); + test_hi_resolution_clock(); + //test_mixed_clock(); + test_clock<system_clock>(); +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + test_clock<steady_clock>(); +#endif + test_clock<high_resolution_clock>(); + + + + return 0; +} + diff --git a/src/boost/libs/chrono/example/test_duration.cpp b/src/boost/libs/chrono/example/test_duration.cpp new file mode 100644 index 00000000..71489065 --- /dev/null +++ b/src/boost/libs/chrono/example/test_duration.cpp @@ -0,0 +1,203 @@ +// test_duration.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/assert.hpp> +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +template <class Rep, class Period> +void inspect_duration(boost::chrono::duration<Rep, Period> d, const std::string& name) +{ + typedef boost::chrono::duration<Rep, Period> Duration; + std::cout << "********* " << name << " *********\n"; + std::cout << "The period of " << name << " is " << (double)Period::num/Period::den << " seconds.\n"; + std::cout << "The frequency of " << name << " is " << (double)Period::den/Period::num << " Hz.\n"; + std::cout << "The representation is "; + if (boost::is_floating_point<Rep>::value) + { + std::cout << "floating point\n"; + std::cout << "The precision is the most significant "; + std::cout << std::numeric_limits<Rep>::digits10 << " decimal digits.\n"; + } + else if (boost::is_integral<Rep>::value) + { + std::cout << "integral\n"; + d = Duration(Rep(1)); + boost::chrono::duration<double> dsec = d; + std::cout << "The precision is " << dsec.count() << " seconds.\n"; + } + else + { + std::cout << "a class type\n"; + d = Duration(Rep(1)); + boost::chrono::duration<double> dsec = d; + std::cout << "The precision is " << dsec.count() << " seconds.\n"; + } + d = Duration((std::numeric_limits<Rep>::max)()); + using namespace boost::chrono; + typedef duration<double, boost::ratio_multiply<boost::ratio<24*3652425,10000>, hours::period>::type> Years; + Years years = d; + std::cout << "The range is +/- " << years.count() << " years.\n"; + std::cout << "sizeof(" << name << ") = " << sizeof(d) << '\n'; +} + +void inspect_all() +{ + using namespace boost::chrono; + std::cout.precision(6); + inspect_duration(nanoseconds(), "nanoseconds"); + inspect_duration(microseconds(), "microseconds"); + inspect_duration(milliseconds(), "milliseconds"); + inspect_duration(seconds(), "seconds"); + inspect_duration(minutes(), "minutes"); + inspect_duration(hours(), "hours"); + inspect_duration(duration<double>(), "duration<double>"); +} + + + +using namespace boost::chrono; +void test_duration_division() +{ + typedef boost::common_type<boost::chrono::hours::rep, boost::chrono::minutes::rep>::type h_min_rep; + h_min_rep r3 = hours(3) / minutes(5); + std::cout << r3 << '\n'; + std::cout << hours(3) / minutes(5) << '\n'; + std::cout << hours(3) / milliseconds(5) << '\n'; + std::cout << milliseconds(5) / hours(3) << '\n'; + std::cout << hours(1) / milliseconds(1) << '\n'; +} + +void test_duration_multiply() +{ + hours h15= 5 * hours(3); + (void)h15; + hours h6= hours(3) *2; + (void)h6; +} + +void f(duration<double> d, double res) // accept floating point seconds +{ + // d.count() == 3.e-6 when passed microseconds(3) + BOOST_ASSERT(d.count()==res); +} + +void g(nanoseconds d, boost::intmax_t res) +{ + // d.count() == 3000 when passed microseconds(3) + std::cout << d.count() << " " <<res << std::endl; + BOOST_ASSERT(d.count()==res); +} + +template <class Rep, class Period> +void tmpl(duration<Rep, Period> d, boost::intmax_t res) +{ + // convert d to nanoseconds, rounding up if it is not an exact conversion + nanoseconds ns = duration_cast<nanoseconds>(d); + if (ns < d) + ++ns; + // ns.count() == 333333334 when passed 1/3 of a floating point second + BOOST_ASSERT(ns.count()==res); +} + +template <class Period> +void tmpl2(duration<long long, Period> d, boost::intmax_t res) +{ + // convert d to nanoseconds, rounding up if it is not an exact conversion + nanoseconds ns = duration_cast<nanoseconds>(d); + if (ns < d) + ++ns; + // ns.count() == 333333334 when passed 333333333333 picoseconds + BOOST_ASSERT(ns.count()==res); +} + + + +int main() +{ + minutes m1(3); // m1 stores 3 + minutes m2(2); // m2 stores 2 + minutes m3 = m1 + m2; // m3 stores 5 + BOOST_ASSERT(m3.count()==5); + + microseconds us1(3); // us1 stores 3 + microseconds us2(2); // us2 stores 2 + microseconds us3 = us1 + us2; // us3 stores 5 + BOOST_ASSERT(us3.count()==5); + + microseconds us4 = m3 + us3; // us4 stores 300000005 + BOOST_ASSERT(us4.count()==300000005); + microseconds us5 = m3; // us4 stores 300000000 + BOOST_ASSERT(us5.count()==300000000); + + //minutes m4 = m3 + us3; // won't compile + + minutes m4 = duration_cast<minutes>(m3 + us3); // m4.count() == 5 + BOOST_ASSERT(m4.count()==5); + + typedef duration<double, boost::ratio<60> > dminutes; + dminutes dm4 = m3 + us3; // dm4.count() == 5.000000083333333 + BOOST_ASSERT(dm4.count()==5.000000083333333); + + f(microseconds(3), 0.000003); + g(microseconds(3), 3000); + duration<double> s(1./3); // 1/3 of a second + g(duration_cast<nanoseconds>(s), 333333333); // round towards zero in conversion to nanoseconds + //f(s); // does not compile + tmpl(duration<double>(1./3), 333333334); + tmpl2(duration<long long, boost::pico>(333333333333LL), 333333334); // About 1/3 of a second worth of picoseconds + + //f(3,3); // Will not compile, 3 is not implicitly convertible to any `duration` + //g(3,3); // Will not compile, 3 is not implicitly convertible to any `duration` + //tmpl(3,3); // Will not compile, 3 is not implicitly convertible to any `duration` + //tmpl2(3,3); // Will not compile, 3 is not implicitly convertible to any `duration` + + { + double r = double(milliseconds(3) / milliseconds(3)); + std::cout << r << '\n'; + + duration<double, boost::milli> d = milliseconds(3) * 2.5; + duration<double, boost::milli> d2 = 2.5 * milliseconds(3) ; + (void)d2; + duration<double, boost::milli> d3 = milliseconds(3) / 2.5; + (void)d3; + duration<double, boost::milli> d4 = milliseconds(3) + milliseconds(5) ; + (void)d4; + inspect_duration(milliseconds(3) * 2.5, "milliseconds(3) * 2.5"); + std::cout << d.count() << '\n'; +// milliseconds ms(3.5); // doesn't compile + std::cout << "milliseconds ms(3.5) doesn't compile\n"; + } + + test_duration_division(); + test_duration_multiply(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/test_minmax.cpp b/src/boost/libs/chrono/example/test_minmax.cpp new file mode 100644 index 00000000..a4eca7f6 --- /dev/null +++ b/src/boost/libs/chrono/example/test_minmax.cpp @@ -0,0 +1,17 @@ +// test_duration.cpp ----------------------------------------------------------// + +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +#if !defined(__GNUC__) + +#define min(A,B) ((A)<(B)?(A):(B)) +#define max(A,B) ((A)>(B)?(A):(B)) + +#include <boost/chrono/chrono.hpp> + +#endif + + diff --git a/src/boost/libs/chrono/example/test_special_values.cpp b/src/boost/libs/chrono/example/test_special_values.cpp new file mode 100644 index 00000000..dcda2a76 --- /dev/null +++ b/src/boost/libs/chrono/example/test_special_values.cpp @@ -0,0 +1,51 @@ +// test_special_values.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +using namespace boost::chrono; + +void test_special_values() +{ + std::cout << "duration<unsigned>::min().count() = " << ((duration<unsigned>::min)()).count() << '\n'; + std::cout << "duration<unsigned>::zero().count() = " << duration<unsigned>::zero().count() << '\n'; + std::cout << "duration<unsigned>::max().count() = " << ((duration<unsigned>::max)()).count() << '\n'; + std::cout << "duration<int>::min().count() = " << ((duration<int>::min)()).count() << '\n'; + std::cout << "duration<int>::zero().count() = " << duration<int>::zero().count() << '\n'; + std::cout << "duration<int>::max().count() = " << ((duration<int>::max)()).count() << '\n'; +} + +int main() +{ + test_special_values(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/test_thread_clock.cpp b/src/boost/libs/chrono/example/test_thread_clock.cpp new file mode 100644 index 00000000..2bc7610e --- /dev/null +++ b/src/boost/libs/chrono/example/test_thread_clock.cpp @@ -0,0 +1,42 @@ +// test_thread_clock.cpp ----------------------------------------------------------// + +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + + +#include <boost/chrono/thread_clock.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + + +void test_thread_clock() +{ +#if defined(BOOST_CHRONO_HAS_THREAD_CLOCK) + using namespace boost::chrono; + + std::cout << "thread_clock test" << std::endl; + thread_clock::duration delay = milliseconds(5); + thread_clock::time_point start = thread_clock::now(); + while (thread_clock::now() - start <= delay) + ; + thread_clock::time_point stop = thread_clock::now(); + thread_clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; + start = thread_clock::now(); + stop = thread_clock::now(); + std::cout << "thread_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +#else + std::cout << "thread_clock not available\n"; +#endif +} + + +int main() +{ + test_thread_clock(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/time2_demo.cpp b/src/boost/libs/chrono/example/time2_demo.cpp new file mode 100644 index 00000000..35d93b7b --- /dev/null +++ b/src/boost/libs/chrono/example/time2_demo.cpp @@ -0,0 +1,1655 @@ +// time2_demo.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* + +This code was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <cassert> +#include <climits> +#include <iostream> +#include <ostream> +#include <stdexcept> + +#include <windows.h> + +namespace +{ + //struct timeval { + // long tv_sec; /* seconds */ + // long tv_usec; /* and microseconds */ + //}; + + int gettimeofday(struct timeval * tp, void *) + { + FILETIME ft; + ::GetSystemTimeAsFileTime( &ft ); // never fails + long long t = (static_cast<long long>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + # if !defined( BOOST_MSVC ) || BOOST_MSVC > 1300 // > VC++ 7.0 + t -= 116444736000000000LL; + # else + t -= 116444736000000000; + # endif + t /= 10; // microseconds + tp->tv_sec = static_cast<long>( t / 1000000UL); + tp->tv_usec = static_cast<long>( t % 1000000UL); + return 0; + } +} // unnamed namespace + +////////////////////////////////////////////////////////// +///////////// simulated thread interface ///////////////// +////////////////////////////////////////////////////////// + + +namespace std { + +void __print_time(boost::chrono::system_clock::time_point t) +{ + using namespace boost::chrono; + time_t c_time = system_clock::to_time_t(t); + std::tm* tmptr = std::localtime(&c_time); + system_clock::duration d = t.time_since_epoch(); + std::cout << tmptr->tm_hour << ':' << tmptr->tm_min << ':' << tmptr->tm_sec + << '.' << (d - duration_cast<seconds>(d)).count(); +} + +namespace this_thread { + +template <class Rep, class Period> +void sleep_for(const boost::chrono::duration<Rep, Period>& d) +{ + boost::chrono::microseconds t = boost::chrono::duration_cast<boost::chrono::microseconds>(d); + if (t < d) + ++t; + if (t > boost::chrono::microseconds(0)) + std::cout << "sleep_for " << t.count() << " microseconds\n"; +} + +template <class Clock, class Duration> +void sleep_until(const boost::chrono::time_point<Clock, Duration>& t) +{ + using namespace boost::chrono; + typedef time_point<Clock, Duration> Time; + typedef system_clock::time_point SysTime; + if (t > Clock::now()) + { + typedef typename boost::common_type<typename Time::duration, + typename SysTime::duration>::type D; + /* auto */ D d = t - Clock::now(); + microseconds us = duration_cast<microseconds>(d); + if (us < d) + ++us; + SysTime st = system_clock::now() + us; + std::cout << "sleep_until "; + __print_time(st); + std::cout << " which is " << (st - system_clock::now()).count() << " microseconds away\n"; + } +} + +} // this_thread + +struct mutex {}; + +struct timed_mutex +{ + bool try_lock() {std::cout << "timed_mutex::try_lock()\n"; return true;} + + template <class Rep, class Period> + bool try_lock_for(const boost::chrono::duration<Rep, Period>& d) + { + boost::chrono::microseconds t = boost::chrono::duration_cast<boost::chrono::microseconds>(d); + if (t <= boost::chrono::microseconds(0)) + return try_lock(); + std::cout << "try_lock_for " << t.count() << " microseconds\n"; + return true; + } + + template <class Clock, class Duration> + bool try_lock_until(const boost::chrono::time_point<Clock, Duration>& t) + { + using namespace boost::chrono; + typedef time_point<Clock, Duration> Time; + typedef system_clock::time_point SysTime; + if (t <= Clock::now()) + return try_lock(); + typedef typename boost::common_type<typename Time::duration, + typename Clock::duration>::type D; + /* auto */ D d = t - Clock::now(); + microseconds us = duration_cast<microseconds>(d); + SysTime st = system_clock::now() + us; + std::cout << "try_lock_until "; + __print_time(st); + std::cout << " which is " << (st - system_clock::now()).count() + << " microseconds away\n"; + return true; + } +}; + +struct condition_variable +{ + template <class Rep, class Period> + bool wait_for(mutex&, const boost::chrono::duration<Rep, Period>& d) + { + boost::chrono::microseconds t = boost::chrono::duration_cast<boost::chrono::microseconds>(d); + std::cout << "wait_for " << t.count() << " microseconds\n"; + return true; + } + + template <class Clock, class Duration> + bool wait_until(mutex&, const boost::chrono::time_point<Clock, Duration>& t) + { + using namespace boost::chrono; + typedef time_point<Clock, Duration> Time; + typedef system_clock::time_point SysTime; + if (t <= Clock::now()) + return false; + typedef typename boost::common_type<typename Time::duration, + typename Clock::duration>::type D; + /* auto */ D d = t - Clock::now(); + microseconds us = duration_cast<microseconds>(d); + SysTime st = system_clock::now() + us; + std::cout << "wait_until "; + __print_time(st); + std::cout << " which is " << (st - system_clock::now()).count() + << " microseconds away\n"; + return true; + } +}; + +} // namespace std + +////////////////////////////////////////////////////////// +//////////// Simple sleep and wait examples ////////////// +////////////////////////////////////////////////////////// + +std::mutex m; +std::timed_mutex mut; +std::condition_variable cv; + +void basic_examples() +{ + std::cout << "Running basic examples\n"; + using namespace std; + using namespace boost::chrono; + system_clock::time_point time_limit = system_clock::now() + seconds(4) + milliseconds(500); + this_thread::sleep_for(seconds(3)); + this_thread::sleep_for(nanoseconds(300)); + this_thread::sleep_until(time_limit); +// this_thread::sleep_for(time_limit); // desired compile-time error +// this_thread::sleep_until(seconds(3)); // desired compile-time error + mut.try_lock_for(milliseconds(30)); + mut.try_lock_until(time_limit); +// mut.try_lock_for(time_limit); // desired compile-time error +// mut.try_lock_until(milliseconds(30)); // desired compile-time error + cv.wait_for(m, minutes(1)); // real code would put this in a loop + cv.wait_until(m, time_limit); // real code would put this in a loop + // For those who prefer floating point + this_thread::sleep_for(duration<double>(0.25)); + this_thread::sleep_until(system_clock::now() + duration<double>(1.5)); +} + +////////////////////////////////////////////////////////// +//////////////////// User1 Example /////////////////////// +////////////////////////////////////////////////////////// + +namespace User1 +{ +// Example type-safe "physics" code interoperating with boost::chrono::duration types +// and taking advantage of the boost::ratio infrastructure and design philosophy. + +// length - mimics boost::chrono::duration except restricts representation to double. +// Uses boost::ratio facilities for length units conversions. + +template <class Ratio> +class length +{ +public: + typedef Ratio ratio; +private: + double len_; +public: + + length() : len_(1) {} + length(const double& len) : len_(len) {} + + // conversions + template <class R> + length(const length<R>& d) + : len_(d.count() * boost::ratio_divide<Ratio, R>::type::den / + boost::ratio_divide<Ratio, R>::type::num) {} + + // observer + + double count() const {return len_;} + + // arithmetic + + length& operator+=(const length& d) {len_ += d.count(); return *this;} + length& operator-=(const length& d) {len_ -= d.count(); return *this;} + + length operator+() const {return *this;} + length operator-() const {return length(-len_);} + + length& operator*=(double rhs) {len_ *= rhs; return *this;} + length& operator/=(double rhs) {len_ /= rhs; return *this;} +}; + +// Sparse sampling of length units +typedef length<boost::ratio<1> > meter; // set meter as "unity" +typedef length<boost::centi> centimeter; // 1/100 meter +typedef length<boost::kilo> kilometer; // 1000 meters +typedef length<boost::ratio<254, 10000> > inch; // 254/10000 meters +// length takes ratio instead of two integral types so that definitions can be made like so: +typedef length<boost::ratio_multiply<boost::ratio<12>, inch::ratio>::type> foot; // 12 inchs +typedef length<boost::ratio_multiply<boost::ratio<5280>, foot::ratio>::type> mile; // 5280 feet + +// Need a floating point definition of seconds +typedef boost::chrono::duration<double> seconds; // unity +// Demo of (scientific) support for sub-nanosecond resolutions +typedef boost::chrono::duration<double, boost::pico> picosecond; // 10^-12 seconds +typedef boost::chrono::duration<double, boost::femto> femtosecond; // 10^-15 seconds +typedef boost::chrono::duration<double, boost::atto> attosecond; // 10^-18 seconds + +// A very brief proof-of-concept for SIUnits-like library +// Hard-wired to floating point seconds and meters, but accepts other units (shown in testUser1()) +template <class R1, class R2> +class quantity +{ + double q_; +public: + quantity() : q_(1) {} + + double get() const {return q_;} + void set(double q) {q_ = q;} +}; + +template <> +class quantity<boost::ratio<1>, boost::ratio<0> > +{ + double q_; +public: + quantity() : q_(1) {} + quantity(seconds d) : q_(d.count()) {} // note: only User1::seconds needed here + + double get() const {return q_;} + void set(double q) {q_ = q;} +}; + +template <> +class quantity<boost::ratio<0>, boost::ratio<1> > +{ + double q_; +public: + quantity() : q_(1) {} + quantity(meter d) : q_(d.count()) {} // note: only User1::meter needed here + + double get() const {return q_;} + void set(double q) {q_ = q;} +}; + +template <> +class quantity<boost::ratio<0>, boost::ratio<0> > +{ + double q_; +public: + quantity() : q_(1) {} + quantity(double d) : q_(d) {} + + double get() const {return q_;} + void set(double q) {q_ = q;} +}; + +// Example SI-Units +typedef quantity<boost::ratio<0>, boost::ratio<0> > Scalar; +typedef quantity<boost::ratio<1>, boost::ratio<0> > Time; // second +typedef quantity<boost::ratio<0>, boost::ratio<1> > Distance; // meter +typedef quantity<boost::ratio<-1>, boost::ratio<1> > Speed; // meter/second +typedef quantity<boost::ratio<-2>, boost::ratio<1> > Acceleration; // meter/second^2 + +template <class R1, class R2, class R3, class R4> +quantity<typename boost::ratio_subtract<R1, R3>::type, typename boost::ratio_subtract<R2, R4>::type> +operator/(const quantity<R1, R2>& x, const quantity<R3, R4>& y) +{ + typedef quantity<typename boost::ratio_subtract<R1, R3>::type, typename boost::ratio_subtract<R2, R4>::type> R; + R r; + r.set(x.get() / y.get()); + return r; +} + +template <class R1, class R2, class R3, class R4> +quantity<typename boost::ratio_add<R1, R3>::type, typename boost::ratio_add<R2, R4>::type> +operator*(const quantity<R1, R2>& x, const quantity<R3, R4>& y) +{ + typedef quantity<typename boost::ratio_add<R1, R3>::type, typename boost::ratio_add<R2, R4>::type> R; + R r; + r.set(x.get() * y.get()); + return r; +} + +template <class R1, class R2> +quantity<R1, R2> +operator+(const quantity<R1, R2>& x, const quantity<R1, R2>& y) +{ + typedef quantity<R1, R2> R; + R r; + r.set(x.get() + y.get()); + return r; +} + +template <class R1, class R2> +quantity<R1, R2> +operator-(const quantity<R1, R2>& x, const quantity<R1, R2>& y) +{ + typedef quantity<R1, R2> R; + R r; + r.set(x.get() - y.get()); + return r; +} + +// Example type-safe physics function +Distance +compute_distance(Speed v0, Time t, Acceleration a) +{ + return v0 * t + Scalar(.5) * a * t * t; // if a units mistake is made here it won't compile +} + +} // User1 + + +// Exercise example type-safe physics function and show interoperation +// of custom time durations (User1::seconds) and standard time durations (std::hours). +// Though input can be arbitrary (but type-safe) units, output is always in SI-units +// (a limitation of the simplified Units lib demoed here). +void testUser1() +{ + std::cout << "*************\n"; + std::cout << "* testUser1 *\n"; + std::cout << "*************\n"; + User1::Distance d( User1::mile(110) ); + User1::Time t( boost::chrono::hours(2) ); + User1::Speed s = d / t; + std::cout << "Speed = " << s.get() << " meters/sec\n"; + User1::Acceleration a = User1::Distance( User1::foot(32.2) ) / User1::Time() / User1::Time(); + std::cout << "Acceleration = " << a.get() << " meters/sec^2\n"; + User1::Distance df = compute_distance(s, User1::Time( User1::seconds(0.5) ), a); + std::cout << "Distance = " << df.get() << " meters\n"; + std::cout << "There are " << User1::mile::ratio::den << '/' << User1::mile::ratio::num << " miles/meter"; + User1::meter mt = 1; + User1::mile mi = mt; + std::cout << " which is approximately " << mi.count() << '\n'; + std::cout << "There are " << User1::mile::ratio::num << '/' << User1::mile::ratio::den << " meters/mile"; + mi = 1; + mt = mi; + std::cout << " which is approximately " << mt.count() << '\n'; + User1::attosecond as(1); + User1::seconds sec = as; + std::cout << "1 attosecond is " << sec.count() << " seconds\n"; + std::cout << "sec = as; // compiles\n"; + sec = User1::seconds(1); + as = sec; + std::cout << "1 second is " << as.count() << " attoseconds\n"; + std::cout << "as = sec; // compiles\n"; + std::cout << "\n"; +} + +////////////////////////////////////////////////////////// +//////////////////// User2 Example /////////////////////// +////////////////////////////////////////////////////////// + +// Demonstrate User2: +// A "saturating" signed integral type is developed. This type has +/- infinity and a nan +// (like IEEE floating point) but otherwise obeys signed integral arithmetic. +// This class is subsequently used as the rep in boost::chrono::duration to demonstrate a +// duration class that does not silently ignore overflow. + +namespace User2 +{ + +template <class I> +class saturate +{ +public: + typedef I int_type; + + static const int_type nan = int_type(int_type(1) << (sizeof(int_type) * CHAR_BIT - 1)); + static const int_type neg_inf = nan + 1; + static const int_type pos_inf = -neg_inf; +private: + int_type i_; + +// static_assert(std::is_integral<int_type>::value && std::is_signed<int_type>::value, +// "saturate only accepts signed integral types"); +// static_assert(nan == -nan && neg_inf < pos_inf, +// "saturate assumes two's complement hardware for signed integrals"); + +public: + saturate() : i_(nan) {} + explicit saturate(int_type i) : i_(i) {} + // explicit + operator int_type() const; + + saturate& operator+=(saturate x); + saturate& operator-=(saturate x) {return *this += -x;} + saturate& operator*=(saturate x); + saturate& operator/=(saturate x); + saturate& operator%=(saturate x); + + saturate operator- () const {return saturate(-i_);} + saturate& operator++() {*this += saturate(int_type(1)); return *this;} + saturate operator++(int) {saturate tmp(*this); ++(*this); return tmp;} + saturate& operator--() {*this -= saturate(int_type(1)); return *this;} + saturate operator--(int) {saturate tmp(*this); --(*this); return tmp;} + + friend saturate operator+(saturate x, saturate y) {return x += y;} + friend saturate operator-(saturate x, saturate y) {return x -= y;} + friend saturate operator*(saturate x, saturate y) {return x *= y;} + friend saturate operator/(saturate x, saturate y) {return x /= y;} + friend saturate operator%(saturate x, saturate y) {return x %= y;} + + friend bool operator==(saturate x, saturate y) + { + if (x.i_ == nan || y.i_ == nan) + return false; + return x.i_ == y.i_; + } + + friend bool operator!=(saturate x, saturate y) {return !(x == y);} + + friend bool operator<(saturate x, saturate y) + { + if (x.i_ == nan || y.i_ == nan) + return false; + return x.i_ < y.i_; + } + + friend bool operator<=(saturate x, saturate y) + { + if (x.i_ == nan || y.i_ == nan) + return false; + return x.i_ <= y.i_; + } + + friend bool operator>(saturate x, saturate y) + { + if (x.i_ == nan || y.i_ == nan) + return false; + return x.i_ > y.i_; + } + + friend bool operator>=(saturate x, saturate y) + { + if (x.i_ == nan || y.i_ == nan) + return false; + return x.i_ >= y.i_; + } + + friend std::ostream& operator<<(std::ostream& os, saturate s) + { + switch (s.i_) + { + case pos_inf: + return os << "inf"; + case nan: + return os << "nan"; + case neg_inf: + return os << "-inf"; + }; + return os << s.i_; + } +}; + +template <class I> +saturate<I>::operator int_type() const +{ + switch (i_) + { + case nan: + case neg_inf: + case pos_inf: + throw std::out_of_range("saturate special value can not convert to int_type"); + } + return i_; +} + +template <class I> +saturate<I>& +saturate<I>::operator+=(saturate x) +{ + switch (i_) + { + case pos_inf: + switch (x.i_) + { + case neg_inf: + case nan: + i_ = nan; + } + return *this; + case nan: + return *this; + case neg_inf: + switch (x.i_) + { + case pos_inf: + case nan: + i_ = nan; + } + return *this; + } + switch (x.i_) + { + case pos_inf: + case neg_inf: + case nan: + i_ = x.i_; + return *this; + } + if (x.i_ >= 0) + { + if (i_ < pos_inf - x.i_) + i_ += x.i_; + else + i_ = pos_inf; + return *this; + } + if (i_ > neg_inf - x.i_) + i_ += x.i_; + else + i_ = neg_inf; + return *this; +} + +template <class I> +saturate<I>& +saturate<I>::operator*=(saturate x) +{ + switch (i_) + { + case 0: + switch (x.i_) + { + case pos_inf: + case neg_inf: + case nan: + i_ = nan; + } + return *this; + case pos_inf: + switch (x.i_) + { + case nan: + case 0: + i_ = nan; + return *this; + } + if (x.i_ < 0) + i_ = neg_inf; + return *this; + case nan: + return *this; + case neg_inf: + switch (x.i_) + { + case nan: + case 0: + i_ = nan; + return *this; + } + if (x.i_ < 0) + i_ = pos_inf; + return *this; + } + switch (x.i_) + { + case 0: + i_ = 0; + return *this; + case nan: + i_ = nan; + return *this; + case pos_inf: + if (i_ < 0) + i_ = neg_inf; + else + i_ = pos_inf; + return *this; + case neg_inf: + if (i_ < 0) + i_ = pos_inf; + else + i_ = neg_inf; + return *this; + } + int s = (i_ < 0 ? -1 : 1) * (x.i_ < 0 ? -1 : 1); + i_ = i_ < 0 ? -i_ : i_; + int_type x_i_ = x.i_ < 0 ? -x.i_ : x.i_; + if (i_ <= pos_inf / x_i_) + i_ *= x_i_; + else + i_ = pos_inf; + i_ *= s; + return *this; +} + +template <class I> +saturate<I>& +saturate<I>::operator/=(saturate x) +{ + switch (x.i_) + { + case pos_inf: + case neg_inf: + switch (i_) + { + case pos_inf: + case neg_inf: + case nan: + i_ = nan; + break; + default: + i_ = 0; + break; + } + return *this; + case nan: + i_ = nan; + return *this; + case 0: + switch (i_) + { + case pos_inf: + case neg_inf: + case nan: + return *this; + case 0: + i_ = nan; + return *this; + } + if (i_ > 0) + i_ = pos_inf; + else + i_ = neg_inf; + return *this; + } + switch (i_) + { + case 0: + case nan: + return *this; + case pos_inf: + case neg_inf: + if (x.i_ < 0) + i_ = -i_; + return *this; + } + i_ /= x.i_; + return *this; +} + +template <class I> +saturate<I>& +saturate<I>::operator%=(saturate x) +{ +// *this -= *this / x * x; // definition + switch (x.i_) + { + case nan: + case neg_inf: + case 0: + case pos_inf: + i_ = nan; + return *this; + } + switch (i_) + { + case neg_inf: + case pos_inf: + i_ = nan; + case nan: + return *this; + } + i_ %= x.i_; + return *this; +} + +// Demo overflow-safe integral durations ranging from picoseconds resolution to millennium resolution +typedef boost::chrono::duration<saturate<long long>, boost::pico > picoseconds; +typedef boost::chrono::duration<saturate<long long>, boost::nano > nanoseconds; +typedef boost::chrono::duration<saturate<long long>, boost::micro > microseconds; +typedef boost::chrono::duration<saturate<long long>, boost::milli > milliseconds; +typedef boost::chrono::duration<saturate<long long> > seconds; +typedef boost::chrono::duration<saturate<long long>, boost::ratio< 60LL> > minutes; +typedef boost::chrono::duration<saturate<long long>, boost::ratio< 3600LL> > hours; +typedef boost::chrono::duration<saturate<long long>, boost::ratio< 86400LL> > days; +typedef boost::chrono::duration<saturate<long long>, boost::ratio< 31556952LL> > years; +typedef boost::chrono::duration<saturate<long long>, boost::ratio<31556952000LL> > millennium; + +} // User2 + +// Demonstrate custom promotion rules (needed only if there are no implicit conversions) +namespace User2 { namespace detail { + +template <class T1, class T2, bool = boost::is_integral<T1>::value> +struct promote_helper; + +template <class T1, class T2> +struct promote_helper<T1, saturate<T2>, true> // integral +{ + typedef typename boost::common_type<T1, T2>::type rep; + typedef User2::saturate<rep> type; +}; + +template <class T1, class T2> +struct promote_helper<T1, saturate<T2>, false> // floating +{ + typedef T1 type; +}; + +} } + +namespace boost +{ + +template <class T1, class T2> +struct common_type<User2::saturate<T1>, User2::saturate<T2> > +{ + typedef typename common_type<T1, T2>::type rep; + typedef User2::saturate<rep> type; +}; + +template <class T1, class T2> +struct common_type<T1, User2::saturate<T2> > + : User2::detail::promote_helper<T1, User2::saturate<T2> > {}; + +template <class T1, class T2> +struct common_type<User2::saturate<T1>, T2> + : User2::detail::promote_helper<T2, User2::saturate<T1> > {}; + + +// Demonstrate specialization of duration_values: + +namespace chrono { + +template <class I> +struct duration_values<User2::saturate<I> > +{ + typedef User2::saturate<I> Rep; +public: + static Rep zero() {return Rep(0);} + static Rep max BOOST_PREVENT_MACRO_SUBSTITUTION () {return Rep(Rep::pos_inf-1);} + static Rep min BOOST_PREVENT_MACRO_SUBSTITUTION () {return -(max) ();} +}; + +} // namespace chrono + +} // namespace boost + + +void testUser2() +{ + std::cout << "*************\n"; + std::cout << "* testUser2 *\n"; + std::cout << "*************\n"; + using namespace User2; + typedef seconds::rep sat; + years yr(sat(100)); + std::cout << "100 years expressed as years = " << yr.count() << '\n'; + nanoseconds ns = yr; + std::cout << "100 years expressed as nanoseconds = " << ns.count() << '\n'; + ns += yr; + std::cout << "200 years expressed as nanoseconds = " << ns.count() << '\n'; + ns += yr; + std::cout << "300 years expressed as nanoseconds = " << ns.count() << '\n'; +// yr = ns; // does not compile + std::cout << "yr = ns; // does not compile\n"; +// picoseconds ps1 = yr; // does not compile, compile-time overflow in ratio arithmetic + std::cout << "ps = yr; // does not compile\n"; + ns = yr; + picoseconds ps = ns; + std::cout << "100 years expressed as picoseconds = " << ps.count() << '\n'; + ps = ns / sat(1000); + std::cout << "0.1 years expressed as picoseconds = " << ps.count() << '\n'; + yr = years(sat(-200000000)); + std::cout << "200 million years ago encoded in years: " << yr.count() << '\n'; + days d = boost::chrono::duration_cast<days>(yr); + std::cout << "200 million years ago encoded in days: " << d.count() << '\n'; + millennium c = boost::chrono::duration_cast<millennium>(yr); + std::cout << "200 million years ago encoded in millennium: " << c.count() << '\n'; + std::cout << "Demonstrate \"uninitialized protection\" behavior:\n"; + seconds sec; + for (++sec; sec < seconds(sat(10)); ++sec) + ; + std::cout << sec.count() << '\n'; + std::cout << "\n"; +} + +void testStdUser() +{ + std::cout << "***************\n"; + std::cout << "* testStdUser *\n"; + std::cout << "***************\n"; + using namespace boost::chrono; + hours hr = hours(100); + std::cout << "100 hours expressed as hours = " << hr.count() << '\n'; + nanoseconds ns = hr; + std::cout << "100 hours expressed as nanoseconds = " << ns.count() << '\n'; + ns += hr; + std::cout << "200 hours expressed as nanoseconds = " << ns.count() << '\n'; + ns += hr; + std::cout << "300 hours expressed as nanoseconds = " << ns.count() << '\n'; +// hr = ns; // does not compile + std::cout << "hr = ns; // does not compile\n"; +// hr * ns; // does not compile + std::cout << "hr * ns; // does not compile\n"; + duration<double> fs(2.5); + std::cout << "duration<double> has count() = " << fs.count() << '\n'; +// seconds sec = fs; // does not compile + std::cout << "seconds sec = duration<double> won't compile\n"; + seconds sec = duration_cast<seconds>(fs); + std::cout << "seconds has count() = " << sec.count() << '\n'; + std::cout << "\n"; +} + +// timeval clock demo +// Demonstrate the use of a timeval-like struct to be used as the representation +// type for both duraiton and time_point. + +namespace timeval_demo +{ + +class xtime { +private: + long tv_sec; + long tv_usec; + + void fixup() { + if (tv_usec < 0) { + tv_usec += 1000000; + --tv_sec; + } + } + +public: + + explicit xtime(long sec, long usec) { + tv_sec = sec; + tv_usec = usec; + if (tv_usec < 0 || tv_usec >= 1000000) { + tv_sec += tv_usec / 1000000; + tv_usec %= 1000000; + fixup(); + } + } + + explicit xtime(long long usec) + { + tv_usec = static_cast<long>(usec % 1000000); + tv_sec = static_cast<long>(usec / 1000000); + fixup(); + } + + // explicit + operator long long() const {return static_cast<long long>(tv_sec) * 1000000 + tv_usec;} + + xtime& operator += (xtime rhs) { + tv_sec += rhs.tv_sec; + tv_usec += rhs.tv_usec; + if (tv_usec >= 1000000) { + tv_usec -= 1000000; + ++tv_sec; + } + return *this; + } + + xtime& operator -= (xtime rhs) { + tv_sec -= rhs.tv_sec; + tv_usec -= rhs.tv_usec; + fixup(); + return *this; + } + + xtime& operator %= (xtime rhs) { + long long t = tv_sec * 1000000 + tv_usec; + long long r = rhs.tv_sec * 1000000 + rhs.tv_usec; + t %= r; + tv_sec = static_cast<long>(t / 1000000); + tv_usec = static_cast<long>(t % 1000000); + fixup(); + return *this; + } + + friend xtime operator+(xtime x, xtime y) {return x += y;} + friend xtime operator-(xtime x, xtime y) {return x -= y;} + friend xtime operator%(xtime x, xtime y) {return x %= y;} + + friend bool operator==(xtime x, xtime y) + { return (x.tv_sec == y.tv_sec && x.tv_usec == y.tv_usec); } + + friend bool operator<(xtime x, xtime y) { + if (x.tv_sec == y.tv_sec) + return (x.tv_usec < y.tv_usec); + return (x.tv_sec < y.tv_sec); + } + + friend bool operator!=(xtime x, xtime y) { return !(x == y); } + friend bool operator> (xtime x, xtime y) { return y < x; } + friend bool operator<=(xtime x, xtime y) { return !(y < x); } + friend bool operator>=(xtime x, xtime y) { return !(x < y); } + + friend std::ostream& operator<<(std::ostream& os, xtime x) + {return os << '{' << x.tv_sec << ',' << x.tv_usec << '}';} +}; + +class xtime_clock +{ +public: + typedef xtime rep; + typedef boost::micro period; + typedef boost::chrono::duration<rep, period> duration; + typedef boost::chrono::time_point<xtime_clock> time_point; + + static time_point now(); +}; + +xtime_clock::time_point +xtime_clock::now() +{ + time_point t(duration(xtime(0))); + gettimeofday((timeval*)&t, 0); + return t; +} + +void test_xtime_clock() +{ + using namespace boost::chrono; + std::cout << "timeval_demo system clock test\n"; + std::cout << "sizeof xtime_clock::time_point = " << sizeof(xtime_clock::time_point) << '\n'; + std::cout << "sizeof xtime_clock::duration = " << sizeof(xtime_clock::duration) << '\n'; + std::cout << "sizeof xtime_clock::rep = " << sizeof(xtime_clock::rep) << '\n'; + xtime_clock::duration delay(milliseconds(5)); + xtime_clock::time_point start = xtime_clock::now(); + while (xtime_clock::now() - start <= delay) + { + } + xtime_clock::time_point stop = xtime_clock::now(); + xtime_clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; +} + +} // timeval_demo + +// Handle duration with resolution not known until run time + +namespace runtime_resolution +{ + +class duration +{ +public: + typedef long long rep; +private: + rep rep_; + + static const double ticks_per_nanosecond; + +public: + typedef boost::chrono::duration<double, boost::nano> tonanosec; + + duration() {} // = default; + explicit duration(const rep& r) : rep_(r) {} + + // conversions + explicit duration(const tonanosec& d) + : rep_(static_cast<rep>(d.count() * ticks_per_nanosecond)) {} + + // explicit + operator tonanosec() const {return tonanosec(rep_/ticks_per_nanosecond);} + + // observer + + rep count() const {return rep_;} + + // arithmetic + + duration& operator+=(const duration& d) {rep_ += d.rep_; return *this;} + duration& operator-=(const duration& d) {rep_ += d.rep_; return *this;} + duration& operator*=(rep rhs) {rep_ *= rhs; return *this;} + duration& operator/=(rep rhs) {rep_ /= rhs; return *this;} + + duration operator+() const {return *this;} + duration operator-() const {return duration(-rep_);} + duration& operator++() {++rep_; return *this;} + duration operator++(int) {return duration(rep_++);} + duration& operator--() {--rep_; return *this;} + duration operator--(int) {return duration(rep_--);} + + friend duration operator+(duration x, duration y) {return x += y;} + friend duration operator-(duration x, duration y) {return x -= y;} + friend duration operator*(duration x, rep y) {return x *= y;} + friend duration operator*(rep x, duration y) {return y *= x;} + friend duration operator/(duration x, rep y) {return x /= y;} + + friend bool operator==(duration x, duration y) {return x.rep_ == y.rep_;} + friend bool operator!=(duration x, duration y) {return !(x == y);} + friend bool operator< (duration x, duration y) {return x.rep_ < y.rep_;} + friend bool operator<=(duration x, duration y) {return !(y < x);} + friend bool operator> (duration x, duration y) {return y < x;} + friend bool operator>=(duration x, duration y) {return !(x < y);} +}; + +static +double +init_duration() +{ + //mach_timebase_info_data_t MachInfo; + //mach_timebase_info(&MachInfo); + //return static_cast<double>(MachInfo.denom) / MachInfo.numer; + return static_cast<double>(1) / 1000; // Windows FILETIME is 1 per microsec +} + +const double duration::ticks_per_nanosecond = init_duration(); + +class clock; + +class time_point +{ +public: + typedef runtime_resolution::clock clock; + typedef long long rep; +private: + rep rep_; + + + rep count() const {return rep_;} +public: + + time_point() : rep_(0) {} + explicit time_point(const duration& d) + : rep_(d.count()) {} + + // arithmetic + + time_point& operator+=(const duration& d) {rep_ += d.count(); return *this;} + time_point& operator-=(const duration& d) {rep_ -= d.count(); return *this;} + + friend time_point operator+(time_point x, duration y) {return x += y;} + friend time_point operator+(duration x, time_point y) {return y += x;} + friend time_point operator-(time_point x, duration y) {return x -= y;} + friend duration operator-(time_point x, time_point y) {return duration(x.rep_ - y.rep_);} +}; + +class clock +{ +public: + typedef duration::rep rep; + typedef runtime_resolution::duration duration; + typedef runtime_resolution::time_point time_point; + + static time_point now() + { + timeval tv; + gettimeofday( &tv, 0 ); + return time_point(duration((static_cast<rep>(tv.tv_sec)<<32) | tv.tv_usec)); + } +}; + +void test() +{ + using namespace boost::chrono; + std::cout << "runtime_resolution test\n"; + clock::duration delay(boost::chrono::milliseconds(5)); + clock::time_point start = clock::now(); + while (clock::now() - start <= delay) + ; + clock::time_point stop = clock::now(); + clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(duration_cast<nanoseconds>(duration::tonanosec(elapsed))).count() + << " nanoseconds\n"; +} + +} // runtime_resolution + +// miscellaneous tests and demos: + + +using namespace boost::chrono; + +void physics_function(duration<double> d) +{ + std::cout << "d = " << d.count() << '\n'; +} + +void drive_physics_function() +{ + physics_function(nanoseconds(3)); + physics_function(hours(3)); + physics_function(duration<double>(2./3)); + std::cout.precision(16); + physics_function( hours(3) + nanoseconds(-3) ); +} + +void test_range() +{ + using namespace boost::chrono; + hours h1 = hours(24 * ( 365 * 292 + 292/4)); + nanoseconds n1 = h1 + nanoseconds(1); + nanoseconds delta = n1 - h1; + std::cout << "292 years of hours = " << h1.count() << "hr\n"; + std::cout << "Add a nanosecond = " << n1.count() << "ns\n"; + std::cout << "Find the difference = " << delta.count() << "ns\n"; +} + +void test_extended_range() +{ + using namespace boost::chrono; + hours h1 = hours(24 * ( 365 * 244000 + 244000/4)); + /*auto*/ microseconds u1 = h1 + microseconds(1); + /*auto*/ microseconds delta = u1 - h1; + std::cout << "244,000 years of hours = " << h1.count() << "hr\n"; + std::cout << "Add a microsecond = " << u1.count() << "us\n"; + std::cout << "Find the difference = " << delta.count() << "us\n"; +} + +template <class Rep, class Period> +void inspect_duration(boost::chrono::duration<Rep, Period> d, const std::string& name) +{ + typedef boost::chrono::duration<Rep, Period> Duration; + std::cout << "********* " << name << " *********\n"; + std::cout << "The period of " << name << " is " << (double)Period::num/Period::den << " seconds.\n"; + std::cout << "The frequency of " << name << " is " << (double)Period::den/Period::num << " Hz.\n"; + std::cout << "The representation is "; + if (boost::is_floating_point<Rep>::value) + { + std::cout << "floating point\n"; + std::cout << "The precision is the most significant "; + std::cout << std::numeric_limits<Rep>::digits10 << " decimal digits.\n"; + } + else if (boost::is_integral<Rep>::value) + { + std::cout << "integral\n"; + d = Duration(Rep(1)); + boost::chrono::duration<double> dsec = d; + std::cout << "The precision is " << dsec.count() << " seconds.\n"; + } + else + { + std::cout << "a class type\n"; + d = Duration(Rep(1)); + boost::chrono::duration<double> dsec = d; + std::cout << "The precision is " << dsec.count() << " seconds.\n"; + } + d = Duration((std::numeric_limits<Rep>::max)()); + using namespace boost::chrono; + using namespace std; + typedef duration<double, boost::ratio_multiply<boost::ratio<24*3652425,10000>, hours::period>::type> Years; + Years years = d; + std::cout << "The range is +/- " << years.count() << " years.\n"; + std::cout << "sizeof(" << name << ") = " << sizeof(d) << '\n'; +} + +void inspect_all() +{ + using namespace boost::chrono; + std::cout.precision(6); + inspect_duration(nanoseconds(), "nanoseconds"); + inspect_duration(microseconds(), "microseconds"); + inspect_duration(milliseconds(), "milliseconds"); + inspect_duration(seconds(), "seconds"); + inspect_duration(minutes(), "minutes"); + inspect_duration(hours(), "hours"); + inspect_duration(duration<double>(), "duration<double>"); +} + +void test_milliseconds() +{ + using namespace boost::chrono; + milliseconds ms(250); + ms += milliseconds(1); + milliseconds ms2(150); + milliseconds msdiff = ms - ms2; + if (msdiff == milliseconds(101)) + std::cout << "success\n"; + else + std::cout << "failure: " << msdiff.count() << '\n'; +} + + using namespace std; + using namespace boost::chrono; + +// Example round_up utility: converts d to To, rounding up for inexact conversions +// Being able to *easily* write this function is a major feature! +template <class To, class Rep, class Period> +To +round_up(duration<Rep, Period> d) +{ + To result = duration_cast<To>(d); + if (result < d) + ++result; + return result; +} + +// demonstrate interaction with xtime-like facility: + +using namespace boost::chrono; + +struct xtime +{ + long sec; + unsigned long usec; +}; + +template <class Rep, class Period> +xtime +to_xtime_truncate(duration<Rep, Period> d) +{ + xtime xt; + xt.sec = static_cast<long>(duration_cast<seconds>(d).count()); + xt.usec = static_cast<long>(duration_cast<microseconds>(d - seconds(xt.sec)).count()); + return xt; +} + +template <class Rep, class Period> +xtime +to_xtime_round_up(duration<Rep, Period> d) +{ + xtime xt; + xt.sec = static_cast<long>(duration_cast<seconds>(d).count()); + xt.usec = static_cast<unsigned long>(round_up<microseconds>(d - seconds(xt.sec)).count()); + return xt; +} + +microseconds +from_xtime(xtime xt) +{ + return seconds(xt.sec) + microseconds(xt.usec); +} + +void print(xtime xt) +{ + cout << '{' << xt.sec << ',' << xt.usec << "}\n"; +} + +void test_with_xtime() +{ + cout << "test_with_xtime\n"; + xtime xt = to_xtime_truncate(seconds(3) + milliseconds(251)); + print(xt); + milliseconds ms = duration_cast<milliseconds>(from_xtime(xt)); + cout << ms.count() << " milliseconds\n"; + xt = to_xtime_round_up(ms); + print(xt); + xt = to_xtime_truncate(seconds(3) + nanoseconds(999)); + print(xt); + xt = to_xtime_round_up(seconds(3) + nanoseconds(999)); + print(xt); +} + +void test_system_clock() +{ + cout << "system_clock test" << endl; + system_clock::duration delay = milliseconds(5); + system_clock::time_point start = system_clock::now(); + while (system_clock::now() - start <= delay) + ; + system_clock::time_point stop = system_clock::now(); + system_clock::duration elapsed = stop - start; + cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; + start = system_clock::now(); + stop = system_clock::now(); + cout << "system_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +} + +void test_steady_clock() +{ + cout << "steady_clock test" << endl; + steady_clock::duration delay = milliseconds(5); + steady_clock::time_point start = steady_clock::now(); + while (steady_clock::now() - start <= delay) + ; + steady_clock::time_point stop = steady_clock::now(); + steady_clock::duration elapsed = stop - start; + cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; + start = steady_clock::now(); + stop = steady_clock::now(); + cout << "steady_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +} + +void test_hi_resolution_clock() +{ + cout << "high_resolution_clock test" << endl; + high_resolution_clock::duration delay = milliseconds(5); + high_resolution_clock::time_point start = high_resolution_clock::now(); + while (high_resolution_clock::now() - start <= delay) + ; + high_resolution_clock::time_point stop = high_resolution_clock::now(); + high_resolution_clock::duration elapsed = stop - start; + cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; + start = high_resolution_clock::now(); + stop = high_resolution_clock::now(); + cout << "high_resolution_clock resolution estimate: " << nanoseconds(stop-start).count() << " nanoseconds\n"; +} + +//void test_mixed_clock() +//{ +// cout << "mixed clock test" << endl; +// high_resolution_clock::time_point hstart = high_resolution_clock::now(); +// cout << "Add 5 milliseconds to a high_resolution_clock::time_point\n"; +// steady_clock::time_point mend = hstart + milliseconds(5); +// bool b = hstart == mend; +// system_clock::time_point sstart = system_clock::now(); +// std::cout << "Subtracting system_clock::time_point from steady_clock::time_point doesn't compile\n"; +//// mend - sstart; // doesn't compile +// cout << "subtract high_resolution_clock::time_point from steady_clock::time_point" +// " and add that to a system_clock::time_point\n"; +// system_clock::time_point send = sstart + duration_cast<system_clock::duration>(mend - hstart); +// cout << "subtract two system_clock::time_point's and output that in microseconds:\n"; +// microseconds ms = send - sstart; +// cout << ms.count() << " microseconds\n"; +//} +// +//void test_c_mapping() +//{ +// cout << "C map test\n"; +// using namespace boost::chrono; +// system_clock::time_point t1 = system_clock::now(); +// std::time_t c_time = system_clock::to_time_t(t1); +// std::tm* tmptr = std::localtime(&c_time); +// std::cout << "It is now " << tmptr->tm_hour << ':' << tmptr->tm_min << ':' << tmptr->tm_sec << ' ' +// << tmptr->tm_year + 1900 << '-' << tmptr->tm_mon + 1 << '-' << tmptr->tm_mday << '\n'; +// c_time = std::mktime(tmptr); +// system_clock::time_point t2 = system_clock::from_time_t(c_time); +// microseconds ms = t1 - t2; +// std::cout << "Round-tripping through the C interface truncated the precision by " << ms.count() << " microseconds\n"; +//} + +void test_duration_division() +{ + cout << hours(3) / milliseconds(5) << '\n'; + cout << milliseconds(5) / hours(3) << '\n'; + cout << hours(1) / milliseconds(1) << '\n'; +} + +namespace I_dont_like_the_default_duration_behavior +{ + +// Here's how you override the duration's default constructor to do anything you want (in this case zero) + +template <class R> +class zero_default +{ +public: + typedef R rep; + +private: + rep rep_; +public: + zero_default(rep i = 0) : rep_(i) {} + operator rep() const {return rep_;} + + zero_default& operator+=(zero_default x) {rep_ += x.rep_; return *this;} + zero_default& operator-=(zero_default x) {rep_ -= x.rep_; return *this;} + zero_default& operator*=(zero_default x) {rep_ *= x.rep_; return *this;} + zero_default& operator/=(zero_default x) {rep_ /= x.rep_; return *this;} + + zero_default operator+ () const {return *this;} + zero_default operator- () const {return zero_default(-rep_);} + zero_default& operator++() {++rep_; return *this;} + zero_default operator++(int) {return zero_default(rep_++);} + zero_default& operator--() {--rep_; return *this;} + zero_default operator--(int) {return zero_default(rep_--);} + + friend zero_default operator+(zero_default x, zero_default y) {return x += y;} + friend zero_default operator-(zero_default x, zero_default y) {return x -= y;} + friend zero_default operator*(zero_default x, zero_default y) {return x *= y;} + friend zero_default operator/(zero_default x, zero_default y) {return x /= y;} + + friend bool operator==(zero_default x, zero_default y) {return x.rep_ == y.rep_;} + friend bool operator!=(zero_default x, zero_default y) {return !(x == y);} + friend bool operator< (zero_default x, zero_default y) {return x.rep_ < y.rep_;} + friend bool operator<=(zero_default x, zero_default y) {return !(y < x);} + friend bool operator> (zero_default x, zero_default y) {return y < x;} + friend bool operator>=(zero_default x, zero_default y) {return !(x < y);} +}; + +typedef boost::chrono::duration<zero_default<long long>, boost::nano > nanoseconds; +typedef boost::chrono::duration<zero_default<long long>, boost::micro > microseconds; +typedef boost::chrono::duration<zero_default<long long>, boost::milli > milliseconds; +typedef boost::chrono::duration<zero_default<long long> > seconds; +typedef boost::chrono::duration<zero_default<long long>, boost::ratio<60> > minutes; +typedef boost::chrono::duration<zero_default<long long>, boost::ratio<3600> > hours; + +void test() +{ + milliseconds ms; + cout << ms.count() << '\n'; +} + +} // I_dont_like_the_default_duration_behavior + +// Build a min for two time_points + +template <class Rep, class Period> +void +print_duration(ostream& os, duration<Rep, Period> d) +{ + os << d.count() << " * " << Period::num << '/' << Period::den << " seconds\n"; +} + +// Example min utility: returns the earliest time_point +// Being able to *easily* write this function is a major feature! +template <class Clock, class Duration1, class Duration2> +inline +typename boost::common_type<time_point<Clock, Duration1>, + time_point<Clock, Duration2> >::type +min BOOST_PREVENT_MACRO_SUBSTITUTION (time_point<Clock, Duration1> t1, time_point<Clock, Duration2> t2) +{ + return t2 < t1 ? t2 : t1; +} + +void test_min() +{ + typedef time_point<system_clock, + boost::common_type<system_clock::duration, seconds>::type> T1; + typedef time_point<system_clock, + boost::common_type<system_clock::duration, nanoseconds>::type> T2; + typedef boost::common_type<T1, T2>::type T3; + /*auto*/ T1 t1 = system_clock::now() + seconds(3); + /*auto*/ T2 t2 = system_clock::now() + nanoseconds(3); + /*auto*/ T3 t3 = (min)(t1, t2); + print_duration(cout, t1 - t3); + print_duration(cout, t2 - t3); +} + +void explore_limits() +{ + typedef duration<long long, boost::ratio_multiply<boost::ratio<24*3652425,10000>, + hours::period>::type> Years; + steady_clock::time_point t1( Years(250)); + steady_clock::time_point t2(-Years(250)); + // nanosecond resolution is likely to overflow. "up cast" to microseconds. + // The "up cast" trades precision for range. + microseconds d = time_point_cast<microseconds>(t1) - time_point_cast<microseconds>(t2); + cout << d.count() << " microseconds\n"; +} + +void manipulate_clock_object(system_clock clock) +{ + system_clock::duration delay = milliseconds(5); + system_clock::time_point start = clock.now(); + while (clock.now() - start <= delay) + ; + system_clock::time_point stop = clock.now(); + system_clock::duration elapsed = stop - start; + cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; +}; + +template <long long speed> +struct cycle_count +{ + typedef typename boost::ratio_multiply<boost::ratio<speed>, boost::mega>::type frequency; // Mhz + typedef typename boost::ratio_divide<boost::ratio<1>, frequency>::type period; + typedef long long rep; + typedef boost::chrono::duration<rep, period> duration; + typedef boost::chrono::time_point<cycle_count> time_point; + + static time_point now() + { + static long long tick = 0; + // return exact cycle count + return time_point(duration(++tick)); // fake access to clock cycle count + } +}; + +template <long long speed> +struct approx_cycle_count +{ + static const long long frequency = speed * 1000000; // MHz + typedef nanoseconds duration; + typedef duration::rep rep; + typedef duration::period period; + static const long long nanosec_per_sec = period::den; + typedef boost::chrono::time_point<approx_cycle_count> time_point; + + static time_point now() + { + static long long tick = 0; + // return cycle count as an approximate number of nanoseconds + // compute as if nanoseconds is only duration in the std::lib + return time_point(duration(++tick * nanosec_per_sec / frequency)); + } +}; + +void cycle_count_delay() +{ + { + typedef cycle_count<400> clock; + cout << "\nSimulated " << clock::frequency::num / boost::mega::num << "MHz clock which has a tick period of " + << duration<double, boost::nano>(clock::duration(1)).count() << " nanoseconds\n"; + nanoseconds delayns(500); + clock::duration delay = duration_cast<clock::duration>(delayns); + cout << "delay = " << delayns.count() << " nanoseconds which is " << delay.count() << " cycles\n"; + clock::time_point start = clock::now(); + clock::time_point stop = start + delay; + while (clock::now() < stop) // no multiplies or divides in this loop + ; + clock::time_point end = clock::now(); + clock::duration elapsed = end - start; + cout << "paused " << elapsed.count() << " cycles "; + cout << "which is " << duration_cast<nanoseconds>(elapsed).count() << " nanoseconds\n"; + } + { + typedef approx_cycle_count<400> clock; + cout << "\nSimulated " << clock::frequency / 1000000 << "MHz clock modeled with nanoseconds\n"; + clock::duration delay = nanoseconds(500); + cout << "delay = " << delay.count() << " nanoseconds\n"; + clock::time_point start = clock::now(); + clock::time_point stop = start + delay; + while (clock::now() < stop) // 1 multiplication and 1 division in this loop + ; + clock::time_point end = clock::now(); + clock::duration elapsed = end - start; + cout << "paused " << elapsed.count() << " nanoseconds\n"; + } + { + typedef cycle_count<1500> clock; + cout << "\nSimulated " << clock::frequency::num / boost::mega::num << "MHz clock which has a tick period of " + << duration<double, boost::nano>(clock::duration(1)).count() << " nanoseconds\n"; + nanoseconds delayns(500); + clock::duration delay = duration_cast<clock::duration>(delayns); + cout << "delay = " << delayns.count() << " nanoseconds which is " << delay.count() << " cycles\n"; + clock::time_point start = clock::now(); + clock::time_point stop = start + delay; + while (clock::now() < stop) // no multiplies or divides in this loop + ; + clock::time_point end = clock::now(); + clock::duration elapsed = end - start; + cout << "paused " << elapsed.count() << " cycles "; + cout << "which is " << duration_cast<nanoseconds>(elapsed).count() << " nanoseconds\n"; + } + { + typedef approx_cycle_count<1500> clock; + cout << "\nSimulated " << clock::frequency / 1000000 << "MHz clock modeled with nanoseconds\n"; + clock::duration delay = nanoseconds(500); + cout << "delay = " << delay.count() << " nanoseconds\n"; + clock::time_point start = clock::now(); + clock::time_point stop = start + delay; + while (clock::now() < stop) // 1 multiplication and 1 division in this loop + ; + clock::time_point end = clock::now(); + clock::duration elapsed = end - start; + cout << "paused " << elapsed.count() << " nanoseconds\n"; + } +} + +void test_special_values() +{ + std::cout << "duration<unsigned>::min().count() = " << (duration<unsigned>::min)().count() << '\n'; + std::cout << "duration<unsigned>::zero().count() = " << duration<unsigned>::zero().count() << '\n'; + std::cout << "duration<unsigned>::max().count() = " << (duration<unsigned>::max)().count() << '\n'; + std::cout << "duration<int>::min().count() = " << (duration<int>::min)().count() << '\n'; + std::cout << "duration<int>::zero().count() = " << duration<int>::zero().count() << '\n'; + std::cout << "duration<int>::max().count() = " << (duration<int>::max)().count() << '\n'; +} + +int main() +{ + basic_examples(); + testStdUser(); + testUser1(); + testUser2(); + drive_physics_function(); + test_range(); + test_extended_range(); + inspect_all(); + test_milliseconds(); + test_with_xtime(); + test_system_clock(); + test_steady_clock(); + test_hi_resolution_clock(); + //test_mixed_clock(); + timeval_demo::test_xtime_clock(); + runtime_resolution::test(); + //test_c_mapping(); + test_duration_division(); + I_dont_like_the_default_duration_behavior::test(); + test_min(); + inspect_duration(common_type<duration<double>, hours, microseconds>::type(), + "common_type<duration<double>, hours, microseconds>::type"); + explore_limits(); + manipulate_clock_object(system_clock()); + duration<double, boost::milli> d = milliseconds(3) * 2.5; + inspect_duration(milliseconds(3) * 2.5, "milliseconds(3) * 2.5"); + cout << d.count() << '\n'; +// milliseconds ms(3.5); // doesn't compile + cout << "milliseconds ms(3.5) doesn't compile\n"; + cycle_count_delay(); + test_special_values(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/time2_demo_output.txt b/src/boost/libs/chrono/example/time2_demo_output.txt new file mode 100644 index 00000000..af978e9a --- /dev/null +++ b/src/boost/libs/chrono/example/time2_demo_output.txt @@ -0,0 +1,191 @@ +// time2_demo.output ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +Running basic examples +sleep_for 3000000 microseconds +sleep_for 1 microseconds +sleep_until 10:47:17.728293 which is 4499340 microseconds away +try_lock_for 30000 microseconds +try_lock_until 10:47:17.728285 which is 4499303 microseconds away +wait_for 60000000 microseconds +wait_until 10:47:17.728285 which is 4499264 microseconds away +sleep_for 250000 microseconds +sleep_until 10:47:14.729077 which is 1499979 microseconds away +*************** +* testStdUser * +*************** +100 hours expressed as hours = 100 +100 hours expressed as nanoseconds = 360000000000000 +200 hours expressed as nanoseconds = 720000000000000 +300 hours expressed as nanoseconds = 1080000000000000 +hr = ns; // does not compile +hr * ns; // does not compile +duration<double> has count() = 2.5 +seconds sec = duration<double> won't compile +seconds has count() = 2 + +************* +* testUser1 * +************* +Speed = 24.5872 meters/sec +Acceleration = 9.81456 meters/sec^2 +Distance = 13.5204 meters +There are 125/201168 miles/meter which is approximately 0.000621371 +There are 201168/125 meters/mile which is approximately 1609.34 +1 attosecond is 1e-18 seconds +sec = as; // compiles +1 second is 1e+18 attoseconds +as = sec; // compiles + +************* +* testUser2 * +************* +100 years expressed as years = 100 +100 years expressed as nanoseconds = 3155695200000000000 +200 years expressed as nanoseconds = 6311390400000000000 +300 years expressed as nanoseconds = inf +yr = ns; // does not compile +ps = yr; // does not compile +100 years expressed as picoseconds = inf +0.1 years expressed as picoseconds = 3155695200000000000 +200 million years ago encoded in years: -200000000 +200 million years ago encoded in days: -73048500000 +200 million years ago encoded in millennium: -200000 +Demonstrate "uninitialized protection" behavior: +nan + +d = 3e-09 +d = 10800 +d = 0.666667 +d = 10799.999999997 +292 years of hours = 2559672hr +Add a nanosecond = 9214819200000000001ns +Find the difference = 1ns +244,000 years of hours = 2138904000hr +Add a microsecond = 7700054400000000001us +Find the difference = 1us +********* nanoseconds ********* +The period of nanoseconds is 1e-09 seconds. +The frequency of nanoseconds is 1e+09 Hz. +The representation is integral +The precision is 1e-09 seconds. +The range is +/- 292.277 years. +sizeof(nanoseconds) = 8 +********* microseconds ********* +The period of microseconds is 1e-06 seconds. +The frequency of microseconds is 1e+06 Hz. +The representation is integral +The precision is 1e-06 seconds. +The range is +/- 292277 years. +sizeof(microseconds) = 8 +********* milliseconds ********* +The period of milliseconds is 0.001 seconds. +The frequency of milliseconds is 1000 Hz. +The representation is integral +The precision is 0.001 seconds. +The range is +/- 2.92277e+08 years. +sizeof(milliseconds) = 8 +********* seconds ********* +The period of seconds is 1 seconds. +The frequency of seconds is 1 Hz. +The representation is integral +The precision is 1 seconds. +The range is +/- 2.92277e+11 years. +sizeof(seconds) = 8 +********* minutes ********* +The period of minutes is 60 seconds. +The frequency of minutes is 0.0166667 Hz. +The representation is integral +The precision is 60 seconds. +The range is +/- 4083.06 years. +sizeof(minutes) = 4 +********* hours ********* +The period of hours is 3600 seconds. +The frequency of hours is 0.000277778 Hz. +The representation is integral +The precision is 3600 seconds. +The range is +/- 244984 years. +sizeof(hours) = 4 +********* duration<double> ********* +The period of duration<double> is 1 seconds. +The frequency of duration<double> is 1 Hz. +The representation is floating point +The precision is the most significant 15 decimal digits. +The range is +/- 5.69666e+300 years. +sizeof(duration<double>) = 8 +success +test_with_xtime +{3,251000} +3251 milliseconds +{3,251000} +{3,0} +{3,1} +system_clock test +paused 5001000 nanoseconds +system_clock resolution estimate: 0 nanoseconds +monotonic_clock test +paused 5000181 nanoseconds +monotonic_clock resolution estimate: 97 nanoseconds +high_resolution_clock test +paused 5000277 nanoseconds +high_resolution_clock resolution estimate: 96 nanoseconds +mixed clock test +Add 5 milliseconds to a high_resolution_clock::time_point +Subtracting system_clock::time_point from monotonic_clock::time_point doesn't compile +subtract high_resolution_clock::time_point from monotonic_clock::time_point and add that to a system_clock::time_point +subtract two system_clock::time_point's and output that in microseconds: +5000 microseconds +timeval_demo system clock test +sizeof xtime_clock::time_point = 8 +sizeof xtime_clock::duration = 8 +sizeof xtime_clock::rep = 8 +paused 5001000 nanoseconds +runtime_resolution test +paused 5000205 nanoseconds +C map test +It is now 10:47:13 2008-4-22 +Round-tripping through the C interface truncated the precision by 255445 microseconds +2160000 +0 +3600000 +0 +2999998997 * 1/1000000000 seconds +0 * 1/1000000000 seconds +15778476000000000 microseconds +paused 5001000 nanoseconds +********* milliseconds(3) * 2.5 ********* +The period of milliseconds(3) * 2.5 is 0.001 seconds. +The frequency of milliseconds(3) * 2.5 is 1000 Hz. +The representation is floating point +The precision is the most significant 15 decimal digits. +The range is +/- 5.69666e+297 years. +sizeof(milliseconds(3) * 2.5) = 8 +7.5 +milliseconds ms(3.5) doesn't compile + +Simulated 400MHz clock which has a tick period of 2.5 nanoseconds +delay = 500 nanoseconds which is 200 cycles +paused 201 cycles which is 502 nanoseconds + +Simulated 400MHz clock modeled with nanoseconds +delay = 500 nanoseconds +paused 503 nanoseconds + +Simulated 1500MHz clock which has a tick period of 0.666667 nanoseconds +delay = 500 nanoseconds which is 750 cycles +paused 751 cycles which is 500 nanoseconds + +Simulated 1500MHz clock modeled with nanoseconds +delay = 500 nanoseconds +paused 500 nanoseconds +duration<unsigned>::min().count() = 0 +duration<unsigned>::zero().count() = 0 +duration<unsigned>::max().count() = 4294967295 +duration<int>::min().count() = -2147483647 +duration<int>::zero().count() = 0 +duration<int>::max().count() = 2147483647 diff --git a/src/boost/libs/chrono/example/timer.hpp b/src/boost/libs/chrono/example/timer.hpp new file mode 100644 index 00000000..c65930d0 --- /dev/null +++ b/src/boost/libs/chrono/example/timer.hpp @@ -0,0 +1,62 @@ +// boost/chrono/timer.hpp ------------------------------------------------------------// + +// Copyright Beman Dawes 2008 +// Copyright 2009 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/system for documentation. + +#ifndef BOOSTEX_CHRONO_TIMER_HPP +#define BOOSTEX_CHRONO_TIMER_HPP + +#include <boost/chrono/chrono.hpp> +#include <boost/system/error_code.hpp> + +namespace boost_ex +{ + namespace chrono + { + +//--------------------------------------------------------------------------------------// +// timer // +//--------------------------------------------------------------------------------------// + + template <class Clock=boost::chrono::high_resolution_clock> + class timer + { + public: + typedef Clock clock; + typedef typename Clock::duration duration; + typedef typename Clock::time_point time_point; + + explicit timer( boost::system::error_code & ec = ::boost::throws() ) + { + start(ec); + } + + ~timer() {} // never throws + + void start( boost::system::error_code & ec = ::boost::throws() ) + { + m_start = clock::now( ec ); + } + + duration elapsed( boost::system::error_code & ec = ::boost::throws() ) + { return clock::now( ec ) - m_start; } + + private: + time_point m_start; + }; + + typedef chrono::timer< boost::chrono::system_clock > system_timer; +#ifdef BOOST_CHRONO_HAS_CLOCK_STEADY + typedef chrono::timer< boost::chrono::steady_clock > steady_timer; +#endif + typedef chrono::timer< boost::chrono::high_resolution_clock > high_resolution_timer; + + } // namespace chrono +} // namespace boost_ex + +#endif diff --git a/src/boost/libs/chrono/example/timeval_demo.cpp b/src/boost/libs/chrono/example/timeval_demo.cpp new file mode 100644 index 00000000..2294489d --- /dev/null +++ b/src/boost/libs/chrono/example/timeval_demo.cpp @@ -0,0 +1,237 @@ +// timeval_demo.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba +// Copyright (c) Microsoft Corporation 2014 + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ +#define _CRT_SECURE_NO_WARNINGS // disable VC++ foolishness + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +#if defined(BOOST_CHRONO_MAC_API) +#include <sys/time.h> //for gettimeofday and timeval +#endif + +#if defined(BOOST_CHRONO_WINDOWS_API) +# include <windows.h> +#endif + +#if defined(BOOST_CHRONO_WINDOWS_API) + +namespace +{ + #if defined UNDER_CE || BOOST_PLAT_WINDOWS_RUNTIME + // Windows CE and Windows store does not define timeval + struct timeval { + long tv_sec; /* seconds */ + long tv_usec; /* and microseconds */ + }; + #endif + + int gettimeofday(struct timeval * tp, void *) + { + FILETIME ft; + #if defined(UNDER_CE) + // Windows CE does not define GetSystemTimeAsFileTime so we do it in two steps. + SYSTEMTIME st; + ::GetSystemTime( &st ); + ::SystemTimeToFileTime( &st, &ft ); + #else + ::GetSystemTimeAsFileTime( &ft ); // never fails + #endif + long long t = (static_cast<long long>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime; + # if !defined( BOOST_MSVC ) || BOOST_MSVC > 1300 // > VC++ 7.0 + t -= 116444736000000000LL; + # else + t -= 116444736000000000; + # endif + t /= 10; // microseconds + tp->tv_sec = static_cast<long>( t / 1000000UL); + tp->tv_usec = static_cast<long>( t % 1000000UL); + return 0; + } +} // unnamed namespace + +#endif + +// timeval clock demo +// Demonstrate the use of a timeval-like struct to be used as the representation +// type for both duraiton and time_point. + +namespace timeval_demo +{ + +class xtime { +private: + long tv_sec; + long tv_usec; + + void fixup() { + if (tv_usec < 0) { + tv_usec += 1000000; + --tv_sec; + } + } + +public: + + explicit xtime(long sec, long usec) { + tv_sec = sec; + tv_usec = usec; + if (tv_usec < 0 || tv_usec >= 1000000) { + tv_sec += tv_usec / 1000000; + tv_usec %= 1000000; + fixup(); + } + } + + explicit xtime(long long usec) + { + tv_usec = static_cast<long>(usec % 1000000); + tv_sec = static_cast<long>(usec / 1000000); + fixup(); + } + + // explicit + operator long long() const {return static_cast<long long>(tv_sec) * 1000000 + tv_usec;} + + xtime& operator += (xtime rhs) { + tv_sec += rhs.tv_sec; + tv_usec += rhs.tv_usec; + if (tv_usec >= 1000000) { + tv_usec -= 1000000; + ++tv_sec; + } + return *this; + } + + xtime& operator -= (xtime rhs) { + tv_sec -= rhs.tv_sec; + tv_usec -= rhs.tv_usec; + fixup(); + return *this; + } + + xtime& operator %= (xtime rhs) { + long long t = tv_sec * 1000000 + tv_usec; + long long r = rhs.tv_sec * 1000000 + rhs.tv_usec; + t %= r; + tv_sec = static_cast<long>(t / 1000000); + tv_usec = static_cast<long>(t % 1000000); + fixup(); + return *this; + } + + friend xtime operator+(xtime x, xtime y) {return x += y;} + friend xtime operator-(xtime x, xtime y) {return x -= y;} + friend xtime operator%(xtime x, xtime y) {return x %= y;} + + friend bool operator==(xtime x, xtime y) + { return (x.tv_sec == y.tv_sec && x.tv_usec == y.tv_usec); } + + friend bool operator<(xtime x, xtime y) { + if (x.tv_sec == y.tv_sec) + return (x.tv_usec < y.tv_usec); + return (x.tv_sec < y.tv_sec); + } + + friend bool operator!=(xtime x, xtime y) { return !(x == y); } + friend bool operator> (xtime x, xtime y) { return y < x; } + friend bool operator<=(xtime x, xtime y) { return !(y < x); } + friend bool operator>=(xtime x, xtime y) { return !(x < y); } + + friend std::ostream& operator<<(std::ostream& os, xtime x) + {return os << '{' << x.tv_sec << ',' << x.tv_usec << '}';} +}; + +class xtime_clock +{ +public: + typedef xtime rep; + typedef boost::micro period; + typedef boost::chrono::duration<rep, period> duration; + typedef boost::chrono::time_point<xtime_clock> time_point; + + static time_point now(); +}; + + +xtime_clock::time_point +xtime_clock::now() +{ +#if defined(BOOST_CHRONO_WINDOWS_API) + timeval tv; + gettimeofday(&tv, 0); + xtime xt( tv.tv_sec, tv.tv_usec); + return time_point(duration(xt)); + +#elif defined(BOOST_CHRONO_MAC_API) + + timeval tv; + gettimeofday(&tv, 0); + xtime xt( tv.tv_sec, tv.tv_usec); + return time_point(duration(xt)); + +#elif defined(BOOST_CHRONO_POSIX_API) + //time_point t(0,0); + + timespec ts; + ::clock_gettime( CLOCK_REALTIME, &ts ); + + xtime xt( ts.tv_sec, ts.tv_nsec/1000); + return time_point(duration(xt)); +#endif // POSIX + +} + +void test_xtime_clock() +{ + using namespace boost::chrono; + std::cout << "timeval_demo system clock test\n"; + std::cout << "sizeof xtime_clock::time_point = " << sizeof(xtime_clock::time_point) << '\n'; + std::cout << "sizeof xtime_clock::duration = " << sizeof(xtime_clock::duration) << '\n'; + std::cout << "sizeof xtime_clock::rep = " << sizeof(xtime_clock::rep) << '\n'; + xtime_clock::duration delay(milliseconds(5)); + xtime_clock::time_point start = xtime_clock::now(); + + while (xtime_clock::now() - start <= delay) + { + } + xtime_clock::time_point stop = xtime_clock::now(); + xtime_clock::duration elapsed = stop - start; + std::cout << "paused " << nanoseconds(elapsed).count() << " nanoseconds\n"; +} + +} // timeval_demo + +int main() +{ + timeval_demo::test_xtime_clock(); + return 0; +} + diff --git a/src/boost/libs/chrono/example/xtime.cpp b/src/boost/libs/chrono/example/xtime.cpp new file mode 100644 index 00000000..4bf4d9db --- /dev/null +++ b/src/boost/libs/chrono/example/xtime.cpp @@ -0,0 +1,108 @@ +// xtime.cpp ----------------------------------------------------------// + +// Copyright 2008 Howard Hinnant +// Copyright 2008 Beman Dawes +// Copyright 2009 Vicente J. Botet Escriba + +// Distributed under the Boost Software License, Version 1.0. +// See http://www.boost.org/LICENSE_1_0.txt + +/* +This code was extracted by Vicente J. Botet Escriba from Beman Dawes time2_demo.cpp which +was derived by Beman Dawes from Howard Hinnant's time2_demo prototype. +Many thanks to Howard for making his code available under the Boost license. +The original code was modified to conform to Boost conventions and to section +20.9 Time utilities [time] of the C++ committee's working paper N2798. +See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2798.pdf. + +time2_demo contained this comment: + + Much thanks to Andrei Alexandrescu, + Walter Brown, + Peter Dimov, + Jeff Garland, + Terry Golubiewski, + Daniel Krugler, + Anthony Williams. +*/ + +#include <boost/chrono/chrono.hpp> +#include <boost/type_traits.hpp> + +#include <iostream> + +using namespace boost::chrono; + +// Example round_up utility: converts d to To, rounding up for inexact conversions +// Being able to *easily* write this function is a major feature! +template <class To, class Rep, class Period> +To +round_up(duration<Rep, Period> d) +{ + To result = duration_cast<To>(d); + if (result < d) + ++result; + return result; +} + +// demonstrate interaction with xtime-like facility: + +struct xtime +{ + long sec; + unsigned long usec; +}; + +template <class Rep, class Period> +xtime +to_xtime_truncate(duration<Rep, Period> d) +{ + xtime xt; + xt.sec = static_cast<long>(duration_cast<seconds>(d).count()); + xt.usec = static_cast<long>(duration_cast<microseconds>(d - seconds(xt.sec)).count()); + return xt; +} + +template <class Rep, class Period> +xtime +to_xtime_round_up(duration<Rep, Period> d) +{ + xtime xt; + xt.sec = static_cast<long>(duration_cast<seconds>(d).count()); + xt.usec = static_cast<unsigned long>(round_up<microseconds>(d - seconds(xt.sec)).count()); + return xt; +} + +microseconds +from_xtime(xtime xt) +{ + return seconds(xt.sec) + microseconds(xt.usec); +} + +void print(xtime xt) +{ + std::cout << '{' << xt.sec << ',' << xt.usec << "}\n"; +} + +void test_with_xtime() +{ + std::cout << "test_with_xtime\n"; + xtime xt = to_xtime_truncate(seconds(3) + milliseconds(251)); + print(xt); + milliseconds ms = duration_cast<milliseconds>(from_xtime(xt)); + std::cout << ms.count() << " milliseconds\n"; + xt = to_xtime_round_up(ms); + print(xt); + xt = to_xtime_truncate(seconds(3) + nanoseconds(999)); + print(xt); + xt = to_xtime_round_up(seconds(3) + nanoseconds(999)); + print(xt); +} + + +int main() +{ + test_with_xtime(); + return 0; +} + |