forked from Alepha/Alepha
A unix file descriptor streambuf for writing.
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
add_subdirectory( IStreamable.test )
|
||||
add_subdirectory( OStreamable.test )
|
||||
add_subdirectory( streamable.test )
|
||||
add_subdirectory( OutUnixFileBuf.test )
|
||||
|
47
IOStreams/OutUnixFileBuf.h
Normal file
47
IOStreams/OutUnixFileBuf.h
Normal file
@ -0,0 +1,47 @@
|
||||
static_assert( __cplusplus > 2020'99 );
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Alepha/Alepha.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <streambuf>
|
||||
|
||||
namespace Alepha::Hydrogen::IOStreams ::detail:: OutUnixFileBuf_m
|
||||
{
|
||||
inline namespace exports
|
||||
{
|
||||
class OutUnixFileBuf;
|
||||
}
|
||||
|
||||
class exports::OutUnixFileBuf
|
||||
: virtual public std::streambuf
|
||||
{
|
||||
private:
|
||||
int fd;
|
||||
|
||||
int
|
||||
overflow( const int ch_ ) override
|
||||
{
|
||||
if( ch_ == EOF ) throw std::runtime_error{ "Unexpected EOF" };
|
||||
const char ch= ch_;
|
||||
xsputn( &ch, 1 );
|
||||
return ch_;
|
||||
}
|
||||
|
||||
std::streamsize
|
||||
xsputn( const char *const data, const std::streamsize amt ) override
|
||||
{
|
||||
return ::write( fd, data, amt );
|
||||
}
|
||||
|
||||
public:
|
||||
explicit OutUnixFileBuf( const int fd ) : fd( fd ) {}
|
||||
};
|
||||
}
|
||||
|
||||
namespace Alepha::Hydrogen::IOStreams::inline exports::inline OutUnixFileBuf_m
|
||||
{
|
||||
using namespace detail::OutUnixFileBuf_m::exports;
|
||||
}
|
28
IOStreams/OutUnixFileBuf.test/0.cc
Normal file
28
IOStreams/OutUnixFileBuf.test/0.cc
Normal file
@ -0,0 +1,28 @@
|
||||
static_assert( __cplusplus > 2020'99 );
|
||||
|
||||
#include "../OutUnixFileBuf.h"
|
||||
|
||||
#include <Alepha/Testing/test.h>
|
||||
#include <Alepha/AutoRAII.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <Alepha/Utility/evaluation_helpers.h>
|
||||
|
||||
auto init= Alepha::Utility::enroll <=[]
|
||||
{
|
||||
using namespace Alepha::Testing::exports;
|
||||
|
||||
"Can we write to /dev/null?"_test <=[]
|
||||
{
|
||||
const auto fd= Alepha::AutoRAII
|
||||
{
|
||||
[]{ return open( "/dev/null", O_WRONLY ); },
|
||||
::close
|
||||
};
|
||||
|
||||
Alepha::IOStreams::OutUnixFileBuf buf{ fd };
|
||||
std::ostream file{ &buf };
|
||||
};
|
||||
};
|
1
IOStreams/OutUnixFileBuf.test/CMakeLists.txt
Normal file
1
IOStreams/OutUnixFileBuf.test/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
unit_test( 0 )
|
Reference in New Issue
Block a user