Aeon Engine c550894
AeonGames Open Source Game Engine
Loading...
Searching...
No Matches
StringId.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 AEONGAMES_STRINGID_H
17#define AEONGAMES_STRINGID_H
18#include <cstdint>
19#include <string>
20#include <functional>
21#include "aeongames/CRC.hpp"
22
23namespace AeonGames
24{
31 {
32 public:
37 template<uint32_t aStringSize>
38 constexpr StringId ( const char ( &aString ) [aStringSize] ) :
39 mString{aString}, mStringSize{aStringSize}, mId{crc32r ( aString ) } {}
40
41 constexpr StringId() noexcept = default;
43 constexpr StringId ( const StringId& ) noexcept = default;
45 constexpr StringId ( StringId&& aString ) noexcept = default;
47 constexpr StringId& operator= ( const StringId& ) noexcept = default;
49 constexpr StringId& operator= ( StringId&& ) noexcept = default;
50
52 constexpr uint32_t GetId() const
53 {
54 return mId;
55 };
56
57 constexpr const char* GetString() const
58 {
59 return mString;
60 };
61
62 constexpr uint32_t GetStringSize() const
63 {
64 return mStringSize;
65 };
66
67 constexpr bool operator == ( const StringId &b ) const
68 {
69 return mId == b.mId;
70 }
71
72 constexpr bool operator == ( uint32_t b ) const
73 {
74 return mId == b;
75 }
76
77 constexpr operator uint32_t() const
78 {
79 return mId;
80 }
81
82 operator std::string() const
83 {
84 return std::string{mString};
85 }
86 private:
87 const char* mString{};
88 uint32_t mStringSize{};
89 uint32_t mId{};
90 };
91}
92
93namespace std
94{
96 template <> struct hash<AeonGames::StringId>
97 {
99 constexpr size_t operator() ( const AeonGames::StringId& aStringId ) const
100 {
101 return aStringId;
102 }
103 };
104}
105#endif
CRC-based compile-time string identifier.
Definition StringId.hpp:31
constexpr const char * GetString() const
Get the underlying string pointer.
Definition StringId.hpp:57
constexpr uint32_t GetId() const
Get the CRC32 identifier.
Definition StringId.hpp:52
constexpr StringId(const char(&aString)[aStringSize])
Construct a StringId from a string literal.
Definition StringId.hpp:38
constexpr uint32_t GetStringSize() const
Get the size of the string including null terminator.
Definition StringId.hpp:62
constexpr bool operator==(const StringId &b) const
Compare two StringId objects for equality.
Definition StringId.hpp:67
<- This is here just for the literals
Definition AABB.hpp:31
STL namespace.