arg_router  1.4.0
C++ command line argument parsing and routing
result.hpp
1 // Copyright (C) 2022 by Camden Mannett.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
4 
5 #pragma once
6 
7 #include "arg_router/exception.hpp"
8 
9 #include <variant>
10 
11 namespace arg_router::utility
12 {
18 template <typename ResultType, typename ExceptionType>
19 class result
20 {
21  static_assert(!std::is_same_v<ResultType, ExceptionType>,
22  "Result and exception argument cannot be same type");
23 
24 public:
26  using result_type = std::decay_t<ResultType>;
28  using exception_type = ExceptionType;
29 
34  // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
35  constexpr result(result_type value) noexcept : data_{std::move(value)} {}
36 
41  // NOLINTNEXTLINE(google-explicit-constructor, hicpp-explicit-conversions)
42  constexpr result(ExceptionType ex) noexcept : data_{std::move(ex)} {}
43 
48  [[nodiscard]] constexpr bool has_result() const noexcept { return data_.index() == 0; }
49 
54  [[nodiscard]] constexpr bool has_error() const noexcept { return !has_result(); }
55 
60  [[nodiscard]] explicit constexpr operator bool() const noexcept { return has_result(); }
61 
66  [[nodiscard]] constexpr bool operator!() const noexcept { return has_error(); }
67 
73  [[nodiscard]] constexpr bool operator==(const result& other) const noexcept
74  {
75  if (has_error() || other.has_error()) {
76  return false;
77  }
78  return std::get<0>(data_) == std::get<0>(other.data_);
79  }
80 
86  [[nodiscard]] constexpr bool operator!=(const result& other) const noexcept
87  {
88  return !(*this == other);
89  }
90 
97  [[nodiscard]] friend constexpr bool operator==(const result& lhs,
98  const result_type& rhs) noexcept
99  {
100  if (lhs.has_error()) {
101  return false;
102  }
103  return std::get<0>(lhs.data_) == rhs;
104  }
105 
112  [[nodiscard]] friend constexpr bool operator!=(const result& lhs,
113  const result_type& rhs) noexcept
114  {
115  return !(lhs == rhs);
116  }
117 
124  [[nodiscard]] friend constexpr bool operator==(const result_type& lhs,
125  const result& rhs) noexcept
126  {
127  return rhs == lhs;
128  }
129 
136  [[nodiscard]] friend constexpr bool operator!=(const result_type& lhs,
137  const result& rhs) noexcept
138  {
139  return !(lhs == rhs);
140  }
141 
147  [[nodiscard]] result_type* get_if() noexcept
148  {
149  return has_result() ? &std::get<0>(data_) : nullptr;
150  }
151 
156  [[nodiscard]] const result_type* get_if() const noexcept
157  {
158  return has_result() ? &std::get<0>(data_) : nullptr;
159  }
160 
169  {
170  if (has_result()) {
171  return std::move(std::get<0>(data_));
172  }
173  throw std::move(std::get<1>(data_));
174  }
175 
182  [[nodiscard]] auto get() const
183  -> std::conditional_t<(sizeof(std::size_t) >= sizeof(result_type)) &&
184  std::is_copy_constructible_v<result_type>,
185  result_type,
186  const result_type&>
187  {
188  if (has_result()) {
189  return std::get<0>(data_);
190  }
191  throw exception_type{std::get<1>(data_)};
192  }
193 
198  void throw_exception() const
199  {
200  if (has_error()) {
201  throw exception_type{std::get<1>(data_)};
202  }
203  }
204 
205 private:
206  std::variant<result_type, exception_type> data_;
207 };
208 } // namespace arg_router::utility
constexpr bool has_error() const noexcept
Definition: result.hpp:54
constexpr bool operator!=(const result &other) const noexcept
Definition: result.hpp:86
constexpr bool operator==(const result &other) const noexcept
Definition: result.hpp:73
constexpr friend bool operator!=(const result &lhs, const result_type &rhs) noexcept
Definition: result.hpp:112
result_type * get_if() noexcept
Definition: result.hpp:147
constexpr bool operator!() const noexcept
Definition: result.hpp:66
auto get() const -> std::conditional_t<(sizeof(std::size_t) >=sizeof(result_type)) &&std::is_copy_constructible_v< result_type >, result_type, const result_type & >
Definition: result.hpp:182
void throw_exception() const
Definition: result.hpp:198
constexpr friend bool operator==(const result &lhs, const result_type &rhs) noexcept
Definition: result.hpp:97
const result_type * get_if() const noexcept
Definition: result.hpp:156
constexpr result(ExceptionType ex) noexcept
Definition: result.hpp:42
constexpr friend bool operator==(const result_type &lhs, const result &rhs) noexcept
Definition: result.hpp:124
constexpr bool has_result() const noexcept
Definition: result.hpp:48
std::decay_t< ResultType > result_type
Definition: result.hpp:26
constexpr result(result_type value) noexcept
Definition: result.hpp:35
ExceptionType exception_type
Definition: result.hpp:28
constexpr friend bool operator!=(const result_type &lhs, const result &rhs) noexcept
Definition: result.hpp:136