LCOV - code coverage report
Current view: top level - servus - result.h (source / functions) Hit Total Coverage
Test: Servus Lines: 14 15 93.3 %
Date: 2018-10-03 03:09:57 Functions: 9 11 81.8 %

          Line data    Source code
       1             : /* Copyright (c) 2013-2015, Stefan.Eilemann@epfl.ch
       2             :  *
       3             :  * This file is part of Servus <https://github.com/HBPVIS/Servus>
       4             :  *
       5             :  * This library is free software; you can redistribute it and/or modify it under
       6             :  * the terms of the GNU Lesser General Public License version 3.0 as published
       7             :  * by the Free Software Foundation.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful, but WITHOUT
      10             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      11             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      12             :  * details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public License
      15             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      16             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      17             :  */
      18             : 
      19             : #ifndef SERVUS_RESULT_H
      20             : #define SERVUS_RESULT_H
      21             : 
      22             : #include <servus/api.h>
      23             : #include <servus/types.h>
      24             : 
      25             : #include <iostream>
      26             : 
      27             : namespace servus
      28             : {
      29             : /** A result returns an error code and behaves like a boolean. */
      30             : class Result
      31             : {
      32             :     typedef void (Result::*bool_t)() const;
      33           0 :     void bool_true() const {}
      34             : public:
      35             :     static const int32_t SUCCESS = 0;
      36             : 
      37             :     /** Construct a new result. */
      38          53 :     explicit Result(const int32_t code)
      39          53 :         : code_(code)
      40             :     {
      41          53 :     }
      42             : 
      43             :     /** Destruct the result. */
      44          53 :     virtual ~Result() {}
      45             :     /** @return true if no error occured, false otherwise. */
      46           6 :     operator bool_t() const
      47             :     {
      48           6 :         return code_ == SUCCESS ? &Result::bool_true : 0;
      49             :     }
      50             : 
      51             :     /** @return true if an error occured, false otherwise. */
      52           6 :     bool operator!() const { return code_ != SUCCESS; }
      53             :     /** @return true if the result is equal to the given value. */
      54           7 :     bool operator==(const int32_t code) const { return code_ == code; }
      55             :     /** @return true if the result is not equal to the rhs. */
      56           3 :     bool operator!=(const int32_t code) const { return code != code_; }
      57             :     /** @return the result code. */
      58           8 :     int32_t getCode() const { return code_; }
      59             :     /** @return the result string. */
      60           4 :     virtual std::string getString() const
      61             :     {
      62           4 :         return code_ == SUCCESS ? "success" : "result";
      63             :     }
      64             : 
      65             : protected:
      66             :     int32_t code_;
      67             : };
      68             : 
      69           2 : inline std::ostream& operator<<(std::ostream& os, const Result& result)
      70             : {
      71           2 :     return os << result.getString() << " (" << result.getCode() << ")";
      72             : }
      73             : }
      74             : #endif // SERVUS_RESULT_H

Generated by: LCOV version 1.11