// // Created by erki on 16/01/24. // #pragma once #include #include #include #include #include #include #include #define TRY(x) ({ \ const auto& _x = (x); \ if (!_x) { \ return std::unexpected(_x.error()); \ } \ _x.value(); \ }) #define TRY_ESP(x) ({ \ const esp_err_t& _x = (x); \ if (_x != ESP_OK) \ return std::unexpected(_x); \ _x; \ }) [[noreturn]] inline void abortWithError(const char* error, const std::source_location location = std::source_location::current()) { esp_rom_printf("abortWithError called, reason: %s\n", error); esp_rom_printf("file: \"%s\" line %d\n", location.file_name(), location.line()); if (location.function_name() != nullptr) esp_rom_printf("func: %s\n", location.function_name()); abort(); } template decltype(auto) unwrapOrAbort(Expected&& expected, const std::source_location location = std::source_location::current()) { if (!expected.has_value()) abortWithError("Unwrapped an errored expected.", location); else return std::forward(expected).value(); } template decltype(auto) unwrapOr(Expected&& expected, F&& f) { if (!expected.has_value()) std::invoke(std::forward(f), expected.error()); else return std::forward(expected).value(); std::unreachable(); }