Skip to content

Commit 8a68874

Browse files
committed
Add guard macros to workaround windows defined min/max instead of the parenthesis trick
1 parent 9a7556e commit 8a68874

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ jobs:
134134
135135
- name: format
136136
run: |
137-
git ls-files | grep -E '\.(cpp|hpp)' | xargs clang-format --dry-run --Werror
137+
clang-format --version
138+
git ls-files | grep -E '\.(cpp|hpp)' | xargs clang-format --dry-run --Werror
138139
139140
- name: build
140141
env:

zmq.hpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
#ifndef NOMINMAX
3131
#define NOMINMAX
3232
#endif
33+
// To avoid the `(std::min)` workaround for when a min macro is defined
34+
#ifdef min
35+
#define WINDOWS_MIN min
36+
#undef min
37+
#endif
38+
#ifdef max
39+
#define WINDOWS_MAX max
40+
#undef max
41+
#endif
3342
#endif
3443

3544
// included here for _HAS_CXX* macros
@@ -701,7 +710,7 @@ class message_t
701710
std::stringstream os;
702711

703712
const unsigned char *msg_data = this->data<unsigned char>();
704-
size_t size_to_print = (std::min) (this->size(), max_size);
713+
size_t size_to_print = std::min(this->size(), max_size);
705714
int is_ascii[2] = {0, 0};
706715
// Set is_ascii for the first character
707716
if (size_to_print > 0)
@@ -1101,8 +1110,7 @@ class mutable_buffer
11011110
constexpr size_t size() const noexcept { return _size; }
11021111
mutable_buffer &operator+=(size_t n) noexcept
11031112
{
1104-
// (std::min) is a workaround for when a min macro is defined
1105-
const auto shift = (std::min) (n, _size);
1113+
const auto shift = std::min(n, _size);
11061114
_data = static_cast<char *>(_data) + shift;
11071115
_size -= shift;
11081116
return *this;
@@ -1115,8 +1123,8 @@ class mutable_buffer
11151123

11161124
inline mutable_buffer operator+(const mutable_buffer &mb, size_t n) noexcept
11171125
{
1118-
return mutable_buffer(static_cast<char *>(mb.data()) + (std::min) (n, mb.size()),
1119-
mb.size() - (std::min) (n, mb.size()));
1126+
return mutable_buffer(static_cast<char *>(mb.data()) + std::min(n, mb.size()),
1127+
mb.size() - std::min(n, mb.size()));
11201128
}
11211129
inline mutable_buffer operator+(size_t n, const mutable_buffer &mb) noexcept
11221130
{
@@ -1142,7 +1150,7 @@ class const_buffer
11421150
constexpr size_t size() const noexcept { return _size; }
11431151
const_buffer &operator+=(size_t n) noexcept
11441152
{
1145-
const auto shift = (std::min) (n, _size);
1153+
const auto shift = std::min(n, _size);
11461154
_data = static_cast<const char *>(_data) + shift;
11471155
_size -= shift;
11481156
return *this;
@@ -1156,8 +1164,8 @@ class const_buffer
11561164
inline const_buffer operator+(const const_buffer &cb, size_t n) noexcept
11571165
{
11581166
return const_buffer(static_cast<const char *>(cb.data())
1159-
+ (std::min) (n, cb.size()),
1160-
cb.size() - (std::min) (n, cb.size()));
1167+
+ std::min(n, cb.size()),
1168+
cb.size() - std::min(n, cb.size()));
11611169
}
11621170
inline const_buffer operator+(size_t n, const const_buffer &cb) noexcept
11631171
{
@@ -1180,15 +1188,15 @@ constexpr mutable_buffer buffer(const mutable_buffer &mb) noexcept
11801188
}
11811189
inline mutable_buffer buffer(const mutable_buffer &mb, size_t n) noexcept
11821190
{
1183-
return mutable_buffer(mb.data(), (std::min) (mb.size(), n));
1191+
return mutable_buffer(mb.data(), std::min(mb.size(), n));
11841192
}
11851193
constexpr const_buffer buffer(const const_buffer &cb) noexcept
11861194
{
11871195
return cb;
11881196
}
11891197
inline const_buffer buffer(const const_buffer &cb, size_t n) noexcept
11901198
{
1191-
return const_buffer(cb.data(), (std::min) (cb.size(), n));
1199+
return const_buffer(cb.data(), std::min(cb.size(), n));
11921200
}
11931201

11941202
namespace detail
@@ -1241,7 +1249,7 @@ auto buffer_contiguous_sequence(Seq &&seq, size_t n_bytes) noexcept
12411249

12421250
const auto size = seq_size(seq);
12431251
return buffer(size != 0u ? std::addressof(*std::begin(seq)) : nullptr,
1244-
(std::min) (size * sizeof(T), n_bytes));
1252+
std::min(size * sizeof(T), n_bytes));
12451253
}
12461254

12471255
} // namespace detail
@@ -2050,7 +2058,7 @@ class socket_base
20502058
zmq_recv(_handle, buf.data(), buf.size(), static_cast<int>(flags));
20512059
if (nbytes >= 0) {
20522060
return recv_buffer_size{
2053-
(std::min) (static_cast<size_t>(nbytes), buf.size()),
2061+
std::min(static_cast<size_t>(nbytes), buf.size()),
20542062
static_cast<size_t>(nbytes)};
20552063
}
20562064
if (zmq_errno() == EAGAIN)
@@ -2778,7 +2786,7 @@ template<typename T = no_user_data> class poller_t
27782786
{
27792787
int rc = zmq_poller_size(const_cast<void *>(poller_ptr.get()));
27802788
ZMQ_ASSERT(rc >= 0);
2781-
return static_cast<size_t>((std::max) (rc, 0));
2789+
return static_cast<size_t>(std::max(rc, 0));
27822790
}
27832791
#endif
27842792

@@ -2900,4 +2908,15 @@ class timers
29002908

29012909
} // namespace zmq
29022910

2911+
#ifdef _WIN32
2912+
#ifdef WINDOWS_MIN
2913+
#define min WINDOWS_MIN
2914+
#undef WINDOWS_MIN
2915+
#endif
2916+
#ifdef WINDOWS_MAX
2917+
#define max WINDOWS_MAX
2918+
#undef WINDOWS_MAX
2919+
#endif
2920+
#endif
2921+
29032922
#endif // __ZMQ_HPP_INCLUDED__

0 commit comments

Comments
 (0)