/src/CMake/build-dir/Source/cmsys/Status.hxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file Copyright.txt or https://cmake.org/licensing#kwsys for details. */ |
3 | | #ifndef cmsys_Status_hxx |
4 | | #define cmsys_Status_hxx |
5 | | |
6 | | #include <cmsys/Configure.hxx> |
7 | | |
8 | | #include <string> |
9 | | |
10 | | /* |
11 | | * Detect a symbol collision with the name of this class. X11 headers use |
12 | | * `#define Status int` instead of using `typedef` which poisons any other |
13 | | * usage of this name. |
14 | | */ |
15 | | #if defined(Status) && defined(_X11_XLIB_H_) |
16 | | # error \ |
17 | | "Status.hxx must be included *before* any X11 headers to avoid a collision with the `Status` define that is made in its API." |
18 | | #endif |
19 | | |
20 | | namespace cmsys { |
21 | | |
22 | | /** \class Status |
23 | | * \brief OS-specific status of a system operation. |
24 | | */ |
25 | | class cmsys_EXPORT Status |
26 | | { |
27 | | public: |
28 | | enum class Kind |
29 | | { |
30 | | Success, |
31 | | POSIX, |
32 | | #ifdef _WIN32 |
33 | | Windows, |
34 | | #endif |
35 | | }; |
36 | | |
37 | | /** Construct with kind "Success". */ |
38 | 330k | Status() = default; |
39 | | |
40 | | /** Construct with kind "Success". */ |
41 | 267k | static Status Success() { return Status(); } |
42 | | |
43 | | /** Construct with kind "POSIX" using given errno-style value. */ |
44 | | static Status POSIX(int e) |
45 | 35.5k | { |
46 | 35.5k | Status s(Kind::POSIX); |
47 | 35.5k | s.POSIX_ = e; |
48 | 35.5k | return s; |
49 | 35.5k | } |
50 | | |
51 | | /** Construct with kind "POSIX" using errno. */ |
52 | | static Status POSIX_errno(); |
53 | | |
54 | | #ifdef _WIN32 |
55 | | /** Construct with kind "Windows" using given GetLastError()-style value. */ |
56 | | static Status Windows(unsigned int e) |
57 | | { |
58 | | Status s(Kind::Windows); |
59 | | s.Windows_ = e; |
60 | | return s; |
61 | | } |
62 | | |
63 | | /** Construct with kind "Windows" using GetLastError(). */ |
64 | | static Status Windows_GetLastError(); |
65 | | #endif |
66 | | |
67 | | /** Return true on "Success", false otherwise. */ |
68 | 194k | bool IsSuccess() const { return this->Kind_ == Kind::Success; } |
69 | | |
70 | | /** Return true on "Success", false otherwise. */ |
71 | 92.1k | explicit operator bool() const { return this->IsSuccess(); } |
72 | | |
73 | | /** Return the kind of status. */ |
74 | 0 | Kind GetKind() const { return this->Kind_; } |
75 | | |
76 | | /** If the kind is "POSIX", returns the errno-style value. |
77 | | Otherwise, returns 0. */ |
78 | | int GetPOSIX() const |
79 | 0 | { |
80 | 0 | return this->Kind_ == Kind::POSIX ? this->POSIX_ : 0; |
81 | 0 | } |
82 | | |
83 | | #ifdef _WIN32 |
84 | | /** If the kind is "Windows", returns the GetLastError()-style value. |
85 | | Otherwise, returns 0. */ |
86 | | unsigned int GetWindows() const |
87 | | { |
88 | | return this->Kind_ == Kind::Windows ? this->Windows_ : 0; |
89 | | } |
90 | | #endif |
91 | | |
92 | | /** Return a human-readable description of the status. */ |
93 | | std::string GetString() const; |
94 | | |
95 | | private: |
96 | | Status(Kind kind) |
97 | 35.5k | : Kind_(kind) |
98 | 35.5k | { |
99 | 35.5k | } |
100 | | |
101 | | Kind Kind_ = Kind::Success; |
102 | | |
103 | | union |
104 | | { |
105 | | int POSIX_; |
106 | | #ifdef _WIN32 |
107 | | unsigned int Windows_; |
108 | | #endif |
109 | | }; |
110 | | }; |
111 | | |
112 | | } // namespace cmsys |
113 | | |
114 | | #endif |