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