JKQTPlotter trunk/v5.0.0
an extensive Qt5+Qt6 Plotter framework (including a feature-richt plotter widget, a speed-optimized, but limited variant and a LaTeX equation renderer!), written fully in C/C++ and without external dependencies
Loading...
Searching...
No Matches
jkqtpexpected.h
1
2/*
3 Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
4
5
6
7 This software is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19*/
20
21#ifndef JKQTPEXPECTED_H_INCLUDED
22#define JKQTPEXPECTED_H_INCLUDED
23
24#include <stdexcept>
25
26
27/** \brief tag type for an unexpected/error result in JKQTPExpected
28 * \ingroup jkqtptools_general
29 *
30 * \see JKQTPExpected, JKQTPUnexpected
31 */
33/** \brief tag for an unexpected/error result in JKQTPExpected
34 * \ingroup jkqtptools_general
35 *
36 * \see JKQTPExpected, JKQTPExpectedUnexpectedType
37 */
38#define JKQTPUnexpected JKQTPExpectedUnexpectedType()
39
40/** \brief an "expected" datatype, which can either represent a function result of type \c T or an error of type \c E
41 * \ingroup jkqtptools_general
42 *
43 * Usage example:
44 * \code
45 * JKQTPExpected<double,QString> toInt(QString s) {
46 * bool ok=false;
47 * int i=s.toInt(&ok);
48 * if (ok) return {i}
49 * else return { JKQTPUnexpected, QString("cannot convert to int") };
50 * }
51 *
52 * auto r=toInt("1.0a");
53 * if (r.hasValue()) qDebug()<<"SUCCESS! i="<<r.value();
54 * else qDebug()<<"FAILURE: "<<r.error();
55 * \endcode
56 *
57 *
58 * \see JKQTPExpectedUnexpectedType, JKQTPUnexpected
59 */
60template<class T, class E>
62 JKQTPExpected()=delete;
63 JKQTPExpected(const T& value_): m_hasValue(true), m_value(value_), m_error() {};
64 JKQTPExpected(const JKQTPExpected<T,E>& other)=default;
65 JKQTPExpected(JKQTPExpectedUnexpectedType, const E& error_): m_hasValue(false), m_value(), m_error(error_) {};
66
67 const T& value() const {
68 if (m_hasValue) return m_value;
69 throw std::runtime_error("value not available");
70 }
71 const E& error() const {
72 if (!m_hasValue) return m_error;
73 throw std::runtime_error("error not available");
74 }
75 bool has_value() const { return m_hasValue; }
76 operator bool() const { return m_hasValue; }
77private:
78 const bool m_hasValue;
79 const T m_value;
80 const E m_error;
81};
82
83
84#endif // JKQTPEXPECTED_H_INCLUDED
tag type for an unexpected/error result in JKQTPExpected
Definition jkqtpexpected.h:32
an "expected" datatype, which can either represent a function result of type T or an error of type E
Definition jkqtpexpected.h:61
const E & error() const
Definition jkqtpexpected.h:71
const T m_value
Definition jkqtpexpected.h:79
const E m_error
Definition jkqtpexpected.h:80
const T & value() const
Definition jkqtpexpected.h:67
JKQTPExpected(const T &value_)
Definition jkqtpexpected.h:63
JKQTPExpected(JKQTPExpectedUnexpectedType, const E &error_)
Definition jkqtpexpected.h:65
const bool m_hasValue
Definition jkqtpexpected.h:78
bool has_value() const
Definition jkqtpexpected.h:75
JKQTPExpected(const JKQTPExpected< T, E > &other)=default
JKQTPExpected()=delete