c++11 error support for NufxLib

This commit is contained in:
Kelvin Sherlock 2016-01-20 23:40:07 -05:00
parent 830963c0a0
commit a98215e9b4
2 changed files with 76 additions and 0 deletions

48
NuFX/nufx_error.cpp Normal file
View File

@ -0,0 +1,48 @@
#include "nufx_error.h"
#include <string>
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;
}

28
NuFX/nufx_error.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef __nufx_error__
#define __nufx_error__
#include <system_error>
#include <NufxLib.h>
std::error_category &nufx_category();
namespace std {
template<>
struct is_error_code_enum<NuError> : 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