mirror of
https://github.com/hyprwm/Hyprland
synced 2024-11-06 00:25:59 +01:00
1ed1ce9506
moves std::shared_ptrs to a new implementation Advantages: - you can dereference a weak_ptr directly. This will obviously segfault on a nullptr deref if it's expired. - this is useful to avoid the .lock() hell where we are 100% sure the pointer _should_ be valid. (and if it isn't, it should throw.) - weak_ptrs are still valid while the SP is being destroyed. - reasoning: while an object (e.g. CWindow) is being destroyed, its `weak_ptr self` should be accessible (the sp is still alive, and so is CWindow), but it's not because by stl it's already expired (to prevent resurrection) - this impl solves it differently. w_p is expired, but can still be dereferenced and used. Creating `s_p`s is not possible anymore, though. - this is useful in destructors and callbacks.
49 lines
No EOL
1.7 KiB
C++
49 lines
No EOL
1.7 KiB
C++
#include "TokenManager.hpp"
|
|
#include <uuid/uuid.h>
|
|
#include <algorithm>
|
|
|
|
CUUIDToken::CUUIDToken(const std::string& uuid_, std::any data_, std::chrono::system_clock::duration expires) : data(data_), uuid(uuid_) {
|
|
expiresAt = std::chrono::system_clock::now() + expires;
|
|
}
|
|
|
|
std::string CUUIDToken::getUUID() {
|
|
return uuid;
|
|
}
|
|
|
|
std::string CTokenManager::getRandomUUID() {
|
|
std::string uuid;
|
|
do {
|
|
uuid_t uuid_;
|
|
uuid_generate_random(uuid_);
|
|
uuid = std::format("{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", (uint16_t)uuid_[0], (uint16_t)uuid_[1],
|
|
(uint16_t)uuid_[2], (uint16_t)uuid_[3], (uint16_t)uuid_[4], (uint16_t)uuid_[5], (uint16_t)uuid_[6], (uint16_t)uuid_[7], (uint16_t)uuid_[8],
|
|
(uint16_t)uuid_[9], (uint16_t)uuid_[10], (uint16_t)uuid_[11], (uint16_t)uuid_[12], (uint16_t)uuid_[13], (uint16_t)uuid_[14], (uint16_t)uuid_[15]);
|
|
} while (m_mTokens.contains(uuid));
|
|
|
|
return uuid;
|
|
}
|
|
|
|
std::string CTokenManager::registerNewToken(std::any data, std::chrono::system_clock::duration expires) {
|
|
std::string uuid = getRandomUUID();
|
|
|
|
m_mTokens[uuid] = makeShared<CUUIDToken>(uuid, data, expires);
|
|
return uuid;
|
|
}
|
|
|
|
SP<CUUIDToken> CTokenManager::getToken(const std::string& uuid) {
|
|
|
|
// cleanup expired tokens
|
|
const auto NOW = std::chrono::system_clock::now();
|
|
std::erase_if(m_mTokens, [this, &NOW](const auto& el) { return el.second->expiresAt < NOW; });
|
|
|
|
if (!m_mTokens.contains(uuid))
|
|
return {};
|
|
|
|
return m_mTokens.at(uuid);
|
|
}
|
|
|
|
void CTokenManager::removeToken(SP<CUUIDToken> token) {
|
|
if (!token)
|
|
return;
|
|
m_mTokens.erase(token->uuid);
|
|
} |