临时保存
This commit is contained in:
54
windows/src/common/utils/singleton.h
Normal file
54
windows/src/common/utils/singleton.h
Normal file
@@ -0,0 +1,54 @@
|
||||
// Copyright (c) 2022 NetEase, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef BASE_SINGLETON_H_
|
||||
#define BASE_SINGLETON_H_
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
namespace utils {
|
||||
template <typename T>
|
||||
class Singleton {
|
||||
public:
|
||||
static T* getInstance();
|
||||
|
||||
Singleton(const Singleton& other) = delete;
|
||||
Singleton<T>& operator=(const Singleton& other) = delete;
|
||||
|
||||
private:
|
||||
static std::mutex mutex;
|
||||
static T* instance;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
std::mutex Singleton<T>::mutex;
|
||||
template <typename T>
|
||||
T* Singleton<T>::instance;
|
||||
template <typename T>
|
||||
T* Singleton<T>::getInstance() {
|
||||
if (instance == nullptr) {
|
||||
std::lock_guard<std::mutex> locker(mutex);
|
||||
if (instance == nullptr) {
|
||||
instance = new T();
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
#define SINGLETONG(Class) \
|
||||
private: \
|
||||
friend class utils::Singleton<Class>; \
|
||||
\
|
||||
public: \
|
||||
static Class* getInstance() { return utils::Singleton<Class>::getInstance(); }
|
||||
|
||||
#define HIDE_CONSTRUCTOR(Class) \
|
||||
private: \
|
||||
Class() = default; \
|
||||
Class(const Class& other) = delete; \
|
||||
Class& operator=(const Class& other) = delete; \
|
||||
}
|
||||
|
||||
}
|
||||
#endif // BASE_SINGLETON_H_
|
||||
35
windows/src/common/utils/stringHash.hpp
Normal file
35
windows/src/common/utils/stringHash.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright (c) 2022 NetEase, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef BASE_STRINGHASH_H_
|
||||
#define BASE_STRINGHASH_H_
|
||||
|
||||
namespace utils {
|
||||
typedef std::uint64_t hash_t;
|
||||
|
||||
constexpr hash_t prime = 0x100000001B3ull;
|
||||
constexpr hash_t basis = 0xCBF29CE484222325ull;
|
||||
|
||||
inline hash_t hash_(char const* str) {
|
||||
hash_t ret{basis};
|
||||
while (*str) {
|
||||
ret ^= *str;
|
||||
ret *= prime;
|
||||
str++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
constexpr hash_t hash_compile_time(char const* str, hash_t last_value = basis) {
|
||||
return *str ? hash_compile_time(str + 1, (*str ^ last_value) * prime) : last_value;
|
||||
}
|
||||
|
||||
} // namespace utils
|
||||
|
||||
constexpr unsigned long long operator"" _hash(char const* p, size_t) {
|
||||
return utils::hash_compile_time(p);
|
||||
}
|
||||
|
||||
#endif // BASE_STRINGHASH_H_
|
||||
Reference in New Issue
Block a user