1 /** 2 * Copyright 2021 Huawei Technologies Co., Ltd 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 MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_REQUEST_PROCESS_RESULT_CODE_H_ 18 #define MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_REQUEST_PROCESS_RESULT_CODE_H_ 19 20 #include <string> 21 #include <memory> 22 #include <vector> 23 #include <sstream> 24 #include <iostream> 25 26 namespace mindspore { 27 namespace ps { 28 namespace core { 29 enum class RequestProcessResultCode { kSuccess = 0, kSystemError = 1, kInvalidInputs = 2 }; 30 class LogStream { 31 public: LogStream()32 LogStream() { sstream_ = std::make_shared<std::stringstream>(); } 33 ~LogStream() = default; 34 35 template <typename T> 36 LogStream &operator<<(const T &val) noexcept { 37 (*sstream_) << val; 38 return *this; 39 } 40 41 template <typename T> 42 LogStream &operator<<(const std::vector<T> &val) noexcept { 43 (*sstream_) << "["; 44 for (size_t i = 0; i < val.size(); i++) { 45 (*this) << val[i]; 46 if (i + 1 < val.size()) { 47 (*sstream_) << ", "; 48 } 49 } 50 (*sstream_) << "]"; 51 return *this; 52 } 53 54 LogStream &operator<<(std::ostream &func(std::ostream &os)) noexcept { 55 (*sstream_) << func; 56 return *this; 57 } 58 stream()59 const std::shared_ptr<std::stringstream> &stream() const { return sstream_; } 60 61 private: 62 std::shared_ptr<std::stringstream> sstream_; 63 }; 64 65 /* This class encapsulates user defined messages and user defined result codes, used to return http response message. 66 * 67 */ 68 class RequestProcessResult { 69 public: RequestProcessResult()70 RequestProcessResult() : code_(RequestProcessResultCode::kSystemError) {} 71 explicit RequestProcessResult(enum RequestProcessResultCode code, const std::string &msg = "") code_(code)72 : code_(code), msg_(msg) {} 73 ~RequestProcessResult() = default; 74 IsSuccess()75 bool IsSuccess() const { return code_ == RequestProcessResultCode::kSuccess; } ResultCode()76 enum RequestProcessResultCode ResultCode() const { return code_; } StatusMessage()77 std::string StatusMessage() const { return msg_; } 78 79 bool operator==(const RequestProcessResult &other) const { return code_ == other.code_; } 80 bool operator==(enum RequestProcessResultCode other_code) const { return code_ == other_code; } 81 bool operator!=(const RequestProcessResult &other) const { return code_ != other.code_; } 82 bool operator!=(enum RequestProcessResultCode other_code) const { return code_ != other_code; } 83 84 operator bool() const = delete; 85 86 RequestProcessResult &operator<(const LogStream &stream) noexcept __attribute__((visibility("default"))) { 87 msg_ = stream.stream()->str(); 88 return *this; 89 } 90 RequestProcessResult &operator=(const std::string &message) noexcept __attribute__((visibility("default"))) { 91 msg_ = message; 92 return *this; 93 } 94 95 private: 96 enum RequestProcessResultCode code_; 97 std::string msg_; 98 }; 99 } // namespace core 100 } // namespace ps 101 } // namespace mindspore 102 #endif // MINDSPORE_CCSRC_PS_CORE_COMMUNICATOR_REQUEST_PROCESS_RESULT_CODE_H_ 103