LCOV - code coverage report
Current view: top level - lunchbox - result.h (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 7 10 70.0 %
Date: 2018-10-03 05:33:11 Functions: 4 7 57.1 %

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

Generated by: LCOV version 1.11