Remove duplicate util::key_error exception, don't require the key to be convertible to string

This commit is contained in:
Nikita Lisitsa 2024-01-29 17:43:52 +03:00
parent 470b7a0757
commit 368d1edd71
3 changed files with 20 additions and 31 deletions

View file

@ -2,12 +2,30 @@
#include <psemek/util/exception.hpp>
#include <psemek/util/to_string.hpp>
#include <psemek/util/type_name.hpp>
#include <vector>
namespace psemek::util
{
namespace detail
{
template <typename Key>
std::string key_error_to_string(Key const & key, decltype(util::to_string(key)) *)
{
return util::to_string("key ", key, " not found of type ", type_name<Key>());
}
template <typename Key>
std::string key_error_to_string(Key const &, ...)
{
return util::to_string("key not found of type ", type_name<Key>());
}
}
struct key_error_base
: exception
{
@ -19,7 +37,7 @@ namespace psemek::util
: key_error_base
{
key_error(Key const & key, boost::stacktrace::stacktrace stacktrace = {})
: key_error_base(util::to_string("no such key: ", key), std::move(stacktrace))
: key_error_base(detail::key_error_to_string(key), std::move(stacktrace))
, key_(key)
{}

View file

@ -1,29 +0,0 @@
#pragma once
#include <psemek/util/to_string.hpp>
#include <psemek/util/type_name.hpp>
#include <stdexcept>
namespace psemek::util
{
template <typename Key>
struct key_error
: std::out_of_range
{
key_error(Key const & key)
: std::out_of_range(to_string("unknown key ", key, " of type ", type_name<Key>()))
, key_(key)
{}
Key const & key() const noexcept
{
return key_;
}
private:
Key key_;
};
}

View file

@ -1,7 +1,7 @@
#pragma once
#include <psemek/util/functional.hpp>
#include <psemek/util/key_error.hpp>
#include <psemek/util/at.hpp>
#include <list>
#include <unordered_map>