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 "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>
class FileManager
@@ -19,7 +19,6 @@
* employee type - type of the employee logged in
* employee designation - designation if employee type is GENERAL.
*/
AuthenticationDTO ZenvyController::login(const std::string& email, const std::string& password)
{
return m_authenticationManagementService->login(email, password);
@@ -54,37 +53,37 @@ void ZenvyController::changePassword(const std::string& password)
}
//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);
}
bool ZenvyController::deactivateEmployee(const std::string& id)
bool ZenvyController::deactivateEmployee(const std::string& id) const
{
return m_employeeManagementService->deactivateEmployee(id);
}
void ZenvyController::updateProfile(const std::string& name, const std::string& phone)
{
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);
}
std::shared_ptr<const Employee> ZenvyController::getCurrentEmployee()
const Employee* ZenvyController::getCurrentEmployee() const
{
return m_employeeManagementService->getCurrentEmployee();
}
bool ZenvyController::updateDesignation(const std::string& id,Enums::EmployeeDesignation designation)
bool ZenvyController::updateDesignation(const std::string& id, Enums::EmployeeDesignation designation)
{
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();
}
@@ -114,7 +113,7 @@ void ZenvyController::persistStates()
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);
}
@@ -44,6 +44,7 @@ public:
m_talentAcquisitionManagementService(new TalentAcquisitionManagementService()),
m_teamManagementService(new TeamManagementService()),
m_ticketManagementService(new TicketManagementService()) {};
~ZenvyController();
//Authentication
AuthenticationDTO login(const std::string& email, const std::string& password);
@@ -51,16 +52,16 @@ public:
void changePassword(const std::string&);
//Employee Management
void createEmployee(Enums::EmployeeType, Enums::EmployeeDesignation, const std::string&, const std::string&, const std::string&);
bool deactivateEmployee(const std::string&);
std::shared_ptr<const Employee> getCurrentEmployee();
void updateProfile(const std::string&,const std::string&);
std::pair<Enums::EmployeeType, std::vector<std::shared_ptr<const Employee>>> searchEmployee(const std::string&);
bool updateDesignation(const std::string&,Enums::EmployeeDesignation);
std::vector<std::shared_ptr<Candidate>> getShorlistedCandidates();
void createEmployee(Enums::EmployeeType, Enums::EmployeeDesignation, const std::string&, const std::string&, const std::string&) const;
bool deactivateEmployee(const std::string&) const;
const Employee* getCurrentEmployee() const;
void updateProfile(const std::string&, const std::string&);
std::pair<Enums::EmployeeType, std::vector<const Employee*>> searchEmployee(const std::string&);
bool updateDesignation(const std::string&, Enums::EmployeeDesignation);
std::vector<Candidate*> getShorlistedCandidates() const;
template <typename ...Types>
Employees getEmployees(Types ...types)
Employees getEmployees(Types ...types) const
{
return m_employeeManagementService->getEmployees(types...);
}
@@ -68,7 +69,7 @@ public:
//Payslip management
void updateSalary(const std::string&, double, double, double, double, double);
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
void loadStates();
@@ -8,7 +8,7 @@
#include "DataStore.h"
#include "EmployeeManagementService.h"
/*
/*
* Function: getInstance
* Description: provides a singleton instance of the DataStore.
* Parameters:
@@ -84,6 +84,16 @@ payrollMap& DataStore::getPayrolls()
return m_payrolls;
}
payslipMap& DataStore::getPayslips()
{
return m_payslips;
}
candidateMap& DataStore::getCandidates()
{
return m_candidates;
}
DataStore::~DataStore()
{
for (auto& pair : m_employees)
@@ -91,19 +101,25 @@ DataStore::~DataStore()
delete pair.second;
}
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)
{
delete pair.second;
}
m_logs.clear();
if (m_authenticatedEmployee)
for (auto& pair : m_candidates)
{
delete m_authenticatedEmployee;
m_authenticatedEmployee = nullptr;
delete pair.second;
}
}
payslipMap& DataStore::getPayslips()
{
return m_payslips;
m_candidates.clear();
m_authenticatedEmployee = nullptr;
}
@@ -20,11 +20,10 @@ public:
* Args - constructor arguments forwarded to T's constructor
* Returns:
* std::shared_ptr<T> - a shared pointer managing the newly created object
*/
*/;
template<typename T, typename... 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_teamStatus(Enums::TeamStatus::NOT_IN_TEAM),
m_teamId(""),
m_payroll(nullptr),
m_employeeType(Enums::EmployeeType::GENERAL) {}
Employee(const std::string& name,
const std::string& phone,
@@ -7,7 +7,6 @@ File: JobListing.h
#pragma once
#include <string>
#include <map>
#include <memory>
#include "Candidate.h"
#include "Enums.h"
using candidateMap = std::map<std::string, Candidate*>;
@@ -4,6 +4,7 @@ File: Payroll.cpp
* Author : Trenser
* Created : 31-Mar-2026
*/
#include <sstream>
#include "Payroll.h"
#include "StringHelper.h"
#include "Factory.h"
@@ -6,7 +6,6 @@ File: Payroll.h
*/
#pragma once
#include <string>
#include <memory>
#include "Timestamp.h"
class Payroll
@@ -5,7 +5,6 @@ File: Payslip.h
* Created : 31-Mar-2026
*/
#pragma once
#include <memory>
#include <string>
#include "Timestamp.h"
@@ -7,7 +7,6 @@ File: Room.h
#pragma once
#include <string>
#include <map>
#include <memory>
#include "Booking.h"
using bookingMap = std::map<std::string, Booking*>;
@@ -7,7 +7,6 @@ File: Team.h
#pragma once
#include <string>
#include <map>
#include <memory>
#include "Employee.h"
using employeeMap = std::map<std::string, Employee*>;
@@ -24,8 +24,8 @@ void EmployeeManagementService::createEmployee(Enums::EmployeeType employeeType,
throw std::runtime_error("No authenticated user");
}
Enums::EmployeeType authenticatedEmployeeType = authenticatedEmployee->getEmployeeType();
Employee* employee;
Payroll* payroll;
Employee* employee = nullptr;
Payroll* payroll = nullptr;
if (employeeType != Enums::EmployeeType::GENERAL && util::hasActiveEmployeeOfType(employeeType, employees))
{
throw std::runtime_error("Cannot create more than one employee of type " + Enums::getEmployeeTypeString(employeeType));
@@ -7,7 +7,7 @@
#include "Enums.h"
#include "StringHelper.h"
using Employees = std::vector<std::shared_ptr<const Employee>>;
using Employees = std::vector<const Employee*>;
class EmployeeManagementService
{
@@ -17,11 +17,11 @@ public:
EmployeeManagementService() : m_dataStore(DataStore::getInstance()) {};
void createEmployee(Enums::EmployeeType, Enums::EmployeeDesignation, const std::string&, const std::string&, const std::string&);
bool deactivateEmployee(const std::string&);
bool updateDesignation(const std::string&,Enums::EmployeeDesignation);
void updateProfile(const std::string&,const std::string&);
std::pair<Enums::EmployeeType, std::vector<std::shared_ptr<const Employee>>> searchEmployee(const std::string&);
std::shared_ptr<const Employee> getCurrentEmployee();
std::vector<std::shared_ptr<Candidate>> getShorlistedCandidates();
bool updateDesignation(const std::string&, Enums::EmployeeDesignation);
void updateProfile(const std::string&, const std::string&);
std::pair<Enums::EmployeeType, std::vector<const Employee*>> searchEmployee(const std::string&);
const Employee* getCurrentEmployee();
std::vector<Candidate*> getShorlistedCandidates();
void loadEmployees();
void saveEmployees();
@@ -52,8 +52,8 @@ public:
std::sort(
filteredEmployees.begin(),
filteredEmployees.end(),
[](const std::shared_ptr<const Employee>& employeeOne,
const std::shared_ptr<const Employee>& employeeTwo)
[](const Employee* employeeOne,
const Employee* employeeTwo)
{
return util::extractNumber(employeeOne->getId()) <
util::extractNumber(employeeTwo->getId());
@@ -112,7 +112,7 @@ bool util::isPasswordValid(const std::string& password)
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)
{
@@ -125,7 +125,7 @@ bool util::hasActiveEmployeeOfType(Enums::EmployeeType employeeType, const std::
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)
{
@@ -138,7 +138,7 @@ bool util::isEmailDuplicate(const std::string& email, const std::map<std::string
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)
{
@@ -151,7 +151,7 @@ bool util::isPhoneDuplicate(const std::string& phone, const std::map<std::string
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)
{
@@ -4,7 +4,6 @@
* Author: Trenser
* Created: 01-Apr-2026
*/
#pragma once
#include<string>
#include<map>
@@ -21,8 +20,8 @@ namespace util
bool isPhoneNumberValid(const std::string&);
bool isEmailValid(const std::string&);
bool isPasswordValid(const std::string&);
bool hasActiveEmployeeOfType(Enums::EmployeeType, const std::map<std::string, std::shared_ptr<Employee>>&);
bool isEmailDuplicate(const std::string&, const std::map<std::string, std::shared_ptr<Employee>>&);
bool isPhoneDuplicate(const std::string&, const std::map<std::string, std::shared_ptr<Employee>>&);
bool isPhoneDuplicate(const std::string&, const std::vector<std::shared_ptr<const Employee>>&);
bool hasActiveEmployeeOfType(Enums::EmployeeType, const std::map<std::string, Employee*>&);
bool isEmailDuplicate(const std::string&, const std::map<std::string, Employee*>&);
bool isPhoneDuplicate(const std::string&, const std::map<std::string, Employee*>&);
bool isPhoneDuplicate(const std::string&, const std::vector<const Employee*>&);
}
@@ -1,6 +1,5 @@
#pragma once
#include <map>
#include <memory>
#include <vector>
#include <string>
#include <iostream>
@@ -22,7 +21,7 @@ void addShortlistedCandidateAsEmployee(const ZenvyController* m_zenvyController)
inline void viewPayslipHistory(ZenvyController* m_zenvyController)
{
auto employeePayslips = m_zenvyController->getCurrentEmployee()->getEmployeePayslips();
auto& employeePayslips = m_zenvyController->getCurrentEmployee()->getEmployeePayslips();
util::clear();
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;
std::map<int, const Employee*> employeeList;
@@ -353,7 +352,7 @@ inline void viewPayslip(ZenvyController* m_zenvyController)
if (payroll && payslip)
{
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 << "House Rent Allowance : " << payroll->getHouseRentAllowance() << "\n";
std::cout << "Food Allowance : " << payroll->getFoodAllowance() << "\n";