1 // Copyright 2018 The Amber Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef AMBER_RESULT_H_ 16 #define AMBER_RESULT_H_ 17 18 #include <string> 19 #include <vector> 20 21 namespace amber { 22 23 /// Holds the results for an operation. 24 class Result { 25 public: 26 /// Creates a result which succeeded. 27 Result() = default; 28 29 /// Creates a result which failed and will return |err|. 30 explicit Result(const std::string& err); 31 inline Result(const Result&) = default; 32 inline Result(Result&&) = default; 33 34 inline Result& operator=(const Result&) = default; 35 inline Result& operator=(Result&&) = default; 36 37 /// Adds the errors from |res| to this Result. 38 Result& operator+=(const Result& res); 39 40 /// Adds the error |err| to this Result. 41 Result& operator+=(const std::string& err); 42 43 /// Returns true if the result is a success. IsSuccess()44 bool IsSuccess() const { return errors_.size() == 0; } 45 46 /// Returns the error string if |IsSuccess| is false. 47 std::string Error() const; 48 49 private: 50 std::vector<std::string> errors_; 51 }; 52 53 } // namespace amber 54 55 #endif // AMBER_RESULT_H_ 56