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

22
room.h
View File

@@ -16,7 +16,7 @@ namespace Yc {
class Server;
class Room: public Base {
class Room: public Base, public std::enable_shared_from_this<Room> {
public:
enum RoomType {
none = 0,
@@ -26,15 +26,15 @@ namespace Yc {
password = 8
};
Room(Server *parent, Json::Value roomParams);
Room(std::shared_ptr<Server> parent, Json::Value roomParams);
~Room();
void run();
std::string name();
bool addUser(std::string userName, std::string color, std::string password, int socket);
bool addUser(User *user, std::string password);
bool addUser(std::shared_ptr<User> user, std::string password);
bool userNameExists(std::string userName);
void removeUser(std::string _token, bool silent = false);
void removeUser(User *user, bool silent = false);
void removeUser(std::shared_ptr<User> user, bool silent = false);
void setStop();
void addMessage(User::MsgType type, const char* messageText, std::string userName = "", std::string color = "");
void addMessage(User::MsgType type, std::string messageText, std::string userName = "", std::string color = "");
@@ -42,11 +42,11 @@ namespace Yc {
RoomType type();
bool isType(RoomType type);
bool canDice();
unsigned int addDice(User *user, int diceValue);
unsigned int addDice(std::shared_ptr<User> user, int diceValue);
bool accessAllowed(std::string userName, std::string password);
bool userIsInRoom(std::string userName);
void addUserWhenQueueEmpty(User *user);
bool userToNewRoom(User *user, std::string newRoom, std::string password);
void addUserWhenQueueEmpty(std::shared_ptr<User> user);
bool userToNewRoom(std::shared_ptr<User> user, std::string newRoom, std::string password);
unsigned int flags();
Json::Value userList();
private:
@@ -57,21 +57,21 @@ namespace Yc {
std::string color;
};
Server *_parent;
std::shared_ptr<Server> _parent;
std::string _name;
std::string _password;
std::vector<std::string> _allowedUsers;
RoomType _type;
std::vector<User*> _users;
std::vector<std::shared_ptr<User>> _users;
bool _blocked;
bool _stop;
std::queue<Message> _msgQueue;
std::thread *thread;
std::unique_ptr<std::thread> thread;
bool _roundRunning;
time_t _roundStart;
time_t _lastRoundEnd;
int _roundLength;
std::vector<std::pair<User *, int>> _diceValues;
std::vector<std::pair<std::shared_ptr<User>, int>> _diceValues;
void _handleDice();
void _startDiceRound();
void _endDiceRound();