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 5.1 kB

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