Back to home page

EIC code displayed by LXR

 
 

    


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/internal/nullguard.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // NullGuard exists such that NullGuard<T>::Guard(v) returns v, unless passed a
0020 // nullptr_t, or a null char* or const char*, in which case it returns "(null)".
0021 // This allows streaming NullGuard<T>::Guard(v) to an output stream without
0022 // hitting undefined behavior for null values.
0023 
0024 #ifndef ABSL_LOG_INTERNAL_NULLGUARD_H_
0025 #define ABSL_LOG_INTERNAL_NULLGUARD_H_
0026 
0027 #include <array>
0028 #include <cstddef>
0029 
0030 #include "absl/base/attributes.h"
0031 #include "absl/base/config.h"
0032 
0033 namespace absl {
0034 ABSL_NAMESPACE_BEGIN
0035 namespace log_internal {
0036 
0037 ABSL_CONST_INIT ABSL_DLL extern const std::array<char, 7> kCharNull;
0038 ABSL_CONST_INIT ABSL_DLL extern const std::array<signed char, 7>
0039     kSignedCharNull;
0040 ABSL_CONST_INIT ABSL_DLL extern const std::array<unsigned char, 7>
0041     kUnsignedCharNull;
0042 
0043 template <typename T>
0044 struct NullGuard final {
0045   static const T& Guard(const T& v) { return v; }
0046 };
0047 template <>
0048 struct NullGuard<char*> final {
0049   static const char* Guard(const char* v) { return v ? v : kCharNull.data(); }
0050 };
0051 template <>
0052 struct NullGuard<const char*> final {
0053   static const char* Guard(const char* v) { return v ? v : kCharNull.data(); }
0054 };
0055 template <>
0056 struct NullGuard<signed char*> final {
0057   static const signed char* Guard(const signed char* v) {
0058     return v ? v : kSignedCharNull.data();
0059   }
0060 };
0061 template <>
0062 struct NullGuard<const signed char*> final {
0063   static const signed char* Guard(const signed char* v) {
0064     return v ? v : kSignedCharNull.data();
0065   }
0066 };
0067 template <>
0068 struct NullGuard<unsigned char*> final {
0069   static const unsigned char* Guard(const unsigned char* v) {
0070     return v ? v : kUnsignedCharNull.data();
0071   }
0072 };
0073 template <>
0074 struct NullGuard<const unsigned char*> final {
0075   static const unsigned char* Guard(const unsigned char* v) {
0076     return v ? v : kUnsignedCharNull.data();
0077   }
0078 };
0079 template <>
0080 struct NullGuard<std::nullptr_t> final {
0081   static const char* Guard(const std::nullptr_t&) { return kCharNull.data(); }
0082 };
0083 
0084 }  // namespace log_internal
0085 ABSL_NAMESPACE_END
0086 }  // namespace absl
0087 
0088 #endif  // ABSL_LOG_INTERNAL_NULLGUARD_H_