From: Otto Moerbeek Date: Thu, 9 Jul 2026 11:30:42 +0000 (+0200) Subject: Separate out expected.hh X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=46443286b4dc343cefb58ada6a078a3d81fc12aa;p=thirdparty%2Fpdns.git Separate out expected.hh Signed-off-by: Otto Moerbeek --- diff --git a/pdns/Makefile.am b/pdns/Makefile.am index 934d1f400b..b385081d91 100644 --- a/pdns/Makefile.am +++ b/pdns/Makefile.am @@ -1,4 +1,4 @@ -JSON11_LIBS = $(top_builddir)/ext/json11/libjson11.la +sJSON11_LIBS = $(top_builddir)/ext/json11/libjson11.la ARC4RANDOM_LIBS = $(top_builddir)/ext/arc4random/libarc4random.la AM_CPPFLAGS += \ @@ -237,6 +237,7 @@ pdns_server_SOURCES = \ ednscookies.cc ednscookies.hh \ ednsoptions.cc ednsoptions.hh \ ednssubnet.cc ednssubnet.hh \ + expected.hh \ gettime.cc gettime.hh \ gss_context.cc gss_context.hh \ histogram.hh \ diff --git a/pdns/dnsdistdist/expected.hh b/pdns/dnsdistdist/expected.hh new file mode 120000 index 0000000000..aa08a48fe9 --- /dev/null +++ b/pdns/dnsdistdist/expected.hh @@ -0,0 +1 @@ +../expected.hh \ No newline at end of file diff --git a/pdns/expected.hh b/pdns/expected.hh new file mode 100644 index 0000000000..74128f67ae --- /dev/null +++ b/pdns/expected.hh @@ -0,0 +1,68 @@ +/* + * This file is part of PowerDNS or dnsdist. + * Copyright -- PowerDNS.COM B.V. and its contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of version 2 of the GNU General Public License as + * published by the Free Software Foundation. + * + * In addition, for the avoidance of any doubt, permission is granted to + * link this program with OpenSSL and to (re)distribute the binaries + * produced as the result of such linking. + * + * 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. + */ +#pragma once + +#include + +// A poor man's std::expected, which only becomes available for real with C++23 + +namespace pdns +{ +template +class unexpected +{ +public: + unexpected(const E& arg) : + err(arg) {} + const E& error() const + { + return err; + } + +private: + E err; +}; + +template +class expected : private std::variant +{ +public: + expected(const T& arg) : + std::variant(arg) {} + + expected(const unexpected& arg) : + std::variant(arg.error()) {} + + [[nodiscard]] bool has_value() const + { + return std::holds_alternative(*this); + } + + const T& value() const + { + return std::get(*this); + } + const E& error() const + { + return std::get(*this); + } +}; +} diff --git a/pdns/iputils.hh b/pdns/iputils.hh index 4afcc9ac9e..4bc6e9526a 100644 --- a/pdns/iputils.hh +++ b/pdns/iputils.hh @@ -32,7 +32,7 @@ #include #include #include -#include +#include "expected.hh" #include "namespaces.hh" @@ -2078,51 +2078,6 @@ bool HarvestDestinationAddress(const struct msghdr* msgh, ComboAddress* destinat bool HarvestTimestamp(struct msghdr* msgh, struct timeval* timeval); void fillMSGHdr(struct msghdr* msgh, struct iovec* iov, cmsgbuf_aligned* cbuf, size_t cbufsize, char* data, size_t datalen, ComboAddress* addr); int sendOnNBSocket(int fileDesc, const struct msghdr* msgh); - -// A poor man's std::expected, which only becomes available for real with C++23 -namespace pdns -{ -template -class unexpected -{ -public: - unexpected(const E& arg) : - err(arg) {} - const E& error() const - { - return err; - } - -private: - E err; -}; - -template -class expected : private std::variant -{ -public: - expected(const T& arg) : - std::variant(arg) {} - - expected(const unexpected& arg) : - std::variant(arg.error()) {} - - [[nodiscard]] bool has_value() const - { - return std::holds_alternative(*this); - } - - const T& value() const - { - return std::get(*this); - } - const E& error() const - { - return std::get(*this); - } -}; -} - [[nodiscard]] pdns::expected sendMsgWithOptions(int socketDesc, const void* buffer, size_t len, const ComboAddress* dest, const ComboAddress* local, unsigned int localItf, int flags); /* requires a non-blocking, connected TCP socket */ diff --git a/pdns/recursordist/expected.hh b/pdns/recursordist/expected.hh new file mode 120000 index 0000000000..aa08a48fe9 --- /dev/null +++ b/pdns/recursordist/expected.hh @@ -0,0 +1 @@ +../expected.hh \ No newline at end of file