-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathscopedsocket.h
36 lines (30 loc) · 932 Bytes
/
scopedsocket.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021-2023 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of The Open Software License 3.0 (OSL-3.0).
See LICENSE for license details.
*/
#ifndef SCOPEDSOCKET_H
#define SCOPEDSOCKET_H
#include <fcntl.h>
#include <unistd.h>
#include "listener.h"
/**
* @brief The ScopedSocket struct allows for a bit of RAII and move semantics on a socket fd.
*/
class ScopedSocket
{
int socket = -1;
std::weak_ptr<Listener> listener;
public:
ScopedSocket() = default;
ScopedSocket(int socket, const std::shared_ptr<Listener> &listener);
ScopedSocket(const ScopedSocket &other) = delete;
ScopedSocket(ScopedSocket &&other);
~ScopedSocket();
int get() const;
ScopedSocket &operator=(ScopedSocket &&other);
std::shared_ptr<Listener> getListener() const;
};
#endif // SCOPEDSOCKET_H