NEW add JKQTPExpected

This commit is contained in:
jkriege2 2024-01-21 22:05:26 +01:00
parent a4fef01642
commit 2559097fd2
4 changed files with 94 additions and 3 deletions

View File

@ -27,7 +27,8 @@ Changes, compared to \ref page_whatsnew_V4_0_0 "v4.0.0" include:
<li>NEW/BREAKING: provide general targets JKQTPlotter5/6::JKQTPlotter5/6, JKQTPlotter5/6::JKQTMathText5/6, ... which are independent of the type of build (shared/static)</li>
<li>NEW/BREAKING: refactor CMake-Code, so static/dynamic switch is done via <code>BUILD_SHARED_LIBS</code>, which retires <code>JKQtPlotter_BUILD_STATIC_LIBS</code>, <code>JKQtPlotter_BUILD_SHARED_LIBS</code> and removes the capability to build static and shared libraries in one location (fixes issue #104)</li>
<li>NEW: prepareed library for CMake's <a href="https://cmake.org/cmake/help/latest/module/FetchContent.html">FetchContent</a>-API</li>
<li>NEW: the different sub-libraries JKQTPlotter, JKQTFastPlotter (DEPRECATED), JKQTMath, JKQTMathText can be activated/deactivated with CMake options JKQtPlotter_BUILD_LIB_JKQTPLOTTER, JKQtPlotter_BUILD_LIB_JKQTFASTPLOTTER, JKQtPlotter_BUILD_LIB_JKQTMATHTEXT, JKQtPlotter_BUILD_LIB_JKQTMATH</li>
<li>NEW: the different sub-libraries JKQTPlotter, JKQTFastPlotter (DEPRECATED), JKQTMath, JKQTMathText can be activated/deactivated with CMake options JKQtPlotter_BUILD_LIB_JKQTPLOTTER, JKQtPlotter_BUILD_LIB_JKQTFASTPLOTTER, JKQtPlotter_BUILD_LIB_JKQTMATHTEXT, JKQtPlotter_BUILD_LIB_JKQTMATH</li>
<li>NEW add JKQTPExpected datatype</li>
</ul></li>
<li>JKQTPlotter:<ul>

View File

@ -27,7 +27,10 @@ isEmpty(JKQTP_COMMON_PRI_INCLUDED) {
$$PWD/jkqtcommon/jkqtpenhancedpainter.h \
$$PWD/jkqtcommon/jkqtphighrestimer.h \
$$PWD/jkqtcommon/jkqttools.h \
$$PWD/jkqtcommon/jkqtpicons.h
$$PWD/jkqtcommon/jkqtpicons.h \
$$PWD/jkqtcommon/jkqtpcachingtools.h \
$$PWD/jkqtcommon/jkqtpconcurrencytools.h \
$$PWD/jkqtcommon/jkqtpexpected.h
@ -41,7 +44,9 @@ isEmpty(JKQTP_COMMON_PRI_INCLUDED) {
$$PWD/jkqtcommon/jkqtpenhancedpainter.cpp \
$$PWD/jkqtcommon/jkqtphighrestimer.cpp \
$$PWD/jkqtcommon/jkqttools.cpp \
$$PWD/jkqtcommon/jkqtpicons.cpp
$$PWD/jkqtcommon/jkqtpicons.cpp \
$$PWD/jkqtcommon/jkqtpcachingtools.cpp \
$$PWD/jkqtcommon/jkqtpconcurrencytools.cpp
INCLUDEPATH += $$PWD

View File

@ -47,6 +47,7 @@ target_sources(${lib_name} PUBLIC FILE_SET HEADERS TYPE HEADERS
jkqtpgeometrytools.h
jkqtpconcurrencytools.h
jkqtpcachingtools.h
jkqtpexpected.h
)

View File

@ -0,0 +1,84 @@
/*
Copyright (c) 2008-2024 Jan W. Krieger (<jan@jkrieger.de>)
This software is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef JKQTPEXPECTED_H_INCLUDED
#define JKQTPEXPECTED_H_INCLUDED
#include <exception>
/** \brief tag type for an unexpected/error result in JKQTPExpected
* \ingroup jkqtptools_general
*
* \see JKQTPExpected, JKQTPUnexpected
*/
struct JKQTPExpectedUnexpectedType {};
/** \brief tag for an unexpected/error result in JKQTPExpected
* \ingroup jkqtptools_general
*
* \see JKQTPExpected, JKQTPExpectedUnexpectedType
*/
inline constexpr JKQTPExpectedUnexpectedType JKQTPUnexpected {};
/** \brief an "expected" datatype, which can either represent a function result of type \c T or an error of type \c E
* \ingroup jkqtptools_general
*
* Usage example:
* \code
* JKQTPExpected<double,QString> toInt(QString s) {
* bool ok=false;
* int i=s.toInt(&ok);
* if (ok) return {i}
* else return { JKQTPUnexpected, QString("cannot convert to int") };
* }
*
* auto r=toInt("1.0a");
* if (r.hasValue()) qDebug()<<"SUCCESS! i="<<r.value();
* else qDebug()<<"FAILURE: "<<r.error();
* \endcode
*
*
* \see JKQTPExpectedUnexpectedType, JKQTPUnexpected
*/
template<class T, class E>
struct JKQTPExpected {
JKQTPExpected()=delete;
JKQTPExpected(const T& value_): m_hasValue(true), m_value(value_), m_error() {};
JKQTPExpected(const JKQTPExpected<T,E>& other)=default;
JKQTPExpected(JKQTPExpectedUnexpectedType, const E& error_): m_hasValue(false), m_value(), m_error(error_) {};
const T& value() const {
if (m_hasValue) return m_value;
throw std::runtime_error("value not available");
}
const E& error() const {
if (!m_hasValue) return m_error;
throw std::runtime_error("error not available");
}
const bool has_value() const { return m_hasValue; }
operator bool() const { return m_hasValue; }
private:
const bool m_hasValue;
const E m_error;
const T m_value;
};
#endif // JKQTPEXPECTED_H_INCLUDED