LCOV - code coverage report
Current view: top level - port - cpl_error_internal.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 6 6 100.0 %
Date: 2026-04-20 19:56:30 Functions: 3 3 100.0 %

          Line data    Source code
       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             : #if defined(GDAL_COMPILATION) || defined(DOXYGEN_SKIP)
      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 that stores details about an emitted error.
      31             :  *
      32             :  * Returned by CPLErrorAccumulator::GetErrors()
      33             :  *
      34             :  * @since 3.11
      35             :  */
      36             : class CPL_DLL CPLErrorHandlerAccumulatorStruct
      37             : {
      38             :   public:
      39             :     /** Error level */
      40             :     CPLErr type;
      41             : 
      42             :     /** Error number */
      43             :     CPLErrorNum no;
      44             : 
      45             :     /** Error message */
      46             :     CPLString msg{};
      47             : 
      48             :     /** Default constructor */
      49             :     CPLErrorHandlerAccumulatorStruct() : type(CE_None), no(CPLE_None)
      50             :     {
      51             :     }
      52             : 
      53             :     /** Constructor */
      54         451 :     CPLErrorHandlerAccumulatorStruct(CPLErr eErrIn, CPLErrorNum noIn,
      55             :                                      const char *msgIn)
      56         451 :         : type(eErrIn), no(noIn), msg(msgIn)
      57             :     {
      58         451 :     }
      59             : };
      60             : 
      61             : /************************************************************************/
      62             : /*                         CPLErrorAccumulator                          */
      63             : /************************************************************************/
      64             : 
      65             : /** Class typically used by a worker thread to store errors emitted by their
      66             :  * worker functions, and replay them in the main thread.
      67             :  *
      68             :  * An instance of CPLErrorAccumulator can be shared by several
      69             :  * threads. Each thread calls InstallForCurrentScope() in its processing
      70             :  * function. The main thread may invoke ReplayErrors() to replay errors (and
      71             :  * warnings).
      72             :  *
      73             :  * @since 3.11
      74             :  */
      75             : class CPL_DLL CPLErrorAccumulator
      76             : {
      77             :   public:
      78             :     /** Constructor */
      79       80061 :     CPLErrorAccumulator() = default;
      80             : 
      81             :     /** Object returned by InstallForCurrentScope() during life-time of which,
      82             :      * errors are redirected to the CPLErrorAccumulator instance.
      83             :      */
      84             :     struct CPL_DLL Context
      85             :     {
      86             :         /*! @cond Doxygen_Suppress */
      87             :         ~Context();
      88             : 
      89             :         Context(const Context &) = delete;
      90             :         Context &operator=(const Context &) = delete;
      91             :         Context(Context &&) = delete;
      92             :         Context &operator=(Context &&) = delete;
      93             : 
      94             :       private:
      95             :         friend class CPLErrorAccumulator;
      96             :         explicit Context(CPLErrorAccumulator &sAccumulator);
      97             :         /*! @endcond Doxygen_Suppress */
      98             :     };
      99             : 
     100             :     /** Install a temporary error handler that will store errors and warnings.
     101             :      */
     102             :     Context InstallForCurrentScope() CPL_WARN_UNUSED_RESULT;
     103             : 
     104             :     /** Return error list. */
     105       78674 :     const std::vector<CPLErrorHandlerAccumulatorStruct> &GetErrors() const
     106             :     {
     107       78674 :         return errors;
     108             :     }
     109             : 
     110             :     /** Replay stored errors. */
     111             :     void ReplayErrors();
     112             : 
     113             :   private:
     114             :     std::mutex mutex{};
     115             :     std::vector<CPLErrorHandlerAccumulatorStruct> errors{};
     116             : 
     117             :     static void CPL_STDCALL Accumulator(CPLErr eErr, CPLErrorNum no,
     118             :                                         const char *msg);
     119             : };
     120             : 
     121             : #endif
     122             : 
     123             : #endif  // CPL_ERROR_INTERNAL_H_INCLUDED

Generated by: LCOV version 1.14