container: root: rewrite throw handler, refactor using common types

- Refactor using common types that were once internal
- Removes ancient macro approaches to C++20 solutions
- Changes `Exception` message type to use std::string
  * std::string_view isn't worthwhile in this context
This commit is contained in:
2025-11-19 16:37:46 -08:00
parent fa91fd02e8
commit ca59169116
11 changed files with 134 additions and 121 deletions

View File

@@ -30,6 +30,8 @@
#include <string>
#include <vector>
#include "./type.hh"
//! \namespace dfi
//! \since docker-finance 1.0.0
namespace dfi
@@ -39,6 +41,51 @@ namespace dfi
//! \since docker-finance 1.1.0
namespace common
{
//! \concept Exception
//! \brief Exception type concept for `dfi` exceptions
//! \ingroup cpp_common_impl
//! \since docker-finance 1.1.0
template <typename t_exception>
concept Exception = std::derived_from<t_exception, type::Exception>;
//! \brief Throw exception using `dfi` exception type
//! \param message Message to pass to exception type
//! \param location Source location of exception
//! \ingroup cpp_common_impl
//! \since docker-finance 1.1.0
template <Exception t_exception>
inline void throw_ex(
const std::string& message = {},
const std::source_location location = std::source_location::current())
{
// TODO(unassigned): when throwing because of heap allocation
std::string msg;
msg += std::string(" \n\tFILE = ") + location.file_name();
msg += std::string(" \n\tFUNC = ") + location.function_name();
msg += std::string(" \n\tLINE = ") + location.line();
msg += std::string(" \n\tWHAT = ") + message;
throw t_exception(msg);
}
//! \brief Throw exception using `dfi` exception type (but only on condition)
//! \param condition If boolean condition is true, throw
//! \param message Message to pass to exception type
//! \param location Source location of exception
//! \ingroup cpp_common_impl
//! \since docker-finance 1.1.0
template <Exception t_exception>
inline void throw_ex_if(
const bool condition,
const std::string& message = {},
const std::source_location location = std::source_location::current())
{
if (condition)
{
throw_ex<t_exception>(message, location);
}
}
//! \brief Wrapper to ROOT Cling commands
//! \ingroup cpp_common_impl
//! \since docker-finance 1.0.0
@@ -69,16 +116,13 @@ class Command
static void load(const std::string& path)
{
std::filesystem::path p(path);
if (!std::filesystem::exists(p))
{
throw std::runtime_error(
std::string{"'" + path + "' does not exist!"}.c_str());
}
if (!std::filesystem::is_regular_file(p))
{
throw std::runtime_error(
std::string{"'" + path + "' is not a regular file!"}.c_str());
}
throw_ex_if<type::RuntimeError>(
!std::filesystem::exists(p), "'" + path + "' does not exist!");
throw_ex_if<type::RuntimeError>(
!std::filesystem::is_regular_file(p),
"'" + path + "' is not a regular file!");
std::string cmd{".L " + path};
Command::cmd_handler({cmd});
@@ -101,9 +145,10 @@ class Command
std::string get_env(const std::string& var)
{
const auto* env = gSystem->Getenv(var.c_str());
if (!env)
throw std::runtime_error(
std::string{var + " is not set or is unavailable"}.c_str());
throw_ex_if<type::RuntimeError>(
!env, "'" + var + "' is not set or is unavailable");
return std::string{env};
}