Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
wsa_guard.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _TIME_SHIELD_WSA_GUARD_HPP_INCLUDED
3#define _TIME_SHIELD_WSA_GUARD_HPP_INCLUDED
4
8
9#if TIME_SHIELD_HAS_WINSOCK
10# ifndef WIN32_LEAN_AND_MEAN
11# define WIN32_LEAN_AND_MEAN
12# endif
13# ifndef NOMINMAX
14# define NOMINMAX
15# endif
16# include <winsock2.h> // Must be included before windows.h
17# include <ws2tcpip.h>
18# include <windows.h> // Optional, but safe if later needed
19#else
20# error "WsaGuard requires WinSock support"
21#endif
22#include <mutex>
23#include <string>
24
25namespace time_shield {
26
29 class WsaGuard {
30 public:
32 static const WsaGuard& instance() {
33 static WsaGuard instance;
34 return instance;
35 }
36
38 bool success() const noexcept {
39 return m_ret_code == 0;
40 }
41
43 int ret_code() const noexcept {
44 return m_ret_code;
45 }
46
48 const WSADATA& data() const noexcept {
49 return m_wsa_data;
50 }
51
52 private:
54 m_ret_code = WSAStartup(MAKEWORD(2, 2), &m_wsa_data);
55 }
56
57 ~WsaGuard() = default;
58
59 WsaGuard(const WsaGuard&) = delete;
60 WsaGuard& operator=(const WsaGuard&) = delete;
61
62 WSADATA m_wsa_data{};
63 int m_ret_code = -1;
64 };
65
66} // namespace time_shield
67
68#endif // _TIME_SHIELD_WSA_GUARD_HPP_INCLUDED
69
WsaGuard & operator=(const WsaGuard &)=delete
const WSADATA & data() const noexcept
Returns the WSAData structure (valid only if successful).
Definition wsa_guard.hpp:48
int ret_code() const noexcept
Returns the result code from WSAStartup.
Definition wsa_guard.hpp:43
static const WsaGuard & instance()
Returns the singleton instance, initializing WSA if needed.
Definition wsa_guard.hpp:32
bool success() const noexcept
Returns whether WSAStartup was successful.
Definition wsa_guard.hpp:38
WsaGuard(const WsaGuard &)=delete
Main namespace for the Time Shield library.