Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
wsa_guard.hpp
Go to the documentation of this file.
1
2#pragma once
3#ifndef _TIME_SHIELD_WSA_GUARD_HPP_INCLUDED
4#define _TIME_SHIELD_WSA_GUARD_HPP_INCLUDED
5
9
10#include <winsock2.h> // Must be included before windows.h
11#include <windows.h> // (optional, but safe if later needed)
12#include <mutex>
13#include <string>
14
15namespace time_shield {
16
19 class WsaGuard {
20 public:
22 static const WsaGuard& instance() {
23 static WsaGuard instance;
24 return instance;
25 }
26
28 bool success() const noexcept {
29 return m_ret_code == 0;
30 }
31
33 int ret_code() const noexcept {
34 return m_ret_code;
35 }
36
38 const WSADATA& data() const noexcept {
39 return m_wsa_data;
40 }
41
42 private:
44 m_ret_code = WSAStartup(MAKEWORD(2, 2), &m_wsa_data);
45 }
46
47 ~WsaGuard() = default;
48
49 WsaGuard(const WsaGuard&) = delete;
50 WsaGuard& operator=(const WsaGuard&) = delete;
51
52 WSADATA m_wsa_data{};
53 int m_ret_code = -1;
54 };
55
56} // namespace time_shield
57
58#endif // _TIME_SHIELD_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:38
int ret_code() const noexcept
Returns the result code from WSAStartup.
Definition wsa_guard.hpp:33
static const WsaGuard & instance()
Returns the singleton instance, initializing WSA if needed.
Definition wsa_guard.hpp:22
bool success() const noexcept
Returns whether WSAStartup was successful.
Definition wsa_guard.hpp:28
WsaGuard(const WsaGuard &)=delete
Main namespace for the Time Shield library.