LCOV - code coverage report
Current view: top level - include/esys/repo - result_t.h (source / functions) Hit Total Coverage
Test: esysrepo_coverage.info Lines: 46 51 90.2 %
Date: 2026-08-01 10:43:40 Functions: 6 13 46.2 %

          Line data    Source code
       1             : /*!
       2             :  * \file esys/repo/result_t.h
       3             :  * \brief Result type with an attached return value
       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             : #pragma once
      19             : 
      20             : #include "esys/repo/result.h"
      21             : 
      22             : #ifdef _MSC_VER
      23             : #define ESYSREPO_RESULT_T(result, ...) esys::repo::Result_t(result, __FILE__, __LINE__, __FUNCSIG__, __VA_ARGS__)
      24             : #else
      25             : #define ESYSREPO_RESULT_T(result, ...) \
      26             :     esys::repo::Result_t(result, __FILE__, __LINE__, __PRETTY_FUNCTION__, ##__VA_ARGS__)
      27             : #endif
      28             : 
      29             : //<swig_inc/>
      30             : 
      31             : namespace esys::repo
      32             : {
      33             : 
      34             : /*! \class Result_t esys/repo/result_t.h "esys/repo/result_t.h"
      35             :  * \brief Result with an attached return value
      36             :  */
      37             : /*! \tparam R the return value type */
      38             : template<typename R>
      39             : class Result_t : public Result
      40             : {
      41             : public:
      42             :     using Result::Result;
      43             : 
      44             :     template<typename V>
      45             :     friend void PrintResult_t(std::ostream &os, const Result_t<V> &result);
      46             : 
      47             :     //! Default constructor
      48             :     Result_t();
      49             : 
      50             :     //! Copy constructor
      51             :     Result_t(const Result_t &result);
      52             : 
      53             :     //! Constructor
      54             :     /*!
      55             :      * \param[in] value the return value
      56             :      */
      57             :     Result_t(R value);
      58             : 
      59             :     //! Constructor
      60             :     /*!
      61             :      * \param[in] result the parent result
      62             :      */
      63             :     Result_t(Result result);
      64             : 
      65             :     //! Constructor
      66             :     /*!
      67             :      * \param[in] result the result
      68             :      * \param[in] file the file where the Result was created
      69             :      * \param[in] line_number the line number when the Result was created
      70             :      * \param[in] function the name of the function where the Result was created
      71             :      * \param[in] value the return value
      72             :      */
      73             :     Result_t(const Result &result, const std::string &file, int line_number, const std::string &function, R value);
      74             : 
      75             :     //! Constructor
      76             :     /*!
      77             :      * \param[in] result the result
      78             :      * \param[in] file the file where the Result was created
      79             :      * \param[in] line_number the line number when the Result was created
      80             :      * \param[in] function the name of the function where the Result was created
      81             :      * \param[in] value the return value
      82             :      * \param[in] text additional text associated with the return value
      83             :      */
      84             :     Result_t(const Result &result, const std::string &file, int line_number, const std::string &function, R value,
      85             :              const std::string &text);
      86             : 
      87             :     //! Constructor
      88             :     /*!
      89             :      * \param[in] result_code the Resultcode
      90             :      * \param[in] file the file where the Result was created
      91             :      * \param[in] line_number the line number when the Result was created
      92             :      * \param[in] function the name of the function where the Result was created
      93             :      * \param[in] value the return value
      94             :      */
      95             :     Result_t(ResultCode result_code, const std::string &file, int line_number, const std::string &function, R value);
      96             : 
      97             :     //! Constructor
      98             :     /*!
      99             :      * \param[in] result the result
     100             :      * \param[in] file the file where the Result was created
     101             :      * \param[in] line_number the line number when the Result was created
     102             :      * \param[in] function the name of the function where the Result was created
     103             :      */
     104             :     Result_t(Result_t<R> result, const std::string &file, int line_number, const std::string &function);
     105             : 
     106             :     //! Destructor
     107             :     ~Result_t() override;
     108             : 
     109             :     //! Set the return value
     110             :     /*!
     111             :      * \param[in] return_value the return value
     112             :      */
     113             :     void set(const R &return_value);
     114             : 
     115             :     //! Get the return value
     116             :     /*!
     117             :      * \return the return value
     118             :      */
     119             :     R get() const;
     120             : 
     121             :     //! Conversion operator to the return value type
     122             :     /*!
     123             :      * \return the return value
     124             :      */
     125             :     operator R() const;
     126             : 
     127             :     //! Function call operator
     128             :     /*!
     129             :      * \return the return value
     130             :      */
     131             :     R operator()() const;
     132             : 
     133             :     //! Assignment operator
     134             :     /*!
     135             :      * \return the result code
     136             :      */
     137             :     Result_t &operator=(const Result &other); //<swig_out/>
     138             : 
     139             :     //! Assignment operator
     140             :     /*!
     141             :      * \return the result code
     142             :      */
     143             :     Result_t &operator=(const Result_t &other); //<swig_out/>
     144             : 
     145             :     //! Print the result to the given output stream
     146             :     /*!
     147             :      * \param[in] os the output stream
     148             :      */
     149             :     void print(std::ostream &os) const;
     150             : 
     151             :     static Result_t<R> OK;
     152             : 
     153             : private:
     154             :     //!< \cond DOXY_IMPL
     155             :     R m_return_value; //!< The return value
     156             :     //!< \endcond
     157             : };
     158             : 
     159             : template<typename R>
     160             : Result_t<R> Result_t<R>::OK;
     161             : 
     162             : template<typename R>
     163          15 : Result_t<R>::Result_t()
     164          15 :     : Result()
     165             : {
     166             : }
     167             : 
     168             : template<typename R>
     169          46 : Result_t<R>::Result_t(const Result_t &result)
     170          46 :     : Result(result)
     171             : {
     172          46 :     set(result.get());
     173             : }
     174             : 
     175             : template<typename R>
     176             : Result_t<R>::Result_t(Result result)
     177             :     : Result(result)
     178             : {
     179             : }
     180             : 
     181             : template<typename R>
     182          16 : Result_t<R>::Result_t(const Result &result, const std::string &file, int line_number, const std::string &function,
     183             :                       R value)
     184             :     : Result(result, file, line_number, function)
     185          16 :     , m_return_value(value)
     186             : {
     187             : }
     188             : 
     189             : template<typename R>
     190           1 : Result_t<R>::Result_t(const Result &result, const std::string &file, int line_number, const std::string &function,
     191             :                       R value, const std::string &text)
     192             :     : Result(result, file, line_number, function, text)
     193           1 :     , m_return_value(value)
     194             : {
     195             : }
     196             : 
     197             : template<typename R>
     198         424 : Result_t<R>::Result_t(R value)
     199             :     : Result()
     200         424 :     , m_return_value(value)
     201             : {
     202         195 : }
     203             : 
     204             : template<typename R>
     205           3 : Result_t<R>::Result_t(ResultCode result_code, const std::string &file, int line_number, const std::string &function,
     206             :                       R value)
     207             :     : Result(result_code, file, line_number, function)
     208           3 :     , m_return_value(value)
     209             : {
     210           3 : }
     211             : 
     212             : template<typename R>
     213          16 : Result_t<R>::Result_t(Result_t<R> result, const std::string &file, int line_number, const std::string &function)
     214          16 :     : Result(result, file, line_number, function)
     215             : {
     216          16 :     set(result.get());
     217             : }
     218             : 
     219             : template<typename R>
     220         521 : Result_t<R>::~Result_t()
     221             : {
     222         506 : }
     223             : 
     224             : template<typename R>
     225         113 : void Result_t<R>::set(const R &return_value)
     226             : {
     227         113 :     m_return_value = return_value;
     228             : }
     229             : 
     230             : template<typename R>
     231         328 : R Result_t<R>::get() const
     232             : {
     233         261 :     return m_return_value;
     234             : }
     235             : 
     236             : template<typename R>
     237         242 : Result_t<R>::operator R() const
     238             : {
     239         242 :     return m_return_value;
     240             : }
     241             : 
     242             : template<typename R>
     243           4 : R Result_t<R>::operator()() const
     244             : {
     245           4 :     return m_return_value;
     246             : }
     247             : 
     248             : template<typename R>
     249          30 : Result_t<R> &Result_t<R>::operator=(const Result &other)
     250             : {
     251          30 :     set_result_code(other.get_result_code());
     252          30 :     set_error_info(other.get_error_info());
     253          30 :     return *this;
     254             : }
     255             : 
     256             : template<typename R>
     257          51 : Result_t<R> &Result_t<R>::operator=(const Result_t &other)
     258             : {
     259          51 :     if (&other == this) return *this;
     260             : 
     261          51 :     set_result_code(other.get_result_code());
     262          51 :     set_error_info(other.get_error_info());
     263          51 :     set_line_number(other.get_line_number());
     264          51 :     set(other.get());
     265          51 :     return *this;
     266             : }
     267             : 
     268             : template<typename V>
     269           0 : void PrintResult_t(std::ostream &os, const Result_t<V> &result)
     270             : {
     271           0 :     os << "Return value :" << std::endl;
     272           0 :     os << "    type  : " << typeid(V).name() << std::endl;
     273           0 :     os << "    value : " << result.get() << std::endl;
     274           0 : }
     275             : 
     276             : template<>
     277             : ESYSREPO_API void PrintResult_t<bool>(std::ostream &os, const Result_t<bool> &result);
     278             : 
     279             : template<typename R>
     280           2 : void Result_t<R>::print(std::ostream &os) const
     281             : {
     282           2 :     PrintResult_t<R>(os, *this);
     283           2 : }
     284             : 
     285             : } // namespace esys::repo
     286             : 
     287             : //<swig>%template(ResultBool) esys::repo::Result_t<bool>;</swig>
     288             : 
     289             : namespace std
     290             : {
     291             : 
     292             : template<typename T>
     293          13 : ostream &operator<<(ostream &os, const esys::repo::Result_t<T> &result)
     294             : {
     295          13 :     os << (const esys::repo::Result &)(result) << std::endl;
     296          13 :     esys::repo::PrintResult_t<T>(os, result);
     297          13 :     return os;
     298             : }
     299             : 
     300             : }

Generated by: LCOV version 1.14