LCOV - code coverage report
Current view: top level - src/esys/repo - result.cpp (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 112 134 83.6 %
Date: 2026-11-07 22:18:26 Functions: 26 35 74.3 %

          Line data    Source code
       1             : /*!
       2             :  * \file esys/repo/result.cpp
       3             :  * \brief
       4             :  *
       5             :  * \cond
       6             :  * __legal_b__
       7             :  *
       8             :  * Copyright (c) 2022-2023 Michel Gillet
       9             :  * Distributed under the MIT License.
      10             :  * (See accompanying file LICENSE.txt or
      11             :  * copy at https://opensource.org/licenses/MIT)
      12             :  *
      13             :  * __legal_e__
      14             :  * \endcond
      15             :  *
      16             :  */
      17             : 
      18             : #include "esys/repo/esysrepo_prec.h"
      19             : #include "esys/repo/result.h"
      20             : 
      21             : namespace esys::repo
      22             : {
      23             : 
      24             : Result Result::OK;
      25             : 
      26        4269 : Result::Result() = default;
      27             : 
      28        4148 : Result::Result(const Result &result)
      29             : {
      30        4148 :     set_result_code(result.get_result_code());
      31        4148 :     set_error_info(result.get_error_info());
      32        4148 :     set_line_number(result.get_line_number());
      33        4148 : }
      34             : 
      35        8552 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function)
      36        8552 :     : m_result_code(result_code)
      37             : {
      38        8552 :     handle_result_code(result_code, file, line_number, function);
      39        8552 : }
      40             : 
      41          58 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function,
      42          58 :                const std::string &text)
      43          58 :     : m_result_code(result_code)
      44             : {
      45          58 :     handle_result_code(result_code, file, line_number, function);
      46             : 
      47         116 :     if (get_error_info() != nullptr)
      48             :     {
      49          58 :         auto error_info = get_error_info();
      50          58 :         error_info->set_text(text);
      51          58 :     }
      52          58 : }
      53             : 
      54         107 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function,
      55         107 :                int raw_error, const std::string &text)
      56         107 :     : m_result_code(result_code)
      57             : {
      58         107 :     handle_result_code(result_code, file, line_number, function);
      59             : 
      60         214 :     if (get_error_info() != nullptr)
      61             :     {
      62         107 :         auto error_info = get_error_info();
      63         107 :         error_info->set_raw_error(raw_error);
      64         107 :         error_info->set_text(text);
      65         107 :     }
      66         107 : }
      67             : 
      68        2151 : Result::Result(const Result &result, const std::string &file, int line_number, const std::string &function)
      69             : {
      70        2151 :     handle_result(result, file, line_number, function);
      71        2151 : }
      72             : 
      73         273 : Result::Result(const Result &result, const std::string &file, int line_number, const std::string &function,
      74         273 :                const std::string &text)
      75             : {
      76         273 :     handle_result(result, file, line_number, function);
      77             : 
      78         274 :     if (get_error_info() != nullptr)
      79             :     {
      80           2 :         get_error_info()->set_text(text);
      81             :     }
      82         273 : }
      83             : 
      84           0 : Result::Result(const Result &result, ResultCode result_code, const std::string &file, int line_number,
      85           0 :                const std::string &function, const std::string &text)
      86             : {
      87           0 :     handle_result(result, file, line_number, function);
      88             : 
      89           0 :     set_result_code(result_code);
      90             : 
      91           0 :     if (get_error_info() != nullptr)
      92             :     {
      93           0 :         get_error_info()->set_result_code(result_code);
      94           0 :         get_error_info()->set_text(text);
      95             :     }
      96           0 : }
      97             : 
      98       20168 : Result::~Result() = default;
      99             : 
     100       15488 : void Result::set_result_code(ResultCode result_code)
     101             : {
     102       15488 :     m_result_code = result_code;
     103       15488 : }
     104             : 
     105       11141 : void Result::handle_result_code(ResultCode result_code, const std::string &file, int line_number,
     106             :                                 const std::string &function)
     107             : {
     108       11141 :     set_line_number(line_number);
     109             : 
     110       11141 :     if (result_code != ResultCode::OK)
     111             :     {
     112         354 :         auto error_info = std::make_shared<ErrorInfo>();
     113         354 :         error_info->set_file(file);
     114         354 :         error_info->set_line_number(line_number);
     115         354 :         error_info->set_function(function);
     116         354 :         error_info->set_result_code(result_code);
     117        1062 :         set_error_info(error_info);
     118         354 :     }
     119       11141 : }
     120             : 
     121        2424 : void Result::handle_result(const Result &result, const std::string &file, int line_number, const std::string &function)
     122             : {
     123        2424 :     set_result_code(result.get_result_code());
     124             : 
     125        2424 :     handle_result_code(result.get_result_code(), file, line_number, function);
     126             : 
     127        2583 :     if (get_error_info() != nullptr)
     128             :     {
     129         159 :         auto error_info = get_error_info();
     130         159 :         error_info->set_prev(result.get_error_info());
     131         318 :         error_info->set_index(result.get_error_info()->get_index() + 1);
     132         159 :     }
     133        2424 : }
     134             : 
     135       28802 : ResultCode Result::get_result_code() const
     136             : {
     137       28802 :     return m_result_code;
     138             : }
     139             : 
     140         219 : int Result::get_result_code_int() const
     141             : {
     142         219 :     return static_cast<int>(get_result_code());
     143             : }
     144             : 
     145       24140 : void Result::set_line_number(int line_number)
     146             : {
     147       24140 :     m_line_number = line_number;
     148       24140 : }
     149             : 
     150       13045 : int Result::get_line_number() const
     151             : {
     152       13045 :     return m_line_number;
     153             : }
     154             : 
     155       13418 : void Result::set_error_info(std::shared_ptr<ErrorInfo> error_info)
     156             : {
     157       13418 :     m_error_info = error_info;
     158       13418 : }
     159             : 
     160        3211 : std::shared_ptr<ErrorInfo> Result::get_error_info()
     161             : {
     162        3211 :     return m_error_info;
     163             : }
     164             : 
     165           1 : void Result::add(const Result &result)
     166             : {
     167           1 :     if (result.ok()) return;
     168             : 
     169           1 :     if (get_error_info() == nullptr)
     170             :     {
     171           1 :         auto error_info = std::make_shared<ErrorInfo>();
     172           1 :         error_info->set_result_code(ResultCode::ERROR_VECTOR);
     173           3 :         set_error_info(error_info);
     174           1 :     }
     175             : 
     176           1 :     if (ok()) set_result_code(ResultCode::ERROR_VECTOR);
     177             : 
     178           3 :     get_error_info()->add_prev(result.get_error_info());
     179             : }
     180             : 
     181       13475 : const std::shared_ptr<ErrorInfo> Result::get_error_info() const
     182             : {
     183       13475 :     return m_error_info;
     184             : }
     185             : 
     186        1917 : bool Result::success() const
     187             : {
     188        1917 :     return get_result_code() == ResultCode::OK;
     189             : }
     190             : 
     191        1900 : bool Result::ok() const
     192             : {
     193        1900 :     return success();
     194             : }
     195             : 
     196        8631 : bool Result::error() const
     197             : {
     198        8631 :     return get_result_code() != ResultCode::OK;
     199             : }
     200             : 
     201           0 : Result::operator bool() const
     202             : {
     203           0 :     return success();
     204             : }
     205             : 
     206           0 : Result::operator ResultCode() const
     207             : {
     208           0 :     return get_result_code();
     209             : }
     210             : 
     211        8744 : Result &Result::operator=(const Result &other)
     212             : {
     213        8744 :     if (&other == this) return *this;
     214             : 
     215        8744 :     set_result_code(other.get_result_code());
     216        8744 :     set_line_number(other.get_line_number());
     217        8744 :     set_error_info(other.get_error_info());
     218        8744 :     return *this;
     219             : }
     220             : 
     221          58 : bool Result::operator==(const ResultCode &result_code) const
     222             : {
     223          58 :     return get_result_code() == result_code;
     224             : }
     225             : 
     226           0 : bool Result::operator!=(const ResultCode &result_code) const
     227             : {
     228           0 :     return !operator==(result_code);
     229             : }
     230             : 
     231           0 : bool Result::operator==(const Result &other) const
     232             : {
     233           0 :     if (get_result_code() != other.get_result_code()) return false;
     234             : 
     235             :     //! \TODO should the ErrorInfo be compared as well?
     236             :     return true;
     237             : }
     238             : 
     239           0 : bool Result::operator!=(const Result &other) const
     240             : {
     241           0 :     return !operator==(other);
     242             : }
     243             : 
     244           2 : void Result::print(std::ostream &os) const
     245             : {
     246           2 : }
     247             : 
     248           0 : ESYSREPO_API bool operator==(const ResultCode &result_code, const Result &result)
     249             : {
     250           0 :     return result.get_result_code() == result_code;
     251             : }
     252             : 
     253           0 : ESYSREPO_API bool operator!=(const ResultCode &result_code, const Result &result)
     254             : {
     255           0 :     return result.get_result_code() != result_code;
     256             : }
     257             : 
     258             : } // namespace esys::repo
     259             : 
     260             : namespace std
     261             : {
     262             : 
     263          46 : ESYSREPO_API ostream &operator<<(ostream &os, const esys::repo::Result &result)
     264             : {
     265          46 :     auto resul_code_str = esys::repo::ResultCode::s_find_name(result.get_result_code());
     266             : 
     267          46 :     os << resul_code_str << "(" << result.get_result_code_int() << ") line " << result.get_line_number();
     268          46 :     if (result.ok()) return os;
     269             : 
     270          86 :     if (result.get_error_info() == nullptr) return os;
     271          43 :     os << std::endl;
     272          43 :     os << *result.get_error_info().get();
     273          43 :     return os;
     274          46 : }
     275             : 
     276             : } // namespace std

Generated by: LCOV version 1.14