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-12 14:26:50 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        2979 : Result::Result() = default;
      27             : 
      28        3046 : Result::Result(const Result &result)
      29             : {
      30        3046 :     set_result_code(result.get_result_code());
      31        3046 :     set_error_info(result.get_error_info());
      32        3046 :     set_line_number(result.get_line_number());
      33        3046 : }
      34             : 
      35        5163 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function)
      36        5163 :     : m_result_code(result_code)
      37             : {
      38        5163 :     handle_result_code(result_code, file, line_number, function);
      39        5163 : }
      40             : 
      41          26 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function,
      42          26 :                const std::string &text)
      43          26 :     : m_result_code(result_code)
      44             : {
      45          26 :     handle_result_code(result_code, file, line_number, function);
      46             : 
      47          52 :     if (get_error_info() != nullptr)
      48             :     {
      49          26 :         auto error_info = get_error_info();
      50          26 :         error_info->set_text(text);
      51          26 :     }
      52          26 : }
      53             : 
      54          46 : Result::Result(ResultCode result_code, const std::string &file, int line_number, const std::string &function,
      55          46 :                int raw_error, const std::string &text)
      56          46 :     : m_result_code(result_code)
      57             : {
      58          46 :     handle_result_code(result_code, file, line_number, function);
      59             : 
      60          92 :     if (get_error_info() != nullptr)
      61             :     {
      62          46 :         auto error_info = get_error_info();
      63          46 :         error_info->set_raw_error(raw_error);
      64          46 :         error_info->set_text(text);
      65          46 :     }
      66          46 : }
      67             : 
      68        1410 : Result::Result(const Result &result, const std::string &file, int line_number, const std::string &function)
      69             : {
      70        1410 :     handle_result(result, file, line_number, function);
      71        1410 : }
      72             : 
      73         222 : Result::Result(const Result &result, const std::string &file, int line_number, const std::string &function,
      74         222 :                const std::string &text)
      75             : {
      76         222 :     handle_result(result, file, line_number, function);
      77             : 
      78         223 :     if (get_error_info() != nullptr)
      79             :     {
      80           2 :         get_error_info()->set_text(text);
      81             :     }
      82         222 : }
      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       13168 : Result::~Result() = default;
      99             : 
     100       10893 : void Result::set_result_code(ResultCode result_code)
     101             : {
     102       10893 :     m_result_code = result_code;
     103       10893 : }
     104             : 
     105        6867 : void Result::handle_result_code(ResultCode result_code, const std::string &file, int line_number,
     106             :                                 const std::string &function)
     107             : {
     108        6867 :     set_line_number(line_number);
     109             : 
     110        6867 :     if (result_code != ResultCode::OK)
     111             :     {
     112         170 :         auto error_info = std::make_shared<ErrorInfo>();
     113         170 :         error_info->set_file(file);
     114         170 :         error_info->set_line_number(line_number);
     115         170 :         error_info->set_function(function);
     116         170 :         error_info->set_result_code(result_code);
     117         510 :         set_error_info(error_info);
     118         170 :     }
     119        6867 : }
     120             : 
     121        1632 : void Result::handle_result(const Result &result, const std::string &file, int line_number, const std::string &function)
     122             : {
     123        1632 :     set_result_code(result.get_result_code());
     124             : 
     125        1632 :     handle_result_code(result.get_result_code(), file, line_number, function);
     126             : 
     127        1714 :     if (get_error_info() != nullptr)
     128             :     {
     129          82 :         auto error_info = get_error_info();
     130          82 :         error_info->set_prev(result.get_error_info());
     131         164 :         error_info->set_index(result.get_error_info()->get_index() + 1);
     132          82 :     }
     133        1632 : }
     134             : 
     135       19350 : ResultCode Result::get_result_code() const
     136             : {
     137       19350 :     return m_result_code;
     138             : }
     139             : 
     140         156 : int Result::get_result_code_int() const
     141             : {
     142         156 :     return static_cast<int>(get_result_code());
     143             : }
     144             : 
     145       16110 : void Result::set_line_number(int line_number)
     146             : {
     147       16110 :     m_line_number = line_number;
     148       16110 : }
     149             : 
     150        9265 : int Result::get_line_number() const
     151             : {
     152        9265 :     return m_line_number;
     153             : }
     154             : 
     155        9432 : void Result::set_error_info(std::shared_ptr<ErrorInfo> error_info)
     156             : {
     157        9432 :     m_error_info = error_info;
     158        9432 : }
     159             : 
     160        2105 : std::shared_ptr<ErrorInfo> Result::get_error_info()
     161             : {
     162        2105 :     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        9472 : const std::shared_ptr<ErrorInfo> Result::get_error_info() const
     180             : {
     181        9472 :     return m_error_info;
     182             : }
     183             : 
     184        1407 : bool Result::success() const
     185             : {
     186        1407 :     return get_result_code() == ResultCode::OK;
     187             : }
     188             : 
     189        1398 : bool Result::ok() const
     190             : {
     191        1398 :     return success();
     192             : }
     193             : 
     194        5188 : bool Result::error() const
     195             : {
     196        5188 :     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        6164 : Result &Result::operator=(const Result &other)
     210             : {
     211        6164 :     if (&other == this) return *this;
     212             : 
     213        6164 :     set_result_code(other.get_result_code());
     214        6164 :     set_line_number(other.get_line_number());
     215        6164 :     set_error_info(other.get_error_info());
     216        6164 :     return *this;
     217             : }
     218             : 
     219          49 : bool Result::operator==(const ResultCode &result_code) const
     220             : {
     221          49 :     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          22 : ESYSREPO_API ostream &operator<<(ostream &os, const esys::repo::Result &result)
     262             : {
     263          22 :     auto resul_code_str = esys::repo::ResultCode::s_find_name(result.get_result_code());
     264             : 
     265          22 :     os << resul_code_str << "(" << result.get_result_code_int() << ") line " << result.get_line_number();
     266          22 :     if (result.ok()) return os;
     267             : 
     268          42 :     if (result.get_error_info() == nullptr) return os;
     269          21 :     os << std::endl;
     270          21 :     os << *result.get_error_info().get();
     271          21 :     return os;
     272          22 : }
     273             : 
     274             : } // namespace std

Generated by: LCOV version 1.14