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