Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:41:01

0001 // Copyright 2020 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_STRINGS_INTERNAL_STRING_CONSTANT_H_
0016 #define ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_
0017 
0018 #include "absl/meta/type_traits.h"
0019 #include "absl/strings/string_view.h"
0020 
0021 namespace absl {
0022 ABSL_NAMESPACE_BEGIN
0023 namespace strings_internal {
0024 
0025 // StringConstant<T> represents a compile time string constant.
0026 // It can be accessed via its `absl::string_view value` static member.
0027 // It is guaranteed that the `string_view` returned has constant `.data()`,
0028 // constant `.size()` and constant `value[i]` for all `0 <= i < .size()`
0029 //
0030 // The `T` is an opaque type. It is guaranteed that different string constants
0031 // will have different values of `T`. This allows users to associate the string
0032 // constant with other static state at compile time.
0033 //
0034 // Instances should be made using the `MakeStringConstant()` factory function
0035 // below.
0036 template <typename T>
0037 struct StringConstant {
0038  private:
0039   static constexpr bool TryConstexprEval(absl::string_view view) {
0040     return view.empty() || 2 * view[0] != 1;
0041   }
0042 
0043  public:
0044   static constexpr absl::string_view value = T{}();
0045   constexpr absl::string_view operator()() const { return value; }
0046 
0047   // Check to be sure `view` points to constant data.
0048   // Otherwise, it can't be constant evaluated.
0049   static_assert(TryConstexprEval(value),
0050                 "The input string_view must point to constant data.");
0051 };
0052 
0053 #ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
0054 template <typename T>
0055 constexpr absl::string_view StringConstant<T>::value;
0056 #endif
0057 
0058 // Factory function for `StringConstant` instances.
0059 // It supports callables that have a constexpr default constructor and a
0060 // constexpr operator().
0061 // It must return an `absl::string_view` or `const char*` pointing to constant
0062 // data. This is validated at compile time.
0063 template <typename T>
0064 constexpr StringConstant<T> MakeStringConstant(T) {
0065   return {};
0066 }
0067 
0068 }  // namespace strings_internal
0069 ABSL_NAMESPACE_END
0070 }  // namespace absl
0071 
0072 #endif  // ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_