Echo Writes Code

errors.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef CRUCIBLE_OS_ERRORS_HPP
#define CRUCIBLE_OS_ERRORS_HPP

#include "crucible/core/either.inl"
#include "crucible/core/errors.hpp"

#ifdef CRUCIBLE_UNIX
#include "crucible/unix/errors.hpp"
#endif // CRUCIBLE_UNIX

#ifdef CRUCIBLE_WINDOWS
#include "crucible/windows/errors.hpp"
#endif // CRUCIBLE_WINDOWS

#include <functional>
#include <optional>
#include <string>

namespace crucible::os::errors {
  class OsError final : public core::errors::AbstractError {
  public:
#ifdef CRUCIBLE_UNIX
    explicit OsError(unix::errors::UnixError const &error);
#endif // CRUCIBLE_UNIX

#ifdef CRUCIBLE_WINDOWS
    explicit OsError(windows::errors::WindowsError const &error);
#endif // CRUCIBLE_WINDOWS

    ~OsError() override;

    [[nodiscard]]
    auto describe() const -> std::string override;

    [[nodiscard]]
    auto format() const -> std::string override;

    [[nodiscard]]
    auto source() const & -> std::optional<std::reference_wrapper<core::errors::AbstractError const>> override;

  private:
#ifdef CRUCIBLE_UNIX
    unix::errors::UnixError my_error;
#endif // CRUCIBLE_UNIX

#ifdef CRUCIBLE_WINDOWS
    windows::errors::WindowsError my_error;
#endif // CRUCIBLE_WINDOWS
  };

  template<typename T>
  using OsResult = core::either::Either<OsError, T>;
}

#endif // CRUCIBLE_OS_ERRORS_HPP