Skip to content

Commit 84cc331

Browse files
authored
feat: add config flags and document namespace policy
1 parent 8416317 commit 84cc331

File tree

7 files changed

+113
-25
lines changed

7 files changed

+113
-25
lines changed

README-RU.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
3535
- **Поддержка MQL5** — адаптированные заголовки в каталоге `MQL5` позволяют использовать библиотеку в MetaTrader.
3636
- Совместимость с `C++11``C++17`.
3737

38+
## Конфигурация
39+
40+
Компиляционные флаги в `time_shield/config.hpp` позволяют адаптировать библиотеку под платформу и отключать необязательные модули:
41+
42+
- `TIME_SHIELD_PLATFORM_WINDOWS` / `TIME_SHIELD_PLATFORM_UNIX` — определение целевой платформы.
43+
- `TIME_SHIELD_HAS_WINSOCK` — наличие WinSock API.
44+
- `TIME_SHIELD_ENABLE_NTP_CLIENT` — включает модуль `NtpClient` (по умолчанию `1` на Windows).
45+
46+
Все заголовки библиотеки используют пространство имён `time_shield`. Для доступа к API можно писать `time_shield::` или подключать `using namespace time_shield;`.
47+
3848
> Часть функций зависит от WinAPI и будет работать только под Windows (например, `NtpClient` или получение realtime через `QueryPerformanceCounter`).
3949
4050
## Установка и настройка

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ more academic solutions like `HowardHinnant/date`, the library:
5454
library in MetaTrader.
5555
- Compatible with `C++11``C++17`.
5656

57+
58+
## Configuration
59+
60+
Compile-time flags in `time_shield/config.hpp` control optional parts of the
61+
library and report platform capabilities:
62+
63+
- `TIME_SHIELD_PLATFORM_WINDOWS` / `TIME_SHIELD_PLATFORM_UNIX` — detected
64+
target platform.
65+
- `TIME_SHIELD_HAS_WINSOCK` — set when WinSock APIs are available.
66+
- `TIME_SHIELD_ENABLE_NTP_CLIENT` — enables the optional `NtpClient` module
67+
(defaults to `1` on Windows).
68+
69+
All public headers place their declarations inside the `time_shield` namespace.
70+
Use `time_shield::` or `using namespace time_shield;` to access the API.
71+
5772
> Some functions depend on WinAPI and work only on Windows (for example,
5873
> `NtpClient` or obtaining realtime via `QueryPerformanceCounter`).
5974

docs/mainpage.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ portable, and suitable for scenarios like logging, serialization, MQL5 usage, an
2727
- ISO8601 string parsing
2828
- Utilities for time manipulation and conversion
2929

30+
\section config_sec Configuration
31+
32+
Compile-time macros in `time_shield/config.hpp` allow adapting the library to
33+
the target platform and toggling optional modules:
34+
35+
- `TIME_SHIELD_PLATFORM_WINDOWS` / `TIME_SHIELD_PLATFORM_UNIX` — platform
36+
detection.
37+
- `TIME_SHIELD_HAS_WINSOCK` — WinSock availability.
38+
- `TIME_SHIELD_ENABLE_NTP_CLIENT` — builds the NTP client when set to `1`.
39+
40+
All public symbols are declared inside the `time_shield` namespace.
41+
3042
\section examples_sec Examples
3143

3244
Here is a simple demonstration:

include/time_shield_cpp/time_shield/config.hpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
#define _TIME_SHIELD_CONFIG_HPP_INCLUDED
44

55
/// \file config.hpp
6-
/// \brief Header file with preprocessor definitions for C++ standards and `constexpr` usage.
6+
/// \brief Configuration macros for the library.
77
///
8-
/// This file defines macros to check the C++ standard version being used and configure
9-
/// `constexpr` and `if constexpr` support accordingly.
8+
/// This header provides compile-time options for C++ standard detection,
9+
/// platform capabilities and optional features. The macros can be used to
10+
/// enable or disable parts of the library depending on the target platform or
11+
/// user preferences.
1012

1113
#if defined(_MSVC_LANG)
1214
# define TIME_SHIELD_CXX_VERSION _MSVC_LANG
@@ -41,4 +43,40 @@
4143
#endif
4244
#endif
4345

46+
/// \name Platform detection
47+
///@{
48+
#if defined(_WIN32)
49+
# define TIME_SHIELD_PLATFORM_WINDOWS 1
50+
#else
51+
# define TIME_SHIELD_PLATFORM_WINDOWS 0
52+
#endif
53+
54+
#if defined(__unix__) || defined(__unix) || defined(unix) || \
55+
(defined(__APPLE__) && defined(__MACH__))
56+
# define TIME_SHIELD_PLATFORM_UNIX 1
57+
#else
58+
# define TIME_SHIELD_PLATFORM_UNIX 0
59+
#endif
60+
///@}
61+
62+
/// \name Platform capabilities
63+
///@{
64+
#if TIME_SHIELD_PLATFORM_WINDOWS
65+
# define TIME_SHIELD_HAS_WINSOCK 1
66+
#else
67+
# define TIME_SHIELD_HAS_WINSOCK 0
68+
#endif
69+
///@}
70+
71+
/// \name Optional features
72+
///@{
73+
#ifndef TIME_SHIELD_ENABLE_NTP_CLIENT
74+
# if TIME_SHIELD_HAS_WINSOCK
75+
# define TIME_SHIELD_ENABLE_NTP_CLIENT 1
76+
# else
77+
# define TIME_SHIELD_ENABLE_NTP_CLIENT 0
78+
# endif
79+
#endif
80+
///@}
81+
4482
#endif // _TIME_SHIELD_CONFIG_HPP_INCLUDED

include/time_shield_cpp/time_shield/ntp_client.hpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
/// This module provides a minimal client that can query a remote NTP server over UDP
99
/// and calculate the offset between local system time and the NTP-reported time.
1010
///
11-
/// Currently only Windows is supported (WinSock-based implementation).
11+
/// The feature is optional and controlled by `TIME_SHIELD_ENABLE_NTP_CLIENT`.
1212
/// \ingroup ntp
1313

14-
#if defined(_WIN32)
14+
#include "config.hpp"
15+
16+
#if TIME_SHIELD_ENABLE_NTP_CLIENT
1517

1618
#include "ntp_client/wsa_guard.hpp"
1719
#include "time_utils.hpp"
@@ -209,18 +211,22 @@ namespace time_shield {
209211

210212
} // namespace time_shield
211213

212-
#else // !_WIN32
214+
#else // !TIME_SHIELD_ENABLE_NTP_CLIENT
213215

214-
# warning "NtpClient is only supported on Windows for now."
216+
# warning "NtpClient is disabled or unsupported on this platform."
215217

216218
namespace time_shield {
217219

220+
/// \brief Placeholder used when NTP client is disabled.
218221
class NtpClient {
219-
static_assert(sizeof(void*) == 0, "time_shield::NtpClient is only supported on Windows.");
222+
public:
223+
NtpClient() {
224+
static_assert(sizeof(void*) == 0, "time_shield::NtpClient is disabled by configuration.");
225+
}
220226
};
221227

222-
}
228+
} // namespace time_shield
223229

224-
#endif // _WIN32
230+
#endif // TIME_SHIELD_ENABLE_NTP_CLIENT
225231

226232
#endif // _TIME_SHIELD_NTP_CLIENT_HPP_INCLUDED

include/time_shield_cpp/time_shield/ntp_client/wsa_guard.hpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
/// \brief Singleton guard for WinSock initialization.
77
/// \ingroup ntp
88

9-
#include <winsock2.h> // Must be included before windows.h
10-
#include <ws2tcpip.h>
11-
#include <windows.h> // (optional, but safe if later needed)
9+
#include "../config.hpp"
10+
11+
#if TIME_SHIELD_HAS_WINSOCK
12+
# include <winsock2.h> // Must be included before windows.h
13+
# include <ws2tcpip.h>
14+
# include <windows.h> // (optional, but safe if later needed)
15+
#else
16+
# error "WsaGuard requires WinSock support"
17+
#endif
1218
#include <mutex>
1319
#include <string>
1420

@@ -55,4 +61,5 @@ namespace time_shield {
5561

5662
} // namespace time_shield
5763

58-
#endif // _TIME_SHIELD_WSA_GUARD_HPP_INCLUDED
64+
#endif // _TIME_SHIELD_WSA_GUARD_HPP_INCLUDED
65+

include/time_shield_cpp/time_shield/time_utils.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@
1616
#include <time.h> // For clock(), times(), etc.
1717
#include <mutex> // For std::once_flag
1818

19-
#if defined(_WIN32)
20-
#include <Windows.h>
21-
#elif defined(__unix__) || defined(__APPLE__)
22-
#include <unistd.h>
23-
#include <sys/resource.h>
24-
#include <sys/times.h>
25-
#include <time.h>
19+
#if TIME_SHIELD_PLATFORM_WINDOWS
20+
# include <Windows.h>
21+
#elif TIME_SHIELD_PLATFORM_UNIX
22+
# include <unistd.h>
23+
# include <sys/resource.h>
24+
# include <sys/times.h>
25+
# include <time.h>
2626
#else
27-
#error "Unsupported platform for get_cpu_time()"
27+
# error "Unsupported platform for get_cpu_time()"
2828
#endif
2929

3030
namespace time_shield {
@@ -53,7 +53,7 @@ namespace time_shield {
5353
/// \return Current UTC timestamp in microseconds.
5454
/// \note Windows only. Not available on Unix-like systems.
5555
inline int64_t now_realtime_us() {
56-
# if defined(_WIN32)
56+
# if TIME_SHIELD_PLATFORM_WINDOWS
5757
static std::once_flag init_flag;
5858
static int64_t s_perf_freq = 0;
5959
static int64_t s_anchor_perf = 0;
@@ -196,15 +196,15 @@ namespace time_shield {
196196
/// \note This function attempts multiple fallback methods depending on platform capabilities.
197197
/// \see https://habr.com/ru/articles/282301/ — original implementation idea
198198
inline double get_cpu_time() noexcept {
199-
# if defined(_WIN32)
199+
# if TIME_SHIELD_PLATFORM_WINDOWS
200200
FILETIME create_time{}, exit_time{}, kernel_time{}, user_time{};
201201
if (GetProcessTimes(GetCurrentProcess(), &create_time, &exit_time, &kernel_time, &user_time)) {
202202
ULARGE_INTEGER li{};
203203
li.LowPart = user_time.dwLowDateTime;
204204
li.HighPart = user_time.dwHighDateTime;
205205
return static_cast<double>(li.QuadPart) / 10000000.0;
206206
}
207-
# elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
207+
# elif TIME_SHIELD_PLATFORM_UNIX
208208
// AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris
209209
# if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
210210
clockid_t id = (clockid_t)-1;

0 commit comments

Comments
 (0)