HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
exception.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
8#include "utility.hpp"
9#include <exception>
10#include <stdexcept>
11#include <atomic>
12#include <bit>
13#include <format>
14
15#pragma once
16
17namespace hi { inline namespace v1 {
18
22
30#define hi_set_terminate_message(...) \
31 ::hi::terminate_message.store(__FILE__ ":" hi_stringify(__LINE__) ":" __VA_ARGS__, std::memory_order::relaxed)
32
35[[nodiscard]] uint32_t get_last_error_code() noexcept;
36
42[[nodiscard]] std::string get_last_error_message(uint32_t error_code);
43
48[[nodiscard]] std::string get_last_error_message();
49
63class parse_error : public std::runtime_error {
64public:
66
75 template<typename It, typename... Args>
76 [[nodiscard]] constexpr parse_error(It first, It last, int tab_size, char const *msg, Args const&...args) noexcept :
77 runtime_error(make_what(first, last, tab_size, msg, args...))
78 {
79 }
80
88 template<typename It, typename... Args>
89 [[nodiscard]] constexpr parse_error(It first, It last, char const *msg, Args const&...args) noexcept :
90 parse_error(first, last, 8, msg, args...)
91 {
92 }
93
101 template<typename It>
102 [[nodiscard]] static constexpr std::pair<size_t, size_t> get_line_position(It first, It last, size_t tab_size) noexcept
103 {
104 auto line_nr = 0_uz;
105 auto column_nr = 0_uz;
106
107 auto c32 = char32_t{};
108 auto continue_count = int{};
109 while (first != last) {
110 hilet c8 = char_cast<char8_t>(*first++);
111
112 if ((c8 & 0xc0) == 0x80) {
113 --continue_count;
114 c32 <<= 6;
115 c32 |= c8 & 0x3f;
116
117 } else if (c8 & 0x80) {
118 continue_count = std::countl_one(c8) - 1;
119 c32 = c8 & (0b00'111'111 >> continue_count);
120
121 } else {
122 continue_count = 0;
123 c32 = c8;
124 }
125
126 if (not continue_count) {
127 ++column_nr;
128 switch (c32) {
129 case '\n':
130 case '\v':
131 case '\f':
132 case U'\u0085':
133 case U'\u2028':
134 case U'\u2029':
135 ++line_nr;
136 [[fallthrough]];
137 case '\r':
138 column_nr = 0;
139 break;
140 case '\t':
141 column_nr /= tab_size;
142 column_nr *= tab_size;
143 break;
144 default:;
145 }
146 }
147 }
148
149 return {line_nr, column_nr};
150 }
151
152private:
153 template<typename It, typename... Args>
154 [[nodiscard]] static constexpr std::string
155 make_what(It first, It last, size_t tab_size, char const *msg, Args const&...args) noexcept
156 {
157 hilet[line_nr, column_nr] = get_line_position(first, last, tab_size);
158 return {std::format("{}:{}: {}", line_nr + 1, column_nr + 1, std::format(msg, args...))};
159 }
160};
161
165public:
167};
168
177public:
179};
180
189public:
191};
192
200public:
202};
203
205public:
207};
208
210public:
212};
213
215public:
217};
218
219class uri_error : public parse_error {
220public:
222};
223
229public:
231};
232
233}} // namespace hi::v1
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
std::atomic< char const * > terminate_message
Message to show when the application is terminated.
Definition exception.hpp:21
std::string get_last_error_message()
Get the OS error message from the last error received on this thread.
uint32_t get_last_error_code() noexcept
Get the OS error code from the last error received on this thread.
Exception thrown during parsing on an error.
Definition exception.hpp:63
constexpr parse_error(It first, It last, char const *msg, Args const &...args) noexcept
Create a parse error at a specific point in UTF-8 encoded text.
Definition exception.hpp:89
static constexpr std::pair< size_t, size_t > get_line_position(It first, It last, size_t tab_size) noexcept
Get the line and column position of an iterator in a UTF-8 string.
Definition exception.hpp:102
constexpr parse_error(It first, It last, int tab_size, char const *msg, Args const &...args) noexcept
Create a parse error at a specific point in UTF-8 encoded text.
Definition exception.hpp:76
Exception thrown when an item was not found.
Definition exception.hpp:164
Exception thrown during execution of a dynamic operation.
Definition exception.hpp:176
Exception thrown during I/O on an error.
Definition exception.hpp:188
Exception thrown during an operating system call.
Definition exception.hpp:199
Definition exception.hpp:204
Definition exception.hpp:209
Definition exception.hpp:214
Definition exception.hpp:219
Cancel error is caused by user pressing cancel.
Definition exception.hpp:228
T runtime_error(T... args)