Added ensure() utility to throw an exception if a condition isn't fulfilled.
This commit is contained in:
parent
543d77859d
commit
3a8602edcc
30
source/mijin/util/exception.hpp
Normal file
30
source/mijin/util/exception.hpp
Normal 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
|
||||
Loading…
x
Reference in New Issue
Block a user