numeric_cast.hpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #ifndef BOOST_UUID_DETAIL_NUMERIC_CAST_INCLUDED
  2. #define BOOST_UUID_DETAIL_NUMERIC_CAST_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/detail/static_assert.hpp>
  7. #include <boost/throw_exception.hpp>
  8. #include <stdexcept>
  9. #include <limits>
  10. #include <type_traits>
  11. namespace boost {
  12. namespace uuids {
  13. namespace detail {
  14. template<class T, class U> T numeric_cast( U u )
  15. {
  16. BOOST_UUID_STATIC_ASSERT( std::is_integral<T>::value );
  17. BOOST_UUID_STATIC_ASSERT( std::is_unsigned<T>::value );
  18. BOOST_UUID_STATIC_ASSERT( std::is_integral<U>::value );
  19. BOOST_UUID_STATIC_ASSERT( std::is_unsigned<U>::value );
  20. if( u > std::numeric_limits<T>::max() )
  21. {
  22. BOOST_THROW_EXCEPTION( std::range_error( "Argument to numeric_cast is out of range of destination type" ) );
  23. }
  24. return static_cast<T>( u );
  25. }
  26. } // detail
  27. } // uuids
  28. } // boost
  29. #endif // #ifndef BOOST_UUID_DETAIL_NUMERIC_CAST_INCLUDED