Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Result.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Result.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <optional>
12 #include <sstream>
13 #include <system_error>
14 #include <type_traits>
15 #include <utility>
16 #include <variant>
17 
18 namespace Acts {
19 
24 template <typename T, typename E = std::error_code>
25 class Result {
29  Result(std::variant<T, E>&& var) : m_var(std::move(var)) {}
30 
31  public:
33  Result() = delete;
34 
36  Result(const Result<T, E>& other) = delete;
37 
39  Result<T, E>& operator=(const Result<T, E>& other) = delete;
40 
42  Result(Result<T, E>&& other) : m_var(std::move(other.m_var)) {}
43 
48  m_var = std::move(other.m_var);
49  return *this;
50  }
51 
64  template <
65  typename T2, typename _E = E, typename _T = T,
66  typename = std::enable_if_t<
67  (!std::is_same_v<_T, _E> && !std::is_constructible_v<_T, _E> &&
68  !std::is_convertible_v<_T, _E> && !std::is_constructible_v<_E, _T> &&
69  !std::is_convertible_v<_E, _T> &&
70  !(std::is_convertible_v<T2, _T> && std::is_convertible_v<T2, _E>))>>
71  Result(T2 value) noexcept
72  : m_var(std::conditional_t<std::is_convertible_v<T2, _T>, T, E>{
73  std::move(value)}) {}
74 
82  template <
83  typename T2, typename _E = E, typename _T = T,
84  typename = std::enable_if_t<
85  (!std::is_same_v<_T, _E> && !std::is_constructible_v<_T, _E> &&
86  !std::is_convertible_v<_T, _E> && !std::is_constructible_v<_E, _T> &&
87  !std::is_convertible_v<_E, _T> &&
88  !(std::is_convertible_v<T2, _T> && std::is_convertible_v<T2, _E>))>>
89  Result<T, E>& operator=(T2 value) noexcept {
90  m_var = std::move(std::conditional_t<std::is_convertible_v<T2, _T>, T, E>{
91  std::move(value)});
92  return *this;
93  }
94 
98  static Result<T, E> success(T value) {
99  return Result<T, E>(
100  std::variant<T, E>{std::in_place_index<0>, std::move(value)});
101  }
102 
106  static Result<T, E> failure(E error) {
107  return Result<T, E>(
108  std::variant<T, E>{std::in_place_index<1>, std::move(error)});
109  }
110 
113  bool ok() const noexcept { return m_var.index() == 0; }
114 
118  T& operator*() noexcept { return std::get<T>(m_var); }
119 
123  const T& operator*() const noexcept { return std::get<T>(m_var); }
124 
129  T* operator->() noexcept { return &std::get<T>(m_var); }
130 
135  const T* operator->() const noexcept { return &std::get<T>(m_var); }
136 
140  E& error() & noexcept { return std::get<E>(m_var); }
141 
145  const E& error() const& noexcept { return std::get<E>(m_var); }
146 
150  E error() && noexcept { return std::move(std::get<E>(m_var)); }
151 
155  T& value() & {
157  return std::get<T>(m_var);
158  }
159 
163  const T& value() const& {
165  return std::get<T>(m_var);
166  }
167 
172  T value() && {
174  return std::move(std::get<T>(m_var));
175  }
176 
177  private:
178  std::variant<T, E> m_var;
179 
180  void checkValueAccess() const {
181  if (m_var.index() != 0) {
182  if constexpr (std::is_same_v<E, std::error_code>) {
183  std::stringstream ss;
184  const auto& e = std::get<E>(m_var);
185  ss << "Value called on error value: " << e.category().name() << ": "
186  << e.message() << " [" << e.value() << "]";
187  throw std::runtime_error(ss.str());
188  } else {
189  throw std::runtime_error("Value called on error value");
190  }
191  }
192  }
193 };
194 
207 template <typename E>
208 class Result<void, E> {
209  public:
211  Result() = default;
212 
214  Result(const Result<void, E>& other) = default;
215 
217  Result<void, E>& operator=(const Result<void, E>& other) = default;
218 
221  Result(Result<void, E>&& other) : m_opt(std::move(other.m_opt)) {}
222 
226  m_opt = std::move(other.m_opt);
227  return *this;
228  }
229 
234  template <typename E2>
235  Result(E2 error) noexcept : m_opt(std::move(error)) {}
236 
241  template <typename E2>
243  m_opt = std::move(error);
244  return *this;
245  }
246 
249  static Result<void, E> success() { return Result<void, E>(); }
250 
255  return Result<void, E>(std::move(error));
256  }
257 
260  bool ok() const noexcept { return !m_opt; }
261 
265  E& error() & noexcept { return m_opt.value(); }
266 
270  const E& error() const& noexcept { return m_opt.value(); }
271 
275  E error() && noexcept { return std::move(m_opt.value()); }
276 
277  private:
278  std::optional<E> m_opt;
279 };
280 
281 } // namespace Acts