summaryrefslogtreecommitdiffstats
path: root/Source/kwsys/Status.hxx.in
blob: 16efaefdf3397e013d90cecb19f2e4ec285432be (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
#ifndef @KWSYS_NAMESPACE@_Status_hxx
#define @KWSYS_NAMESPACE@_Status_hxx

#include <@KWSYS_NAMESPACE@/Configure.hxx>

#include <string>

namespace @KWSYS_NAMESPACE@ {

/** \class Status
 * \brief OS-specific status of a system operation.
 */
class @KWSYS_NAMESPACE@_EXPORT Status
{
public:
  enum class Kind
  {
    Success,
    POSIX,
#ifdef _WIN32
    Windows,
#endif
  };

  /** Construct with kind "Success".  */
  Status() = default;

  /** Construct with kind "Success".  */
  static Status Success() { return Status(); }

  /** Construct with kind "POSIX" using given errno-style value.  */
  static Status POSIX(int e)
  {
    Status s(Kind::POSIX);
    s.POSIX_ = e;
    return s;
  }

  /** Construct with kind "POSIX" using errno.  */
  static Status POSIX_errno();

#ifdef _WIN32
  /** Construct with kind "Windows" using given GetLastError()-style value.  */
  static Status Windows(unsigned int e)
  {
    Status s(Kind::Windows);
    s.Windows_ = e;
    return s;
  }

  /** Construct with kind "Windows" using GetLastError().  */
  static Status Windows_GetLastError();
#endif

  /** Return true on "Success", false otherwise.  */
  bool IsSuccess() const { return this->Kind_ == Kind::Success; }

  /** Return true on "Success", false otherwise.  */
  explicit operator bool() const { return this->IsSuccess(); }

  /** Return the kind of status.  */
  Kind GetKind() const { return this->Kind_; }

  /** If the kind is "POSIX", returns the errno-style value.
      Otherwise, returns 0.  */
  int GetPOSIX() const
  {
    return this->Kind_ == Kind::POSIX ? this->POSIX_ : 0;
  }

#ifdef _WIN32
  /** If the kind is "Windows", returns the GetLastError()-style value.
      Otherwise, returns 0.  */
  unsigned int GetWindows() const
  {
    return this->Kind_ == Kind::Windows ? this->Windows_ : 0;
  }
#endif

  /** Return a human-readable description of the status.  */
  std::string GetString() const;

private:
  Status(Kind kind)
    : Kind_(kind)
  {
  }

  Kind Kind_ = Kind::Success;

  union
  {
    int POSIX_;
#ifdef _WIN32
    unsigned int Windows_;
#endif
  };
};

} // namespace @KWSYS_NAMESPACE@

#endif