/src/gdal/port/cpl_error_internal.h
Line | Count | Source (jump to first uncovered line) |
1 | | /********************************************************************** |
2 | | * |
3 | | * Name: cpl_error_internal.h |
4 | | * Project: CPL - Common Portability Library |
5 | | * Purpose: CPL Error handling |
6 | | * Author: Even Rouault, <even.rouault at spatialys.com> |
7 | | * |
8 | | ********************************************************************** |
9 | | * Copyright (c) 2019, Even Rouault, <even.rouault at spatialys.com> |
10 | | * |
11 | | * SPDX-License-Identifier: MIT |
12 | | ****************************************************************************/ |
13 | | |
14 | | #ifndef CPL_ERROR_INTERNAL_H_INCLUDED |
15 | | #define CPL_ERROR_INTERNAL_H_INCLUDED |
16 | | |
17 | | #ifdef GDAL_COMPILATION |
18 | | // internal only |
19 | | |
20 | | #include "cpl_error.h" |
21 | | #include "cpl_string.h" |
22 | | |
23 | | #include <mutex> |
24 | | #include <vector> |
25 | | |
26 | | /************************************************************************/ |
27 | | /* CPLErrorHandlerAccumulatorStruct */ |
28 | | /************************************************************************/ |
29 | | |
30 | | class CPL_DLL CPLErrorHandlerAccumulatorStruct |
31 | | { |
32 | | public: |
33 | | CPLErr type; |
34 | | CPLErrorNum no; |
35 | | CPLString msg{}; |
36 | | |
37 | | CPLErrorHandlerAccumulatorStruct() : type(CE_None), no(CPLE_None) |
38 | 0 | { |
39 | 0 | } |
40 | | |
41 | | CPLErrorHandlerAccumulatorStruct(CPLErr eErrIn, CPLErrorNum noIn, |
42 | | const char *msgIn) |
43 | 0 | : type(eErrIn), no(noIn), msg(msgIn) |
44 | 0 | { |
45 | 0 | } |
46 | | }; |
47 | | |
48 | | /************************************************************************/ |
49 | | /* CPLErrorAccumulator */ |
50 | | /************************************************************************/ |
51 | | |
52 | | /** Class typically used by a worker thread to store errors emitted by their |
53 | | * worker functions, and replay them in the main thread. |
54 | | * |
55 | | * An instance of CPLErrorAccumulator can be shared by several |
56 | | * threads. Each thread calls InstallForCurrentScope() in its processing |
57 | | * function. The main thread may invoke ReplayErrors() to replay errors (and |
58 | | * warnings). |
59 | | * |
60 | | * @since 3.11 |
61 | | */ |
62 | | class CPL_DLL CPLErrorAccumulator |
63 | | { |
64 | | public: |
65 | | /** Constructor */ |
66 | 0 | CPLErrorAccumulator() = default; |
67 | | |
68 | | struct CPL_DLL Context |
69 | | { |
70 | | ~Context(); |
71 | | }; |
72 | | |
73 | | /** Install a temporary error handler that will store errors and warnings. |
74 | | */ |
75 | | Context InstallForCurrentScope() CPL_WARN_UNUSED_RESULT; |
76 | | |
77 | | /** Return error list. */ |
78 | | const std::vector<CPLErrorHandlerAccumulatorStruct> &GetErrors() const |
79 | 0 | { |
80 | 0 | return errors; |
81 | 0 | } |
82 | | |
83 | | /** Replay stored errors. */ |
84 | | void ReplayErrors(); |
85 | | |
86 | | private: |
87 | | std::mutex mutex{}; |
88 | | std::vector<CPLErrorHandlerAccumulatorStruct> errors{}; |
89 | | |
90 | | static void CPL_STDCALL Accumulator(CPLErr eErr, CPLErrorNum no, |
91 | | const char *msg); |
92 | | }; |
93 | | |
94 | | #endif |
95 | | |
96 | | #endif // CPL_ERROR_INTERNAL_H_INCLUDED |