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
21 #ifndef ABSL_LOG_DIE_IF_NULL_H_
22 #define ABSL_LOG_DIE_IF_NULL_H_
23
24 #include <stdint.h>
25
26 #include <utility>
27
28 #include "absl/base/attributes.h"
29 #include "absl/base/config.h"
30 #include "absl/base/optimization.h"
31
32 // ABSL_DIE_IF_NULL()
33 //
34 // `ABSL_DIE_IF_NULL` behaves as `CHECK_NE` against `nullptr` but *also*
35 // "returns" its argument. It is useful in initializers where statements (like
36 // `CHECK_NE`) can't be used. Outside initializers, prefer `CHECK` or
37 // `CHECK_NE`. `ABSL_DIE_IF_NULL` works for both raw pointers and (compatible)
38 // smart pointers including `std::unique_ptr` and `std::shared_ptr`; more
39 // generally, it works for any type that can be compared to nullptr_t. For
40 // types that aren't raw pointers, `ABSL_DIE_IF_NULL` returns a reference to
41 // its argument, preserving the value category. Example:
42 //
43 // Foo() : bar_(ABSL_DIE_IF_NULL(MethodReturningUniquePtr())) {}
44 //
45 // Use `CHECK(ptr)` or `CHECK(ptr != nullptr)` if the returned pointer is
46 // unused.
47 #define ABSL_DIE_IF_NULL(val) \
48 ::absl::log_internal::DieIfNull(__FILE__, __LINE__, #val, (val))
49
50 namespace absl {
51 ABSL_NAMESPACE_BEGIN
52 namespace log_internal {
53
54 // Crashes the process after logging `exprtext` annotated at the `file` and
55 // `line` location. Called when `ABSL_DIE_IF_NULL` fails. Calling this function
56 // generates less code than its implementation would if inlined, for a slight
57 // code size reduction each time `ABSL_DIE_IF_NULL` is called.
58 [[noreturn]] ABSL_ATTRIBUTE_NOINLINE void DieBecauseNull(
59 const char* file, int line, const char* exprtext);
60
61 // Helper for `ABSL_DIE_IF_NULL`.
62 template <typename T>
DieIfNull(const char * file,int line,const char * exprtext,T && t)63 ABSL_MUST_USE_RESULT T DieIfNull(const char* file, int line,
64 const char* exprtext, T&& t) {
65 if (ABSL_PREDICT_FALSE(t == nullptr)) {
66 // Call a non-inline helper function for a small code size improvement.
67 DieBecauseNull(file, line, exprtext);
68 }
69 return std::forward<T>(t);
70 }
71
72 } // namespace log_internal
73 ABSL_NAMESPACE_END
74 } // namespace absl
75
76 #endif // ABSL_LOG_DIE_IF_NULL_H_
77