Instrument Neutral Distributed Interface INDI 2.2.5
result.h
Go to the documentation of this file.
1/*
2 Gemini Power Box Hub Advanced v3 INDI Driver
3
4 Copyright (C) 2026 Dieter R Kedrowitsch <[email protected]>
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 The full GNU General Public License is included in this distribution in the
21 file called LICENSE.
22*/
23
24#pragma once
25
26#include <optional>
27#include <utility>
28
29namespace geminipbh
30{
31
46
47class Result
48{
49 public:
50 constexpr Result() = default;
51
52 static constexpr Result success()
53 {
54 return Result(Error::None);
55 }
56 static constexpr Result failure(Error error)
57 {
59 }
60
61 constexpr bool ok() const
62 {
63 return error_ == Error::None;
64 }
65 constexpr explicit operator bool() const
66 {
67 return ok();
68 }
69 constexpr Error error() const
70 {
71 return error_;
72 }
73
74 private:
75 constexpr explicit Result(Error error) : error_(error) {}
76
77 Error error_ = Error::None;
78};
79
80template <typename T>
82{
83 public:
84 explicit ValueResult(T value) : error_(Error::None), value_(std::move(value)) {}
85
90
91 bool ok() const
92 {
93 return error_ == Error::None;
94 }
95 explicit operator bool() const
96 {
97 return ok();
98 }
99 Error error() const
100 {
101 return error_;
102 }
103 const T &value() const
104 {
105 return *value_;
106 }
107
108 private:
109 explicit ValueResult(Error error) : error_(error) {}
110
111 Error error_ = Error::None;
112 std::optional<T> value_;
113};
114
115} // namespace geminipbh
constexpr Error error() const
Definition result.h:69
constexpr bool ok() const
Definition result.h:61
static constexpr Result failure(Error error)
Definition result.h:56
constexpr Result()=default
static constexpr Result success()
Definition result.h:52
const T & value() const
Definition result.h:103
ValueResult(T value)
Definition result.h:84
bool ok() const
Definition result.h:91
static ValueResult failure(Error error)
Definition result.h:86
Error error() const
Definition result.h:99