Coverage Report

Created: 2026-08-13 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pdns/pdns/dnsdistdist/expected.hh
Line
Count
Source
1
/*
2
 * This file is part of PowerDNS or dnsdist.
3
 * Copyright -- PowerDNS.COM B.V. and its contributors
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of version 2 of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * In addition, for the avoidance of any doubt, permission is granted to
10
 * link this program with OpenSSL and to (re)distribute the binaries
11
 * produced as the result of such linking.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.
20
 */
21
#pragma once
22
23
#include <variant>
24
25
// A poor man's std::expected, which only becomes available for real with C++23
26
27
namespace pdns
28
{
29
template <class E>
30
class unexpected
31
{
32
public:
33
  explicit unexpected(const E& arg) :
34
0
    err(arg) {}
35
  const E& error() const
36
0
  {
37
0
    return err;
38
0
  }
39
40
private:
41
  E err;
42
};
43
44
template <class T, class E>
45
class expected : private std::variant<T, E>
46
{
47
public:
48
  expected(const T& arg) :
49
    std::variant<T, E>(arg) {}
50
  expected(T&& arg) :
51
0
    std::variant<T, E>(std::forward<T>(arg)) {}
52
53
  expected(const unexpected<E>& arg) :
54
0
    std::variant<T, E>(arg.error()) {}
55
56
  [[nodiscard]] bool has_value() const
57
0
  {
58
0
    return std::holds_alternative<T>(*this);
59
0
  }
60
61
  const T& value() const
62
0
  {
63
0
    return std::get<T>(*this);
64
0
  }
65
  const E& error() const
66
0
  {
67
0
    return std::get<E>(*this);
68
0
  }
69
};
70
}