From: Francis Deslauriers Date: Tue, 19 Apr 2022 20:49:47 +0000 (-0400) Subject: cpp-common: Add common exceptions X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=f197dce340f4723816cd473b7803129911606008 cpp-common: Add common exceptions Add `bt2_common::{End, Error, MemoryError, TryAgain}` exception classes to use in in-tree components and elsewhere. Each exception inherits a C++ standard exception that I believe matches more closely in meaning. Signed-off-by: Francis Deslauriers Change-Id: I7335465f2377e032ae9706108708e35d78d38e6e Reviewed-on: https://review.lttng.org/c/babeltrace/+/7918 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/10809 Tested-by: jenkins CI-Build: Philippe Proulx --- diff --git a/src/cpp-common/Makefile.am b/src/cpp-common/Makefile.am index 761a23e0..bcf43c22 100644 --- a/src/cpp-common/Makefile.am +++ b/src/cpp-common/Makefile.am @@ -1,3 +1,8 @@ # SPDX-License-Identifier: MIT -EXTRA_DIST = bt2 optional.hpp string_view.hpp nlohmann/json.hpp log-cfg.hpp +EXTRA_DIST = bt2 \ + exc.hpp \ + log-cfg.hpp \ + nlohmann/json.hpp \ + optional.hpp \ + string_view.hpp diff --git a/src/cpp-common/exc.hpp b/src/cpp-common/exc.hpp new file mode 100644 index 00000000..f624c26f --- /dev/null +++ b/src/cpp-common/exc.hpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 Francis Deslauriers + * + * SPDX-License-Identifier: MIT + */ + +#ifndef BABELTRACE_CPP_COMMON_EXC_HPP +#define BABELTRACE_CPP_COMMON_EXC_HPP + +#include +#include +#include +#include + +namespace bt2_common { + +/* + * End of iteration. + */ +class End : public std::exception +{ +public: + explicit End() noexcept : std::exception {} + { + } +}; + +/* + * General error. + */ +class Error : public std::runtime_error +{ +public: + explicit Error(std::string msg = "Error") : std::runtime_error {std::move(msg)} + { + } +}; + +/* + * Memory error. + */ +class MemoryError : public std::bad_alloc +{ +public: + explicit MemoryError() noexcept : std::bad_alloc {} + { + } +}; + +/* + * Not available right now: try again later. + */ +class TryAgain : public std::exception +{ +public: + explicit TryAgain() noexcept : std::exception {} + { + } +}; + +} /* namespace bt2_common */ + +#endif /* BABELTRACE_CPP_COMMON_EXC_HPP */