LCOV - code coverage report
Current view: top level - src/esys/repo - result.cpp (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 111 133 83.5 %
Date: 2026-08-01 10:43:40 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        3370 : Result::Result() = default;
      27             : 
      28        3488 : Result::Result(const Result &result)
      29             : {
      30        3488 :     set_result_code(result.get_result_code());
      31        3488 :     set_error_info(result.get_error_info());
      32        3488 :     set_line_number(result.get_line_number());
      33        3488 : }
      34             : 
      35        4600 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function)
      36        4600 :     : m_result_code(result_code)
      37             : {
      38        4600 :     handle_result_code(result_code, file, line_number, function);
      39        4600 : }
      40             : 
      41          23 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function,
      42          23 :                const std::string &text)
      43          23 :     : m_result_code(result_code)
      44             : {
      45          23 :     handle_result_code(result_code, file, line_number, function);
      46             : 
      47          46 :     if (get_error_info() != nullptr)
      48             :     {
      49          23 :         auto error_info = get_error_info();
      50          23 :         error_info->set_text(text);
      51          23 :     }
      52          23 : }
      53             : 
      54          70 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function,
      55          70 :                int raw_error, const std::string &text)
      56          70 :     : m_result_code(result_code)
      57             : {
      58          70 :     handle_result_code(result_code, file, line_number, function);
      59             : 
      60         140 :     if (get_error_info() != nullptr)
      61             :     {
      62          70 :         auto error_info = get_error_info();
      63          70 :         error_info->set_raw_error(raw_error);
      64          70 :         error_info->set_text(text);
      65          70 :     }
      66          70 : }
      67             : 
      68        1424 : Result::Result(const Result &result, const std::string &file, int line_number, const std::string &function)
      69             : {
      70        1424 :     handle_result(result, file, line_number, function);
      71        1424 : }
      72             : 
      73         173 : Result::Result(const Result &result, const std::string &file, int line_number, const std::string &function,
      74         173 :                const std::string &text)
      75             : {
      76         173 :     handle_result(result, file, line_number, function);
      77             : 
      78         174 :     if (get_error_info() != nullptr)
      79             :     {
      80           2 :         get_error_info()->set_text(text);
      81             :     }
      82         173 : }
      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       13398 : Result::~Result() = default;
      99             : 
     100       11332 : void Result::set_result_code(ResultCode result_code)
     101             : {
     102       11332 :     m_result_code = result_code;
     103       11332 : }
     104             : 
     105        6290 : void Result::handle_result_code(ResultCode result_code, const std::string &file, int line_number,
     106             :                                 const std::string &function)
     107             : {
     108        6290 :     set_line_number(line_number);
     109             : 
     110        6290 :     if (result_code != ResultCode::OK)
     111             :     {
     112         151 :         auto error_info = std::make_shared<ErrorInfo>();
     113         151 :         error_info->set_file(file);
     114         151 :         error_info->set_line_number(line_number);
     115         151 :         error_info->set_function(function);
     116         151 :         error_info->set_result_code(result_code);
     117         453 :         set_error_info(error_info);
     118         151 :     }
     119        6290 : }
     120             : 
     121        1597 : void Result::handle_result(const Result &result, const std::string &file, int line_number, const std::string &function)
     122             : {
     123        1597 :     set_result_code(result.get_result_code());
     124             : 
     125        1597 :     handle_result_code(result.get_result_code(), file, line_number, function);
     126             : 
     127        1639 :     if (get_error_info() != nullptr)
     128             :     {
     129          42 :         auto error_info = get_error_info();
     130          42 :         error_info->set_prev(result.get_error_info());
     131          84 :         error_info->set_index(result.get_error_info()->get_index() + 1);
     132          42 :     }
     133        1597 : }
     134             : 
     135       19718 : ResultCode Result::get_result_code() const
     136             : {
     137       19718 :     return m_result_code;
     138             : }
     139             : 
     140          98 : int Result::get_result_code_int() const
     141             : {
     142          98 :     return static_cast<int>(get_result_code());
     143             : }
     144             : 
     145       15995 : void Result::set_line_number(int line_number)
     146             : {
     147       15995 :     m_line_number = line_number;
     148       15995 : }
     149             : 
     150        9718 : int Result::get_line_number() const
     151             : {
     152        9718 :     return m_line_number;
     153             : }
     154             : 
     155        9887 : void Result::set_error_info(std::shared_ptr<ErrorInfo> error_info)
     156             : {
     157        9887 :     m_error_info = error_info;
     158        9887 : }
     159             : 
     160        2023 : std::shared_ptr<ErrorInfo> Result::get_error_info()
     161             : {
     162        2023 :     return m_error_info;
     163             : }
     164             : 
     165           2 : void Result::add(const Result &result)
     166             : {
     167           2 :     if (result.ok()) return;
     168             : 
     169           3 :     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           6 :     get_error_info()->add_prev(result.get_error_info());
     177             : }
     178             : 
     179        9836 : const std::shared_ptr<ErrorInfo> Result::get_error_info() const
     180             : {
     181        9836 :     return m_error_info;
     182             : }
     183             : 
     184        1618 : bool Result::success() const
     185             : {
     186        1618 :     return get_result_code() == ResultCode::OK;
     187             : }
     188             : 
     189        1609 : bool Result::ok() const
     190             : {
     191        1609 :     return success();
     192             : }
     193             : 
     194        5013 : bool Result::error() const
     195             : {
     196        5013 :     return get_result_code() != ResultCode::OK;
     197             : }
     198             : 
     199           0 : Result::operator bool() const
     200             : {
     201           0 :     return success();
     202             : }
     203             : 
     204           0 : Result::operator ResultCode() const
     205             : {
     206           0 :     return get_result_code();
     207             : }
     208             : 
     209        6166 : Result &Result::operator=(const Result &other)
     210             : {
     211        6166 :     if (&other == this) return *this;
     212             : 
     213        6166 :     set_result_code(other.get_result_code());
     214        6166 :     set_line_number(other.get_line_number());
     215        6166 :     set_error_info(other.get_error_info());
     216        6166 :     return *this;
     217             : }
     218             : 
     219          47 : bool Result::operator==(const ResultCode &result_code) const
     220             : {
     221          47 :     return get_result_code() == result_code;
     222             : }
     223             : 
     224           0 : bool Result::operator!=(const ResultCode &result_code) const
     225             : {
     226           0 :     return !operator==(result_code);
     227             : }
     228             : 
     229           0 : bool Result::operator==(const Result &other) const
     230             : {
     231           0 :     if (get_result_code() != other.get_result_code()) return false;
     232             : 
     233             :     //! \TODO should the ErrorInfo be compared as well?
     234             :     return true;
     235             : }
     236             : 
     237           0 : bool Result::operator!=(const Result &other) const
     238             : {
     239           0 :     return !operator==(other);
     240             : }
     241             : 
     242           2 : void Result::print(std::ostream &os) const
     243             : {
     244           2 : }
     245             : 
     246           0 : ESYSREPO_API bool operator==(const ResultCode &result_code, const Result &result)
     247             : {
     248           0 :     return result.get_result_code() == result_code;
     249             : }
     250             : 
     251           0 : ESYSREPO_API bool operator!=(const ResultCode &result_code, const Result &result)
     252             : {
     253           0 :     return result.get_result_code() != result_code;
     254             : }
     255             : 
     256             : } // namespace esys::repo
     257             : 
     258             : namespace std
     259             : {
     260             : 
     261          13 : ESYSREPO_API ostream &operator<<(ostream &os, const esys::repo::Result &result)
     262             : {
     263          13 :     auto resul_code_str = esys::repo::ResultCode::s_find_name(result.get_result_code());
     264             : 
     265          13 :     os << resul_code_str << "(" << result.get_result_code_int() << ") line " << result.get_line_number();
     266          13 :     if (result.ok()) return os;
     267             : 
     268          12 :     if (result.get_error_info() == nullptr) return os;
     269           6 :     os << std::endl;
     270           6 :     os << *result.get_error_info().get();
     271           6 :     return os;
     272          13 : }
     273             : 
     274             : } // namespace std

Generated by: LCOV version 1.14