Reformat coral library

This commit is contained in:
kayomn 2023-02-26 01:16:53 +00:00
parent 6a1eb71ba0
commit 3c44a6c0f3
3 changed files with 66 additions and 49 deletions

View File

@ -4,7 +4,7 @@ import coral;
export namespace coral { export namespace coral {
/** /**
* Platform-generalized identifier for a resource in a [file_store]. * Platform-generalized identifier for a resource in a [fs].
*/ */
struct path { struct path {
/** /**
@ -86,6 +86,8 @@ export namespace coral {
* Returns a new [path] composed of the current path joined with `text`. * Returns a new [path] composed of the current path joined with `text`.
* *
* *Note:* should the new path exceed [max] bytes in size, an empty [path] is returned instead. * *Note:* should the new path exceed [max] bytes in size, an empty [path] is returned instead.
*
* *Note:* should the new path exceed [max] bytes in size, an empty [path] is returned instead.
*/ */
constexpr path joined(slice<char const> const & text) const { constexpr path joined(slice<char const> const & text) const {
if (text.length > this->buffer[max]) return path{}; if (text.length > this->buffer[max]) return path{};
@ -104,15 +106,37 @@ export namespace coral {
char buffer[max + 1]{0}; char buffer[max + 1]{0};
}; };
/**
* [reader] that has a known range of data and may attempt to traverse it freely.
*/
struct file_reader : public reader { struct file_reader : public reader {
/**
* Attempts to seek to the position in the file defined by `offset`, returning the literal absolute position
* that was actually sought to or a [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> seek(u64 offset) = 0; virtual expected<u64, io_error> seek(u64 offset) = 0;
/**
* Attempts to get the current cursor position in the file, returning the literal absolute position of it or a
* [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> tell() = 0; virtual expected<u64, io_error> tell() = 0;
}; };
/**
* [writer] that has a known range of data and may attempt to traverse it freely.
*/
struct file_writer : public writer { struct file_writer : public writer {
/**
* Attempts to seek to the position in the file defined by `offset`, returning the literal absolute position
* that was actually sought to or a [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> seek(u64 offset) = 0; virtual expected<u64, io_error> seek(u64 offset) = 0;
/**
* Attempts to get the current cursor position in the file, returning the literal absolute position of it or a
* [io_error] if the operation failed for whatever reason.
*/
virtual expected<u64, io_error> tell() = 0; virtual expected<u64, io_error> tell() = 0;
}; };

View File

@ -4,8 +4,8 @@ import coral;
export namespace coral { export namespace coral {
/** /**
* Multiplexing byte-based ring buffer of `capacity` size that may be used for memory-backed * Multiplexing byte-based ring buffer of `capacity` size that may be used for memory-backed I/O operations and
* I/O operations and lightweight data construction. * lightweight data construction.
*/ */
template<usize capacity> struct fixed_buffer : public writer, public reader { template<usize capacity> struct fixed_buffer : public writer, public reader {
fixed_buffer() = default; fixed_buffer() = default;
@ -13,8 +13,8 @@ export namespace coral {
/** /**
* Returns a mutable [slice] ranging from the head to the last-filled element. * Returns a mutable [slice] ranging from the head to the last-filled element.
* *
* *Note*: The lifetime and validity of the returned slice is only guaranteed for as long * *Note*: The lifetime and validity of the returned slice is only guaranteed for as long as the source
* as the source [fixed_buffer] is not mutated or out-of-scope. * [fixed_buffer] is not mutated or out-of-scope.
*/ */
slice<u8 const> as_slice() const { slice<u8 const> as_slice() const {
return {0, this->filled}; return {0, this->filled};
@ -56,8 +56,8 @@ export namespace coral {
} }
/** /**
* Attempts to write the single value `data` into the buffer, returning `true` if * Attempts to write the single value `data` into the buffer, returning `true` if successful, otherwise `false`
* successful, otherwise `false` if the buffer is full. * if the buffer is full.
*/ */
bool put(u8 data) { bool put(u8 data) {
if (this->is_full()) return false; if (this->is_full()) return false;
@ -70,8 +70,7 @@ export namespace coral {
} }
/** /**
* Reads whatever data is in the buffer into `data`, returning the number of bytes read * Reads whatever data is in the buffer into `data`, returning the number of bytes read from the buffer.
* from the buffer.
*/ */
expected<usize, io_error> read(slice<u8> const & data) override { expected<usize, io_error> read(slice<u8> const & data) override {
slice const readable_data{this->data, min(this->filled, data.length)}; slice const readable_data{this->data, min(this->filled, data.length)};
@ -87,9 +86,8 @@ export namespace coral {
} }
/** /**
* Attempts to write `data` to the buffer, returning the number of bytes written or * Attempts to write `data` to the buffer, returning the number of bytes written or [io_error::unavailable] if
* [io_error::unavailable] if it has been completely filled and no more bytes can be * it has been completely filled and no more bytes can be written.
* written.
*/ */
expected<usize, io_error> write(slice<u8 const> const & data) override { expected<usize, io_error> write(slice<u8 const> const & data) override {
if (this->is_full()) return io_error::unavailable; if (this->is_full()) return io_error::unavailable;
@ -119,11 +117,11 @@ export namespace coral {
/** /**
* Streams the data from `input` to `output`, using `buffer` as temporary transfer space. * Streams the data from `input` to `output`, using `buffer` as temporary transfer space.
* *
* The returned [expected] can be used to introspect if `input` or `output` encountered any * The returned [expected] can be used to introspect if `input` or `output` encountered any issues during streaming,
* issues during streaming, otherwise it will contain the number of bytes streamed. * otherwise it will contain the number of bytes streamed.
* *
* *Note*: if `buffer` has a length of `0`, no data will be streamed as there is nowhere to * *Note*: if `buffer` has a length of `0`, no data will be streamed as there is nowhere to temporarily place data
* temporarily place data during streaming. * during streaming.
*/ */
expected<usize, io_error> stream(writer & output, reader & input, slice<u8> const & buffer) { expected<usize, io_error> stream(writer & output, reader & input, slice<u8> const & buffer) {
usize total_bytes_written = 0; usize total_bytes_written = 0;
@ -172,8 +170,8 @@ export namespace coral {
/** /**
* Attempts to format and print `value` as an unsigned integer out to `output`. * Attempts to format and print `value` as an unsigned integer out to `output`.
* *
* The returned [expected] can be used to introspect if `output` encountered any issues during * The returned [expected] can be used to introspect if `output` encountered any issues during printing, otherwise
* printing, otherwise it will contain the number of characters used to print `value` as text. * it will contain the number of characters used to print `value` as text.
*/ */
expected<usize, io_error> print_unsigned(writer & output, u64 value) { expected<usize, io_error> print_unsigned(writer & output, u64 value) {
if (value == 0) return output.write(slice{"0"}.as_bytes()); if (value == 0) return output.write(slice{"0"}.as_bytes());

View File

@ -9,8 +9,7 @@ export namespace coral {
* *
* [push_result::ok] indicates that an push operation was successful. * [push_result::ok] indicates that an push operation was successful.
* *
* [push_result::out_of_memory] alerts that the memory required to perform the push operation * [push_result::out_of_memory] alerts that the memory required to perform the push operation failed.
* failed.
*/ */
enum class [[nodiscard]] push_result { enum class [[nodiscard]] push_result {
ok, ok,
@ -20,8 +19,8 @@ export namespace coral {
/** /**
* Base type for all stack types. * Base type for all stack types.
* *
* Sequences are any data structure which owns a linear, non-unique set of elements which may * Sequences are any data structure which owns a linear, non-unique set of elements which may be queried and/or
* be queried and/or mutated. * mutated.
*/ */
template<typename element> struct stack { template<typename element> struct stack {
virtual ~stack() {}; virtual ~stack() {};
@ -29,8 +28,8 @@ export namespace coral {
/** /**
* Returns a read-only [slice] of the current range values. * Returns a read-only [slice] of the current range values.
* *
* *Note*: the behavior of retaining the returned value past the scope of the source * *Note*: the behavior of retaining the returned value past the scope of the source [stack] or any subsequent
* [stack] or any subsequent modifications to it is implementation-defined. * modifications to it is implementation-defined.
*/ */
virtual slice<element const> as_slice() const = 0; virtual slice<element const> as_slice() const = 0;
@ -39,24 +38,21 @@ export namespace coral {
* *
* The returned [push_result] indicates whether the operation was successful or not. * The returned [push_result] indicates whether the operation was successful or not.
* *
* If the returned [push_result] is anything but [push_result::ok], the [stack] will be * If the returned [push_result] is anything but [push_result::ok], the [stack] will be left in an
* left in an implementation-defined state. * implementation-defined state.
*/ */
virtual push_result push_all(slice<element const> const & source_elements) = 0; virtual push_result push_all(slice<element const> const & source_elements) = 0;
}; };
/** /**
* Last-in-first-out contiguous sequence of `element` values optimized for small numbers of * Last-in-first-out contiguous sequence of `element` values optimized for small numbers of small-sized elements.
* small-sized elements.
* *
* [small_stack] types will default to using an inline array of `init_capacity` at first. After * [small_stack] types will default to using an inline array of `init_capacity` at first. After all local storage
* all local storage has been exhausted, the [small_stack] will switch to a dynamic buffer. * has been exhausted, the [small_stack] will switch to a dynamic buffer. Because of this, it is recommended to use
* Because of this, it is recommended to use larger `init_capacity` values for data which has a * larger `init_capacity` values for data which has a known or approximate upper bound at compile-time. Otherwise,
* known or approximate upper bound at compile-time. Otherwise, the `init_capacity` value may * the `init_capacity` value may be left at its default.
* be left at its default.
* *
* *Note*: the [allocator] referenced in the stack must remain valid for the duration of the * *Note*: the [allocator] referenced in the stack must remain valid for the duration of the stack lifetime.
* stack lifetime.
*/ */
template<typename element, usize init_capacity = 1> struct small_stack : public stack<element> { template<typename element, usize init_capacity = 1> struct small_stack : public stack<element> {
small_stack(allocator * dynamic_allocator) { small_stack(allocator * dynamic_allocator) {
@ -74,8 +70,8 @@ export namespace coral {
/** /**
* Returns a read-only [slice] of the current stack values. * Returns a read-only [slice] of the current stack values.
* *
* *Note*: the returned slice should be considered invalid if any mutable operation is * *Note*: the returned slice should be considered invalid if any mutable operation is performed on the source
* performed on the source [stack] or it is no longer in scope. * [stack] or it is no longer in scope.
*/ */
slice<element const> as_slice() const override { slice<element const> as_slice() const override {
return this->elements.sliced(0, this->filled); return this->elements.sliced(0, this->filled);
@ -93,8 +89,8 @@ export namespace coral {
* *
* The returned [push_result] indicates whether the operation was successful or not. * The returned [push_result] indicates whether the operation was successful or not.
* *
* If the returned [push_result] is anything but [push_result::ok], the stack will be * If the returned [push_result] is anything but [push_result::ok], the stack will be be left in an empty but
* be left in an empty but valid state. * valid state.
* *
* *Note* that [push_all] is recommended when appending many values at once. * *Note* that [push_all] is recommended when appending many values at once.
*/ */
@ -116,8 +112,8 @@ export namespace coral {
* *
* The returned [push_result] indicates whether the operation was successful or not. * The returned [push_result] indicates whether the operation was successful or not.
* *
* If the returned [push_result] is anything but [push_result::ok], the stack will be left * If the returned [push_result] is anything but [push_result::ok], the stack will be left in an empty but valid
* in an empty but valid state. * state.
* *
* *Note* that [push] is recommended when appending singular values. * *Note* that [push] is recommended when appending singular values.
*/ */
@ -139,16 +135,15 @@ export namespace coral {
} }
/** /**
* Attempts to reserve `capacity` number of elements additional space on the stack, forcing * Attempts to reserve `capacity` number of elements additional space on the stack, forcing it to use dynamic
* it to use dynamic memory _even_ if it hasn't exhausted the local buffer yet. * memory _even_ if it hasn't exhausted the local buffer yet.
* *
* The returned [push_result] indicates whether the operation was successful or not. * The returned [push_result] indicates whether the operation was successful or not.
* *
* If the returned [push_result] is anything but [push_result::ok], the stack will be left * If the returned [push_result] is anything but [push_result::ok], the stack will be left in an empty but valid
* in an empty but valid state. * state.
* *
* *Note* that manual invocation is not recommended if the [stack] has a large * *Note* that manual invocation is not recommended if the [stack] has a large `init_capacity` argument.
* `initial_capacity` argument.
*/ */
push_result reserve(usize capacity) { push_result reserve(usize capacity) {
usize const requested_capacity = this->filled + capacity; usize const requested_capacity = this->filled + capacity;
@ -236,8 +231,8 @@ export namespace coral {
} }
/** /**
* Attempts to write `buffer` to the target stack, returning the number of bytes written or * Attempts to write `buffer` to the target stack, returning the number of bytes written or an [io_error] if it
* an [io_error] if it failed to commit `buffer` to the stack memory. * failed to commit `buffer` to the stack memory.
*/ */
expected<usize, io_error> write(slice<u8 const> const & buffer) override { expected<usize, io_error> write(slice<u8 const> const & buffer) override {
switch (this->stack->push_all(buffer)) { switch (this->stack->push_all(buffer)) {