errored_clock.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // errored_clock.hpp --------------------------------------------------------------//
  2. // Copyright 2010 Vicente J. Botet Escriba
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. #ifndef BOOST_CHRONO_ERRORED_CLOCKS_HPP
  6. #define BOOST_CHRONO_ERRORED_CLOCKS_HPP
  7. #include <boost/chrono/config.hpp>
  8. #include <boost/chrono/duration.hpp>
  9. #include <boost/chrono/time_point.hpp>
  10. #include <boost/system/error_code.hpp>
  11. #include <boost/system/system_error.hpp>
  12. #include <boost/throw_exception.hpp>
  13. #include <boost/chrono/detail/system.hpp>
  14. class errored_clock
  15. {
  16. public:
  17. typedef boost::chrono::nanoseconds duration;
  18. typedef duration::rep rep;
  19. typedef duration::period period;
  20. typedef boost::chrono::time_point<errored_clock> time_point;
  21. static const bool is_steady = true;
  22. static int errno_;
  23. static void set_errno(int err) {
  24. errno_=err;
  25. }
  26. // throws on error
  27. static time_point now() {
  28. boost::throw_exception(
  29. boost::system::system_error(
  30. errno_,
  31. ::boost::system::system_category(),
  32. "errored_clock"
  33. )
  34. );
  35. return time_point();
  36. }
  37. // never throws and set ec
  38. static time_point now(boost::system::error_code & ec) {
  39. if (::boost::chrono::is_throws(ec))
  40. {
  41. boost::throw_exception(
  42. boost::system::system_error(
  43. errno_,
  44. ::boost::system::system_category(),
  45. "errored_clock"
  46. )
  47. );
  48. }
  49. ec.assign( errno_, ::boost::system::system_category() );
  50. return time_point();
  51. };
  52. };
  53. int errored_clock::errno_;
  54. #endif