]> git.sesse.net Git - cubemap/blobdiff - server.cpp
Use unique_ptr a few places instead of explicit delete.
[cubemap] / server.cpp
index 5026deb7ecb6fd911c16888e070fa33e31482a4b..0b4d076cd9f3fd36ac71fea0508e2e58d6d478cb 100644 (file)
@@ -69,10 +69,6 @@ Server::Server()
 
 Server::~Server()
 {
-       for (Stream *stream : streams) {
-               delete stream;
-       }
-
        safe_close(epoll_fd);
 }
 
@@ -122,7 +118,7 @@ void Server::do_work()
 
                // Process each client where its stream has new data,
                // even if there was no socket activity.
-               for (Stream *stream : streams) {
+               for (unique_ptr<Stream> &stream : streams) {
                        vector<Client *> to_process;
                        swap(stream->to_process, to_process);
                        for (Client *client : to_process) {
@@ -195,7 +191,7 @@ CubemapStateProto Server::serialize()
        for (const auto &fd_and_client : clients) {
                serialized.add_clients()->MergeFrom(fd_and_client.second.serialize());
        }
-       for (Stream *stream : streams) {
+       for (unique_ptr<Stream> &stream : streams) {
                serialized.add_streams()->MergeFrom(stream->serialize());
        }
        return serialized;
@@ -265,7 +261,7 @@ void Server::add_client_from_serialized(const ClientProto &client)
                assert(client.state() != Client::SENDING_DATA);
                stream = NULL;
        } else {
-               stream = streams[stream_index];
+               stream = streams[stream_index].get();
        }
        auto inserted = clients.insert(make_pair(client.sock(), Client(client, stream)));
        assert(inserted.second == true);  // Should not already exist.
@@ -319,7 +315,7 @@ int Server::add_stream(const string &url, size_t backlog_size, size_t prebufferi
 {
        MutexLock lock(&mutex);
        stream_url_map.insert(make_pair(url, streams.size()));
-       streams.push_back(new Stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding));
+       streams.emplace_back(new Stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding));
        return streams.size() - 1;
 }
 
@@ -327,7 +323,7 @@ int Server::add_stream_from_serialized(const StreamProto &stream, int data_fd)
 {
        MutexLock lock(&mutex);
        stream_url_map.insert(make_pair(stream.url(), streams.size()));
-       streams.push_back(new Stream(stream, data_fd));
+       streams.emplace_back(new Stream(stream, data_fd));
        return streams.size() - 1;
 }
        
@@ -852,7 +848,7 @@ int Server::parse_request(Client *client)
                }
        }
 
-       Stream *stream = streams[stream_url_map_it->second];
+       Stream *stream = streams[stream_url_map_it->second].get();
        if (stream->http_header.empty()) {
                return 503;  // Service unavailable.
        }
@@ -990,7 +986,7 @@ void Server::process_queued_data()
                queued_add_clients.clear();
        }
 
-       for (Stream *stream : streams) {
+       for (unique_ptr<Stream> &stream : streams) {
                stream->process_queued_data();
        }
 }