diff --git a/include/boost/uuid/detail/throw_invalid_uuid.hpp b/include/boost/uuid/detail/throw_invalid_uuid.hpp index 9b15142..900a046 100644 --- a/include/boost/uuid/detail/throw_invalid_uuid.hpp +++ b/include/boost/uuid/detail/throw_invalid_uuid.hpp @@ -1,41 +1,21 @@ #ifndef BOOST_UUID_DETAIL_THROW_INVALID_UUID_INCLUDED #define BOOST_UUID_DETAIL_THROW_INVALID_UUID_INCLUDED -// Copyright 2025 Peter Dimov +// Copyright 2025, 2026 Peter Dimov // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt -#include +#include #include -#include -#include -#include -#include +#include namespace boost { namespace uuids { namespace detail { -BOOST_CXX14_CONSTEXPR inline char const* fc_error_to_string( from_chars_error err ) noexcept +BOOST_NORETURN inline void throw_invalid_uuid( std::ptrdiff_t pos, from_chars_error err, boost::source_location const& loc = BOOST_CURRENT_LOCATION ) { - switch( err ) - { - case from_chars_error::none: return "no error"; - case from_chars_error::unexpected_end_of_input: return "unexpected end of input"; - case from_chars_error::hex_digit_expected: return "hex digit expected"; - case from_chars_error::dash_expected: return "dash expected"; - case from_chars_error::closing_brace_expected: return "closing brace expected"; - case from_chars_error::unexpected_extra_input: return "unexpected extra input"; - default: return "unknown error"; - } -} - -BOOST_NORETURN inline void throw_invalid_uuid( std::ptrdiff_t pos, from_chars_error err ) -{ - char buffer[ 128 ]; - std::snprintf( buffer, sizeof( buffer ), "Invalid UUID string at position %td: %s", pos, fc_error_to_string( err ) ); - - BOOST_THROW_EXCEPTION( std::runtime_error( buffer ) ); + boost::throw_exception( invalid_uuid( pos, err ), loc ); } }}} // namespace boost::uuids::detail diff --git a/include/boost/uuid/invalid_uuid.hpp b/include/boost/uuid/invalid_uuid.hpp new file mode 100644 index 0000000..0a19335 --- /dev/null +++ b/include/boost/uuid/invalid_uuid.hpp @@ -0,0 +1,72 @@ +#ifndef BOOST_UUID_INVALID_UUID_HPP_INCLUDED +#define BOOST_UUID_INVALID_UUID_HPP_INCLUDED + +// Copyright 2026 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include +#include +#include + +namespace boost { +namespace uuids { + +namespace detail +{ + +BOOST_CXX14_CONSTEXPR inline char const* fc_error_to_string( from_chars_error err ) noexcept +{ + switch( err ) + { + case from_chars_error::none: return "no error"; + case from_chars_error::unexpected_end_of_input: return "unexpected end of input"; + case from_chars_error::hex_digit_expected: return "hex digit expected"; + case from_chars_error::dash_expected: return "dash expected"; + case from_chars_error::closing_brace_expected: return "closing brace expected"; + case from_chars_error::unexpected_extra_input: return "unexpected extra input"; + default: return "unknown error"; + } +} + +} // namespace detail + +class BOOST_SYMBOL_VISIBLE invalid_uuid: public std::runtime_error +{ +private: + + std::ptrdiff_t pos_; + from_chars_error err_; + +private: + + static std::runtime_error create_base( std::ptrdiff_t pos, from_chars_error err ) + { + char buffer[ 128 ]; + std::snprintf( buffer, sizeof( buffer ), "Invalid UUID string at position %td: %s", pos, detail::fc_error_to_string( err ) ); + + return std::runtime_error( buffer ); + } + +public: + + invalid_uuid( std::ptrdiff_t pos, from_chars_error err ): std::runtime_error( create_base( pos, err ) ), pos_( pos ), err_( err ) + { + } + + std::ptrdiff_t position() const noexcept + { + return pos_; + } + + from_chars_error error() const noexcept + { + return err_; + } +}; + +}} // namespace boost::uuids + +#endif // BOOST_UUID_INVALID_UUID_HPP_INCLUDED