Added ensure() utility to throw an exception if a condition isn't fulfilled.

This commit is contained in:
Patrick 2024-07-26 23:28:03 +02:00
parent 543d77859d
commit 3a8602edcc

View File

@ -0,0 +1,30 @@
#pragma once
#ifndef MIJIN_UTIL_EXCEPTION_HPP_INCLUDED
#define MIJIN_UTIL_EXCEPTION_HPP_INCLUDED 1
#include <stdexcept>
namespace mijin
{
//
// public defines
//
//
// public functions
//
template<typename TCondition, typename TException = std::runtime_error, typename... TExceptionArgs>
inline decltype(auto) ensure(TCondition&& condition, TExceptionArgs&&... args)
{
if (!static_cast<bool>(std::forward<TCondition>(condition)))
{
throw TException(std::forward<TExceptionArgs>(args)...);
}
return std::forward<TCondition>(condition);
}
} // namespace mijin
#endif // MIJIN_UTIL_EXCEPTION_HPP_INCLUDED