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