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.

contract.h 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_UTILS_CONTRACT_H
  17. #define MINDSPORE_CCSRC_UTILS_CONTRACT_H
  18. #include <string>
  19. #include <exception>
  20. #include <memory>
  21. #include <utility>
  22. #include <type_traits>
  23. #include "utils/log_adapter.h"
  24. namespace mindspore {
  25. class ContractError : public std::logic_error {
  26. public:
  27. explicit ContractError(const std::string &msg) : std::logic_error(msg) {}
  28. explicit ContractError(const char *msg) : std::logic_error(msg) {}
  29. };
  30. struct Signatory {
  31. LocationInfo location_info;
  32. const char *extra_info;
  33. };
  34. struct NotNullRule {
  35. template <class T>
  36. constexpr static bool Check(const T &val) {
  37. return val != nullptr;
  38. }
  39. constexpr static const char *Desc() { return " must not be null"; }
  40. };
  41. template <class T, class R, class E = void>
  42. class EnsuresAccess {};
  43. template <class T>
  44. using RemoveCVR = std::remove_cv_t<std::remove_reference_t<T>>;
  45. template <class T, class R>
  46. class Ensures : public EnsuresAccess<T, R> {
  47. public:
  48. Ensures(T v, const Signatory &signatory) : value_(v) {
  49. if (!R::Check(value_)) {
  50. LogStream contract_stream;
  51. contract_stream << "contract error: " << signatory.extra_info << R::Desc();
  52. LogWriter(signatory.location_info, EXCEPTION, ArgumentError) ^ contract_stream;
  53. }
  54. }
  55. template <class O, typename = std::enable_if_t<std::is_convertible_v<O, T>>>
  56. Ensures(const Ensures<O, R> &other) : value_(other.get()) {}
  57. T get() const { return value_; }
  58. T &get() { return value_; }
  59. operator T() const { return value_; }
  60. private:
  61. T value_;
  62. };
  63. template <class T, class R>
  64. class EnsuresAccess<T, R, std::enable_if_t<std::is_pointer_v<std::remove_cv_t<T>>>> {
  65. public:
  66. T operator->() const {
  67. auto ptr = static_cast<const Ensures<T, R> *>(this)->get();
  68. return ptr;
  69. }
  70. };
  71. template <typename T>
  72. struct IsSharedPtr : std::false_type {};
  73. template <typename T>
  74. struct IsSharedPtr<std::shared_ptr<T>> : std::true_type {};
  75. template <typename T>
  76. struct IsSharedPtr<const std::shared_ptr<T> &> : std::true_type {};
  77. template <class T, class R>
  78. class EnsuresAccess<T, R, std::enable_if_t<IsSharedPtr<T>::value>> {
  79. using element_type = typename std::remove_cv_t<std::remove_reference_t<T>>::element_type;
  80. public:
  81. element_type *operator->() const {
  82. auto ptr = static_cast<const Ensures<T, R> *>(this)->get();
  83. return ptr.get();
  84. }
  85. };
  86. template <class T, class R>
  87. using Expects = Ensures<T, R>;
  88. template <class T>
  89. using NotNull = Ensures<T, NotNullRule>;
  90. #define ENSURE(_v, _rule) Ensures<decltype(_v), _rule>(_v, {{__FILE__, __LINE__, __FUNCTION__}, #_v})
  91. #define NOT_NULL(_v) ENSURE(_v, NotNullRule)
  92. } // namespace mindspore
  93. #endif // MINDSPORE_CCSRC_UTILS_CONTRACT_H