tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nullguard.h (2846B)


      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  static const T& Guard(const T& v) { return v; }
     46 };
     47 template <>
     48 struct NullGuard<char*> final {
     49  static const char* Guard(const char* v) { return v ? v : kCharNull.data(); }
     50 };
     51 template <>
     52 struct NullGuard<const char*> final {
     53  static const char* Guard(const char* v) { return v ? v : kCharNull.data(); }
     54 };
     55 template <>
     56 struct NullGuard<signed char*> final {
     57  static const signed char* Guard(const signed char* v) {
     58    return v ? v : kSignedCharNull.data();
     59  }
     60 };
     61 template <>
     62 struct NullGuard<const signed char*> final {
     63  static const signed char* Guard(const signed char* v) {
     64    return v ? v : kSignedCharNull.data();
     65  }
     66 };
     67 template <>
     68 struct NullGuard<unsigned char*> final {
     69  static const unsigned char* Guard(const unsigned char* v) {
     70    return v ? v : kUnsignedCharNull.data();
     71  }
     72 };
     73 template <>
     74 struct NullGuard<const unsigned char*> final {
     75  static const unsigned char* Guard(const unsigned char* v) {
     76    return v ? v : kUnsignedCharNull.data();
     77  }
     78 };
     79 template <>
     80 struct NullGuard<std::nullptr_t> final {
     81  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_