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
nested_exception.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_NESTED_EXCEPTION_H
13 #define DOCWIRE_NESTED_EXCEPTION_H
14 
15 #include <exception>
16 
17 namespace docwire::errors
18 {
19 
33 template<typename T>
34 class nested : virtual public std::nested_exception, public T
35 {
36 public:
37 
46  nested(T&& t)
47  : T(std::forward<T>(t))
48  {
49  }
50 
59  nested(const T& t)
60  : T(t)
61  {
62  }
63 };
64 
83 template <typename Inner, typename Outer>
84 auto make_nested(Inner&& inner, Outer&& outer)
85 {
86  try
87  {
88  if constexpr (std::is_same_v<std::remove_cvref_t<Inner>, std::exception_ptr>)
89  std::rethrow_exception(std::forward<Inner>(inner));
90  else
91  throw std::forward<Inner>(inner);
92  }
93  catch(...)
94  {
95  return nested<std::remove_cvref_t<Outer>>(std::forward<Outer>(outer));
96  }
97 }
98 
118 template <typename Inner, typename Outer, typename... Rest>
119 auto make_nested(Inner&& inner, Outer&& outer, Rest&&... rest)
120 {
121  return make_nested(make_nested(std::forward<Inner>(inner), std::forward<Outer>(outer)), std::forward<Rest>(rest)...);
122 }
123 
144 template <typename Inner, typename Outer, typename... Rest>
145 std::exception_ptr make_nested_ptr(Inner&& inner, Outer&& outer, Rest&&... rest)
146 {
147  return std::make_exception_ptr(make_nested(std::forward<Inner>(inner), std::forward<Outer>(outer), std::forward<Rest>(rest)...));
148 }
149 
150 } // namespace docwire::errors
151 
152 #endif
A template class that simplifies the creation of nested exceptions.
nested(const T &t)
Constructs a nested object from a const lvalue reference to an object of type T.
nested(T &&t)
Constructs a nested object from an rvalue reference to an object of type T.
Provides features for reporting and handling errors with context data using nested exceptions.
Definition: contains_type.h:19
auto make_nested(Inner &&inner, Outer &&outer)
Creates a nested exception from an inner exception and an outer exception.
std::exception_ptr make_nested_ptr(Inner &&inner, Outer &&outer, Rest &&... rest)
Creates a pointer to a nested exception from an inner exception and an outer exception.