tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ResultVariant.h (2293B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: set ts=8 sts=2 et sw=2 tw=80:
      3 * This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /* A type suitable for returning either a value or an error from a function. */
      8 
      9 #ifndef mozilla_ResultVariant_h
     10 #define mozilla_ResultVariant_h
     11 
     12 #include "mozilla/Result.h"
     13 #include "mozilla/Variant.h"
     14 
     15 namespace mozilla::detail {
     16 
     17 template <typename V, typename E>
     18 class ResultImplementation<V, E, PackingStrategy::Variant> {
     19  mozilla::Variant<V, E> mStorage;
     20 
     21 public:
     22  static constexpr PackingStrategy Strategy = PackingStrategy::Variant;
     23 
     24  ResultImplementation(ResultImplementation&&) = default;
     25  ResultImplementation(const ResultImplementation&) = delete;
     26  ResultImplementation& operator=(const ResultImplementation&) = delete;
     27  ResultImplementation& operator=(ResultImplementation&&) = default;
     28 
     29  explicit ResultImplementation(V&& aValue) : mStorage(std::move(aValue)) {}
     30  explicit ResultImplementation(const V& aValue) : mStorage(aValue) {}
     31  template <typename... Args>
     32  explicit ResultImplementation(std::in_place_t, Args&&... aArgs)
     33      : mStorage(VariantType<V>{}, std::forward<Args>(aArgs)...) {}
     34 
     35  explicit ResultImplementation(const E& aErrorValue) : mStorage(aErrorValue) {}
     36  explicit ResultImplementation(E&& aErrorValue)
     37      : mStorage(std::move(aErrorValue)) {}
     38 
     39  bool isOk() const { return mStorage.template is<V>(); }
     40 
     41  // The callers of these functions will assert isOk() has the proper value, so
     42  // these functions (in all ResultImplementation specializations) don't need
     43  // to do so.
     44  V unwrap() { return std::move(mStorage.template as<V>()); }
     45  const V& inspect() const { return mStorage.template as<V>(); }
     46 
     47  E unwrapErr() { return std::move(mStorage.template as<E>()); }
     48  const E& inspectErr() const { return mStorage.template as<E>(); }
     49 
     50  void updateAfterTracing(V&& aValue) {
     51    mStorage.template emplace<V>(std::move(aValue));
     52  }
     53  void updateErrorAfterTracing(E&& aErrorValue) {
     54    mStorage.template emplace<E>(std::move(aErrorValue));
     55  }
     56 };
     57 
     58 }  // namespace mozilla::detail
     59 
     60 #endif  // mozilla_ResultVariant_h