Home | History | Annotate | Download | only in cmdline
      1 /*
      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>
     21 #include "base/utils.h"
     22 
     23 namespace art {
     24 // 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.
     27 struct CmdlineResult {
     28   enum Status {
     29     kSuccess,
     30     // Error codes:
     31     kUsage,
     32     kFailure,
     33     kOutOfRange,
     34     kUnknown,
     35   };
     36 
     37   // Short-hand for checking if the result was successful.
     38   operator bool() const {
     39     return IsSuccess();
     40   }
     41 
     42   // 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
     78 static 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 }
    100 }  // namespace art
    101 
    102 #endif  // ART_CMDLINE_CMDLINE_RESULT_H_
    103