forked from EvergreenCrypto/docker-finance
container: root: factor out internal types to common
These types and handlers are too generic to be kept internal only. They can now be used across all root macros, plugins and overall API.
This commit is contained in:
130
container/src/root/common/type.hh
Normal file
130
container/src/root/common/type.hh
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
// docker-finance | modern accounting for the power-user
|
||||||
|
//
|
||||||
|
// Copyright (C) 2021-2025 Aaron Fiore (Founder, Evergreen Crypto LLC)
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
//! \file
|
||||||
|
//! \author Aaron Fiore (Founder, Evergreen Crypto LLC)
|
||||||
|
//! \note File intended to be loaded into ROOT.cern framework / Cling interpreter
|
||||||
|
//! \since docker-finance 1.1.0
|
||||||
|
|
||||||
|
#ifndef CONTAINER_SRC_ROOT_COMMON_TYPE_HH_
|
||||||
|
#define CONTAINER_SRC_ROOT_COMMON_TYPE_HH_
|
||||||
|
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
//! \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 dfi
|
||||||
|
//! \since docker-finance 1.0.0
|
||||||
|
namespace dfi
|
||||||
|
{
|
||||||
|
//! \namespace dfi::common
|
||||||
|
//! \brief Shared common functionality
|
||||||
|
//! \since docker-finance 1.1.0
|
||||||
|
namespace common
|
||||||
|
{
|
||||||
|
//! \namespace dfi::common::type
|
||||||
|
//! \brief docker-finance defined common types
|
||||||
|
//! \since docker-finance 1.1.0
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace type
|
||||||
|
} // namespace common
|
||||||
|
} // namespace dfi
|
||||||
|
|
||||||
|
#endif // CONTAINER_SRC_ROOT_COMMON_TYPE_HH_
|
||||||
|
|
||||||
|
// # vim: sw=2 sts=2 si ai et
|
||||||
@@ -30,30 +30,7 @@
|
|||||||
#include <string_view>
|
#include <string_view>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
//! \brief Conditional throw handler
|
#include "../../common/type.hh"
|
||||||
//! \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 dfi
|
//! \namespace dfi
|
||||||
//! \since docker-finance 1.0.0
|
//! \since docker-finance 1.0.0
|
||||||
@@ -70,62 +47,20 @@ namespace internal
|
|||||||
//! \todo *_v for all the booleans
|
//! \todo *_v for all the booleans
|
||||||
namespace type
|
namespace type
|
||||||
{
|
{
|
||||||
//! \brief Base exception class
|
//! \deprecated This will be removed in the v2 API; use `dfi::common::type` namespace instead
|
||||||
//! \ingroup cpp_type_exceptions
|
//! \todo Remove in 2.0.0
|
||||||
class Exception : virtual public std::exception
|
//! \since docker-finance 1.1.0
|
||||||
{
|
using Exception = ::dfi::common::type::Exception;
|
||||||
public:
|
|
||||||
//! \brief Exception type
|
|
||||||
enum struct kType : uint8_t
|
|
||||||
{
|
|
||||||
RuntimeError,
|
|
||||||
InvalidArgument,
|
|
||||||
};
|
|
||||||
|
|
||||||
//! \brief Construct by type with given message
|
//! \deprecated This will be removed in the v2 API; use `dfi::common::type` namespace instead
|
||||||
Exception(const kType type, const std::string_view what)
|
//! \todo Remove in 2.0.0
|
||||||
: m_type(type), m_what(what)
|
//! \since docker-finance 1.1.0
|
||||||
{
|
using RuntimeError = ::dfi::common::type::RuntimeError;
|
||||||
}
|
|
||||||
virtual ~Exception() = default;
|
|
||||||
|
|
||||||
Exception(const Exception&) = default;
|
//! \deprecated This will be removed in the v2 API; use `dfi::common::type` namespace instead
|
||||||
Exception& operator=(const Exception&) = default;
|
//! \todo Remove in 2.0.0
|
||||||
|
//! \since docker-finance 1.1.0
|
||||||
Exception(Exception&&) = default;
|
using InvalidArgument = ::dfi::common::type::InvalidArgument;
|
||||||
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
|
//! \ingroup cpp_type_traits
|
||||||
template <typename t_type>
|
template <typename t_type>
|
||||||
|
|||||||
Reference in New Issue
Block a user