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

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