mirror of
https://github.com/matrix-construct/construct
synced 2024-06-17 17:38:22 +02:00
101 lines
1.9 KiB
C++
101 lines
1.9 KiB
C++
// Matrix Construct
|
|
//
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
#ifndef __GNUC__
|
|
#pragma STDC FENV_ACCESS on
|
|
#endif
|
|
|
|
ircd::fpe::errors_handle::errors_handle()
|
|
{
|
|
syscall(std::fegetexceptflag, &theirs, FE_ALL_EXCEPT);
|
|
clear_pending();
|
|
}
|
|
|
|
ircd::fpe::errors_handle::~errors_handle()
|
|
noexcept(false)
|
|
{
|
|
const auto pending(this->pending());
|
|
syscall(std::fesetexceptflag, &theirs, FE_ALL_EXCEPT);
|
|
throw_errors(pending);
|
|
}
|
|
|
|
void
|
|
ircd::fpe::errors_handle::clear_pending()
|
|
{
|
|
syscall(std::feclearexcept, FE_ALL_EXCEPT);
|
|
}
|
|
|
|
void
|
|
ircd::fpe::errors_handle::throw_pending()
|
|
const
|
|
{
|
|
throw_errors(pending());
|
|
}
|
|
|
|
ushort
|
|
ircd::fpe::errors_handle::pending()
|
|
const
|
|
{
|
|
return std::fetestexcept(FE_ALL_EXCEPT);
|
|
}
|
|
|
|
//
|
|
// util
|
|
//
|
|
|
|
void
|
|
ircd::fpe::throw_errors(const ushort &flags)
|
|
{
|
|
if(!flags)
|
|
return;
|
|
|
|
thread_local char buf[128];
|
|
throw std::domain_error
|
|
{
|
|
reflect(buf, flags)
|
|
};
|
|
}
|
|
|
|
ircd::string_view
|
|
ircd::fpe::reflect(const mutable_buffer &buf,
|
|
const ushort &flags)
|
|
{
|
|
window_buffer wb{buf};
|
|
const auto append{[&wb](const auto &flag)
|
|
{
|
|
wb([&flag](const mutable_buffer &buf)
|
|
{
|
|
return strlcpy(buf, reflect(flag));
|
|
});
|
|
}};
|
|
|
|
for(size_t i(0); i < sizeof(flags) * 8; ++i)
|
|
if(flags & (1 << i))
|
|
append(1 << i);
|
|
|
|
return wb.completed();
|
|
}
|
|
|
|
ircd::string_view
|
|
ircd::fpe::reflect(const ushort &flag)
|
|
{
|
|
switch(flag)
|
|
{
|
|
case 0: return "";
|
|
case FE_INVALID: return "INVALID";
|
|
case FE_DIVBYZERO: return "DIVBYZERO";
|
|
case FE_UNDERFLOW: return "UNDERFLOW";
|
|
case FE_OVERFLOW: return "OVERFLOW";
|
|
case FE_INEXACT: return "INEXACT";
|
|
}
|
|
|
|
return "?????";
|
|
}
|