blob: 0ae1145a520eba41a9b735c3f4ada180a3668b3d [file] [log] [blame]
Igor Murashkinaaebaa02015-01-26 10:55:53 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_CMDLINE_CMDLINE_RESULT_H_
18#define ART_CMDLINE_CMDLINE_RESULT_H_
19
20#include <assert.h>
David Sehrc431b9d2018-03-02 12:01:51 -080021#include "base/utils.h"
Igor Murashkinaaebaa02015-01-26 10:55:53 -080022
23namespace art {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080024// Result of an attempt to process the command line arguments. If fails, specifies
25// the specific error code and an error message.
26// Use the value-carrying CmdlineParseResult<T> to get an additional value out in a success case.
27struct CmdlineResult {
28 enum Status {
29 kSuccess,
30 // Error codes:
31 kUsage,
32 kFailure,
33 kOutOfRange,
34 kUnknown,
Igor Murashkinaaebaa02015-01-26 10:55:53 -080035 };
36
Igor Murashkin2ffb7032017-11-08 13:35:21 -080037 // Short-hand for checking if the result was successful.
38 operator bool() const {
39 return IsSuccess();
Igor Murashkinaaebaa02015-01-26 10:55:53 -080040 }
41
Igor Murashkin2ffb7032017-11-08 13:35:21 -080042 // Check if the operation has succeeded.
43 bool IsSuccess() const { return status_ == kSuccess; }
44 // Check if the operation was not a success.
45 bool IsError() const { return status_ != kSuccess; }
46 // Get the specific status, regardless of whether it's failure or success.
47 Status GetStatus() const { return status_; }
48
49 // Get the error message, *must* only be called for error status results.
50 const std::string& GetMessage() const { assert(IsError()); return message_; }
51
52 // Constructor any status. No message.
53 explicit CmdlineResult(Status status) : status_(status) {}
54
55 // Constructor with an error status, copying the message.
56 CmdlineResult(Status status, const std::string& message)
57 : status_(status), message_(message) {
58 assert(status != kSuccess);
59 }
60
61 // Constructor with an error status, taking over the message.
62 CmdlineResult(Status status, std::string&& message)
63 : status_(status), message_(message) {
64 assert(status != kSuccess);
65 }
66
67 // Make sure copying exists
68 CmdlineResult(const CmdlineResult&) = default;
69 // Make sure moving is cheap
70 CmdlineResult(CmdlineResult&&) = default;
71
72 private:
73 const Status status_;
74 const std::string message_;
75};
76
77// TODO: code-generate this
78static inline std::ostream& operator<<(std::ostream& stream, CmdlineResult::Status status) {
79 switch (status) {
80 case CmdlineResult::kSuccess:
81 stream << "kSuccess";
82 break;
83 case CmdlineResult::kUsage:
84 stream << "kUsage";
85 break;
86 case CmdlineResult::kFailure:
87 stream << "kFailure";
88 break;
89 case CmdlineResult::kOutOfRange:
90 stream << "kOutOfRange";
91 break;
92 case CmdlineResult::kUnknown:
93 stream << "kUnknown";
94 break;
95 default:
96 UNREACHABLE();
97 }
98 return stream;
99}
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800100} // namespace art
101
102#endif // ART_CMDLINE_CMDLINE_RESULT_H_