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;
123 [[fallthrough]];
124 case '\r':
125 column_nr = 0;
126 break;
127 case '\t':
128 column_nr /= tab_size;
129 column_nr *= tab_size;
130 break;
131 default:;
132 }
133 }
134 }
135
136 return {line_nr, column_nr};
137 }
138
139private:
140 template<typename It, typename... Args>
141 [[nodiscard]] static constexpr std::string
142 make_what(It first, It last, size_t tab_size, char const *msg, Args const&...args) noexcept
143 {
144 hilet[line_nr, column_nr] = get_line_position(first, last, tab_size);
145 return {std::format("{}:{}: {}", line_nr + 1, column_nr + 1, std::format(msg, args...))};
146 }
147};
148
152public:
154};
155
164public:
166};
167
176public:
178};
179
187public:
189};
190
192public:
194};
195
197public:
199};
200
202public:
204};
205
206class uri_error : public parse_error {
207public:
209};
210
216public:
218};
219
220}} // 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:151
Exception thrown during execution of a dynamic operation.
Definition exception.hpp:163
Exception thrown during I/O on an error.
Definition exception.hpp:175
Exception thrown during an operating system call.
Definition exception.hpp:186
Definition exception.hpp:191
Definition exception.hpp:196
Definition exception.hpp:201
Definition exception.hpp:206
Cancel error is caused by user pressing cancel.
Definition exception.hpp:215
T runtime_error(T... args)