Audacity 3.2.0
socket_guard.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file socket_guard.h
6
7 @author Vitaly Sverchinsky
8
9 Part of lib-ipc library
10
11**********************************************************************/
12
13#pragma once
14
15#include <cassert>
16#include "ipc-types.h"
17
24{
26public:
27 explicit socket_guard(const socket_guard&) = delete;
29
30 explicit socket_guard(SOCKET sock = INVALID_SOCKET) noexcept : mSocket(sock) { }
31
32 explicit socket_guard(socket_guard&& rhs) noexcept
33 : mSocket(rhs.mSocket)
34 {
35 rhs.mSocket = INVALID_SOCKET;
36 }
37
38 explicit operator bool() const noexcept
39 {
40 return mSocket != INVALID_SOCKET;
41 }
42
44 {
45 if(this == &rhs)
46 return * this;
47
48 assert(mSocket == INVALID_SOCKET || rhs.mSocket != mSocket);
49
50 std::swap(mSocket, rhs.mSocket);
51 rhs.reset();
52
53 return *this;
54 }
55
57 SOCKET operator*() const noexcept { return mSocket; }
58
60 SOCKET get() const noexcept { return mSocket; }
61
63 SOCKET release() noexcept
64 {
66 std::swap(sock, mSocket);
67 return sock;
68 }
69
71 void reset() noexcept
72 {
74 {
77 }
78 }
79
80 ~socket_guard() noexcept
81 {
82 reset();
83 }
84};
RAII-style socket wrapper. Since socket is closed on wrapper destruction, initializing multiple guard...
Definition: socket_guard.h:24
SOCKET get() const noexcept
Definition: socket_guard.h:60
void reset() noexcept
Closes the socket.
Definition: socket_guard.h:71
SOCKET release() noexcept
Returns the socket descriptor and releases ownership.
Definition: socket_guard.h:63
socket_guard & operator=(const socket_guard &)=delete
socket_guard(socket_guard &&rhs) noexcept
Definition: socket_guard.h:32
SOCKET operator*() const noexcept
Alias for socket_guard::get.
Definition: socket_guard.h:57
socket_guard(SOCKET sock=INVALID_SOCKET) noexcept
Definition: socket_guard.h:30
SOCKET mSocket
Definition: socket_guard.h:25
~socket_guard() noexcept
Definition: socket_guard.h:80
socket_guard(const socket_guard &)=delete
socket_guard & operator=(socket_guard &&rhs) noexcept
Definition: socket_guard.h:43
#define INVALID_SOCKET
Definition: ipc-types.h:28
#define CLOSE_SOCKET
Definition: ipc-types.h:31
#define SOCKET
Definition: ipc-types.h:30
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:634