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# include <winsock2.h> // Must be included before windows.h
14# include <ws2tcpip.h>
15# include <windows.h> // Optional, but safe if later needed
16#else
17# error "WsaGuard requires WinSock support"
18#endif
19#include <mutex>
20#include <string>
21
22namespace time_shield {
23
26 class WsaGuard {
27 public:
29 static const WsaGuard& instance() {
30 static WsaGuard instance;
31 return instance;
32 }
33
35 bool success() const noexcept {
36 return m_ret_code == 0;
37 }
38
40 int ret_code() const noexcept {
41 return m_ret_code;
42 }
43
45 const WSADATA& data() const noexcept {
46 return m_wsa_data;
47 }
48
49 private:
51 m_ret_code = WSAStartup(MAKEWORD(2, 2), &m_wsa_data);
52 }
53
54 ~WsaGuard() = default;
55
56 WsaGuard(const WsaGuard&) = delete;
57 WsaGuard& operator=(const WsaGuard&) = delete;
58
59 WSADATA m_wsa_data{};
60 int m_ret_code = -1;
61 };
62
63} // namespace time_shield
64
65#endif // _TIME_SHIELD_WSA_GUARD_HPP_INCLUDED
66
WsaGuard & operator=(const WsaGuard &)=delete
const WSADATA & data() const noexcept
Returns the WSAData structure (valid only if successful).
Definition wsa_guard.hpp:45
int ret_code() const noexcept
Returns the result code from WSAStartup.
Definition wsa_guard.hpp:40
static const WsaGuard & instance()
Returns the singleton instance, initializing WSA if needed.
Definition wsa_guard.hpp:29
bool success() const noexcept
Returns whether WSAStartup was successful.
Definition wsa_guard.hpp:35
WsaGuard(const WsaGuard &)=delete
Main namespace for the Time Shield library.