Skip to content

Commit 2420fe3

Browse files
mhaynieclaude
andcommitted
fix: Resolve include structure and sign-compare warnings
- Simplify source.inl and sink.inl to just include fd_source.hpp/fd_sink.hpp - Move all implementations to fd_source.inl and fd_sink.inl - Add static write_safe/read_safe helpers in io_native_handle_test.cpp to avoid sign-compare warnings when comparing with size_t Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 729b5fe commit 2420fe3

File tree

5 files changed

+53
-48
lines changed

5 files changed

+53
-48
lines changed

cpp/include/mh/io/fd_sink.inl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ namespace mh::io
7676
return std::make_shared<fd_sink>(fd, true);
7777
#else
7878
throw mh::not_implemented_error();
79+
#endif
80+
}
81+
82+
MH_COMPILE_LIBRARY_INLINE sink_ptr sink::stdin_sink()
83+
{
84+
#ifdef __unix__
85+
static auto instance = std::make_shared<fd_sink>(STDIN_FILENO, false);
86+
return instance;
87+
#else
88+
throw mh::not_implemented_error();
7989
#endif
8090
}
8191
}

cpp/include/mh/io/fd_source.inl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ namespace mh::io
8282
return std::make_shared<fd_source>(fd, true);
8383
#else
8484
throw mh::not_implemented_error();
85+
#endif
86+
}
87+
88+
MH_COMPILE_LIBRARY_INLINE source_ptr source::stdout_source()
89+
{
90+
#ifdef __unix__
91+
static auto instance = std::make_shared<fd_source>(STDOUT_FILENO, false);
92+
return instance;
93+
#else
94+
throw mh::not_implemented_error();
95+
#endif
96+
}
97+
98+
MH_COMPILE_LIBRARY_INLINE source_ptr source::stderr_source()
99+
{
100+
#ifdef __unix__
101+
static auto instance = std::make_shared<fd_source>(STDERR_FILENO, false);
102+
return instance;
103+
#else
104+
throw mh::not_implemented_error();
85105
#endif
86106
}
87107
}

cpp/include/mh/io/sink.inl

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
#ifdef MH_COMPILE_LIBRARY
2-
#include "sink.hpp"
3-
#else
4-
#define MH_COMPILE_LIBRARY_INLINE inline
5-
#endif
6-
7-
#ifdef __unix__
1+
#pragma once
82

93
#include "fd_sink.hpp"
10-
#include <unistd.h>
11-
12-
namespace mh::io
13-
{
14-
MH_COMPILE_LIBRARY_INLINE sink_ptr sink::stdin_sink()
15-
{
16-
static auto instance = std::make_shared<fd_sink>(STDIN_FILENO, false);
17-
return instance;
18-
}
19-
}
20-
21-
#endif // __unix__

cpp/include/mh/io/source.inl

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
#ifdef MH_COMPILE_LIBRARY
2-
#include "source.hpp"
3-
#else
4-
#define MH_COMPILE_LIBRARY_INLINE inline
5-
#endif
6-
7-
#ifdef __unix__
1+
#pragma once
82

93
#include "fd_source.hpp"
10-
#include <unistd.h>
11-
12-
namespace mh::io
13-
{
14-
MH_COMPILE_LIBRARY_INLINE source_ptr source::stdout_source()
15-
{
16-
static auto instance = std::make_shared<fd_source>(STDOUT_FILENO, false);
17-
return instance;
18-
}
19-
20-
MH_COMPILE_LIBRARY_INLINE source_ptr source::stderr_source()
21-
{
22-
static auto instance = std::make_shared<fd_source>(STDERR_FILENO, false);
23-
return instance;
24-
}
25-
}
26-
27-
#endif // __unix__

test/io_native_handle_test.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,25 @@
66
#include <unistd.h>
77
#include <cstdio>
88
#include <filesystem>
9+
#include <stdexcept>
910
#include "last_include.hpp"
1011

12+
static size_t write_safe(int fd, const void* buf, size_t count)
13+
{
14+
ssize_t result = ::write(fd, buf, count);
15+
if (result < 0)
16+
throw std::runtime_error("write failed");
17+
return static_cast<size_t>(result);
18+
}
19+
20+
static size_t read_safe(int fd, void* buf, size_t count)
21+
{
22+
ssize_t result = ::read(fd, buf, count);
23+
if (result < 0)
24+
throw std::runtime_error("read failed");
25+
return static_cast<size_t>(result);
26+
}
27+
1128
TEST_CASE("fd_traits basic functionality", "[io][native_handle]")
1229
{
1330
using traits = mh::io::detail::native_handle_hpp::fd_traits;
@@ -229,13 +246,13 @@ TEST_CASE("unique_native_handle with pipe", "[io][native_handle]")
229246

230247
// Test writing and reading
231248
const char* message = "test message";
232-
ssize_t written = write(write_end.value(), message, strlen(message));
249+
size_t written = write_safe(write_end.value(), message, strlen(message));
233250
REQUIRE(written == strlen(message));
234-
251+
235252
write_end.reset(); // Close write end to signal EOF
236-
253+
237254
char buffer[100] = {0};
238-
ssize_t read_bytes = read(read_end.value(), buffer, sizeof(buffer) - 1);
255+
size_t read_bytes = read_safe(read_end.value(), buffer, sizeof(buffer) - 1);
239256
REQUIRE(read_bytes == strlen(message));
240257
REQUIRE(strcmp(buffer, message) == 0);
241258

0 commit comments

Comments
 (0)