Skip to content

Commit 376f61b

Browse files
committed
Improve StringLiteral interface
1 parent 52e1bff commit 376f61b

File tree

3 files changed

+86
-25
lines changed

3 files changed

+86
-25
lines changed

lib/core/include/qx/core/qx-abstracterror.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ friend class Error;
8888
//-Class Variables----------------------------------------------------------------------------------------------------------
8989
public:
9090
static constexpr quint16 TYPE_CODE = ECode;
91-
static constexpr QLatin1StringView TYPE_NAME{EName.value};
91+
static constexpr QLatin1StringView TYPE_NAME{EName};
9292

9393
private:
9494
static const bool REGISTER;
Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,58 @@
11
#ifndef QX_STRINGLITERAL_H
22
#define QX_STRINGLITERAL_H
33

4+
// Standard Library Includes
45
#include <algorithm>
6+
#include <concepts>
7+
8+
// Qt Includes
9+
#include <QLatin1String>
510

611
namespace Qx
712
{
813

9-
template<size_t N>
10-
struct StringLiteral
14+
template<std::integral C, size_t N>
15+
class TStringLiteral
1116
{
12-
//-Instance Fields---------------------------------------------------------------------------------------------------
13-
char value[N];
17+
//-Instance Variables---------------------------------------------------------------------------------------------------
18+
public:
19+
C _str[N]; // Must be public due to C++20 limitations
1420

1521
//-Constructor----------------------------------------------------------------------------------------------------------
16-
constexpr StringLiteral(const char (&str)[N])
17-
{
18-
std::copy_n(str, N, value);
19-
}
20-
};
22+
private:
23+
constexpr TStringLiteral() = default;
2124

22-
template <size_t N>
23-
struct StringLiteral16
24-
{
25-
char16_t value[N];
25+
public:
26+
constexpr TStringLiteral(const C (&str)[N]) { std::copy_n(str, N, _str); }
27+
28+
//-Instance Functions----------------------------------------------------------------------------------------------------
29+
public:
30+
const C* data() const { return _str; }
31+
size_t size() const { return N - 1; }
2632

27-
constexpr StringLiteral16(const char16_t (&str)[N])
33+
//-Operators--------------------------------------------------------------------------------------------------------
34+
public:
35+
constexpr std::strong_ordering operator<=>(const TStringLiteral& other) const = default;
36+
37+
template<size_t M>
38+
constexpr auto operator+(const TStringLiteral<C, M>& other) const
2839
{
29-
std::copy_n(str, N, value);
40+
TStringLiteral<C, N + M - 1> res;
41+
std::copy_n(this->_str, N - 1, res); // N chars
42+
std::copy_n(other._str, M, res + (N - 1)); // M chars + '/0'
43+
return res;
3044
}
45+
46+
constexpr operator QLatin1StringView() const requires std::same_as<C, char> { return QLatin1StringView(_str, N - 1); }
47+
constexpr operator QStringView() const requires std::same_as<C, char16_t > { return QStringView(_str, N - 1); }
3148
};
3249

50+
template<size_t N>
51+
using StringLiteral = TStringLiteral<char, N>;
52+
53+
template<size_t N>
54+
using StringLiteral16 = TStringLiteral<char16_t, N>;
55+
3356
}
3457

3558
#endif // QX_STRINGLITERAL_H

lib/utility/src/qx-stringliteral.dox

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,65 @@ namespace Qx
55
//===============================================================================================================
66

77
/*!
8-
* @struct StringLiteral qx/utility/qx-stringliteral.h
8+
* @struct TStringLiteral qx/utility/qx-stringliteral.h
99
* @ingroup qx-utility
1010
*
11-
* @brief The StringLiteral template struct acts as a literal class type wrapper around a C-style string
12-
* that effectively allows the string to be used as a non-type template parameter.
11+
* @brief The TStringLiteral template class acts as a literal class type wrapper around a C-style string
12+
* that effectively allows the string to be used as a non-type template parameter. Generally you should
13+
* use the predefined aliases instead of the template directly.
1314
*
1415
* @snippet qx-stringliteral.cpp 0
1516
*/
1617

17-
//-Instance Fields---------------------------------------------------------------------------------------------
18+
//-Constructor-------------------------------------------------------------------------------------------------
19+
//Public:
1820
/*!
19-
* @var StringLiteral::value
21+
* @fn TStringLiteral::TStringLiteral(const C (&str)[N])
2022
*
21-
* The wrapped C-Style string.
23+
* Wraps the C-Style string @a str of length @a N.
2224
*/
2325

24-
//-Constructor-------------------------------------------------------------------------------------------------
26+
//-Instance Functions----------------------------------------------------------------------------------------------------
2527
//Public:
2628
/*!
27-
* @fn StringLiteral<N>::StringLiteral(const char (&str)[N])
29+
* @fn const C* TStringLiteral::data() const
2830
*
29-
* Wraps the C-Style string @a str of length @a N.
31+
* Returns a pointer to the c-style string.
32+
*/
33+
34+
/*!
35+
* @fn size_t TStringLiteral::size() const
36+
*
37+
* Returns the size of the string.
38+
*/
39+
40+
//-Operators-------------------------------------------------------------------------------------------------------
41+
//Public:
42+
/*!
43+
* @fn bool TStringLiteral::operator<=>(const TStringLiteral& other) const
44+
*
45+
* Returns @c true if the TStringLiteral is equal to @a other; otherwise, returns @c false.
46+
*/
47+
48+
/*!
49+
* @fn auto TStringLiteral::operator+(const TStringLiteral<C, M>& other) const
50+
*
51+
* Returns a string which is the result of concatenating @c this and @a other.
52+
*/
53+
54+
//===============================================================================================================
55+
// <file>
56+
//===============================================================================================================
57+
58+
/*!
59+
* @typedef StringLiteral
60+
*
61+
* A TStringLiteral with character type @c char.
62+
*/
63+
64+
/*!
65+
* @typedef StringLiteral16
66+
*
67+
* A TStringLiteral with character type @c char16_t.
3068
*/
3169
}

0 commit comments

Comments
 (0)