v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
string-literal.h
Go to the documentation of this file.
1// Copyright 2024 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_BASE_TEMPLATE_META_PROGRAMMING_STRING_LITERAL_H_
6#define V8_BASE_TEMPLATE_META_PROGRAMMING_STRING_LITERAL_H_
7
8#include <algorithm>
9
11#include "src/base/logging.h"
12
13namespace v8::base::tmp {
14
15#ifdef HAS_CPP_CLASS_TYPES_AS_TEMPLATE_ARGS
16
17#ifdef __cpp_lib_to_array
18using std::to_array;
19#else
20namespace detail {
21template <typename T, size_t N, size_t... I>
22constexpr std::array<std::remove_cv_t<T>, N> to_array_impl(
23 T (&a)[N], std::index_sequence<I...>) {
24 return {{a[I]...}};
25}
26} // namespace detail
27template <typename T, size_t N>
28constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]) {
29 return detail::to_array_impl(a, std::make_index_sequence<N>{});
30}
31#endif
32
33// This class provides a way to pass compile time string literals to templates
34// using extended non-type template parameters.
35template <size_t N>
36class StringLiteral {
37 public:
38 constexpr StringLiteral(const char (&s)[N]) // NOLINT(runtime/explicit)
39 : data_(to_array(s)) {
40 // We assume '\0' terminated strings.
41 DCHECK_EQ(data_[N - 1], '\0');
42 }
43
44 size_t size() const {
45 DCHECK_EQ(data_[N - 1], '\0');
46 // `size` does not include the terminating '\0'.
47 return N - 1;
48 }
49
50 const char* c_str() const { return data_.data(); }
51
52 // `data_` cannot be private to satisify requirements of a structural type.
53 const std::array<char, N> data_;
54};
55
56// Deduction guide for `StringLiteral`.
57template <size_t N>
58StringLiteral(const char (&)[N]) -> StringLiteral<N>;
59
60#endif
61
62} // namespace v8::base::tmp
63
64#endif // V8_BASE_TEMPLATE_META_PROGRAMMING_STRING_LITERAL_H_
#define T
uint8_t data_[MAX_STACK_LENGTH]
constexpr int N
constexpr std::array< std::remove_cv_t< T >, N > to_array(T(&a)[N])
#define I(name, number_of_args, result_size)
Definition runtime.cc:36
#define DCHECK_EQ(v1, v2)
Definition logging.h:485