Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:40:42

0001 // Copyright 2023 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 #ifndef ABSL_BASE_INTERNAL_NULLABILITY_IMPL_H_
0016 #define ABSL_BASE_INTERNAL_NULLABILITY_IMPL_H_
0017 
0018 #include <memory>
0019 #include <type_traits>
0020 
0021 #include "absl/base/attributes.h"
0022 #include "absl/base/config.h"
0023 #include "absl/meta/type_traits.h"
0024 
0025 namespace absl {
0026 ABSL_NAMESPACE_BEGIN
0027 namespace nullability_internal {
0028 
0029 // `IsNullabilityCompatible` checks whether its first argument is a class
0030 // explicitly tagged as supporting nullability annotations. The tag is the type
0031 // declaration `absl_nullability_compatible`.
0032 template <typename, typename = void>
0033 struct IsNullabilityCompatible : std::false_type {};
0034 
0035 template <typename T>
0036 struct IsNullabilityCompatible<
0037     T, absl::void_t<typename T::absl_nullability_compatible>> : std::true_type {
0038 };
0039 
0040 template <typename T>
0041 constexpr bool IsSupportedType = IsNullabilityCompatible<T>::value;
0042 
0043 template <typename T>
0044 constexpr bool IsSupportedType<T*> = true;
0045 
0046 template <typename T, typename U>
0047 constexpr bool IsSupportedType<T U::*> = true;
0048 
0049 template <typename T, typename... Deleter>
0050 constexpr bool IsSupportedType<std::unique_ptr<T, Deleter...>> = true;
0051 
0052 template <typename T>
0053 constexpr bool IsSupportedType<std::shared_ptr<T>> = true;
0054 
0055 template <typename T>
0056 struct EnableNullable {
0057   static_assert(nullability_internal::IsSupportedType<std::remove_cv_t<T>>,
0058                 "Template argument must be a raw or supported smart pointer "
0059                 "type. See absl/base/nullability.h.");
0060   using type = T;
0061 };
0062 
0063 template <typename T>
0064 struct EnableNonnull {
0065   static_assert(nullability_internal::IsSupportedType<std::remove_cv_t<T>>,
0066                 "Template argument must be a raw or supported smart pointer "
0067                 "type. See absl/base/nullability.h.");
0068   using type = T;
0069 };
0070 
0071 template <typename T>
0072 struct EnableNullabilityUnknown {
0073   static_assert(nullability_internal::IsSupportedType<std::remove_cv_t<T>>,
0074                 "Template argument must be a raw or supported smart pointer "
0075                 "type. See absl/base/nullability.h.");
0076   using type = T;
0077 };
0078 
0079 // Note: we do not apply Clang nullability attributes (e.g. _Nullable).  These
0080 // only support raw pointers, and conditionally enabling them only for raw
0081 // pointers inhibits template arg deduction.  Ideally, they would support all
0082 // pointer-like types.
0083 template <typename T, typename = typename EnableNullable<T>::type>
0084 using NullableImpl
0085 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate)
0086     [[clang::annotate("Nullable")]]
0087 #endif
0088     = T;
0089 
0090 template <typename T, typename = typename EnableNonnull<T>::type>
0091 using NonnullImpl
0092 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate)
0093     [[clang::annotate("Nonnull")]]
0094 #endif
0095     = T;
0096 
0097 template <typename T, typename = typename EnableNullabilityUnknown<T>::type>
0098 using NullabilityUnknownImpl
0099 #if ABSL_HAVE_CPP_ATTRIBUTE(clang::annotate)
0100     [[clang::annotate("Nullability_Unspecified")]]
0101 #endif
0102     = T;
0103 
0104 }  // namespace nullability_internal
0105 ABSL_NAMESPACE_END
0106 }  // namespace absl
0107 
0108 #endif  // ABSL_BASE_INTERNAL_NULLABILITY_IMPL_H_