container: root: add internal throw/exception handler

This commit is contained in:
2024-07-13 19:43:15 -07:00
parent cc832c6f77
commit 612f9e5499
2 changed files with 87 additions and 0 deletions

View File

@@ -25,10 +25,36 @@
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
//! \brief Conditional throw handler
//! \ingroup cpp_type_exceptions
#define THROW_IF(condition, exception, message) \
if (condition) \
THROW(exception, message);
//! \brief Throw handler
//! \ingroup cpp_type_exceptions
#define THROW(exception, message) \
THROW_IMPL(exception, \
\n\tFILE = __FILE__ \
\n\tLINE = __LINE__ \
\n\tWHAT = message);
//! \brief Throw handler implementation
//! \warning Should not be called directly, use THROW handlers
//! \ingroup cpp_type_exceptions
#define THROW_IMPL(exception, message) \
throw exception(THROW_IMPL_EXPAND(message));
//! \brief Throw handler implementation (message)
//! \warning Should not be called directly, use THROW handlers
//! \ingroup cpp_type_exceptions
#define THROW_IMPL_EXPAND(message) #message
//! \namespace docker_finance
//! \since docker-finance 1.0.0
namespace docker_finance
@@ -44,6 +70,63 @@ namespace internal
//! \todo *_v for all the booleans
namespace type
{
//! \brief Base exception class
//! \ingroup cpp_type_exceptions
class Exception : virtual public std::exception
{
public:
//! \brief Exception type
enum struct kType : uint8_t
{
RuntimeError,
InvalidArgument,
};
//! \brief Construct by type with given message
Exception(const kType type, const std::string_view what)
: m_type(type), m_what(what)
{
}
virtual ~Exception() = default;
Exception(const Exception&) = default;
Exception& operator=(const Exception&) = default;
Exception(Exception&&) = default;
Exception& operator=(Exception&&) = default;
public:
//! \return Exeption type
kType type() const noexcept { return m_type; }
//! \return Exception message
const char* what() const noexcept { return m_what.data(); }
private:
kType m_type;
std::string_view m_what;
};
//! \brief Exception class for runtime errors
//! \ingroup cpp_type_exceptions
struct RuntimeError final : public Exception
{
explicit RuntimeError(const std::string_view what = {})
: Exception(Exception::kType::RuntimeError, what)
{
}
};
//! \brief Exception class for invalid arguments (logic error)
//! \ingroup cpp_type_exceptions
struct InvalidArgument final : public Exception
{
explicit InvalidArgument(const std::string_view what = {})
: Exception(Exception::kType::InvalidArgument, what)
{
}
};
//! \ingroup cpp_type_traits
template <typename t_type>
struct is_byte : std::false_type