relx 0.1.0
A Modern C++23 Type-Safe SQL Query Builder
Loading...
Searching...
No Matches
fixed_string.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cstddef>
6#include <ostream>
7#include <string_view>
8
9namespace relx {
10namespace schema {
11
24template <std::size_t N>
26 constexpr fixed_string(const char (&str)[N]) : value{} { std::copy_n(str, N, value); }
27
28 constexpr fixed_string(const fixed_string&) = default;
29
30 char value[N];
31
32 constexpr operator std::string_view() const {
33 return std::string_view(value, N - 1); // exclude null terminator
34 }
35
36 constexpr const char* c_str() const { return value; }
37
38 constexpr std::size_t size() const {
39 return N - 1; // exclude null terminator
40 }
41
42 constexpr bool empty() const { return size() == 0; }
43
44 template <std::size_t M>
45 constexpr bool operator==(const fixed_string<M>& other) const {
46 return std::string_view(*this) == std::string_view(other);
47 }
48
49 template <std::size_t M>
50 constexpr bool operator!=(const fixed_string<M>& other) const {
51 return !(*this == other);
52 }
53};
54
55// Output stream operator
56template <std::size_t N>
57std::ostream& operator<<(std::ostream& os, const fixed_string<N>& str) {
58 return os << std::string_view(str);
59}
60} // namespace schema
61namespace literals {
62// Standard C++11 user-defined literal syntax
63template <char... Chars>
64constexpr auto operator""_fs() {
65 constexpr char str[] = {Chars..., '\0'};
66 return schema::fixed_string<sizeof...(Chars) + 1>(str);
67}
68} // namespace literals
69
70} // namespace relx
std::ostream & operator<<(std::ostream &os, const fixed_string< N > &str)
relx database connection
Compile-time string type that can be used as template non-type parameter in C++20.
constexpr const char * c_str() const
constexpr fixed_string(const char(&str)[N])
constexpr fixed_string(const fixed_string &)=default
constexpr bool operator==(const fixed_string< M > &other) const
constexpr bool empty() const
constexpr std::size_t size() const
constexpr bool operator!=(const fixed_string< M > &other) const