Skip to content

Commit eb206ea

Browse files
Mike GrierEmJayGee
authored andcommitted
Rearrange the chrono project, make the windows chrono stuff a separate target
1 parent 5b8c773 commit eb206ea

File tree

15 files changed

+318
-314
lines changed

15 files changed

+318
-314
lines changed

src/libraries/chrono/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ list(APPEND m_installation_targets
1212
m_chrono
1313
)
1414

15+
add_subdirectory(Platforms)
16+
1517
set(m_installation_targets ${m_installation_targets} PARENT_SCOPE)
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
cmake_minimum_required(VERSION 3.23)
2-
3-
if(WIN32)
4-
add_subdirectory(Windows)
5-
endif()
6-
7-
set(m_installation_targets ${m_installation_targets} PARENT_SCOPE)
1+
cmake_minimum_required(VERSION 3.23)
2+
3+
if(WIN32)
4+
5+
add_subdirectory(Windows)
6+
7+
endif()
8+
9+
set(m_installation_targets ${m_installation_targets} PARENT_SCOPE)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.23)
2+
3+
add_library(m_windows_chrono STATIC)
4+
5+
target_compile_features(m_windows_chrono PUBLIC cxx_std_23)
6+
7+
add_subdirectory(include)
8+
add_subdirectory(src)
9+
add_subdirectory(test)
10+
11+
list(APPEND m_installation_targets
12+
m_windows_chrono
13+
)
14+
15+
set(m_installation_targets ${m_installation_targets} PARENT_SCOPE)
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
cmake_minimum_required(VERSION 3.23)
2-
3-
target_sources(m_chrono PUBLIC FILE_SET HEADERS FILES
4-
m/chrono/windows_chrono_casts.h
5-
)
6-
7-
set(m_installation_targets ${m_installation_targets} PARENT_SCOPE)
1+
cmake_minimum_required(VERSION 3.23)
2+
3+
target_sources(m_windows_chrono PUBLIC FILE_SET HEADERS FILES
4+
m/windows_chrono/windows_chrono_casts.h
5+
)
6+
7+
set(m_installation_targets ${m_installation_targets} PARENT_SCOPE)
Original file line numberDiff line numberDiff line change
@@ -1,154 +1,154 @@
1-
// Copyright (c) Microsoft Corporation.
2-
// Licensed under the MIT License.
3-
4-
#pragma once
5-
6-
#include <m/utility/compiler.h>
7-
8-
#include <bit>
9-
#include <chrono>
10-
11-
#include <m/cast/to.h>
12-
#include <m/cast/try_cast.h>
13-
#include <m/exception/exception.h>
14-
#include <m/tracing/tracing.h>
15-
#include <m/windows_wrappers/win32_dword_ms.h>
16-
17-
#undef NOMINMAX
18-
#define NOMINMAX
19-
20-
#include <Windows.h>
21-
22-
namespace m
23-
{
24-
template <typename ResultType, typename Clock, typename Duration = Clock::duration>
25-
struct time_point_cast_helper;
26-
27-
template <typename ResultType, typename Clock, typename Duration>
28-
auto
29-
utc_time_point_cast(std::chrono::time_point<Clock, Duration> tp)
30-
{
31-
return time_point_cast_helper<ResultType, Clock, Duration>::cast_utc_time_point(tp);
32-
}
33-
34-
template <typename Rep, typename Period>
35-
win32_dword_ms
36-
win32_dword_ms_cast(std::chrono::duration<Rep, Period> const& d)
37-
{
38-
auto const as_ms = std::chrono::duration_cast<std::chrono::milliseconds>(d);
39-
40-
using value_type = typename win32_dword_ms::value_type;
41-
42-
if (as_ms.count() < (std::numeric_limits<value_type>::min)() ||
43-
as_ms.count() > (std::numeric_limits<value_type>::max)())
44-
{
45-
m::trace_error("Attempted to convert value {} to DWORD milliseconds; out of range",
46-
as_ms.count());
47-
throw m::invalid_parameter("d");
48-
}
49-
50-
return win32_dword_ms(m::to<DWORD>(as_ms.count()));
51-
}
52-
53-
template <typename Rep, typename Period>
54-
requires(std::integral<Rep>)
55-
struct try_cast_helper<std::chrono::duration<Rep, Period>, win32_dword_ms, void>
56-
{
57-
static constexpr decltype(auto)
58-
do_cast(std::chrono::duration<Rep, Period> const& d)
59-
{
60-
return win32_dword_ms_cast(d);
61-
}
62-
};
63-
64-
template <typename Clock, typename Duration>
65-
struct time_point_cast_helper<SYSTEMTIME, Clock, Duration>
66-
{
67-
68-
template <typename DestinationClock>
69-
static SYSTEMTIME
70-
cast_time_point(std::chrono::time_point<Clock, Duration> tp)
71-
{
72-
auto const as_destination_clock = std::chrono::clock_cast<DestinationClock>(tp);
73-
auto const sys_time = std::chrono::utc_clock::to_sys(as_destination_clock);
74-
auto const time_t_time = std::chrono::system_clock::to_time_t(sys_time);
75-
std::tm tm_time{};
76-
77-
gmtime_s(&tm_time, &time_t_time);
78-
79-
SYSTEMTIME st{};
80-
81-
st.wDay = m::to<WORD>(tm_time.tm_mday);
82-
st.wDayOfWeek = m::to<WORD>(tm_time.tm_wday);
83-
st.wHour = m::to<WORD>(tm_time.tm_hour);
84-
st.wMilliseconds = 0;
85-
st.wMinute = m::to<WORD>(tm_time.tm_min);
86-
st.wMonth = m::to<WORD>(tm_time.tm_mon) + 1;
87-
st.wSecond = m::to<WORD>(tm_time.tm_sec);
88-
st.wYear = m::to<WORD>(tm_time.tm_year) + 1900;
89-
90-
return st;
91-
}
92-
93-
static SYSTEMTIME
94-
cast_utc_time_point(std::chrono::time_point<Clock, Duration> tp)
95-
{
96-
return cast_time_point<std::chrono::utc_clock>(tp);
97-
}
98-
};
99-
100-
template <typename Clock, typename Duration>
101-
struct try_cast_helper<std::chrono::time_point<Clock, Duration>, SYSTEMTIME, void>
102-
{
103-
static constexpr decltype(auto)
104-
do_cast(std::chrono::time_point<Clock, Duration> const& tp)
105-
{
106-
return time_point_cast_helper<SYSTEMTIME, Clock, Duration>::cast_utc_time_point(tp);
107-
}
108-
};
109-
110-
template <typename Rep, typename Period>
111-
struct try_cast_helper<std::chrono::duration<Rep, Period>, FILETIME, void>
112-
{
113-
template <typename Rep, typename Period>
114-
static FILETIME
115-
DurationToFILETIME(std::chrono::duration<Rep, Period> const& duration)
116-
{
117-
if (duration.count() < 0)
118-
{
119-
trace_error("Programming error: invalid duration passed in; count < 0: {}",
120-
duration.count());
121-
throw m::invalid_parameter("duration");
122-
}
123-
124-
//
125-
// FILETIME is 100ns units, so for the ratio
126-
// have the denominator be 1 billion divided by
127-
// 100.
128-
//
129-
using FiletimeRatio = std::ratio<1, 1'000'000'000 / 100>;
130-
using FiletimeDuration = std::chrono::duration<int64_t, FiletimeRatio>;
131-
132-
auto const as_filetime_duration =
133-
std::chrono::duration_cast<FiletimeDuration>(duration);
134-
135-
static_assert(std::numeric_limits<int64_t>::digits ==
136-
std::numeric_limits<typename FiletimeDuration::rep>::digits);
137-
138-
int64_t count = as_filetime_duration.count();
139-
140-
// Durations in FILETIME are negative.
141-
//
142-
count = -count;
143-
144-
return std::bit_cast<FILETIME>(count);
145-
}
146-
147-
static constexpr decltype(auto)
148-
do_cast(std::chrono::duration<Rep, Period> const& d)
149-
{
150-
return DurationToFILETIME(d);
151-
}
152-
};
153-
154-
} // namespace m
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
6+
#include <m/utility/compiler.h>
7+
8+
#include <bit>
9+
#include <chrono>
10+
11+
#include <m/cast/to.h>
12+
#include <m/cast/try_cast.h>
13+
#include <m/exception/exception.h>
14+
#include <m/tracing/tracing.h>
15+
#include <m/windows_wrappers/win32_dword_ms.h>
16+
17+
#undef NOMINMAX
18+
#define NOMINMAX
19+
20+
#include <Windows.h>
21+
22+
namespace m
23+
{
24+
template <typename ResultType, typename Clock, typename Duration = Clock::duration>
25+
struct time_point_cast_helper;
26+
27+
template <typename ResultType, typename Clock, typename Duration>
28+
auto
29+
utc_time_point_cast(std::chrono::time_point<Clock, Duration> tp)
30+
{
31+
return time_point_cast_helper<ResultType, Clock, Duration>::cast_utc_time_point(tp);
32+
}
33+
34+
template <typename Rep, typename Period>
35+
win32_dword_ms
36+
win32_dword_ms_cast(std::chrono::duration<Rep, Period> const& d)
37+
{
38+
auto const as_ms = std::chrono::duration_cast<std::chrono::milliseconds>(d);
39+
40+
using value_type = typename win32_dword_ms::value_type;
41+
42+
if (as_ms.count() < (std::numeric_limits<value_type>::min)() ||
43+
as_ms.count() > (std::numeric_limits<value_type>::max)())
44+
{
45+
m::trace_error("Attempted to convert value {} to DWORD milliseconds; out of range",
46+
as_ms.count());
47+
throw m::invalid_parameter("d");
48+
}
49+
50+
return win32_dword_ms(m::to<DWORD>(as_ms.count()));
51+
}
52+
53+
template <typename Rep, typename Period>
54+
requires(std::integral<Rep>)
55+
struct try_cast_helper<std::chrono::duration<Rep, Period>, win32_dword_ms, void>
56+
{
57+
static constexpr decltype(auto)
58+
do_cast(std::chrono::duration<Rep, Period> const& d)
59+
{
60+
return win32_dword_ms_cast(d);
61+
}
62+
};
63+
64+
template <typename Clock, typename Duration>
65+
struct time_point_cast_helper<SYSTEMTIME, Clock, Duration>
66+
{
67+
68+
template <typename DestinationClock>
69+
static SYSTEMTIME
70+
cast_time_point(std::chrono::time_point<Clock, Duration> tp)
71+
{
72+
auto const as_destination_clock = std::chrono::clock_cast<DestinationClock>(tp);
73+
auto const sys_time = std::chrono::utc_clock::to_sys(as_destination_clock);
74+
auto const time_t_time = std::chrono::system_clock::to_time_t(sys_time);
75+
std::tm tm_time{};
76+
77+
gmtime_s(&tm_time, &time_t_time);
78+
79+
SYSTEMTIME st{};
80+
81+
st.wDay = m::to<WORD>(tm_time.tm_mday);
82+
st.wDayOfWeek = m::to<WORD>(tm_time.tm_wday);
83+
st.wHour = m::to<WORD>(tm_time.tm_hour);
84+
st.wMilliseconds = 0;
85+
st.wMinute = m::to<WORD>(tm_time.tm_min);
86+
st.wMonth = m::to<WORD>(tm_time.tm_mon) + 1;
87+
st.wSecond = m::to<WORD>(tm_time.tm_sec);
88+
st.wYear = m::to<WORD>(tm_time.tm_year) + 1900;
89+
90+
return st;
91+
}
92+
93+
static SYSTEMTIME
94+
cast_utc_time_point(std::chrono::time_point<Clock, Duration> tp)
95+
{
96+
return cast_time_point<std::chrono::utc_clock>(tp);
97+
}
98+
};
99+
100+
template <typename Clock, typename Duration>
101+
struct try_cast_helper<std::chrono::time_point<Clock, Duration>, SYSTEMTIME, void>
102+
{
103+
static constexpr decltype(auto)
104+
do_cast(std::chrono::time_point<Clock, Duration> const& tp)
105+
{
106+
return time_point_cast_helper<SYSTEMTIME, Clock, Duration>::cast_utc_time_point(tp);
107+
}
108+
};
109+
110+
template <typename Rep, typename Period>
111+
struct try_cast_helper<std::chrono::duration<Rep, Period>, FILETIME, void>
112+
{
113+
template <typename Rep, typename Period>
114+
static FILETIME
115+
DurationToFILETIME(std::chrono::duration<Rep, Period> const& duration)
116+
{
117+
if (duration.count() < 0)
118+
{
119+
trace_error("Programming error: invalid duration passed in; count < 0: {}",
120+
duration.count());
121+
throw m::invalid_parameter("duration");
122+
}
123+
124+
//
125+
// FILETIME is 100ns units, so for the ratio
126+
// have the denominator be 1 billion divided by
127+
// 100.
128+
//
129+
using FiletimeRatio = std::ratio<1, 1'000'000'000 / 100>;
130+
using FiletimeDuration = std::chrono::duration<int64_t, FiletimeRatio>;
131+
132+
auto const as_filetime_duration =
133+
std::chrono::duration_cast<FiletimeDuration>(duration);
134+
135+
static_assert(std::numeric_limits<int64_t>::digits ==
136+
std::numeric_limits<typename FiletimeDuration::rep>::digits);
137+
138+
int64_t count = as_filetime_duration.count();
139+
140+
// Durations in FILETIME are negative.
141+
//
142+
count = -count;
143+
144+
return std::bit_cast<FILETIME>(count);
145+
}
146+
147+
static constexpr decltype(auto)
148+
do_cast(std::chrono::duration<Rep, Period> const& d)
149+
{
150+
return DurationToFILETIME(d);
151+
}
152+
};
153+
154+
} // namespace m
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 3.23)
2+
3+
target_sources(m_windows_chrono PRIVATE
4+
windows_chrono_casts.cpp
5+
)
6+
7+
target_include_directories(m_windows_chrono PUBLIC
8+
../include/Platforms/Windows
9+
)
10+
11+
target_link_libraries(m_windows_chrono PUBLIC
12+
m_cast
13+
m_chrono
14+
m_tracing
15+
m_windows_wrappers
16+
)
17+
18+
set(m_installation_targets ${m_installation_targets} PARENT_SCOPE)
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation.
2-
// Licensed under the MIT License.
3-
4-
#include <m/chrono/windows_chrono_casts.h>
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#include <m/windows_chrono/windows_chrono_casts.h>

0 commit comments

Comments
 (0)