rcpp_framework/modules/users/user_manager.cpp

79 lines
1.4 KiB
C++
Raw Normal View History

2021-08-04 19:03:53 +02:00
#include "user_manager.h"
#include "core/http/http_session.h"
#include "core/http/session_manager.h"
2021-08-04 19:05:38 +02:00
#include "user.h"
#include <stdlib.h>
2021-08-04 19:05:38 +02:00
2021-08-04 19:03:53 +02:00
void UserManager::add_user(User *user) {
if (!user) {
printf("UserManager::add_user: ERROR, user is null!\n");
return;
}
std::lock_guard<std::mutex> lock(_mutex);
_users_vec.push_back(user);
_users[user->name] = user;
2021-08-04 19:03:53 +02:00
}
2021-08-04 19:03:53 +02:00
void UserManager::remove_user(User *user) {
if (!user) {
printf("UserManager::remove_user: ERROR, user is null!\n");
return;
}
std::lock_guard<std::mutex> lock(_mutex);
_users.erase(user->name);
2021-08-04 19:03:53 +02:00
for (int i = 0; i < _users_vec.size(); ++i) {
if (_users_vec[i] == user) {
_users_vec[i] = _users_vec[_users_vec.size() - 1];
_users_vec.pop_back();
break;
2021-08-04 19:03:53 +02:00
}
}
}
User *UserManager::get_user(const std::string &user_name) {
return _users[user_name];
}
void UserManager::clear() {
SessionManager *sm = SessionManager::get_singleton();
2021-08-04 19:03:53 +02:00
std::lock_guard<std::mutex> lock(_mutex);
2021-08-04 19:03:53 +02:00
for (int i = 0; i < _users_vec.size(); ++i) {
delete _users_vec[i];
2021-08-04 19:03:53 +02:00
}
_users.clear();
_users_vec.clear();
2021-08-04 19:03:53 +02:00
}
UserManager *UserManager::get_singleton() {
return _self;
}
UserManager::UserManager() :
Object() {
if (_self) {
printf("UserManager::UserManager(): Error! self is not null!/n");
}
_self = this;
}
UserManager::~UserManager() {
clear();
if (_self == this) {
_self = nullptr;
}
2021-08-04 19:03:53 +02:00
}
UserManager *UserManager::_self = nullptr;