Berserk
StringName.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_STRINGNAME_HPP
29 #define BERSERK_STRINGNAME_HPP
30 
31 #include <core/Config.hpp>
32 #include <core/Typedefs.hpp>
33 #include <core/string/String.hpp>
34 #include <core/templates/Ref.hpp>
36 
37 #include <mutex>
38 #include <ostream>
39 #include <unordered_map>
40 #include <utility>
41 
43 
61 class StringName {
62 public:
64  BRK_API explicit StringName(const String &str = String());
65 
67  StringName(const StringName &other);
68 
70  StringName(StringName &&other) noexcept;
71 
74 
76  BRK_API StringName &operator=(const StringName &other);
77 
79  BRK_API StringName &operator=(StringName &&other) noexcept;
80 
82  BRK_API bool operator==(const StringName &other) const;
83 
85  BRK_API bool operator!=(const StringName &other) const;
86 
88  BRK_API const String &GetStr() const;
89 
91  BRK_API size_t GetHash() const;
92 
93 private:
95  class Node : public RefCnt {
96  public:
97  explicit Node(String str) : mString(std::move(str)), mHash(std::hash<String>{}(mString)) {}
98  const String &GetStr() const { return mString; }
99  size_t GetHash() const { return mHash; }
100 
101  private:
102  String mString;
103  size_t mHash;
104  };
105 
106  void Release();
107 
109  Ref<Node> mNode;
110 
111 private:
113  static std::mutex &GetAccessMutex();
114  static std::unordered_map<String, Ref<Node>> &GetCachedNames();
115 };
116 
122 
123 namespace std {
124 
125  template<>
126  struct hash<BRK_NS::StringName> {
127  public:
128  std::size_t operator()(const BRK_NS::StringName &stringName) const {
129  return stringName.GetHash();
130  }
131  };
132 
133 }// namespace std
134 
135 inline std::ostream &operator<<(std::ostream &stream, const BRK_NS::StringName &name) {
136  stream << name.GetStr();
137  return stream;
138 }
139 
140 #endif//BERSERK_STRINGNAME_HPP
#define BRK_NS_END
Definition: Config.hpp:48
#define BRK_API
Definition: Config.hpp:32
#define BRK_NS
Definition: Config.hpp:50
std::ostream & operator<<(std::ostream &stream, const BRK_NS::StringName &name)
Definition: StringName.hpp:135
std::size_t size_t
Definition: Typedefs.hpp:49
Reference counted base object.
Definition: RefCnt.hpp:52
Cached shared utf-8 string id.
Definition: StringName.hpp:61
BRK_API StringName & operator=(const StringName &other)
Definition: StringName.cpp:64
BRK_API bool operator!=(const StringName &other) const
Definition: StringName.cpp:87
BRK_API ~StringName()
Definition: StringName.cpp:60
BRK_API StringName(const String &str=String())
Definition: StringName.cpp:32
BRK_API const String & GetStr() const
Definition: StringName.cpp:91
BRK_API bool operator==(const StringName &other) const
Definition: StringName.cpp:83
BRK_API size_t GetHash() const
Definition: StringName.cpp:96
Utf-8 encoded std based default string class.
std::string String
Definition: String.hpp:47
Definition: GLDevice.cpp:46
Definition: TQuat.hpp:431
std::size_t operator()(const BRK_NS::StringName &stringName) const
Definition: StringName.hpp:128