|
|
|||
File indexing completed on 2025-12-16 09:40:53
0001 // Copyright 2022 The Abseil Authors. 0002 // 0003 // Licensed under the Apache License, Version 2.0 (the "License"); 0004 // you may not use this file except in compliance with the License. 0005 // You may obtain a copy of the License at 0006 // 0007 // https://www.apache.org/licenses/LICENSE-2.0 0008 // 0009 // Unless required by applicable law or agreed to in writing, software 0010 // distributed under the License is distributed on an "AS IS" BASIS, 0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0012 // See the License for the specific language governing permissions and 0013 // limitations under the License. 0014 // 0015 // ----------------------------------------------------------------------------- 0016 // File: log/die_if_null.h 0017 // ----------------------------------------------------------------------------- 0018 // 0019 // This header declares macro `ABSL_DIE_IF_NULL`. 0020 0021 #ifndef ABSL_LOG_DIE_IF_NULL_H_ 0022 #define ABSL_LOG_DIE_IF_NULL_H_ 0023 0024 #include <stdint.h> 0025 0026 #include <utility> 0027 0028 #include "absl/base/attributes.h" 0029 #include "absl/base/config.h" 0030 #include "absl/base/optimization.h" 0031 0032 // ABSL_DIE_IF_NULL() 0033 // 0034 // `ABSL_DIE_IF_NULL` behaves as `CHECK_NE` against `nullptr` but *also* 0035 // "returns" its argument. It is useful in initializers where statements (like 0036 // `CHECK_NE`) can't be used. Outside initializers, prefer `CHECK` or 0037 // `CHECK_NE`. `ABSL_DIE_IF_NULL` works for both raw pointers and (compatible) 0038 // smart pointers including `std::unique_ptr` and `std::shared_ptr`; more 0039 // generally, it works for any type that can be compared to nullptr_t. For 0040 // types that aren't raw pointers, `ABSL_DIE_IF_NULL` returns a reference to 0041 // its argument, preserving the value category. Example: 0042 // 0043 // Foo() : bar_(ABSL_DIE_IF_NULL(MethodReturningUniquePtr())) {} 0044 // 0045 // Use `CHECK(ptr)` or `CHECK(ptr != nullptr)` if the returned pointer is 0046 // unused. 0047 #define ABSL_DIE_IF_NULL(val) \ 0048 ::absl::log_internal::DieIfNull(__FILE__, __LINE__, #val, (val)) 0049 0050 namespace absl { 0051 ABSL_NAMESPACE_BEGIN 0052 namespace log_internal { 0053 0054 // Crashes the process after logging `exprtext` annotated at the `file` and 0055 // `line` location. Called when `ABSL_DIE_IF_NULL` fails. Calling this function 0056 // generates less code than its implementation would if inlined, for a slight 0057 // code size reduction each time `ABSL_DIE_IF_NULL` is called. 0058 [[noreturn]] ABSL_ATTRIBUTE_NOINLINE void DieBecauseNull( 0059 const char* file, int line, const char* exprtext); 0060 0061 // Helper for `ABSL_DIE_IF_NULL`. 0062 template <typename T> 0063 ABSL_MUST_USE_RESULT T DieIfNull(const char* file, int line, 0064 const char* exprtext, T&& t) { 0065 if (ABSL_PREDICT_FALSE(t == nullptr)) { 0066 // Call a non-inline helper function for a small code size improvement. 0067 DieBecauseNull(file, line, exprtext); 0068 } 0069 return std::forward<T>(t); 0070 } 0071 0072 } // namespace log_internal 0073 ABSL_NAMESPACE_END 0074 } // namespace absl 0075 0076 #endif // ABSL_LOG_DIE_IF_NULL_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|