123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_POSIX_CODE_HPP
- #define BOOST_OUTCOME_SYSTEM_ERROR2_POSIX_CODE_HPP
- #ifdef BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX
- #error <posix_code.hpp> is not includable when BOOST_OUTCOME_SYSTEM_ERROR2_NOT_POSIX is defined!
- #endif
- #include "quick_status_code_from_enum.hpp"
- #include <cstring> // for strchr and strerror_r
- BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
- #if !defined(_MSC_VER) && !defined(__APPLE__)
- namespace detail
- {
- namespace avoid_string_include
- {
- #if defined(__GLIBC__) && !defined(__UCLIBC__)
-
- extern "C" char *strerror_r(int errnum, char *buf, size_t buflen);
- #else
- extern "C" int strerror_r(int errnum, char *buf, size_t buflen);
- #endif
- }
- }
- #endif
- class _posix_code_domain;
- using posix_code = status_code<_posix_code_domain>;
- using posix_error = status_error<_posix_code_domain>;
- namespace mixins
- {
- template <class Base> struct mixin<Base, _posix_code_domain> : public Base
- {
- using Base::Base;
-
- static posix_code current() noexcept;
- };
- }
- class _posix_code_domain : public status_code_domain
- {
- template <class DomainType> friend class status_code;
- using _base = status_code_domain;
- static _base::string_ref _make_string_ref(int c) noexcept
- {
- char buffer[1024] = "";
- #ifdef _WIN32
- strerror_s(buffer, sizeof(buffer), c);
- #elif defined(__GLIBC__) && !defined(__UCLIBC__)
- char *s = detail::avoid_string_include::strerror_r(c, buffer, sizeof(buffer));
- if(s != nullptr)
- {
- strncpy(buffer, s, sizeof(buffer) - 1);
- buffer[1023] = 0;
- }
- #elif !defined(__APPLE__)
- detail::avoid_string_include::strerror_r(c, buffer, sizeof(buffer));
- #else
- strerror_r(c, buffer, sizeof(buffer));
- #endif
- size_t length = strlen(buffer);
- auto *p = static_cast<char *>(malloc(length + 1));
- if(p == nullptr)
- {
- return _base::string_ref("failed to get message from system");
- }
- memcpy(p, buffer, length + 1);
- return _base::atomic_refcounted_string_ref(p, length);
- }
- public:
-
- using value_type = int;
- using _base::string_ref;
-
- constexpr explicit _posix_code_domain(typename _base::unique_id_type id = 0xa59a56fe5f310933) noexcept
- : _base(id)
- {
- }
- _posix_code_domain(const _posix_code_domain &) = default;
- _posix_code_domain(_posix_code_domain &&) = default;
- _posix_code_domain &operator=(const _posix_code_domain &) = default;
- _posix_code_domain &operator=(_posix_code_domain &&) = default;
- ~_posix_code_domain() = default;
-
- static inline constexpr const _posix_code_domain &get();
- virtual string_ref name() const noexcept override { return string_ref("posix domain"); }
- virtual payload_info_t payload_info() const noexcept override
- {
- return {sizeof(value_type), sizeof(status_code_domain *) + sizeof(value_type),
- (alignof(value_type) > alignof(status_code_domain *)) ? alignof(value_type) : alignof(status_code_domain *)};
- }
- protected:
- virtual bool _do_failure(const status_code<void> &code) const noexcept override
- {
- assert(code.domain() == *this);
- return static_cast<const posix_code &>(code).value() != 0;
- }
- virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override
- {
- assert(code1.domain() == *this);
- const auto &c1 = static_cast<const posix_code &>(code1);
- if(code2.domain() == *this)
- {
- const auto &c2 = static_cast<const posix_code &>(code2);
- return c1.value() == c2.value();
- }
- if(code2.domain() == generic_code_domain)
- {
- const auto &c2 = static_cast<const generic_code &>(code2);
- if(static_cast<int>(c2.value()) == c1.value())
- {
- return true;
- }
- }
- return false;
- }
- virtual generic_code _generic_code(const status_code<void> &code) const noexcept override
- {
- assert(code.domain() == *this);
- const auto &c = static_cast<const posix_code &>(code);
- return generic_code(static_cast<errc>(c.value()));
- }
- virtual string_ref _do_message(const status_code<void> &code) const noexcept override
- {
- assert(code.domain() == *this);
- const auto &c = static_cast<const posix_code &>(code);
- return _make_string_ref(c.value());
- }
- #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
- BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override
- {
- assert(code.domain() == *this);
- const auto &c = static_cast<const posix_code &>(code);
- throw status_error<_posix_code_domain>(c);
- }
- #endif
- };
- constexpr _posix_code_domain posix_code_domain;
- inline constexpr const _posix_code_domain &_posix_code_domain::get()
- {
- return posix_code_domain;
- }
- namespace mixins
- {
- template <class Base> inline posix_code mixin<Base, _posix_code_domain>::current() noexcept
- {
- return posix_code(errno);
- }
- }
- BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
- #endif
|