xref: /aosp_15_r20/external/webrtc/third_party/abseil-cpp/absl/log/internal/nullguard.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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/internal/nullguard.h
17 // -----------------------------------------------------------------------------
18 //
19 // NullGuard exists such that NullGuard<T>::Guard(v) returns v, unless passed a
20 // nullptr_t, or a null char* or const char*, in which case it returns "(null)".
21 // This allows streaming NullGuard<T>::Guard(v) to an output stream without
22 // hitting undefined behavior for null values.
23 
24 #ifndef ABSL_LOG_INTERNAL_NULLGUARD_H_
25 #define ABSL_LOG_INTERNAL_NULLGUARD_H_
26 
27 #include <cstddef>
28 
29 #include "absl/base/config.h"
30 
31 namespace absl {
32 ABSL_NAMESPACE_BEGIN
33 namespace log_internal {
34 
35 template <typename T>
36 struct NullGuard final {
Guardfinal37   static const T& Guard(const T& v) { return v; }
38 };
39 template <>
40 struct NullGuard<char*> final {
41   static const char* Guard(const char* v) { return v ? v : "(null)"; }
42 };
43 template <>
44 struct NullGuard<const char*> final {
45   static const char* Guard(const char* v) { return v ? v : "(null)"; }
46 };
47 template <>
48 struct NullGuard<std::nullptr_t> final {
49   static const char* Guard(const std::nullptr_t&) { return "(null)"; }
50 };
51 
52 }  // namespace log_internal
53 ABSL_NAMESPACE_END
54 }  // namespace absl
55 
56 #endif  // ABSL_LOG_INTERNAL_NULLGUARD_H_
57