time_generator_v6.hpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef BOOST_UUID_TIME_GENERATOR_V6_HPP_INCLUDED
  2. #define BOOST_UUID_TIME_GENERATOR_V6_HPP_INCLUDED
  3. // Copyright 2024 Peter Dimov
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // https://www.boost.org/LICENSE_1_0.txt
  6. #include <boost/uuid/time_generator_v1.hpp>
  7. #include <boost/uuid/uuid.hpp>
  8. #include <boost/uuid/detail/endian.hpp>
  9. namespace boost {
  10. namespace uuids {
  11. // time_generator_v6
  12. class time_generator_v6: public time_generator_v1
  13. {
  14. public:
  15. using result_type = uuid;
  16. using time_generator_v1::time_generator_v1;
  17. result_type operator()() noexcept;
  18. };
  19. // operator()
  20. inline time_generator_v6::result_type time_generator_v6::operator()() noexcept
  21. {
  22. uuid result = time_generator_v1::operator()();
  23. std::uint64_t timestamp = result.timestamp_v1();
  24. std::uint32_t time_high = static_cast< std::uint32_t >( timestamp >> 28 );
  25. detail::store_big_u32( result.data + 0, time_high );
  26. std::uint16_t time_mid = static_cast< std::uint16_t >( timestamp >> 12 );
  27. detail::store_big_u16( result.data + 4, time_mid );
  28. std::uint16_t time_low_and_version = static_cast< std::uint16_t >( timestamp & 0xFFF ) | 0x6000;
  29. detail::store_big_u16( result.data + 6, time_low_and_version );
  30. return result;
  31. }
  32. }} // namespace boost::uuids
  33. #endif // BOOST_UUID_TIME_GENERATOR_V1_HPP_INCLUDED