die_if_null.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2022 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // File: log/die_if_null.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header declares macro `ABSL_DIE_IF_NULL`.
  20. #ifndef ABSL_LOG_DIE_IF_NULL_H_
  21. #define ABSL_LOG_DIE_IF_NULL_H_
  22. #include <stdint.h>
  23. #include <utility>
  24. #include "absl/base/attributes.h"
  25. #include "absl/base/config.h"
  26. #include "absl/base/optimization.h"
  27. // ABSL_DIE_IF_NULL()
  28. //
  29. // `ABSL_DIE_IF_NULL` behaves as `CHECK_NE` against `nullptr` but *also*
  30. // "returns" its argument. It is useful in initializers where statements (like
  31. // `CHECK_NE`) can't be used. Outside initializers, prefer `CHECK` or
  32. // `CHECK_NE`. `ABSL_DIE_IF_NULL` works for both raw pointers and (compatible)
  33. // smart pointers including `std::unique_ptr` and `std::shared_ptr`; more
  34. // generally, it works for any type that can be compared to nullptr_t. For
  35. // types that aren't raw pointers, `ABSL_DIE_IF_NULL` returns a reference to
  36. // its argument, preserving the value category. Example:
  37. //
  38. // Foo() : bar_(ABSL_DIE_IF_NULL(MethodReturningUniquePtr())) {}
  39. //
  40. // Use `CHECK(ptr)` or `CHECK(ptr != nullptr)` if the returned pointer is
  41. // unused.
  42. #define ABSL_DIE_IF_NULL(val) \
  43. ::absl::log_internal::DieIfNull(__FILE__, __LINE__, #val, (val))
  44. namespace absl {
  45. ABSL_NAMESPACE_BEGIN
  46. namespace log_internal {
  47. // Crashes the process after logging `exprtext` annotated at the `file` and
  48. // `line` location. Called when `ABSL_DIE_IF_NULL` fails. Calling this function
  49. // generates less code than its implementation would if inlined, for a slight
  50. // code size reduction each time `ABSL_DIE_IF_NULL` is called.
  51. [[noreturn]] ABSL_ATTRIBUTE_NOINLINE void DieBecauseNull(
  52. const char* file, int line, const char* exprtext);
  53. // Helper for `ABSL_DIE_IF_NULL`.
  54. template <typename T>
  55. ABSL_MUST_USE_RESULT T DieIfNull(const char* file, int line,
  56. const char* exprtext, T&& t) {
  57. if (ABSL_PREDICT_FALSE(t == nullptr)) {
  58. // Call a non-inline helper function for a small code size improvement.
  59. DieBecauseNull(file, line, exprtext);
  60. }
  61. return std::forward<T>(t);
  62. }
  63. } // namespace log_internal
  64. ABSL_NAMESPACE_END
  65. } // namespace absl
  66. #endif // ABSL_LOG_DIE_IF_NULL_H_