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.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_STATUS_H_
  17. #define MINDSPORE_CCSRC_MINDDATA_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. #define RETURN_OK_IF_TRUE(_condition) \
  56. do { \
  57. if (_condition) { \
  58. return Status::OK(); \
  59. } \
  60. } while (false)
  61. enum class StatusCode : char {
  62. kOK = 0,
  63. kOutOfMemory = 1,
  64. kShapeMisMatch = 2,
  65. kInterrupted = 3,
  66. kNoSpace = 4,
  67. kPyFuncException = 5,
  68. kDuplicateKey = 6,
  69. kPythonInterpreterFailure = 7,
  70. kTDTPushFailure = 8,
  71. kFileNotExist = 9,
  72. kProfilingError = 10,
  73. kBoundingBoxOutOfBounds = 11,
  74. kBoundingBoxInvalidShape = 12,
  75. kSyntaxError = 13,
  76. kTimeOut = 14,
  77. kBuddySpaceFull = 15,
  78. kNetWorkError = 16,
  79. // Make this error code the last one. Add new error code above it.
  80. kUnexpectedError = 127
  81. };
  82. std::string CodeAsString(const StatusCode c);
  83. class Status {
  84. public:
  85. Status() noexcept;
  86. explicit Status(StatusCode c) noexcept;
  87. ~Status() noexcept;
  88. // Copy constructor
  89. Status(const Status &s);
  90. Status &operator=(const Status &s);
  91. // Move constructor
  92. Status(Status &&) noexcept;
  93. Status &operator=(Status &&) noexcept;
  94. Status(const StatusCode code, const std::string &msg);
  95. Status(const StatusCode code, int line_of_code, const char *file_name, const std::string &extra = "");
  96. // Return a success status
  97. static Status OK() { return Status(StatusCode::kOK); }
  98. std::string ToString() const;
  99. StatusCode get_code() const;
  100. friend std::ostream &operator<<(std::ostream &os, const Status &s);
  101. explicit operator bool() const { return (get_code() == StatusCode::kOK); }
  102. bool operator==(const Status &other) const { return (this->get_code() == other.get_code()); }
  103. bool operator!=(const Status &other) const { return !(*this == other); }
  104. bool IsOk() const { return (get_code() == StatusCode::kOK); }
  105. bool IsError() const { return !IsOk(); }
  106. bool IsOutofMemory() const { return (get_code() == StatusCode::kOutOfMemory); }
  107. bool IsInterrupted() const { return (get_code() == StatusCode::kInterrupted); }
  108. bool IsShapeIncorrect() const { return (get_code() == StatusCode::kShapeMisMatch); }
  109. bool IsNoSpace() const { return (get_code() == StatusCode::kNoSpace); }
  110. bool IsNetWorkError() const { return (get_code() == StatusCode::kNetWorkError); }
  111. private:
  112. StatusCode code_;
  113. std::string err_msg_;
  114. };
  115. } // namespace dataset
  116. } // namespace mindspore
  117. #endif // MINDSPORE_CCSRC_MINDDATA_DATASET_UTIL_STATUS_H_