AeonGUI
A portable video game graphic user interface library.
Loading...
Searching...
No Matches
StringLiteral.hpp
1/*
2Copyright (C) 2018,2019,2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef AEONGUI_STRINGLITERAL_H
17#define AEONGUI_STRINGLITERAL_H
18#include <cstdint>
19#include <string>
20#include <cstring>
21#include <functional>
22#include <iostream>
23
24namespace AeonGUI
25{
32 {
33 public:
38 template<size_t aStringSize>
39 constexpr StringLiteral ( const char ( &aString ) [aStringSize] ) :
40 mString{aString}, mStringSize{aStringSize} {}
41
43 constexpr StringLiteral() noexcept = default;
45 constexpr StringLiteral ( const StringLiteral& ) noexcept = default;
49 constexpr StringLiteral ( StringLiteral&& aString ) noexcept = default;
53 constexpr StringLiteral& operator= ( const StringLiteral& ) noexcept = default;
57 constexpr StringLiteral& operator= ( StringLiteral&& ) noexcept = default;
58
62 constexpr const char* GetString() const
63 {
64 return mString;
65 }
66
69 constexpr size_t GetStringSize() const
70 {
71 return mStringSize;
72 }
73
77 constexpr bool operator == ( const StringLiteral& b ) const
78 {
79 return ( mString == b.mString || std::char_traits<char>::compare ( mString, b.mString, mStringSize ) == 0 );
80 }
81
85#ifdef __GNUG__
86 constexpr
87#endif
88 bool operator == ( const char* b ) const
89 {
90 return ( mString == b ) || std::char_traits<char>::compare ( mString, b, mStringSize ) == 0;
91 }
92
96 bool operator == ( const std::string &b ) const
97 {
98 return b == mString;
99 }
100
101 operator std::string() const
102 {
103 return std::string{mString};
104 }
105 private:
106 const char* mString{};
107 size_t mStringSize{};
108 };
109
116 {
121 size_t operator() ( const StringLiteral& Key ) const noexcept
122 {
123 return std::hash<std::string> {} ( Key );
124 }
125 };
126}
127
128#endif
Compile-time string literal wrapper.
Definition StringLiteral.hpp:32
constexpr size_t GetStringSize() const
Get the string size including the null terminator.
Definition StringLiteral.hpp:69
constexpr StringLiteral(const char(&aString)[aStringSize])
Construct from a string literal.
Definition StringLiteral.hpp:39
constexpr bool operator==(const StringLiteral &b) const
Compare with another StringLiteral for equality.
Definition StringLiteral.hpp:77
constexpr const char * GetString() const
Get the underlying C string.
Definition StringLiteral.hpp:62
constexpr StringLiteral() noexcept=default
Default constructor.
Hash functor for StringLiteral.
Definition StringLiteral.hpp:116
size_t operator()(const StringLiteral &Key) const noexcept
Compute hash for a StringLiteral.
Definition StringLiteral.hpp:121