Status.h 1.7 KB
Newer Older
Y
Yu Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
14 15 16

#pragma once

Y
Yu Yang 已提交
17 18 19 20 21
#include <memory>
#include <string>

namespace paddle {

22 23 24 25 26
/**
 * Status is Paddle error code. It only contain a std::string as error message.
 * Although Status inherits the std::exception, but do not throw it except you
 * know what you are doing.
 */
Y
Yu Yang 已提交
27 28
class Status final : public std::exception {
public:
29 30 31
  /**
   * Default Status. OK
   */
Y
Yu Yang 已提交
32 33
  Status() noexcept {}

34 35 36 37 38 39 40 41 42 43 44 45 46
  /**
   * @brief Create Status with error message
   * @param msg
   */
  explicit Status(const std::string& msg) : errMsg_(new std::string(msg)) {}

  /**
   * @brief set a error message for status.
   * @param msg
   */
  inline void set(const std::string& msg) noexcept {
    errMsg_.reset(new std::string(msg));
  }
Y
Yu Yang 已提交
47

48 49 50 51
  /**
   * @brief what will return the error message. If status is OK, return nullptr.
   */
  const char* what() const noexcept override {
Y
Yu Yang 已提交
52 53 54 55 56 57 58
    if (errMsg_) {
      return errMsg_->data();
    } else {
      return nullptr;
    }
  }

59 60 61 62
  /**
   * @brief isOK
   * @return true if OK.
   */
Y
Yu Yang 已提交
63 64 65
  inline bool isOK() const noexcept { return errMsg_ == nullptr; }

private:
66
  std::shared_ptr<std::string> errMsg_;
Y
Yu Yang 已提交
67 68 69
};

}  // namespace paddle