refactor: change memory management and fix related code

- Switched shared_ptr to raw pointers
- Added cleanup logic in DataStore
- Fixed Factory object creation
- Updated function signatures to match changes
- Small refactors and formatting fixes
This commit is contained in:
2026-04-17 09:33:37 +05:30
parent 703cea447e
commit 133785dd3f
18 changed files with 154 additions and 144 deletions
@@ -2,7 +2,7 @@
#include <stdexcept> #include <stdexcept>
#include "FileIO.h" #include "FileIO.h"
template <typename T> using objects = std::map<std::string, std::shared_ptr<T>>; template <typename T> using objects = std::map<std::string, T*>;
template <typename T> template <typename T>
class FileManager class FileManager
@@ -19,7 +19,6 @@
* employee type - type of the employee logged in * employee type - type of the employee logged in
* employee designation - designation if employee type is GENERAL. * employee designation - designation if employee type is GENERAL.
*/ */
AuthenticationDTO ZenvyController::login(const std::string& email, const std::string& password) AuthenticationDTO ZenvyController::login(const std::string& email, const std::string& password)
{ {
return m_authenticationManagementService->login(email, password); return m_authenticationManagementService->login(email, password);
@@ -54,12 +53,12 @@ void ZenvyController::changePassword(const std::string& password)
} }
//Employee Management //Employee Management
void ZenvyController::createEmployee(Enums::EmployeeType employeeType, Enums::EmployeeDesignation employeeDesignation, const std::string& email, const std::string& name, const std::string& phone) void ZenvyController::createEmployee(Enums::EmployeeType employeeType, Enums::EmployeeDesignation employeeDesignation, const std::string& email, const std::string& name, const std::string& phone) const
{ {
m_employeeManagementService->createEmployee(employeeType, employeeDesignation, email, name, phone); m_employeeManagementService->createEmployee(employeeType, employeeDesignation, email, name, phone);
} }
bool ZenvyController::deactivateEmployee(const std::string& id) bool ZenvyController::deactivateEmployee(const std::string& id) const
{ {
return m_employeeManagementService->deactivateEmployee(id); return m_employeeManagementService->deactivateEmployee(id);
} }
@@ -69,12 +68,12 @@ void ZenvyController::updateProfile(const std::string& name, const std::string&
m_employeeManagementService->updateProfile(name, phone); m_employeeManagementService->updateProfile(name, phone);
} }
std::pair<Enums::EmployeeType, std::vector<std::shared_ptr<const Employee>>> ZenvyController::searchEmployee(const std::string& name) std::pair<Enums::EmployeeType, std::vector<const Employee*>> ZenvyController::searchEmployee(const std::string& name)
{ {
return m_employeeManagementService->searchEmployee(name); return m_employeeManagementService->searchEmployee(name);
} }
std::shared_ptr<const Employee> ZenvyController::getCurrentEmployee() const Employee* ZenvyController::getCurrentEmployee() const
{ {
return m_employeeManagementService->getCurrentEmployee(); return m_employeeManagementService->getCurrentEmployee();
} }
@@ -84,7 +83,7 @@ bool ZenvyController::updateDesignation(const std::string& id,Enums::EmployeeDes
return m_employeeManagementService->updateDesignation(id, designation); return m_employeeManagementService->updateDesignation(id, designation);
} }
std::vector<std::shared_ptr<Candidate>> ZenvyController::getShorlistedCandidates() std::vector<Candidate*> ZenvyController::getShorlistedCandidates() const
{ {
return m_employeeManagementService->getShorlistedCandidates(); return m_employeeManagementService->getShorlistedCandidates();
} }
@@ -114,7 +113,7 @@ void ZenvyController::persistStates()
m_payslipManagementService->savePayslips(); m_payslipManagementService->savePayslips();
} }
std::pair<std::shared_ptr<Payroll>, std::shared_ptr<Payslip>> ZenvyController::getPayslipForMonth(const std::string& employeeId, int year, int month) std::pair<Payroll*, Payslip*> ZenvyController::getPayslipForMonth(const std::string& employeeId, int year, int month)
{ {
return m_payslipManagementService->getPayslipForMonth(employeeId, year, month); return m_payslipManagementService->getPayslipForMonth(employeeId, year, month);
} }
@@ -44,6 +44,7 @@ public:
m_talentAcquisitionManagementService(new TalentAcquisitionManagementService()), m_talentAcquisitionManagementService(new TalentAcquisitionManagementService()),
m_teamManagementService(new TeamManagementService()), m_teamManagementService(new TeamManagementService()),
m_ticketManagementService(new TicketManagementService()) {}; m_ticketManagementService(new TicketManagementService()) {};
~ZenvyController();
//Authentication //Authentication
AuthenticationDTO login(const std::string& email, const std::string& password); AuthenticationDTO login(const std::string& email, const std::string& password);
@@ -51,16 +52,16 @@ public:
void changePassword(const std::string&); void changePassword(const std::string&);
//Employee Management //Employee Management
void createEmployee(Enums::EmployeeType, Enums::EmployeeDesignation, const std::string&, const std::string&, const std::string&); void createEmployee(Enums::EmployeeType, Enums::EmployeeDesignation, const std::string&, const std::string&, const std::string&) const;
bool deactivateEmployee(const std::string&); bool deactivateEmployee(const std::string&) const;
std::shared_ptr<const Employee> getCurrentEmployee(); const Employee* getCurrentEmployee() const;
void updateProfile(const std::string&, const std::string&); void updateProfile(const std::string&, const std::string&);
std::pair<Enums::EmployeeType, std::vector<std::shared_ptr<const Employee>>> searchEmployee(const std::string&); std::pair<Enums::EmployeeType, std::vector<const Employee*>> searchEmployee(const std::string&);
bool updateDesignation(const std::string&, Enums::EmployeeDesignation); bool updateDesignation(const std::string&, Enums::EmployeeDesignation);
std::vector<std::shared_ptr<Candidate>> getShorlistedCandidates(); std::vector<Candidate*> getShorlistedCandidates() const;
template <typename ...Types> template <typename ...Types>
Employees getEmployees(Types ...types) Employees getEmployees(Types ...types) const
{ {
return m_employeeManagementService->getEmployees(types...); return m_employeeManagementService->getEmployees(types...);
} }
@@ -68,7 +69,7 @@ public:
//Payslip management //Payslip management
void updateSalary(const std::string&, double, double, double, double, double); void updateSalary(const std::string&, double, double, double, double, double);
void generatePayslips(); void generatePayslips();
std::pair<std::shared_ptr<Payroll>, std::shared_ptr<Payslip>>getPayslipForMonth(const std::string&, int, int); std::pair<Payroll*, Payslip*>getPayslipForMonth(const std::string&, int, int);
//File Management //File Management
void loadStates(); void loadStates();
@@ -84,6 +84,16 @@ payrollMap& DataStore::getPayrolls()
return m_payrolls; return m_payrolls;
} }
payslipMap& DataStore::getPayslips()
{
return m_payslips;
}
candidateMap& DataStore::getCandidates()
{
return m_candidates;
}
DataStore::~DataStore() DataStore::~DataStore()
{ {
for (auto& pair : m_employees) for (auto& pair : m_employees)
@@ -91,19 +101,25 @@ DataStore::~DataStore()
delete pair.second; delete pair.second;
} }
m_employees.clear(); m_employees.clear();
for (auto& pair : m_payrolls)
{
delete pair.second;
}
m_payrolls.clear();
for (auto& pair : m_payslips)
{
delete pair.second;
}
m_payslips.clear();
for (auto& pair : m_logs) for (auto& pair : m_logs)
{ {
delete pair.second; delete pair.second;
} }
m_logs.clear(); m_logs.clear();
if (m_authenticatedEmployee) for (auto& pair : m_candidates)
{ {
delete m_authenticatedEmployee; delete pair.second;
}
m_candidates.clear();
m_authenticatedEmployee = nullptr; m_authenticatedEmployee = nullptr;
} }
}
payslipMap& DataStore::getPayslips()
{
return m_payslips;
}
@@ -20,11 +20,10 @@ public:
* Args - constructor arguments forwarded to T's constructor * Args - constructor arguments forwarded to T's constructor
* Returns: * Returns:
* std::shared_ptr<T> - a shared pointer managing the newly created object * std::shared_ptr<T> - a shared pointer managing the newly created object
*/ */;
template<typename T, typename... Args> template<typename T, typename... Args>
static T* getObject(Args&&... args) static T* getObject(Args&&... args)
{ {
return T*(std::forward<Args>(args)...); return new T(std::forward<Args>(args)...);
} }
}; };
@@ -44,6 +44,7 @@ public:
m_accountStatus(Enums::AccountStatus::ACTIVE), m_accountStatus(Enums::AccountStatus::ACTIVE),
m_teamStatus(Enums::TeamStatus::NOT_IN_TEAM), m_teamStatus(Enums::TeamStatus::NOT_IN_TEAM),
m_teamId(""), m_teamId(""),
m_payroll(nullptr),
m_employeeType(Enums::EmployeeType::GENERAL) {} m_employeeType(Enums::EmployeeType::GENERAL) {}
Employee(const std::string& name, Employee(const std::string& name,
const std::string& phone, const std::string& phone,
@@ -7,7 +7,6 @@ File: JobListing.h
#pragma once #pragma once
#include <string> #include <string>
#include <map> #include <map>
#include <memory>
#include "Candidate.h" #include "Candidate.h"
#include "Enums.h" #include "Enums.h"
using candidateMap = std::map<std::string, Candidate*>; using candidateMap = std::map<std::string, Candidate*>;
@@ -4,6 +4,7 @@ File: Payroll.cpp
* Author : Trenser * Author : Trenser
* Created : 31-Mar-2026 * Created : 31-Mar-2026
*/ */
#include <sstream>
#include "Payroll.h" #include "Payroll.h"
#include "StringHelper.h" #include "StringHelper.h"
#include "Factory.h" #include "Factory.h"
@@ -6,7 +6,6 @@ File: Payroll.h
*/ */
#pragma once #pragma once
#include <string> #include <string>
#include <memory>
#include "Timestamp.h" #include "Timestamp.h"
class Payroll class Payroll
@@ -5,7 +5,6 @@ File: Payslip.h
* Created : 31-Mar-2026 * Created : 31-Mar-2026
*/ */
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include "Timestamp.h" #include "Timestamp.h"
@@ -7,7 +7,6 @@ File: Room.h
#pragma once #pragma once
#include <string> #include <string>
#include <map> #include <map>
#include <memory>
#include "Booking.h" #include "Booking.h"
using bookingMap = std::map<std::string, Booking*>; using bookingMap = std::map<std::string, Booking*>;
@@ -7,7 +7,6 @@ File: Team.h
#pragma once #pragma once
#include <string> #include <string>
#include <map> #include <map>
#include <memory>
#include "Employee.h" #include "Employee.h"
using employeeMap = std::map<std::string, Employee*>; using employeeMap = std::map<std::string, Employee*>;
@@ -24,8 +24,8 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
throw std::runtime_error("No authenticated user"); throw std::runtime_error("No authenticated user");
} }
Enums::EmployeeType authenticatedEmployeeType = authenticatedEmployee->getEmployeeType(); Enums::EmployeeType authenticatedEmployeeType = authenticatedEmployee->getEmployeeType();
Employee* employee; Employee* employee = nullptr;
Payroll* payroll; Payroll* payroll = nullptr;
if (employeeType != Enums::EmployeeType::GENERAL && util::hasActiveEmployeeOfType(employeeType, employees)) if (employeeType != Enums::EmployeeType::GENERAL && util::hasActiveEmployeeOfType(employeeType, employees))
{ {
throw std::runtime_error("Cannot create more than one employee of type " + Enums::getEmployeeTypeString(employeeType)); throw std::runtime_error("Cannot create more than one employee of type " + Enums::getEmployeeTypeString(employeeType));
@@ -7,7 +7,7 @@
#include "Enums.h" #include "Enums.h"
#include "StringHelper.h" #include "StringHelper.h"
using Employees = std::vector<std::shared_ptr<const Employee>>; using Employees = std::vector<const Employee*>;
class EmployeeManagementService class EmployeeManagementService
{ {
@@ -19,9 +19,9 @@ public:
bool deactivateEmployee(const std::string&); bool deactivateEmployee(const std::string&);
bool updateDesignation(const std::string&, Enums::EmployeeDesignation); bool updateDesignation(const std::string&, Enums::EmployeeDesignation);
void updateProfile(const std::string&, const std::string&); void updateProfile(const std::string&, const std::string&);
std::pair<Enums::EmployeeType, std::vector<std::shared_ptr<const Employee>>> searchEmployee(const std::string&); std::pair<Enums::EmployeeType, std::vector<const Employee*>> searchEmployee(const std::string&);
std::shared_ptr<const Employee> getCurrentEmployee(); const Employee* getCurrentEmployee();
std::vector<std::shared_ptr<Candidate>> getShorlistedCandidates(); std::vector<Candidate*> getShorlistedCandidates();
void loadEmployees(); void loadEmployees();
void saveEmployees(); void saveEmployees();
@@ -52,8 +52,8 @@ public:
std::sort( std::sort(
filteredEmployees.begin(), filteredEmployees.begin(),
filteredEmployees.end(), filteredEmployees.end(),
[](const std::shared_ptr<const Employee>& employeeOne, [](const Employee* employeeOne,
const std::shared_ptr<const Employee>& employeeTwo) const Employee* employeeTwo)
{ {
return util::extractNumber(employeeOne->getId()) < return util::extractNumber(employeeOne->getId()) <
util::extractNumber(employeeTwo->getId()); util::extractNumber(employeeTwo->getId());
@@ -112,7 +112,7 @@ bool util::isPasswordValid(const std::string& password)
return hasUpper && hasLower && hasDigit && hasSpecial; return hasUpper && hasLower && hasDigit && hasSpecial;
} }
bool util::hasActiveEmployeeOfType(Enums::EmployeeType employeeType, const std::map<std::string, std::shared_ptr<Employee>> & employees) bool util::hasActiveEmployeeOfType(Enums::EmployeeType employeeType, const std::map<std::string, Employee*> & employees)
{ {
for (const auto& employeePair : employees) for (const auto& employeePair : employees)
{ {
@@ -125,7 +125,7 @@ bool util::hasActiveEmployeeOfType(Enums::EmployeeType employeeType, const std::
return false; return false;
} }
bool util::isEmailDuplicate(const std::string& email, const std::map<std::string, std::shared_ptr<Employee>>& employees) bool util::isEmailDuplicate(const std::string& email, const std::map<std::string, Employee*>& employees)
{ {
for (const auto& employeePair : employees) for (const auto& employeePair : employees)
{ {
@@ -138,7 +138,7 @@ bool util::isEmailDuplicate(const std::string& email, const std::map<std::string
return false; return false;
} }
bool util::isPhoneDuplicate(const std::string& phone, const std::map<std::string, std::shared_ptr<Employee>>& employees) bool util::isPhoneDuplicate(const std::string& phone, const std::map<std::string, Employee*>& employees)
{ {
for (const auto& employeePair : employees) for (const auto& employeePair : employees)
{ {
@@ -151,7 +151,7 @@ bool util::isPhoneDuplicate(const std::string& phone, const std::map<std::string
return false; return false;
} }
bool util::isPhoneDuplicate(const std::string& phone, const std::vector<std::shared_ptr<const Employee>>& employees) bool util::isPhoneDuplicate(const std::string& phone, const std::vector<const Employee*>& employees)
{ {
for (const auto& employee : employees) for (const auto& employee : employees)
{ {
@@ -4,7 +4,6 @@
* Author: Trenser * Author: Trenser
* Created: 01-Apr-2026 * Created: 01-Apr-2026
*/ */
#pragma once #pragma once
#include<string> #include<string>
#include<map> #include<map>
@@ -21,8 +20,8 @@ namespace util
bool isPhoneNumberValid(const std::string&); bool isPhoneNumberValid(const std::string&);
bool isEmailValid(const std::string&); bool isEmailValid(const std::string&);
bool isPasswordValid(const std::string&); bool isPasswordValid(const std::string&);
bool hasActiveEmployeeOfType(Enums::EmployeeType, const std::map<std::string, std::shared_ptr<Employee>>&); bool hasActiveEmployeeOfType(Enums::EmployeeType, const std::map<std::string, Employee*>&);
bool isEmailDuplicate(const std::string&, const std::map<std::string, std::shared_ptr<Employee>>&); bool isEmailDuplicate(const std::string&, const std::map<std::string, Employee*>&);
bool isPhoneDuplicate(const std::string&, const std::map<std::string, std::shared_ptr<Employee>>&); bool isPhoneDuplicate(const std::string&, const std::map<std::string, Employee*>&);
bool isPhoneDuplicate(const std::string&, const std::vector<std::shared_ptr<const Employee>>&); bool isPhoneDuplicate(const std::string&, const std::vector<const Employee*>&);
} }
@@ -1,6 +1,5 @@
#pragma once #pragma once
#include <map> #include <map>
#include <memory>
#include <vector> #include <vector>
#include <string> #include <string>
#include <iostream> #include <iostream>
@@ -22,7 +21,7 @@ void addShortlistedCandidateAsEmployee(const ZenvyController* m_zenvyController)
inline void viewPayslipHistory(ZenvyController* m_zenvyController) inline void viewPayslipHistory(ZenvyController* m_zenvyController)
{ {
auto employeePayslips = m_zenvyController->getCurrentEmployee()->getEmployeePayslips(); auto& employeePayslips = m_zenvyController->getCurrentEmployee()->getEmployeePayslips();
util::clear(); util::clear();
if (employeePayslips.empty()) if (employeePayslips.empty())
{ {
@@ -147,7 +146,7 @@ inline void updateProfile(ZenvyController* m_zenvyController)
} }
} }
inline std::string selectEmployeeId(std::vector<const Employee*>& allEmployees) inline std::string selectEmployeeId(const std::vector<const Employee*>& allEmployees)
{ {
int choice; int choice;
std::map<int, const Employee*> employeeList; std::map<int, const Employee*> employeeList;
@@ -353,7 +352,7 @@ inline void viewPayslip(ZenvyController* m_zenvyController)
if (payroll && payslip) if (payroll && payslip)
{ {
util::clear(); util::clear();
std::cout << "Payslip for " << employee->getEmployeeName() << " (" << year << "-" << std::setw(2) << std::setfill('0') << month << ")\n\n"; std::cout << "Payslip for " << employee->getEmployeeName() << " (" << year << "-" << std::setw(2) << month << ")\n\n";
std::cout << "Basic Salary : " << payroll->getBasicSalary() << "\n"; std::cout << "Basic Salary : " << payroll->getBasicSalary() << "\n";
std::cout << "House Rent Allowance : " << payroll->getHouseRentAllowance() << "\n"; std::cout << "House Rent Allowance : " << payroll->getHouseRentAllowance() << "\n";
std::cout << "Food Allowance : " << payroll->getFoodAllowance() << "\n"; std::cout << "Food Allowance : " << payroll->getFoodAllowance() << "\n";