/*************************************************************************** * Copyright (C) gempa GmbH * * All rights reserved. * * Contact: gempa GmbH (seiscomp-dev@gempa.de) * * * * GNU Affero General Public License Usage * * This file may be used under the terms of the GNU Affero * * Public License version 3.0 as published by the Free Software Foundation * * and appearing in the file LICENSE included in the packaging of this * * file. Please review the following information to ensure the GNU Affero * * Public License version 3.0 requirements will be met: * * https://www.gnu.org/licenses/agpl-3.0.html. * * * * Other Usage * * Alternatively, this file may be used in accordance with the terms and * * conditions contained in a signed written agreement between you and * * gempa GmbH. * ***************************************************************************/ #ifndef SEISCOMP_CORE_OPTIONAL_H #define SEISCOMP_CORE_OPTIONAL_H #include #include #include #include #include namespace Seiscomp { namespace Core { /** \brief Redefines std::optional * Optional values can be set or unset. * \code * void print(const Optional &v) { * if ( !v ) * cout << "value of v is not set" << endl; * else * cout << *v << endl; * } * * Optional a = 5; * print(a); // output: "5" * a = None; * print(a); // output: "value of v is not set" * \endcode */ template using Optional = ::std::optional; using NoneType = ::std::nullopt_t; /** Defines None */ SC_SYSTEM_CORE_API extern NoneType const None; template struct False : std::integral_constant {}; template struct True : std::integral_constant {}; // Checks whether a type is wrapped with optional or not. template struct isOptional : std::false_type {}; template class U, typename ...Args> struct isOptional> : std::integral_constant< bool, std::is_base_of, U>::value || std::is_base_of, U>::value > {}; template T value(const Optional&); class SC_SYSTEM_CORE_API ValueError : public std::exception { public: ValueError() throw(); ~ValueError() throw(); public: const char* what() const throw() override; }; /** Macro to use optional values easily */ #define OPT(T) Seiscomp::Core::Optional /** Macro to use optional values as const reference */ #define OPT_CR(T) const Seiscomp::Core::Optional& #include } } #endif