Update CMake configuration and refactor code to use smart pointers for memory management
This commit is contained in:
22
user.cpp
22
user.cpp
@@ -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", "", "");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user