Time Shield Library
C++ library for working with time
Toggle main menu visibility
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
26
namespace
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
:
54
WsaGuard
() {
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
time_shield::WsaGuard::m_ret_code
int m_ret_code
Definition
wsa_guard.hpp:64
time_shield::WsaGuard::~WsaGuard
~WsaGuard()=default
time_shield::WsaGuard::operator=
WsaGuard & operator=(const WsaGuard &)=delete
time_shield::WsaGuard::data
const WSADATA & data() const noexcept
Returns the WSAData structure (valid only if successful).
Definition
wsa_guard.hpp:49
time_shield::WsaGuard::ret_code
int ret_code() const noexcept
Returns the result code from WSAStartup.
Definition
wsa_guard.hpp:44
time_shield::WsaGuard::instance
static const WsaGuard & instance()
Returns the singleton instance, initializing WSA if needed.
Definition
wsa_guard.hpp:33
time_shield::WsaGuard::m_wsa_data
WSADATA m_wsa_data
Definition
wsa_guard.hpp:63
time_shield::WsaGuard::WsaGuard
WsaGuard()
Definition
wsa_guard.hpp:54
time_shield::WsaGuard::success
bool success() const noexcept
Returns whether WSAStartup was successful.
Definition
wsa_guard.hpp:39
time_shield::WsaGuard::WsaGuard
WsaGuard(const WsaGuard &)=delete
time_shield
Main namespace for the Time Shield library.
include
time_shield
ntp_client
wsa_guard.hpp
Generated by
1.17.0