DocWire SDK
DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, document analysis. Offline processing possible for security and confidentiality
error.h
1 /*********************************************************************************************************************************************/
2 /* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3 /* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4 /* document analysis. Offline processing possible for security and confidentiality */
5 /* */
6 /* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7 /* Project homepage: https://github.com/docwire/docwire */
8 /* */
9 /* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10 /*********************************************************************************************************************************************/
11 
12 #ifndef DOCWIRE_ERROR_H
13 #define DOCWIRE_ERROR_H
14 
15 #include "core_export.h"
16 #include "diagnostic_context.h" // IWYU pragma: keep
17 #include <exception>
18 #include "serialization_pair.h" // IWYU pragma: keep
19 #include "stringification.h"
20 #include "source_location.h"
21 #include <tuple>
22 #include <utility>
23 
28 namespace docwire::errors
29 {
30 #ifdef DOCWIRE_ENABLE_SHORT_MACRO_NAMES
31  #define current_location DOCWIRE_CURRENT_LOCATION
32 #endif
33 
74 struct DOCWIRE_CORE_EXPORT base : public std::exception
75 {
78 
84  base(const source_location& location = source_location::current());
85 
93  virtual std::type_info const& context_type(size_t index) const noexcept = 0;
94 
102  virtual std::string context_string(size_t index) const = 0;
103 
107  virtual size_t context_count() const noexcept = 0;
108 
118  virtual const char* what() const noexcept override;
119 };
120 
147 template <typename... T>
148 struct impl : public base
149 {
150 private:
151  template<size_t I>
152  std::string context_string_impl() const
153  {
154  return stringify(std::get<I>(context));
155  }
156 
157  template<size_t I>
158  const std::type_info& context_type_impl() const noexcept
159  {
160  return typeid(std::get<I>(context));
161  }
162 
163  template <size_t... Is>
164  std::string context_string_at(size_t index, std::index_sequence<Is...>) const {
165  using FuncType = std::string(impl::*)() const;
166  static constexpr FuncType funcs[] = { &impl::template context_string_impl<Is>... };
167  return (this->*funcs[index])();
168  }
169 
170  template <size_t... Is>
171  const std::type_info& context_type_at(size_t index, std::index_sequence<Is...>) const noexcept {
172  using FuncType = const std::type_info&(impl::*)() const noexcept;
173  static constexpr FuncType funcs[] = { &impl::template context_type_impl<Is>... };
174  return (this->*funcs[index])();
175  }
176 
177 public:
181  std::tuple<T...> context;
182 
189  explicit impl(const std::tuple<T...>& context_tuple, const source_location& location = source_location::current())
190  : base(location), context(context_tuple)
191  {
192  }
193 
202  std::type_info const& context_type(size_t index) const noexcept override
203  {
204  return context_type_at(index, std::make_index_sequence<sizeof...(T)>{});
205  }
206 
215  std::string context_string(size_t index) const override
216  {
217  return context_string_at(index, std::make_index_sequence<sizeof...(T)>{});
218  }
219 
224  size_t context_count() const noexcept override
225  {
226  return sizeof...(T);
227  }
228 };
229 
230 } // namespace docwire::errors
231 
232 #endif
Provides features for reporting and handling errors with context data using nested exceptions.
Definition: contains_type.h:19
A fallback implementation of source_location for compilers that do not support std::source_location.
Base class for all exceptions in the SDK.
Definition: error.h:75
base(const source_location &location=source_location::current())
Constructs a base object with the current source location.
virtual std::type_info const & context_type(size_t index) const noexcept=0
Get the type information of the context.
virtual std::string context_string(size_t index) const =0
Get the string representation of the context.
virtual size_t context_count() const noexcept=0
Get the number of context items.
source_location location
The source location where the exception was thrown.
Definition: error.h:77
Implementation of the error class for a variadic number of context items.
Definition: error.h:149
std::tuple< T... > context
A tuple holding all context items provided when the error was created.
Definition: error.h:181
size_t context_count() const noexcept override
Get the number of context items.
Definition: error.h:224
std::type_info const & context_type(size_t index) const noexcept override
Get the type information of the context.
Definition: error.h:202
std::string context_string(size_t index) const override
Get the string representation of the context.
Definition: error.h:215
impl(const std::tuple< T... > &context_tuple, const source_location &location=source_location::current())
Constructs an error object from a tuple of context items.
Definition: error.h:189