diff --git a/NuFX/nufx_error.cpp b/NuFX/nufx_error.cpp new file mode 100644 index 0000000..ab1796a --- /dev/null +++ b/NuFX/nufx_error.cpp @@ -0,0 +1,48 @@ +#include "nufx_error.h" +#include + +namespace { + class private_error_category : public std::error_category { + + virtual std::error_condition default_error_condition( int code ) const noexcept; + virtual std::string message( int condition ) const noexcept; + virtual const char* name() const noexcept; + }; + + std::error_condition private_error_category::default_error_condition( int code ) const noexcept + { + + switch(code) + { + case kNuErrInvalidArg: + return std::errc::invalid_argument; + case kNuErrMalloc: + return std::errc::not_enough_memory; + case kNuErrFileNotFound: + return std::errc::no_such_file_or_directory; + case kNuErrNotDir: + return std::errc::not_a_directory; + case kNuErrFileAccessDenied: + return std::errc::permission_denied; + + default: + return std::error_condition(code, *this); + } + } + + const char* private_error_category::name() const noexcept + { + return "NuFX Error"; + } + std::string private_error_category::message( int condition ) const noexcept + { + return NuStrError((NuError)condition); + } + +} + +std::error_category &nufx_category() +{ + static private_error_category ec; + return ec; +} diff --git a/NuFX/nufx_error.h b/NuFX/nufx_error.h new file mode 100644 index 0000000..f310a15 --- /dev/null +++ b/NuFX/nufx_error.h @@ -0,0 +1,28 @@ +#ifndef __nufx_error__ +#define __nufx_error__ + + +#include +#include + + std::error_category &nufx_category(); + +namespace std { + + template<> + struct is_error_code_enum : public true_type {}; + +} + +// hmm... this should not be in a namespace. +inline std::error_condition make_error_condition(NuError e) +{ + + // positive values are posix errors. + return e < 0 + ? std::error_condition(e, nufx_category()); + : std::error_condition(e, std::system_category()); +} + + +#endif