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]] std::string get_last_error_message() noexcept;
36
50class parse_error : public std::runtime_error {
51public:
53
62 template<typename It, typename... Args>
63 [[nodiscard]] constexpr parse_error(It first, It last, int tab_size, char const *msg, Args const&...args) noexcept :
64 runtime_error(make_what(first, last, tab_size, msg, args...))
65 {
66 }
67
75 template<typename It, typename... Args>
76 [[nodiscard]] constexpr parse_error(It first, It last, char const *msg, Args const&...args) noexcept :
77 parse_error(first, last, 8, msg, args...)
78 {
79 }
80
88 template<typename It>
89 [[nodiscard]] static constexpr std::pair<size_t, size_t> get_line_position(It first, It last, size_t tab_size) noexcept
90 {
91 auto line_nr = 0_uz;
92 auto column_nr = 0_uz;
93
94 auto c32 = char32_t{};
95 auto continue_count = int{};
96 while (first != last) {
97 hilet c8 = char_cast<char8_t>(*first++);
98
99 if ((c8 & 0xc0) == 0x80) {
100 --continue_count;
101 c32 <<= 6;
102 c32 |= c8 & 0x3f;
103
104 } else if (c8 & 0x80) {
105 continue_count = std::countl_one(c8) - 1;
106 c32 = c8 & (0b00'111'111 >> continue_count);
107
108 } else {
109 continue_count = 0;
110 c32 = c8;
111 }
112
113 if (not continue_count) {
114 ++column_nr;
115 switch (c32) {
116 case '\n':
117 case '\v':
118 case '\f':
119 case U'\u0085':
120 case U'\u2028':
121 case U'\u2029':
122 ++line_nr; [[fallthrough]]
123 case '\r':
124 column_nr = 0;
125 break;
126 case '\t':
127 column_nr /= tab_size;
128 column_nr *= tab_size;
129 break;
130 default:;
131 }
132 }
133 }
134
135 return {line_nr, column_nr};
136 }
137
138private:
139 template<typename It, typename... Args>
140 [[nodiscard]] static constexpr std::string
141 make_what(It first, It last, size_t tab_size, char const *msg, Args const&...args) noexcept
142 {
143 hilet[line_nr, column_nr] = get_line_position(first, last, tab_size);
144 return {std::format("{}:{}: {}", line_nr + 1, column_nr + 1, std::format(msg, args...))};
145 }
146};
147
151public:
153};
154
163public:
165};
166
175public:
177};
178
186public:
188};
189
191public:
193};
194
196public:
198};
199
201public:
203};
204
205class uri_error : public parse_error {
206public:
208};
209
215public:
217};
218
219}} // 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() noexcept
Get the OS error message from the last error received on this thread.
Exception thrown during parsing on an error.
Definition exception.hpp:50
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:76
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:89
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:63
Exception thrown when an item was not found.
Definition exception.hpp:150
Exception thrown during execution of a dynamic operation.
Definition exception.hpp:162
Exception thrown during I/O on an error.
Definition exception.hpp:174
Exception thrown during an operating system call.
Definition exception.hpp:185
Definition exception.hpp:190
Definition exception.hpp:195
Definition exception.hpp:200
Definition exception.hpp:205
Cancel error is caused by user pressing cancel.
Definition exception.hpp:214
T runtime_error(T... args)