Update CMake configuration and refactor code to use smart pointers for memory management

This commit is contained in:
Torsten Schulz
2025-08-11 11:15:54 +02:00
parent ba6b788075
commit f44d780537
10 changed files with 110 additions and 77 deletions

View File

@@ -14,15 +14,15 @@
namespace Yc {
namespace Lib {
User::User(Room *parent, std::string name, std::string color, int socket) :
_parent(parent),
_name(name),
_color(color),
User::User(std::shared_ptr<Room> parent, std::string name, std::string color, int socket) :
_parent(std::move(parent)),
_name(std::move(name)),
_color(std::move(color)),
_socket(socket),
_stop(false) {
_token = Yc::Lib::Tools::generateRandomString(32);
sendMsg(token, _token, "", "");
thread = new std::thread(&User::checkerTask, this);
thread = std::make_unique<std::thread>(&User::checkerTask, this);
}
User::~User() {
@@ -37,8 +37,8 @@ namespace Yc {
return (token == _token);
}
bool User::isUser(User *toValidate) {
return (toValidate == this);
bool User::isUser(std::shared_ptr<User> toValidate) {
return (toValidate.get() == this);
}
void User::sendMsg(MsgType type, const char *message, std::string userName, std::string color) {
@@ -94,8 +94,8 @@ namespace Yc {
return _color;
}
void User::setParent(Room *parent) {
_parent = parent;
void User::setParent(std::shared_ptr<Room> parent) {
_parent = std::move(parent);
}
void User::send(std::string out) {
@@ -151,7 +151,7 @@ namespace Yc {
}
void User::doDice() {
switch (_parent->addDice(this, (rand() % 6) + 1)) {
switch (_parent->addDice(shared_from_this(), (rand() % 6) + 1)) {
case 1:
sendMsg(system, "dice_not_possible", "", "");
break;
@@ -164,7 +164,7 @@ namespace Yc {
}
void User::changeRoom(std::string newRoom, std::string password) {
if (!_parent->userToNewRoom(this, newRoom, password)) {
if (!_parent->userToNewRoom(shared_from_this(), newRoom, password)) {
sendMsg(User::system, "room_not_possible", "", "");
}
}