LCOV - code coverage report
Current view: top level - error - error.hpp (source / functions) Coverage Total Hit
Test: mxlib Lines: 71.4 % 14 10
Test Date: 2026-02-19 16:58:26 Functions: 100.0 % 3 3

            Line data    Source code
       1              : /** \file error.hpp
       2              :  * \brief The mxlib error reporting system.
       3              :  * \ingroup error_handling_files
       4              :  *
       5              :  */
       6              : 
       7              : //***********************************************************************//
       8              : // Copyright 2025 Jared R. Males (jaredmales@gmail.com)
       9              : //
      10              : // This file is part of mxlib.
      11              : //
      12              : // mxlib is free software: you can redistribute it and/or modify
      13              : // it under the terms of the GNU General Public License as published by
      14              : // the Free Software Foundation, either version 3 of the License, or
      15              : // (at your option) any later version.
      16              : //
      17              : // mxlib is distributed in the hope that it will be useful,
      18              : // but WITHOUT ANY WARRANTY; without even the implied warranty of
      19              : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      20              : // GNU General Public License for more details.
      21              : //
      22              : // You should have received a copy of the GNU General Public License
      23              : // along with mxlib.  If not, see <http://www.gnu.org/licenses/>.
      24              : //***********************************************************************//
      25              : 
      26              : #ifndef error_error_hpp
      27              : #define error_error_hpp
      28              : 
      29              : #include <source_location>
      30              : #include <iostream>
      31              : #include <string>
      32              : 
      33              : #include "error_t.hpp"
      34              : 
      35              : namespace mx
      36              : {
      37              : 
      38              : /// Check if an error_t code is an error
      39              : /** Treats errors as `true/false`, where `false` means equal to `error_t::noerror` and all other values are `true`.
      40              :  *  So `not` returns `true` if \p errc is equal to `error_t::noerror`.  So one can
      41              :  *  use `!!` to check if an code is an error.  Example:
      42              :  *  \code
      43              :  *   error_t errc = error_t::noerror;
      44              :  *   if(!!errc) // will be false, no error
      45              :  *   {
      46              :  *       //handle errors
      47              :  *   }
      48              :  *
      49              :  *   errc = error_t::error;
      50              :  *   if(!!errc) // will be true, error
      51              :  *   {
      52              :  *       //handle errors
      53              :  *   }
      54              :  * \endcode
      55              :  *
      56              :  * \ingroup error_handling
      57              :  */
      58            8 : inline bool operator!( const error_t &errc /**< [in] the error_t code to check */ )
      59              : {
      60            8 :     return ( errc == error_t::noerror );
      61              : }
      62              : 
      63              : /// Check if an error_t code is an error
      64              : /** Treats errors as `true/false`, where `false` means equal to `error_t::noerror` and all other values are `true`.
      65              :  *  Example:
      66              :  *  \code
      67              :  *   error_t errc = error_t::noerror;
      68              :  *   if(errc == true) // will be false, no error
      69              :  *   {
      70              :  *       //handle errors
      71              :  *   }
      72              :  *
      73              :  *   errc = error_t::error;
      74              :  *   if(errc == true) // will be true, error
      75              :  *   {
      76              :  *       //handle errors
      77              :  *   }
      78              :  * \endcode
      79              :  *
      80              :  * \ingroup error_handling
      81              :  */
      82              : inline bool operator==( const error_t &errc, /**< [in] the error_t code to check */
      83              :                         bool tf              /**< [in] the value to compare to */
      84              : )
      85              : {
      86              :     return ( ( errc != error_t::noerror ) == tf );
      87              : }
      88              : 
      89              : /// Check if an error_t code is not an error
      90              : /** Treats errors as `true/false`, where `false` means equal to `error_t::noerror` and all other values are `true`.
      91              :  *  Example:
      92              :  *  \code
      93              :  *   error_t errc = error_t::noerror;
      94              :  *   if(errc != false) // will be false, no error
      95              :  *   {
      96              :  *       //handle errors
      97              :  *   }
      98              :  *
      99              :  *   errc = error_t::error;
     100              :  *   if(errc != false) // will be true, error
     101              :  *   {
     102              :  *       //handle errors
     103              :  *   }
     104              :  * \endcode
     105              :  *
     106              :  * \ingroup error_handling
     107              :  */
     108              : inline bool operator!=( const error_t &errc, /**< [in] the error_t code to check */
     109              :                         bool tf              /**< [in] the value to compare to */
     110              : )
     111              : {
     112              :     return ( ( errc != error_t::noerror ) != tf );
     113              : }
     114              : 
     115              : /// Check if an error_t code is an error
     116              : /** Treats errors as `true/false`, where `false` means equal to `error_t::noerror` and all other values are `true`.
     117              :  *
     118              :  * \ingroup error_handling
     119              :  */
     120              : inline bool isError( const error_t &errc /**< [in] the error_t code to check */ )
     121              : {
     122              :     return ( errc != error_t::noerror );
     123              : }
     124              : 
     125              : /** \defgroup error_verbosity Error Report Verbosity
     126              :  * \ingroup error_handling
     127              :  *
     128              :  * Error reports can be controlled with a verbosity template parameter,
     129              :  * usually called `verboseT`.  The types in this namespace are the standard
     130              :  * ones supported by `mxlib`.
     131              :  *
     132              :  */
     133              : 
     134              : /// Namespace for the error reporting verbosity levels
     135              : /**
     136              :  * \ingroup error_verbosity
     137              :  */
     138              : namespace verbose
     139              : {
     140              : 
     141              : /// Verbosity level 0, no reports are generated or printed to stderr.
     142              : /** o stands for off
     143              :  *
     144              :  * \ingroup error_verbosity
     145              :  */
     146              : struct o
     147              : {
     148              :     static constexpr int level = 0;
     149              : };
     150              : 
     151              : /// Verbosity level 1.  Minimal reports with no source location information.
     152              : /** A typical example:
     153              :  * \verbatim
     154              :   dirnotfound: /tmp/fileUtils_test/dirnf was not found.
     155              :  * \endverbatim:
     156              :  *
     157              :  * \ingroup error_verbosity
     158              :  */
     159              : struct v
     160              : {
     161              :     static constexpr int level = 1;
     162              : };
     163              : 
     164              : /// Verbosity level 2.  Additional information is provided, including source file and line
     165              : /** A typical example:
     166              :  * \verbatim
     167              :   The directory was not found (dirnotfound): /tmp/fileUtils_test/dirnf was not found. [ioutils/fileUtils.cpp 186]
     168              :  * \endverbatim
     169              :  *
     170              :  * \ingroup error_verbosity
     171              :  */
     172              : struct vv
     173              : {
     174              :     static constexpr int level = 2;
     175              : };
     176              : 
     177              : /// Verbosity level 3.  A full report is provided.
     178              : /** A typical example:
     179              :  * \verbatim
     180              :   An error has occurred in mxlib:
     181              :         error: The directory was not found (dirnotfound)
     182              :   explanation: /tmp/fileUtils_test/dirnf was not found
     183              :       in file: ioutils/fileUtils.cpp
     184              :       at line: 186
     185              :        source: mx::error_t mx::ioutils::impl::getFileNames(std::vector<std::__cxx11::basic_string<char> >&,
     186              :   const std::string&, const std::string&, const std::string&, const std::string&) [with verboseT =
     187              :   mx::verbose::vvv; std::string = std::__cxx11::basic_string<char>]
     188              :   \endverbatim
     189              :  *
     190              :  * \ingroup error_verbosity
     191              :  */
     192              : struct vvv
     193              : {
     194              :     static constexpr int level = 3;
     195              : };
     196              : 
     197              : #ifndef MXLIB_DEFAULT_VERBOSITY
     198              :     #define MXLIB_DEFAULT_VERBOSITY vv
     199              : #endif
     200              : 
     201              : /// The default verbosity
     202              : typedef MXLIB_DEFAULT_VERBOSITY d;
     203              : 
     204              : } // namespace verbose
     205              : 
     206              : /** \defgroup error_internal Internal Error Reporting
     207              :  * \ingroup error_handling
     208              :  *
     209              :  * These functions are meant to be used by mxlib itself.  You should use the non-`internal` versions,
     210              :  * which don't start with `mxlib_`, in most
     211              :  * other cases.
     212              :  */
     213              : 
     214              : /// Namespace for the internal error reporting functions.
     215              : /**
     216              :  * \ingroup error_internal
     217              :  */
     218              : namespace internal
     219              : {
     220              : 
     221              : /// Format a report given an mxlib \ref error_t code and explanation.
     222              : /** What is included depends on the verbosity level set by the template parameter
     223              :  *  This is for internal `mxlib` use, it includes `mxlib` in the vvv report.
     224              :  *
     225              :  * \tparam verboseT sets the verbosity level based on its `level` member.
     226              :  *
     227              :  * \returns the formatted message
     228              :  *
     229              :  * \ingroup error_internal
     230              :  */
     231              : template <class verboseT=verbose::d>
     232              : std::string mxlib_error_message( const error_t &code,     /**< [in] is an mx::error_t error code*/
     233              :                                  const std::string &expl, /**< [in] [opt] if more information can be provided,
     234              :                                                                                use this to inform the user.*/
     235              :                                  const std::source_location &loc /**< [in] [opt] source location */
     236              :                                  = std::source_location::current() );
     237              : 
     238              : /// Specialization of \ref mxlib_error_message for \ref verbose::o
     239              : /**
     240              :  * \ingroup error_internal
     241              :  */
     242              : template <>
     243              : std::string
     244              : mxlib_error_message<mx::verbose::o>( const error_t &code, const std::string &expl, const std::source_location &loc );
     245              : 
     246              : // note: keep the mx:: in mx::verbose:: so doxygen matches things up in test cases
     247              : 
     248              : /// Specialization of \ref mxlib_error_message for \ref verbose::v
     249              : /**
     250              :  * \ingroup error_internal
     251              :  */
     252              : template <>
     253              : std::string
     254              : mxlib_error_message<mx::verbose::v>( const error_t &code, const std::string &expl, const std::source_location &loc );
     255              : 
     256              : /// Specialization of \ref mxlib_error_message for \ref verbose::vv
     257              : /**
     258              :  * \ingroup error_internal
     259              :  */
     260              : template <>
     261              : std::string
     262              : mxlib_error_message<mx::verbose::vv>( const error_t &code, const std::string &expl, const std::source_location &loc );
     263              : 
     264              : /// Specialization of \ref mxlib_error_message for \ref verbose::vvv
     265              : /**
     266              :  * \ingroup error_internal
     267              :  */
     268              : template <>
     269              : std::string
     270              : mxlib_error_message<mx::verbose::vvv>( const error_t &code, const std::string &expl, const std::source_location &loc );
     271              : 
     272              : /// Format a report given an mxlib \ref error_t code.
     273              : /** What is included depends on the verbosity level set by the template parameter
     274              :  *  This is for internal mxlib use, it includes `mxlib` in the vvv report.
     275              :  *
     276              :  * \tparam verboseT sets the verbosity level based on its `level` member.
     277              :  *
     278              :  * \returns the formatted message
     279              :  *
     280              :  * \ingroup error_internal
     281              :  */
     282              : template <class verboseT=verbose::d>
     283              : std::string mxlib_error_message( const error_t &code,            /**< [in] is an mx::error_t error code*/
     284              :                                  const std::source_location &loc /**< [in] [opt] source location */
     285              :                                  = std::source_location::current() );
     286              : 
     287              : /** \brief Specialization of \ref mxlib_error_message(const error_t &, const std::source_location&)
     288              :  * "mxlib_error_message" for
     289              :  * \ref verbose::o
     290              :  *
     291              :  * \ingroup error_internal
     292              :  */
     293              : template <>
     294              : std::string mxlib_error_message<mx::verbose::o>( const error_t &code, const std::source_location &loc );
     295              : 
     296              : /** \brief Specialization of \ref mxlib_error_message(const error_t &, const std::source_location&)
     297              :  * "mxlib_error_message" for \ref verbose::v
     298              :  *
     299              :  * \ingroup error_internal
     300              :  */
     301              : template <>
     302              : std::string mxlib_error_message<mx::verbose::v>( const error_t &code, const std::source_location &loc );
     303              : 
     304              : /** \brief  Specialization of \ref mxlib_error_message(const error_t &, const std::source_location&)
     305              :  * "mxlib_error_message" for \ref verbose::vv
     306              :  *
     307              :  * \ingroup error_internal
     308              :  */
     309              : template <>
     310              : std::string mxlib_error_message<mx::verbose::vv>( const error_t &code, const std::source_location &loc );
     311              : 
     312              : /** \brief Specialization of \ref mxlib_error_message(const error_t &, const std::source_location&)
     313              :  * "mxlib_error_message" for \ref verbose::vvv
     314              :  *
     315              :  * \ingroup error_internal
     316              :  */
     317              : template <>
     318              : std::string mxlib_error_message<mx::verbose::vvv>( const error_t &code, const std::source_location &loc );
     319              : 
     320              : /// Print a report to stderr given an mxlib \ref error_t \p code and explanation and return the \p code.
     321              : /** What is printed depends on the verbosity level set by the template parameter
     322              :  * This is for internal mxlib use, it includes `mxlib` in the vvv report.
     323              :  *
     324              :  * \tparam verboseT sets the verbosity level based on its `level` member.
     325              :  *
     326              :  * \returns the provided \ref error_t \p code
     327              :  *
     328              :  * \ingroup error_internal
     329              :  */
     330              : template <class verboseT=verbose::d>
     331           12 : error_t mxlib_error_report( const error_t &code,            /**< [in] is an mx::error_t error code*/
     332              :                             const std::string &expl,        /**< [in] [opt] if more information can be provided,
     333              :                                                                                  use this to inform the user.*/
     334              :                             const std::source_location &loc /**< [in] [opt] source location */
     335           20 :                             = std::source_location::current() )
     336              : {
     337           12 :     if( code == error_t::noerror ) // avoid output at any level
     338              :     {
     339            0 :         return code;
     340              :     }
     341              : 
     342              :     if constexpr( verboseT::level > 0 )
     343              :     {
     344           12 :         std::cerr << mxlib_error_message<verboseT>( code, expl, loc ) << '\n';
     345              :     }
     346              : 
     347           12 :     return code;
     348              : }
     349              : 
     350              : /// Specialization of \ref mxlib_error_report for \ref verbose::o
     351              : /**
     352              :  * \ingroup error_internal
     353              :  */
     354              : template <>
     355              : error_t
     356              : mxlib_error_report<mx::verbose::o>( const error_t &code, const std::string &expl, const std::source_location &loc );
     357              : 
     358              : /// Print a report to stderr given an mxlib \ref error_t \p code and return the \p code.
     359              : /** What is printed depends on the verbosity level set by the template parameter
     360              :  * This is for internal mxlib use, it includes `mxlib` in the vvv report.
     361              :  *
     362              :  * \tparam verboseT sets the verbosity level based on its `level` member.
     363              :  *
     364              :  * \returns the provided \ref error_t \p code
     365              :  *
     366              :  * \ingroup error_internal
     367              :  */
     368              : template <class verboseT=verbose::d>
     369            5 : error_t mxlib_error_report( const error_t &code /**< [in] is an mx::error_t error code*/,
     370              :                             const std::source_location &loc /**< [in] [opt] source location */
     371            0 :                             = std::source_location::current() )
     372              : {
     373            5 :     if( code == error_t::noerror )
     374              :     {
     375            5 :         return code;
     376              :     }
     377              : 
     378              :     if constexpr( verboseT::level > 0 )
     379              :     {
     380            0 :         std::cerr << mxlib_error_message<verboseT>( code, loc ) << '\n';
     381              :     }
     382              : 
     383            0 :     return code;
     384              : }
     385              : 
     386              : /** \brief  Specialization of \ref mxlib_error_report(const error_t &, const std::source_location&) "mxlib_error_report"
     387              :  * for \ref verbose::o
     388              :  *
     389              :  * \ingroup error_internal
     390              :  */
     391              : template <>
     392              : error_t mxlib_error_report<mx::verbose::o>( const error_t &code, const std::source_location &loc );
     393              : 
     394              : /** \brief Perform an error check, if an error occurs report it and return the error.  Does not return on no error.
     395              :  *
     396              :  * Scope protected so the error_t value does not interfere with other values.
     397              :  *
     398              :  * \note this requires that the verbosity template parameter is \b verboseT
     399              :  *
     400              :  * \param fxn is the function to call and check the return value of
     401              :  *
     402              :  * \ingroup error_internal
     403              :  *
     404              :  */
     405              : #define mxlib_error_check( fxn )                                                                                       \
     406              :     {                                                                                                                  \
     407              :         mx::error_t __mxlib_error_check_errc = fxn;                                                                    \
     408              :         if( __mxlib_error_check_errc != mx::error_t::noerror )                                                         \
     409              :         {                                                                                                              \
     410              :             return internal::mxlib_error_report<verboseT>( __mxlib_error_check_errc );                                 \
     411              :         }                                                                                                              \
     412              :     }
     413              : 
     414              : /** \brief Perform an error check, if an error occurs report it, and return the error code even if no error.
     415              :  *
     416              :  * Scope protected so the error_t value does not interfere with other values.
     417              :  *
     418              :  * \note this requires that the verbosity template parameter is \b verboseT
     419              :  *
     420              :  * \param fxn is the function to call and check the return value of
     421              :  *
     422              :  * \ingroup error_internal
     423              :  */
     424              : #define mxlib_error_return( fxn )                                                                                      \
     425              :     {                                                                                                                  \
     426              :         mx::error_t __mxlib_error_return_errc = fxn;                                                                   \
     427              :         if( __mxlib_error_return_errc != mx::error_t::noerror )                                                        \
     428              :         {                                                                                                              \
     429              :             return internal::mxlib_error_report<verboseT>( __mxlib_error_return_errc );                                \
     430              :         }                                                                                                              \
     431              :         return mx::error_t::noerror;                                                                                   \
     432              :     }
     433              : 
     434              : } // namespace internal
     435              : 
     436              : /// Format a report given an mxlib \ref error_t \p code and explanation.
     437              : /** What is included depends on the verbosity level set by the template parameter
     438              :  *
     439              :  * \tparam verboseT sets the verbosity level based on its `level` member.
     440              :  *
     441              :  * \returns the formatted message
     442              :  *
     443              :  * \ingroup error_handling
     444              :  */
     445              : template <class verboseT = verbose::d>
     446              : std::string error_message( const error_t &code,            /**< [in] is an mx::error_t error code*/
     447              :                            const std::string &expl,        /**< [in] [opt] if more information can be provided,
     448              :                                                                      use this to inform the user.*/
     449              :                            const std::source_location &loc /**< [in] [opt] source location */
     450              :                            = std::source_location::current() );
     451              : 
     452              : /// Specialization of \ref error_message for \ref verbose::o
     453              : /**
     454              :  * \ingroup error_handling
     455              :  */
     456              : template <>
     457              : std::string
     458              : error_message<mx::verbose::o>( const error_t &code, const std::string &expl, const std::source_location &loc );
     459              : 
     460              : /// Specialization of \ref error_message for \ref verbose::v
     461              : /**
     462              :  * \ingroup error_handling
     463              :  */
     464              : template <>
     465              : std::string
     466              : error_message<mx::verbose::v>( const error_t &code, const std::string &expl, const std::source_location &loc );
     467              : 
     468              : /// Specialization of \ref error_message for \ref verbose::vv
     469              : /**
     470              :  * \ingroup error_handling
     471              :  */
     472              : template <>
     473              : std::string
     474              : error_message<mx::verbose::vv>( const error_t &code, const std::string &expl, const std::source_location &loc );
     475              : 
     476              : /// Specialization of \ref error_message for \ref verbose::vvv
     477              : /**
     478              :  * \ingroup error_handling
     479              :  */
     480              : template <>
     481              : std::string
     482              : error_message<mx::verbose::vvv>( const error_t &code, const std::string &expl, const std::source_location &loc );
     483              : 
     484              : /// Format a report given an mxlib \ref error_t \p code.
     485              : /** What is included depends on the verbosity level set by the template parameter
     486              :  *
     487              :  * \tparam verboseT sets the verbosity level based on its `level` member.
     488              :  *
     489              :  * \returns the formatted message
     490              :  *
     491              :  * \ingroup error_handling
     492              :  */
     493              : template <class verboseT = verbose::vvv>
     494              : std::string error_message( const error_t &code,            /**< [in] is an mx::error_t error code*/
     495              :                            const std::source_location &loc /**< [in] [opt] source location */
     496              :                            = std::source_location::current() );
     497              : 
     498              : /** \brief Specialization of \ref error_message(const error_t &, const std::source_location&) "error_message" for  \ref
     499              :  * verbose::o
     500              :  *
     501              :  * \ingroup error_handling
     502              :  */
     503              : template <>
     504              : std::string error_message<mx::verbose::o>( const error_t &code, const std::source_location &loc );
     505              : 
     506              : /** \brief Specialization of \ref error_message(const error_t &, const std::source_location&) "error_message" for \ref
     507              :  * verbose::v
     508              :  *
     509              :  * \ingroup error_handling
     510              :  */
     511              : template <>
     512              : std::string error_message<mx::verbose::v>( const error_t &code, const std::source_location &loc );
     513              : 
     514              : /** \brief Specialization of \ref error_message(const error_t &, const std::source_location&) "error_message" for \ref
     515              :  * verbose::vv
     516              :  *
     517              :  * \ingroup error_handling
     518              :  */
     519              : template <>
     520              : std::string error_message<mx::verbose::vv>( const error_t &code, const std::source_location &loc );
     521              : 
     522              : /** \brief  Specialization of \ref error_message(const error_t &, const std::source_location&) "error_message" for \ref
     523              :  * verbose::vvv
     524              :  *
     525              :  * \ingroup error_handling
     526              :  */
     527              : template <>
     528              : std::string error_message<mx::verbose::vvv>( const error_t &code, const std::source_location &loc );
     529              : 
     530              : /// Print a report to stderr given an mxlib \ref error_t \p code and explanation and return the \p code.
     531              : /** What is printed depends on the verbosity level set by the template parameter
     532              :  *
     533              :  * \tparam verboseT sets the verbosity level based on its `level` member.
     534              :  *
     535              :  * \returns the provided \ref error_t \p code
     536              :  *
     537              :  * \ingroup error_handling
     538              :  */
     539              : template <class verboseT = verbose::vvv>
     540              : error_t error_report( const error_t &code,            /**< [in] is an mx::error_t error code*/
     541              :                       const std::string &expl,        /**< [in] [opt] if more information can be provided,
     542              :                                                                            use this to inform the user.*/
     543              :                       const std::source_location &loc /**< [in] [opt] source location */
     544              :                       = std::source_location::current() )
     545              : {
     546              :     if( code == error_t::noerror ) // avoid output at any level
     547              :     {
     548              :         return code;
     549              :     }
     550              : 
     551              :     if constexpr( verboseT::level > 0 )
     552              :     {
     553              :         std::cerr << error_message<verboseT>( code, expl, loc ) << '\n';
     554              :     }
     555              : 
     556              :     return code;
     557              : }
     558              : 
     559              : /** \brief  Specialization of \ref error_report for \ref verbose::o
     560              :  *
     561              :  * \ingroup error_handling
     562              :  */
     563              : template <>
     564              : error_t error_report<mx::verbose::o>( const error_t &code, const std::string &expl, const std::source_location &loc );
     565              : 
     566              : /// Print a report to stderr given an mxlib \ref error_t \p code and return the \p code.
     567              : /** What is printed depends on the verbosity level set by the template parameter
     568              :  *
     569              :  * \tparam verboseT sets the verbosity level based on its `level` member.
     570              :  *
     571              :  * \returns the provided \ref error_t \p code
     572              :  *
     573              :  * \ingroup error_handling
     574              :  */
     575              : template <class verboseT = verbose::vvv>
     576              : error_t error_report( const error_t &code,            /**< [in] is an mx::error_t error code*/
     577              :                       const std::source_location &loc /**< [in] [opt] source location */
     578              :                       = std::source_location::current() )
     579              : {
     580              :     if( code == error_t::noerror ) // avoid output at any level
     581              :     {
     582              :         return code;
     583              :     }
     584              : 
     585              :     if constexpr( verboseT::level > 0 )
     586              :     {
     587              :         std::cerr << error_message<verboseT>( code, loc ) << '\n';
     588              :     }
     589              : 
     590              :     return code;
     591              : }
     592              : 
     593              : /** \brief  Specialization of \ref error_report(const error_t &, const std::source_location&) "error_report"
     594              :  * for \ref verbose::o
     595              :  *
     596              :  * \ingroup error_handling
     597              :  */
     598              : template <>
     599              : error_t error_report<mx::verbose::o>( const error_t &code, const std::source_location &loc );
     600              : 
     601              : /** \brief Perform an error check on the output of a function.
     602              :  *
     603              :  * If an error occurs report it and return the error.  Do not return on no error.
     604              :  *
     605              :  * Scope protected so the error_t value does not interfere with other values.
     606              :  *
     607              :  * \note this requires that the verbosity template parameter is \b verboseT
     608              :  *
     609              :  * \param fxn is the function to call and check the return value of
     610              :  *
     611              :  * \ingroup error_handling
     612              :  */
     613              : #define mx_error_check( fxn )                                                                                          \
     614              :     {                                                                                                                  \
     615              :         mx::error_t __mxlib_error_check_errc = fxn;                                                                    \
     616              :         if( __mxlib_error_check_errc != mx::error_t::noerror )                                                         \
     617              :         {                                                                                                              \
     618              :             return error_report<verboseT>( __mxlib_error_check_errc );                                                 \
     619              :         }                                                                                                              \
     620              :     }
     621              : 
     622              : /** \brief Perform an error check on an error_t code.
     623              :  *
     624              :  * If an error is indicated report it and return the error.  Do not return on no error.
     625              :  *
     626              :  * \note this requires that the verbosity template parameter is \b verboseT
     627              :  *
     628              :  * \param errc is the code to check for an error value
     629              :  *
     630              :  * \ingroup error_handling
     631              :  */
     632              : #define mx_error_check_code( errc )                                                                                    \
     633              :     if( errc != mx::error_t::noerror )                                                                                 \
     634              :     {                                                                                                                  \
     635              :         return error_report<verboseT>( errc );                                                                         \
     636              :     }
     637              : 
     638              : /** \brief Perform an error check on the output of a function.
     639              :  *
     640              :  * If an error occurs report it and return an arbitrary type. Does not return on no error. This is intended to be used
     641              :  * in `int main()`, or any other case where the return value is something other than \ref mx::error_t.
     642              :  *
     643              :  * Scope protected so the \ref mx::error_t value does not interfere with other values.
     644              :  *
     645              :  * \note this requires that the verbosity template parameter is \b verboseT.  You may need to typedef it.
     646              :  *
     647              :  * \param fxn is the function to call and check the return value of (can also just be a code).
     648              :  *
     649              :  * \ingroup error_handling
     650              :  *
     651              :  */
     652              : #define mx_error_check_rv( fxn, rv )                                                                                   \
     653              :     {                                                                                                                  \
     654              :         mx::error_t __mxlib_error_check_errc = fxn;                                                                    \
     655              :         if( __mxlib_error_check_errc != mx::error_t::noerror )                                                         \
     656              :         {                                                                                                              \
     657              :             error_report<verboseT>( __mxlib_error_check_errc );                                                        \
     658              :             return rv;                                                                                                 \
     659              :         }                                                                                                              \
     660              :     }
     661              : 
     662              : /** \brief Perform an error check on an error_t code.
     663              :  *
     664              :  * If an error occurs report it and return an arbitrary type. Does not return on no error. This is intended to be used
     665              :  * in `int main()`, or any other case where the return value is something other than \ref mx::error_t.
     666              :  *
     667              :  * \note this requires that the verbosity template parameter is \b verboseT
     668              :  *
     669              :  * \param errc is the code to check for an error value
     670              :  *
     671              :  * \ingroup error_handling
     672              :  */
     673              : #define mx_error_check_code_rv( errc, rv )                                                                             \
     674              :     if( errc != mx::error_t::noerror )                                                                                 \
     675              :     {                                                                                                                  \
     676              :         error_report<verboseT>( errc );                                                                                \
     677              :         return rv;                                                                                                     \
     678              :     }
     679              : 
     680              : /** \brief Perform an error check on a function and return the result
     681              :  *
     682              :  * If an error occurs it is reported. If no error occurs error_t::noerror is returned.
     683              :  *
     684              :  * Scope protected so the error_t value does not interfere with other values.
     685              :  *
     686              :  * \note this requires that the verbosity template parameter is \b verboseT
     687              :  *
     688              :  * \param fxn is the function to call and check the return value of
     689              :  *
     690              :  * \ingroup error_handling
     691              :  */
     692              : #define mx_error_return( fxn )                                                                                         \
     693              :     {                                                                                                                  \
     694              :         mx::error_t __mx_error_return_errc = fxn;                                                                      \
     695              :         if( __mx_error_return_errc != mx::error_t::noerror )                                                           \
     696              :         {                                                                                                              \
     697              :             return error_report<verboseT>( __mx_error_return_errc );                                                   \
     698              :         }                                                                                                              \
     699              :         return mx::error_t::noerror;                                                                                   \
     700              :     }
     701              : 
     702              : /** \brief Perform an error check on an error_t code and return the result
     703              :  *
     704              :  * If an error occurs it is reported and returned. If no error occurs error_t::noerror is returned.
     705              :  *
     706              :  * \note this requires that the verbosity template parameter is \b verboseT
     707              :  *
     708              :  * \param errc is the function to call and check the return value of
     709              :  *
     710              :  * \ingroup error_handling
     711              :  */
     712              : #define mx_error_return_code( errc )                                                                                   \
     713              :     if( errc != mx::error_t::noerror )                                                                                 \
     714              :     {                                                                                                                  \
     715              :         return error_report<verboseT>( errc );                                                                         \
     716              :     }                                                                                                                  \
     717              :     return mx::error_t::noerror;
     718              : 
     719              : } // namespace mx
     720              : 
     721              : #endif // error_error_hpp
        

Generated by: LCOV version 2.0-1