You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

status.h 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * Copyright 2019 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. #ifndef DATASET_UTIL_STATUS_H_
  17. #define DATASET_UTIL_STATUS_H_
  18. #if defined(__GNUC__) || defined(__clang__)
  19. #define DEPRECATED __attribute__((deprecated))
  20. #elif defined(_MSC_VER)
  21. #define DEPRECATED __declspec(deprecated)
  22. #else
  23. #pragma message("WARNING: You need to implement DEPRECATED for this compiler")
  24. #define DEPRECATED
  25. #endif
  26. #include <iostream>
  27. #include <string>
  28. #include <utility>
  29. namespace mindspore {
  30. namespace dataset {
  31. #define RETURN_IF_NOT_OK(_s) \
  32. do { \
  33. Status __rc = (_s); \
  34. if (__rc.IsError()) { \
  35. return __rc; \
  36. } \
  37. } while (false)
  38. #define RETURN_STATUS_UNEXPECTED(_e) \
  39. do { \
  40. return Status(StatusCode::kUnexpectedError, __LINE__, __FILE__, _e); \
  41. } while (false)
  42. #define CHECK_FAIL_RETURN_UNEXPECTED(_condition, _e) \
  43. do { \
  44. if (!(_condition)) { \
  45. return Status(StatusCode::kUnexpectedError, __LINE__, __FILE__, _e); \
  46. } \
  47. } while (false)
  48. #define RETURN_UNEXPECTED_IF_NULL(_ptr) \
  49. do { \
  50. if ((_ptr) == nullptr) { \
  51. std::string err_msg = "The pointer[" + std::string(#_ptr) + "] is null."; \
  52. RETURN_STATUS_UNEXPECTED(err_msg); \
  53. } \
  54. } while (false)
  55. enum class StatusCode : char {
  56. kOK = 0,
  57. kOutOfMemory = 1,
  58. kShapeMisMatch = 2,
  59. kInterrupted = 3,
  60. kNoSpace = 4,
  61. kPyFuncException = 5,
  62. kDuplicateKey = 6,
  63. kPythonInterpreterFailure = 7,
  64. kTDTPushFailure = 8,
  65. kFileNotExist = 9,
  66. kProfilingError = 10,
  67. kBoundingBoxOutOfBounds = 11,
  68. kBoundingBoxInvalidShape = 12,
  69. // Make this error code the last one. Add new error code above it.
  70. kUnexpectedError = 127
  71. };
  72. std::string CodeAsString(const StatusCode c);
  73. class Status {
  74. public:
  75. Status() noexcept;
  76. explicit Status(StatusCode c) noexcept;
  77. ~Status() noexcept;
  78. // Copy constructor
  79. Status(const Status &s);
  80. Status &operator=(const Status &s);
  81. // Move constructor
  82. Status(Status &&) noexcept;
  83. Status &operator=(Status &&) noexcept;
  84. Status(const StatusCode code, const std::string &msg);
  85. Status(const StatusCode code, int line_of_code, const char *file_name, const std::string &extra = "");
  86. // Return a success status
  87. static Status OK() { return Status(StatusCode::kOK); }
  88. std::string ToString() const;
  89. StatusCode get_code() const;
  90. friend std::ostream &operator<<(std::ostream &os, const Status &s);
  91. explicit operator bool() const { return (get_code() == StatusCode::kOK); }
  92. bool operator==(const Status &other) const { return (this->get_code() == other.get_code()); }
  93. bool operator!=(const Status &other) const { return !(*this == other); }
  94. bool IsOk() const { return (get_code() == StatusCode::kOK); }
  95. bool IsError() const { return !IsOk(); }
  96. bool IsOutofMemory() const { return (get_code() == StatusCode::kOutOfMemory); }
  97. bool IsInterrupted() const { return (get_code() == StatusCode::kInterrupted); }
  98. bool IsShapeIncorrect() const { return (get_code() == StatusCode::kShapeMisMatch); }
  99. bool IsNoSpace() const { return (get_code() == StatusCode::kNoSpace); }
  100. private:
  101. StatusCode code_;
  102. std::string err_msg_;
  103. };
  104. } // namespace dataset
  105. } // namespace mindspore
  106. #endif // DATASET_UTIL_STATUS_H_