Sane C++ Libraries
C++ Platform Abstraction Libraries
FileDescriptor.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Result.h"
5#include "../Foundation/Span.h"
6#include "../Foundation/UniqueHandle.h"
7
10
11namespace SC
12{
13struct SC_COMPILER_EXPORT FileDescriptor;
14struct SC_COMPILER_EXPORT PipeDescriptor;
15namespace detail
16{
17struct SC_COMPILER_EXPORT FileDescriptorDefinition;
18}
19struct SC_COMPILER_EXPORT File;
20} // namespace SC
21
22#if SC_PLATFORM_WINDOWS
23
24struct SC::detail::FileDescriptorDefinition
25{
26 using Handle = void*; // HANDLE
27 static Result releaseHandle(Handle& handle);
28#ifdef __clang__
29 static constexpr void* Invalid = __builtin_constant_p(-1) ? (void*)-1 : (void*)-1; // INVALID_HANDLE_VALUE
30#else
31 static constexpr void* Invalid = (void*)-1; // INVALID_HANDLE_VALUE
32#endif
33};
34
35#else
37struct SC::detail::FileDescriptorDefinition
38{
39 using Handle = int; // fd
40 static Result releaseHandle(Handle& handle);
41
42 static constexpr Handle Invalid = -1; // invalid fd
43};
44
45#endif
46
49
51struct SC::FileDescriptor : public SC::UniqueHandle<SC::detail::FileDescriptorDefinition>
52{
53 using UniqueHandle::UniqueHandle;
54
58
62 [[nodiscard]] Result setBlocking(bool blocking);
63
67 [[nodiscard]] Result setInheritable(bool inheritable);
68
72 [[nodiscard]] Result isInheritable(bool& hasValue) const;
73
79 [[nodiscard]] Result read(Span<char> data, Span<char>& actuallyRead, uint64_t offset);
80
86 [[nodiscard]] Result read(Span<uint8_t> data, Span<uint8_t>& actuallyRead, uint64_t offset);
87
92 [[nodiscard]] Result read(Span<char> data, Span<char>& actuallyRead);
93
98 [[nodiscard]] Result read(Span<uint8_t> data, Span<uint8_t>& actuallyRead);
99
104 [[nodiscard]] Result write(Span<const char> data, uint64_t offset);
105
110 [[nodiscard]] Result write(Span<const uint8_t> data, uint64_t offset);
111
115 [[nodiscard]] Result write(Span<const char> data);
116
120 [[nodiscard]] Result write(Span<const uint8_t> data);
121
124 {
128 };
129
134 [[nodiscard]] Result seek(SeekMode seekMode, uint64_t offset);
135
139 [[nodiscard]] Result currentPosition(size_t& position) const;
140
144 [[nodiscard]] Result sizeInBytes(size_t& sizeInBytes) const;
145
146 private:
147 friend struct File;
148 struct Internal;
149};
150
153{
156 {
159 };
160
163 {
166 };
169
176
179 [[nodiscard]] Result close();
180};
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition: PrimitiveTypes.h:42
File Descriptor (use SC::File to open and use it with strings and containers).
Definition: FileDescriptor.h:52
SeekMode
How the offset to FileDescriptor::seek is defined.
Definition: FileDescriptor.h:124
@ SeekCurrent
Offset to FileDescriptor::seek is to be applied from current descriptor position.
Definition: FileDescriptor.h:127
@ SeekEnd
Offset to FileDescriptor::seek is to be applied (backwards) from end of descriptor.
Definition: FileDescriptor.h:126
@ SeekStart
Offset to FileDescriptor::seek is to be applied from start of descriptor.
Definition: FileDescriptor.h:125
Result read(Span< uint8_t > data, Span< uint8_t > &actuallyRead)
Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
Result openForWriteToDevNull()
Opens a file descriptor handle for writing to /dev/null or equivalent on current OS.
Result write(Span< const uint8_t > data, uint64_t offset)
Writes bytes at offset from start of the file descriptor.
Result setInheritable(bool inheritable)
Set inheritable flag (visibility to child processes).
Result read(Span< char > data, Span< char > &actuallyRead)
Reads bytes from current position (FileDescriptor::seek) into user supplied Span.
Result currentPosition(size_t &position) const
Gets current descriptor position (if seekable)
Result write(Span< const uint8_t > data)
Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
Result write(Span< const char > data, uint64_t offset)
Writes bytes at offset from start of the file descriptor.
Result sizeInBytes(size_t &sizeInBytes) const
Gets total file size in bytes (if seekable)
Result read(Span< char > data, Span< char > &actuallyRead, uint64_t offset)
Reads bytes at offset into user supplied span.
Result setBlocking(bool blocking)
Set blocking mode (read / write waiting for I/O).
Result isInheritable(bool &hasValue) const
Queries the inheritable state of this descriptor.
Result read(Span< uint8_t > data, Span< uint8_t > &actuallyRead, uint64_t offset)
Reads bytes at offset into user supplied span.
Result write(Span< const char > data)
Writes bytes from current position (FileDescriptor::seek) of the file descriptor.
Result seek(SeekMode seekMode, uint64_t offset)
Changes the current position in the file descriptor, if seekable.
Wraps a SC::FileDescriptor to open it and use strings / containers.
Definition: File.h:20
Read / Write pipe (Process stdin/stdout and IPC communication)
Definition: FileDescriptor.h:153
Result createPipe(InheritableReadFlag readFlag=ReadNonInheritable, InheritableWriteFlag writeFlag=WriteNonInheritable)
Creates a Pipe.
InheritableReadFlag
Specifies a flag for read side of the pipe.
Definition: FileDescriptor.h:156
@ ReadNonInheritable
Requests read side of the pipe not to be inheritable from child processes.
Definition: FileDescriptor.h:158
@ ReadInheritable
Requests read side of the pipe to be inheritable from child processes.
Definition: FileDescriptor.h:157
FileDescriptor writePipe
The write side of the pipe.
Definition: FileDescriptor.h:168
Result close()
Closes the pipe.
FileDescriptor readPipe
The read side of the pipe.
Definition: FileDescriptor.h:167
InheritableWriteFlag
Specifies a flag for write side of the pipe.
Definition: FileDescriptor.h:163
@ WriteInheritable
Requests write side of the pipe to be inheritable from child processes.
Definition: FileDescriptor.h:164
@ WriteNonInheritable
Requests write side of the pipe to be inheritable from child processes.
Definition: FileDescriptor.h:165
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition: Result.h:12
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:24
Move only handle that has a special tag value flagging its invalid state.
Definition: UniqueHandle.h:67