Skip to content

Commit 54559ef

Browse files
committed
Use more managed pointers
1 parent f53417f commit 54559ef

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

src/Client.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Client::~Client() {
2929
* Add to Send Queue
3030
* Adds a SendQueueItem object to the end of this client's send queue
3131
*/
32-
void Client::addToSendQueue(SendQueueItem* item) {
32+
void Client::addToSendQueue(std::shared_ptr<SendQueueItem> item) {
3333
sendQueue.push(item);
3434
}
3535

@@ -49,7 +49,7 @@ uint32_t Client::sendQueueSize() const {
4949
*
5050
* @return SendQueueItem object containing the data to send and current offset
5151
*/
52-
SendQueueItem* Client::nextInSendQueue() {
52+
std::shared_ptr<SendQueueItem> Client::nextInSendQueue() {
5353
if (sendQueue.empty())
5454
return nullptr;
5555

@@ -61,10 +61,10 @@ SendQueueItem* Client::nextInSendQueue() {
6161
* Deletes and dequeues first item in the queue
6262
*/
6363
void Client::dequeueFromSendQueue() {
64-
SendQueueItem* item = nextInSendQueue();
64+
std::shared_ptr<SendQueueItem> item = nextInSendQueue();
6565
if (item != nullptr) {
66+
item.reset();
6667
sendQueue.pop();
67-
delete item;
6868
}
6969
}
7070

@@ -74,7 +74,7 @@ void Client::dequeueFromSendQueue() {
7474
*/
7575
void Client::clearSendQueue() {
7676
while (!sendQueue.empty()) {
77-
delete sendQueue.front();
77+
sendQueue.front().reset();
7878
sendQueue.pop();
7979
}
8080
}

src/Client.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include "SendQueueItem.h"
2323

24+
#include <memory>
25+
2426
#include <netinet/in.h>
2527
#include <arpa/inet.h>
2628
#include <queue>
@@ -29,7 +31,7 @@ class Client {
2931
int32_t socketDesc; // Socket Descriptor
3032
sockaddr_in clientAddr;
3133

32-
std::queue<SendQueueItem*> sendQueue;
34+
std::queue<std::shared_ptr<SendQueueItem>> sendQueue;
3335

3436
public:
3537
Client(int32_t fd, sockaddr_in addr);
@@ -50,9 +52,9 @@ class Client {
5052
return inet_ntoa(clientAddr.sin_addr);
5153
}
5254

53-
void addToSendQueue(SendQueueItem* item);
55+
void addToSendQueue(std::shared_ptr<SendQueueItem> item);
5456
uint32_t sendQueueSize() const;
55-
SendQueueItem* nextInSendQueue();
57+
std::shared_ptr<SendQueueItem> nextInSendQueue();
5658
void dequeueFromSendQueue();
5759
void clearSendQueue();
5860
};

src/HTTPServer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <memory>
2626
#include <print>
2727
#include <vector>
28+
#include <utility>
2829

2930
#include <sys/types.h>
3031
#include <sys/socket.h>
@@ -629,7 +630,7 @@ void HTTPServer::sendResponse(std::shared_ptr<Client> cl, std::unique_ptr<HTTPRe
629630

630631
// Get raw data by creating the response (we are responsible for cleaning it up in process())
631632
// Add data to the Client's send queue
632-
cl->addToSendQueue(new SendQueueItem(resp->create(), resp->size(), disconnect));
633+
cl->addToSendQueue(std::make_shared<SendQueueItem>(resp->create(), resp->size(), disconnect));
633634
}
634635

635636
/**
@@ -657,7 +658,6 @@ std::shared_ptr<ResourceHost> HTTPServer::getResourceHostForRequest(const std::s
657658
return it->second;
658659
} else {
659660
// Temporary: HTTP/1.0 are given the first ResouceHost in the hostList
660-
// TODO: Allow admin to specify a 'default resource host'
661661
if (!hostList.empty())
662662
return hostList[0];
663663
}

src/Resource.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class Resource {
3333
public:
3434
explicit Resource(std::string const& loc, bool dir = false);
3535
~Resource();
36+
Resource& operator=(Resource const&) = delete; // Copy assignment
37+
Resource(Resource &&) = delete; // Move
38+
Resource& operator=(Resource &&) = delete; // Move assignment
3639

3740
// Setters
3841

0 commit comments

Comments
 (0)