From 98fb389ff07358382d2d2d7eff6f6ec700770bc8 Mon Sep 17 00:00:00 2001 From: kayomn Date: Sun, 26 Feb 2023 01:19:16 +0000 Subject: [PATCH] Reformat coral library --- source/coral.cpp | 188 +++++++++++++++++++++++------------------------ 1 file changed, 90 insertions(+), 98 deletions(-) diff --git a/source/coral.cpp b/source/coral.cpp index 312627f..40875ae 100644 --- a/source/coral.cpp +++ b/source/coral.cpp @@ -12,8 +12,8 @@ export namespace coral { /** * Triggers safety-checked behavior in debug mode. * - * In release mode, the compiler can use this function as a marker to optimize out safety- - * checked logic branches that should never be executed. + * In release mode, the compiler can use this function as a marker to optimize out safety-checked logic branches + * that should never be executed. */ [[noreturn]] void unreachable() { __builtin_unreachable(); @@ -59,37 +59,38 @@ export namespace coral { virtual ~allocator() {}; /** - * If `allocation` is `nullptr`, the allocator will attempt to allocate a new memory block - * of `requested_size` bytes. Otherwise, the allocator will attempt to reallocate - * `allocation` to be `request_size` bytes in size. + * If `allocation` is `nullptr`, the allocator will attempt to allocate a new memory block of `requested_size` + * bytes. Otherwise, the allocator will attempt to reallocate `allocation` to be `request_size` bytes in size. * - * The returned address will point to a dynamically allocated buffer of `requested_size` if - * the operation was successful, otherwise `nullptr`. + * The returned address will point to a dynamically allocated buffer of `requested_size` if the operation was + * successful, otherwise `nullptr`. * - * *Note*: If the returned address is a non-`nullptr`, it should be deallocated prior to - * program exit. This may be achieved through either [deallocate] or implementation- - * specific allocator functionality. + * *Note*: If the returned address is a non-`nullptr`, it should be deallocated prior to program exit. This may + * be achieved through either [deallocate] or implementation-specific allocator functionality. * - * *Note*: Attempting to pass a non-`nullptr` `allocation` address not allocated by the - * allocator *will* result in erroneous implementation-behavior. + * *Note*: Attempting to pass a non-`nullptr` `allocation` address not allocated by the allocator *will* result + * in erroneous implementation-behavior. * * *Note*: After invocation, `allocation` should be considered an invalid memory address. */ [[nodiscard]] virtual u8 * reallocate(u8 * allocation, usize requested_size) = 0; /** - * If `allocation` points to a non-`nullptr` address, the allocator will deallocate it. - * Otherwise, the function has no side-effects. + * If `allocation` points to a non-`nullptr` address, the allocator will deallocate it. Otherwise, the function + * has no side-effects. * - * *Note* that attempting to pass a non-`nullptr` `allocation` address not allocated by the - * allocator *will* result in erroneous implementation-behavior. + * *Note* that attempting to pass a non-`nullptr` `allocation` address not allocated by the allocator *will* + * result in erroneous implementation-behavior. */ virtual void deallocate(void * allocation) = 0; }; /** - * Length-signed pointer type that describes how many elements of `type` it references, - * providing a type-safe wrapper for passing arrays and zero-terminated strings to functions. + * Length-signed pointer type that describes how many elements of `type` it references, providing a type-safe + * wrapper for passing arrays and zero-terminated strings to functions. + * + * **Note**: slices take no ownership of their data, making it the responsibility of the caller to manage the + * lifetime of any data referenced by it. */ template struct slice { /** @@ -140,8 +141,7 @@ export namespace coral { * * The returned view is constant to protect against inadvertant memory corruption. * - * *Note* the returned value has no guarantees about the validity of any specific character - * encoding set. + * *Note* the returned value has no guarantees about the validity of any specific character encoding set. */ slice as_chars() const { return {reinterpret_cast(this->pointer), this->length * sizeof(type)}; @@ -162,11 +162,11 @@ export namespace coral { } /** - * Returns a new slice with the base-pointer offset by `index` elements and a length of - * `range` elements from `index`. + * Returns a new slice with the base-pointer offset by `index` elements and a length of `range` elements from + * `index`. * - * *Note* that attempting to slice with an `index` or `range` outside of the existing slice - * bounds will result in safety-checked behavior. + * *Note* that attempting to slice with an `index` or `range` outside of the existing slice bounds will result + * in safety-checked behavior. */ constexpr slice sliced(usize index, usize range) const { if ((this->length <= index) || ((range + index) > this->length)) unreachable(); @@ -205,7 +205,9 @@ export namespace coral { /** * Returns `value` clamped between the range of `min_value` and `max_value` (inclusive). */ - template constexpr scalar clamp(scalar const & value, scalar const & min_value, scalar const & max_value) { + template constexpr scalar clamp(scalar const & value, + scalar const & min_value, scalar const & max_value) { + return max(min_value, min(max_value, value)); } @@ -218,11 +220,10 @@ export namespace coral { } /** - * Allocates and initializes a type of `requested_size` in `buffer`, returning its base pointer. As - * a result of accepting a pre-allocated buffer, invocation does not allocate any dynamic memory. + * Allocates and initializes a type of `requested_size` in `buffer`, returning its base pointer. As a result of + * accepting a pre-allocated buffer, invocation does not allocate any dynamic memory. * - * *Note*: passing an `buffer` smaller than `requested_size` will result in safety-checked - * behavior. + * *Note*: passing an `buffer` smaller than `requested_size` will result in safety-checked behavior. */ export void * operator new(coral::usize requested_size, coral::slice const & buffer) { if (buffer.length < requested_size) coral::unreachable(); @@ -231,12 +232,10 @@ export void * operator new(coral::usize requested_size, coral::slice } /** - * Allocates and initializes a series of types at `requested_size` in `buffer`, returning the base - * pointer. As a result of accepting a pre-allocated buffer, invocation does not allocate any - * dynamic memory. + * Allocates and initializes a series of types at `requested_size` in `buffer`, returning the base pointer. As a result + * of accepting a pre-allocated buffer, invocation does not allocate any dynamic memory. * - * *Note*: passing an `buffer` smaller than `requested_size` will result in safety-checked - * behavior. + * *Note*: passing an `buffer` smaller than `requested_size` will result in safety-checked behavior. */ export void * operator new[](coral::usize requested_size, coral::slice const & buffer) { if (buffer.length < requested_size) coral::unreachable(); @@ -247,9 +246,8 @@ export void * operator new[](coral::usize requested_size, coral::slice concept function_pointer = - requires (type value, arguments... value_arguments) { - {*value}; - {value(value_arguments...)}; + template concept function_pointer = + requires (callable callable_value, arguments... value_arguments) { + {*callable_value}; + {callable_value(value_arguments...)}; }; - template concept functor = - requires (type value, arguments... value_arguments) { - {value.operator()(value_arguments...)}; + template concept functor = + requires (callable callable_value, arguments... value_arguments) { + {callable_value.operator()(value_arguments...)}; }; template struct closure; /** - * Type-erasing view wrapper for both function and functor types that have a call operator with - * a return value matching `return_value` and arguments matching `argument_values`. + * Type-erasing view wrapper for both function and functor types that have a call operator with a return value + * matching `result` and arguments matching `arguments`. * * A closure may be constructed from either of the following inputs: * @@ -314,12 +311,12 @@ export namespace coral { * * * An L or R-value functor reference. * - * **Note**: closures take no ownership of allocated memory, making it the responsibility of - * the caller to manage the lifetime of any functor assigned to it. + * **Note**: closures take no ownership of their data, making it the responsibility of the caller to manage the + * lifetime of any functor assigned to it. */ - template struct closure { + template struct closure { template closure(callable call) requires function_pointer { - this->dispatch = [](void * context, arguments... dispatch_arguments) -> returns { + this->dispatch = [](void * context, arguments... dispatch_arguments) -> result { return (reinterpret_cast(context))(dispatch_arguments...); }; @@ -327,7 +324,7 @@ export namespace coral { } template closure(callable && call) requires functor { - this->dispatch = [](void * context, arguments... dispatch_arguments) -> returns { + this->dispatch = [](void * context, arguments... dispatch_arguments) -> result { return (*reinterpret_cast(context))(dispatch_arguments...); }; @@ -335,7 +332,7 @@ export namespace coral { } template closure(callable & call) requires functor { - this->dispatch = [](void * context, arguments... dispatch_arguments) -> returns { + this->dispatch = [](void * context, arguments... dispatch_arguments) -> result { return (*reinterpret_cast(context))(dispatch_arguments...); }; @@ -344,31 +341,31 @@ export namespace coral { closure(closure const &) = delete; - returns operator()(arguments const &... call_arguments) const { + result operator()(arguments const &... call_arguments) const { return this->dispatch(this->context, call_arguments...); } private: void * context; - returns(* dispatch)(void *, arguments...); + result(* dispatch)(void *, arguments...); }; /** - * Monadic container for a descriminating union of either `value_element` or `error_element`. + * Monadic container for a descriminating union of either `expects` or `errors`. */ - template struct [[nodiscard]] expected { - expected(value_element const & value) : buffer{0} { - (*reinterpret_cast(this->buffer)) = value; + template struct [[nodiscard]] expected { + expected(expects const & value) : buffer{0} { + (*reinterpret_cast(this->buffer)) = value; this->buffer[buffer_size] = 1; } - expected(error_element const & error) : buffer{0} { - (*reinterpret_cast(this->buffer)) = error; + expected(errors const & error) : buffer{0} { + (*reinterpret_cast(this->buffer)) = error; } /** - * Returns `true` if the optional contains a value, otherwise `false` if it holds an error. + * Returns `true` if the optional contains the expected value, otherwise `false` if it holds an error. */ bool is_ok() const { return this->buffer[buffer_size]; @@ -377,66 +374,64 @@ export namespace coral { /** * Returns a reference to the contained value. * - * *Note*: attempting to access the value of an erroneous expected will trigger safety- - * checked behavior. + * *Note*: attempting to access the value of an erroneous expected will trigger safety-checked behavior. */ - value_element & value() { + expects & value() { if (!this->is_ok()) unreachable(); - return *reinterpret_cast(this->buffer); + return *reinterpret_cast(this->buffer); } /** * Returns the contained value. * - * *Note*: attempting to access the value of an erroneous expected will trigger safety- - * checked behavior. + * *Note*: attempting to access the value of an erroneous expected will trigger safety-checked behavior. */ - value_element const & value() const { + expects const & value() const { if (!this->is_ok()) unreachable(); - return *reinterpret_cast(this->buffer); + return *reinterpret_cast(this->buffer); } /** * Returns a reference to the contained error. * - * *Note*: attempting to access the error of a non-erroneous expected will trigger safety- - * checked behavior. + * *Note*: attempting to access the error of a non-erroneous expected will trigger safety-checked behavior. */ - error_element & error() { + errors & error() { if (this->is_ok()) unreachable(); - return *reinterpret_cast(this->buffer); + return *reinterpret_cast(this->buffer); } /** * Returns the contained error. * - * *Note*: attempting to access the error of a non-erroneous expected will trigger safety- - * checked behavior. + * *Note*: attempting to access the error of a non-erroneous expected will trigger safety-checked behavior. */ - error_element const & error() const { + errors const & error() const { if (this->is_ok()) unreachable(); - return *reinterpret_cast(this->buffer); + return *reinterpret_cast(this->buffer); } - template expected map(closure const & apply) const { + /** + * + */ + template expected map(closure const & apply) const { if (this->is_ok()) return apply(this->value()); return this->error(); } private: - static constexpr usize buffer_size = max(sizeof(value_element), sizeof(error_element)); + static constexpr usize buffer_size = max(sizeof(expects), sizeof(errors)); u8 buffer[buffer_size + 1]; }; /** - * Errors that may occur while executing an opaque I/O operation via the `readable` and - * `writable` type aliases. + * Errors that may occur while executing an opaque I/O operation via the `readable` and `writable` type aliases. */ enum class io_error { unavailable, @@ -449,8 +444,7 @@ export namespace coral { virtual ~reader() {} /** - * Attempts to fill `data` with whatever data the reader has to offer, returning the number - * of bytes actually read. + * Attempts to fill `data` with whatever the reader has to offer, returning the number of bytes actually read. * * Should the read operation fail for any reason, a [io_error] is returned instead. */ @@ -464,8 +458,7 @@ export namespace coral { virtual ~writer() {} /** - * Attempts to write `data` out to the writer, returning the number of bytes actually - * written. + * Attempts to write `data` out to the writer, returning the number of bytes actually written. * * Should the write operation fail for any reason, a [io_error] is returned instead. */ @@ -516,8 +509,7 @@ export namespace coral { } /** - * Tests the equality of `a` against `b`, returning `true` if they contain identical bytes, - * otherwise `false`. + * Tests the equality of `a` against `b`, returning `true` if they contain identical bytes, otherwise `false`. */ constexpr bool equals(slice const & a, slice const & b) { if (a.length != b.length) return false;