From 0143b6e7148692d02bfa31bb7ed640b4f9a3f80e Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Tue, 28 Nov 2023 13:01:56 +0100 Subject: [PATCH] fixed a buffer detail and added fail function --- include/gp/containers/buffer.hpp | 8 ++++---- include/gp_config.hpp | 24 +++++++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/include/gp/containers/buffer.hpp b/include/gp/containers/buffer.hpp index 1e093f6..130acc3 100644 --- a/include/gp/containers/buffer.hpp +++ b/include/gp/containers/buffer.hpp @@ -213,14 +213,14 @@ namespace gp{ return hold_same_data(this->slice_start(oth.size()), oth); } - optional front() { + optional> front() { if(size() == 0) return nullopt; - return *begin(); + return gp::ref(*slice_start(1).begin()); } - optional back() { + optional> back() { if(size() == 0) return nullopt; - return *slice_end(1).begin(); + return gp::ref(*slice_end(1).begin()); } }; diff --git a/include/gp_config.hpp b/include/gp_config.hpp index 068f6b5..eab16b9 100644 --- a/include/gp_config.hpp +++ b/include/gp_config.hpp @@ -114,9 +114,6 @@ namespace gp_config{ */ constexpr bool has_buffer_bounds = true; - // - // - // /** * @brief A value used to determine the strength used by random number generators * @@ -129,7 +126,7 @@ namespace gp_config{ /** * @brief an exception that represents an assertion failure */ - struct assert_failure{ + struct assert_failure : std::exception { assert_failure(const char* reason) : what_str{reason} {} @@ -141,15 +138,28 @@ namespace gp_config{ * @brief UNUSED */ constexpr size_t assert_buffer_size = 0; + + constexpr auto fail = [] [[noreturn]] (auto reason) noexcept(not has_exceptions) { + if constexpr (has_exceptions) { + throw assert_failure(reason); + } else { + volatile uint8_t n = 0; + { + failure_point: + n = n + 1; + goto failure_point; + } + } + }; /** * @brief an assertion function */ - constexpr auto assertion = [](bool pred, const char* reason) -> void{ + constexpr auto assertion = [](bool pred, const char* reason) + noexcept(std::is_nothrow_invocable_v) -> void { if(!pred) { log_failure(reason); - if constexpr (has_exceptions) - throw assert_failure(reason); + fail(reason); } };