/src/abseil-cpp/absl/log/internal/nullguard.h
Line  | Count  | Source  | 
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 <array>  | 
28  |  | #include <cstddef>  | 
29  |  |  | 
30  |  | #include "absl/base/attributes.h"  | 
31  |  | #include "absl/base/config.h"  | 
32  |  |  | 
33  |  | namespace absl { | 
34  |  | ABSL_NAMESPACE_BEGIN  | 
35  |  | namespace log_internal { | 
36  |  |  | 
37  |  | ABSL_CONST_INIT ABSL_DLL extern const std::array<char, 7> kCharNull;  | 
38  |  | ABSL_CONST_INIT ABSL_DLL extern const std::array<signed char, 7>  | 
39  |  |     kSignedCharNull;  | 
40  |  | ABSL_CONST_INIT ABSL_DLL extern const std::array<unsigned char, 7>  | 
41  |  |     kUnsignedCharNull;  | 
42  |  |  | 
43  |  | template <typename T>  | 
44  |  | struct NullGuard final { | 
45  | 0  |   static const T& Guard(const T& v) { return v; }Unexecuted instantiation: absl::log_internal::NullGuard<bool>::Guard(bool const&) Unexecuted instantiation: absl::log_internal::NullGuard<long>::Guard(long const&) Unexecuted instantiation: absl::log_internal::NullGuard<unsigned long>::Guard(unsigned long const&) Unexecuted instantiation: absl::log_internal::NullGuard<float>::Guard(float const&) Unexecuted instantiation: absl::log_internal::NullGuard<double>::Guard(double const&) Unexecuted instantiation: absl::log_internal::NullGuard<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Guard(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Unexecuted instantiation: absl::log_internal::NullGuard<std::__1::basic_string_view<char, std::__1::char_traits<char> > >::Guard(std::__1::basic_string_view<char, std::__1::char_traits<char> > const&) Unexecuted instantiation: absl::log_internal::NullGuard<char>::Guard(char const&) Unexecuted instantiation: absl::log_internal::NullGuard<signed char>::Guard(signed char const&) Unexecuted instantiation: absl::log_internal::NullGuard<unsigned char>::Guard(unsigned char const&) Unexecuted instantiation: absl::log_internal::NullGuard<short>::Guard(short const&) Unexecuted instantiation: absl::log_internal::NullGuard<unsigned short>::Guard(unsigned short const&) Unexecuted instantiation: absl::log_internal::NullGuard<int>::Guard(int const&) Unexecuted instantiation: absl::log_internal::NullGuard<unsigned int>::Guard(unsigned int const&) Unexecuted instantiation: absl::log_internal::NullGuard<long long>::Guard(long long const&) Unexecuted instantiation: absl::log_internal::NullGuard<unsigned long long>::Guard(unsigned long long const&) Unexecuted instantiation: absl::log_internal::NullGuard<void*>::Guard(void* const&) Unexecuted instantiation: absl::log_internal::NullGuard<void const*>::Guard(void const* const&)  | 
46  |  | };  | 
47  |  | template <>  | 
48  |  | struct NullGuard<char*> final { | 
49  | 0  |   static const char* Guard(const char* v) { return v ? v : kCharNull.data(); } | 
50  |  | };  | 
51  |  | template <>  | 
52  |  | struct NullGuard<const char*> final { | 
53  | 0  |   static const char* Guard(const char* v) { return v ? v : kCharNull.data(); } | 
54  |  | };  | 
55  |  | template <>  | 
56  |  | struct NullGuard<signed char*> final { | 
57  | 0  |   static const signed char* Guard(const signed char* v) { | 
58  | 0  |     return v ? v : kSignedCharNull.data();  | 
59  | 0  |   }  | 
60  |  | };  | 
61  |  | template <>  | 
62  |  | struct NullGuard<const signed char*> final { | 
63  | 0  |   static const signed char* Guard(const signed char* v) { | 
64  | 0  |     return v ? v : kSignedCharNull.data();  | 
65  | 0  |   }  | 
66  |  | };  | 
67  |  | template <>  | 
68  |  | struct NullGuard<unsigned char*> final { | 
69  | 0  |   static const unsigned char* Guard(const unsigned char* v) { | 
70  | 0  |     return v ? v : kUnsignedCharNull.data();  | 
71  | 0  |   }  | 
72  |  | };  | 
73  |  | template <>  | 
74  |  | struct NullGuard<const unsigned char*> final { | 
75  | 0  |   static const unsigned char* Guard(const unsigned char* v) { | 
76  | 0  |     return v ? v : kUnsignedCharNull.data();  | 
77  | 0  |   }  | 
78  |  | };  | 
79  |  | template <>  | 
80  |  | struct NullGuard<std::nullptr_t> final { | 
81  | 0  |   static const char* Guard(const std::nullptr_t&) { return kCharNull.data(); } | 
82  |  | };  | 
83  |  |  | 
84  |  | }  // namespace log_internal  | 
85  |  | ABSL_NAMESPACE_END  | 
86  |  | }  // namespace absl  | 
87  |  |  | 
88  |  | #endif  // ABSL_LOG_INTERNAL_NULLGUARD_H_  |