Servus  1.3.1
C++ network oriented utilities including a zeroconf implementation
result.h
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 
31 class Result
32 {
33  typedef void (Result::*bool_t)() const;
34  void bool_true() const {}
35 
36 public:
37  static const int32_t SUCCESS = 0;
38 
40  explicit Result( const int32_t code ) : code_( code ){}
41 
43  virtual ~Result(){}
44 
46  operator bool_t() const { return code_ == SUCCESS ? &Result::bool_true : 0;}
47 
49  bool operator !() const { return code_ != SUCCESS; }
50 
52  bool operator == ( const int32_t code ) const { return code_ == code; }
53 
55  bool operator != ( const int32_t code ) const { return code != code_; }
56 
58  int32_t getCode() const { return code_; }
59 
61  virtual std::string getString() const
62  { return code_ == SUCCESS ? "success" : "result"; }
63 
64 protected:
65  int32_t code_;
66 };
67 
68 inline std::ostream& operator << ( std::ostream& os, const Result& result )
69 {
70  return os << result.getString() << " (" << result.getCode() << ")";
71 }
72 
73 }
74 #endif //SERVUS_RESULT_H
virtual ~Result()
Destruct the result.
Definition: result.h:43
Defines export visibility macros for library Servus.
A result returns an error code and behaves like a boolean.
Definition: result.h:31
int32_t getCode() const
Definition: result.h:58
bool operator==(const int32_t code) const
Definition: result.h:52
Result(const int32_t code)
Construct a new result.
Definition: result.h:40
virtual std::string getString() const
Definition: result.h:61
bool operator!() const
Definition: result.h:49
bool operator!=(const int32_t code) const
Definition: result.h:55