grpc
third_party
abseil-cpp
absl
strings
internal
abseil-cpp/absl/strings/internal/string_constant.h
Go to the documentation of this file.
1
// Copyright 2020 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
#ifndef ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_
16
#define ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_
17
18
#include "absl/meta/type_traits.h"
19
#include "absl/strings/string_view.h"
20
21
namespace
absl
{
22
ABSL_NAMESPACE_BEGIN
23
namespace
strings_internal {
24
25
// StringConstant<T> represents a compile time string constant.
26
// It can be accessed via its `absl::string_view value` static member.
27
// It is guaranteed that the `string_view` returned has constant `.data()`,
28
// constant `.size()` and constant `value[i]` for all `0 <= i < .size()`
29
//
30
// The `T` is an opaque type. It is guaranteed that different string constants
31
// will have different values of `T`. This allows users to associate the string
32
// constant with other static state at compile time.
33
//
34
// Instances should be made using the `MakeStringConstant()` factory function
35
// below.
36
template
<
typename
T>
37
struct
StringConstant
{
38
private
:
39
static
constexpr
bool
TryConstexprEval
(
absl::string_view
view) {
40
return
view.
empty
() || 2 * view[0] != 1;
41
}
42
43
public
:
44
static
constexpr
absl::string_view
value
=
T
{}();
45
constexpr
absl::string_view
operator()
()
const
{
return
value
; }
46
47
// Check to be sure `view` points to constant data.
48
// Otherwise, it can't be constant evaluated.
49
static_assert(
TryConstexprEval
(
value
),
50
"The input string_view must point to constant data."
);
51
};
52
53
#ifdef ABSL_INTERNAL_NEED_REDUNDANT_CONSTEXPR_DECL
54
template
<
typename
T>
55
constexpr
absl::string_view
StringConstant<T>::value
;
56
#endif
57
58
// Factory function for `StringConstant` instances.
59
// It supports callables that have a constexpr default constructor and a
60
// constexpr operator().
61
// It must return an `absl::string_view` or `const char*` pointing to constant
62
// data. This is validated at compile time.
63
template
<
typename
T>
64
constexpr
StringConstant<T>
MakeStringConstant
(
T
) {
65
return
{};
66
}
67
68
}
// namespace strings_internal
69
ABSL_NAMESPACE_END
70
}
// namespace absl
71
72
#endif // ABSL_STRINGS_INTERNAL_STRING_CONSTANT_H_
absl::string_view
Definition:
abseil-cpp/absl/strings/string_view.h:167
absl::strings_internal::StringConstant::value
static constexpr absl::string_view value
Definition:
abseil-cpp/absl/strings/internal/string_constant.h:44
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition:
third_party/abseil-cpp/absl/base/config.h:171
T
#define T(upbtypeconst, upbtype, ctype, default_value)
absl::strings_internal::StringConstant::TryConstexprEval
static constexpr bool TryConstexprEval(absl::string_view view)
Definition:
abseil-cpp/absl/strings/internal/string_constant.h:39
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition:
third_party/abseil-cpp/absl/base/config.h:170
absl::strings_internal::MakeStringConstant
constexpr StringConstant< T > MakeStringConstant(T)
Definition:
abseil-cpp/absl/strings/internal/string_constant.h:64
absl::strings_internal::StringConstant
Definition:
abseil-cpp/absl/strings/internal/string_constant.h:37
absl::string_view::empty
constexpr bool empty() const noexcept
Definition:
abseil-cpp/absl/strings/string_view.h:292
absl
Definition:
abseil-cpp/absl/algorithm/algorithm.h:31
absl::strings_internal::StringConstant::operator()
constexpr absl::string_view operator()() const
Definition:
abseil-cpp/absl/strings/internal/string_constant.h:45
grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:21