string_literal.h Source File

CPP API: string_literal.h Source File
string_literal.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Rene Schmieding
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_UTILS_STRING_LITERAL_H
21 #define MIO_UTILS_STRING_LITERAL_H
22 
23 #include <algorithm>
24 #include <cstddef>
25 #include <string_view>
26 
27 namespace mio
28 {
29 
31 template <size_t Size>
32 struct StringLiteral {
33  using value_type = char;
34  using pointer = value_type*;
35  using const_pointer = const value_type*;
36  using size_type = size_t;
37  using string_view_type = std::basic_string_view<value_type>;
38 
40  static constexpr size_type size()
41  {
42  return Size;
43  }
44 
50  constexpr StringLiteral(const value_type (&string_literal)[size() + 1])
51  {
52  std::copy_n(string_literal, size() + 1, value);
53  }
54 
56  constexpr StringLiteral() = default;
57 
59  constexpr bool empty() const
60  {
61  return size() == 0;
62  }
63 
65  constexpr pointer data()
66  {
67  return value;
68  }
69 
71  constexpr const_pointer data() const
72  {
73  return value;
74  }
75 
77  constexpr operator string_view_type() const
78  {
79  return string_view_type(data(), size());
80  }
81 
83  constexpr string_view_type string_view() const
84  {
85  return {*this};
86  }
87 
94  template <size_type N>
95  friend constexpr auto operator+(const StringLiteral& left, const StringLiteral<N>& right)
96  {
97  constexpr size_type new_size = left.size() + right.size();
98  StringLiteral<new_size> new_string;
99  pointer val = new_string.data();
100  val = std::copy_n(left.data(), left.size(), val); // do not copy the terminating \0
101  std::copy_n(right.data(), right.size() + 1, val);
102  return new_string;
103  }
104  template <size_type N>
105  friend constexpr auto operator+(const StringLiteral& left, const value_type (&right)[N])
106  {
107  auto r = StringLiteral<N - 1>(right);
108  return left + r;
109  }
110  template <size_type N>
111  friend constexpr auto operator+(const value_type (&left)[N], const StringLiteral& right)
112  {
113  auto l = StringLiteral<N - 1>(left);
114  return l + right;
115  }
124  template <size_type N>
125  friend constexpr auto operator==(const StringLiteral& left, const StringLiteral<N>& right)
126  {
127  return left.string_view() == right.string_view();
128  }
129  template <size_type N>
130  friend constexpr auto operator==(const StringLiteral& left, const value_type (&right)[N])
131  {
132  auto r = StringLiteral<N - 1>(right);
133  return left == r;
134  }
137  value_type value[size() + 1] = {};
138 };
139 
140 // Deduction guide to help the compiler with setting the correct size, i.e. not counting the terminating '\0' char.
141 template <size_t Size>
142 StringLiteral(const char (&string)[Size]) -> StringLiteral<Size - 1>;
143 
144 } // namespace mio
145 
146 #endif // MIO_UTILS_STRING_LITERAL_H
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
StringLiteral(const char(&string)[Size]) -> StringLiteral< Size - 1 >
Wrapper for string literals, that allows passing them as template arguments. Should be used with cons...
Definition: string_literal.h:32
constexpr string_view_type string_view() const
Get a string_view of this literal. Be mindful of the lifetime of the view object.
Definition: string_literal.h:83
constexpr friend auto operator+(const StringLiteral &left, const StringLiteral< N > &right)
Join two literals.
Definition: string_literal.h:95
char value_type
Definition: string_literal.h:33
static constexpr size_type size()
The length of the StringLiteral (not counting the trailing '\0').
Definition: string_literal.h:40
value_type value[size()+1]
Contains the actual characters. Access this through data() or string_view().
Definition: string_literal.h:137
constexpr const_pointer data() const
Access the underlying array.
Definition: string_literal.h:71
constexpr friend auto operator==(const StringLiteral &left, const value_type(&right)[N])
Compare two literals.
Definition: string_literal.h:130
size_t size_type
Definition: string_literal.h:36
const value_type * const_pointer
Definition: string_literal.h:35
constexpr friend auto operator+(const StringLiteral &left, const value_type(&right)[N])
Join two literals.
Definition: string_literal.h:105
constexpr StringLiteral(const value_type(&string_literal)[size()+1])
Construct a StringLiteral.
Definition: string_literal.h:50
constexpr friend auto operator+(const value_type(&left)[N], const StringLiteral &right)
Join two literals.
Definition: string_literal.h:111
value_type * pointer
Definition: string_literal.h:34
constexpr pointer data()
Access the underlying array. Modification is only possible during compile time.
Definition: string_literal.h:65
constexpr friend auto operator==(const StringLiteral &left, const StringLiteral< N > &right)
Compare two literals.
Definition: string_literal.h:125
constexpr bool empty() const
Check whether the StringLiteral is empty.
Definition: string_literal.h:59
std::basic_string_view< value_type > string_view_type
Definition: string_literal.h:37
constexpr StringLiteral()=default
Create a string filled with '\0'. Mainly used by StringLiteral internally.