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
contains_type.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_CONTAINS_TYPE_H
13 #define DOCWIRE_CONTAINS_TYPE_H
14 
15 #include "error.h"
16 #include <exception>
17 
18 namespace docwire::errors
19 {
20 
24 template <typename T>
25 bool contains_type(const std::exception& e)
26 {
27  try
28  {
29  const errors::base& error = dynamic_cast<const errors::base&>(e);
30  for (size_t i = 0; i < error.context_count(); ++i)
31  {
32  if (error.context_type(i) == typeid(T))
33  return true;
34  }
35  }
36  catch (const std::bad_cast&)
37  {
38  // e is not errors::base, but may still have nested exceptions
39  }
40 
41  try
42  {
43  std::rethrow_if_nested(e);
44  }
45  catch (const std::exception& nested_ex)
46  {
47  return contains_type<T>(nested_ex);
48  }
49  catch (...) {}
50 
51  return false;
52 }
53 
54 } // namespace docwire::errors
55 
56 #endif // DOCWIRE_CONTAINS_TYPE_H
Provides features for reporting and handling errors with context data using nested exceptions.
Definition: contains_type.h:19
bool contains_type(const std::exception &e)
Checks if the given nested exceptions chain contains a specific type of context.
Definition: contains_type.h:25
Base class for all exceptions in the SDK.
Definition: error.h:75
virtual std::type_info const & context_type(size_t index) const noexcept=0
Get the type information of the context.
virtual size_t context_count() const noexcept=0
Get the number of context items.