1 // Copyright 2022 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #pragma once 15 16 #include <optional> 17 #include <utility> 18 19 #include "pw_assert/assert.h" 20 21 namespace pw::bluetooth { 22 23 // Deprecated. Use pw::expected instead. 24 // A Result represents the result of an operation which can fail. If it 25 // represents an error, it contains an error value. If it represents success, it 26 // contains zero or one success value. 27 template <typename E, typename... Ts> 28 class Result; 29 30 // Result specialization for returning OK or an error (E). 31 template <typename E> 32 class [[nodiscard]] Result<E> { 33 public: 34 constexpr Result() = default; Result(E error)35 constexpr Result(E error) : error_(error) {} 36 37 constexpr Result(const Result&) = default; 38 constexpr Result& operator=(const Result&) = default; 39 40 constexpr Result(Result&&) = default; 41 constexpr Result& operator=(Result&&) = default; 42 error()43 [[nodiscard]] constexpr E error() const { 44 PW_ASSERT(error_.has_value()); 45 return error_.value(); 46 } ok()47 [[nodiscard]] constexpr bool ok() const { return !error_.has_value(); } 48 49 private: 50 std::optional<E> error_; 51 }; 52 53 // Deprecated. Use pw::expected instead. 54 // Result specialization for returning some data (T) or an error (E). 55 template <typename E, typename T> 56 class [[nodiscard]] Result<E, T> { 57 public: Result(T && value)58 constexpr Result(T&& value) : value_(std::move(value)) {} Result(const T & value)59 constexpr Result(const T& value) : value_(value) {} 60 61 template <typename... Args> Result(std::in_place_t,Args &&...args)62 constexpr Result(std::in_place_t, Args&&... args) 63 : value_(std::forward<Args>(args)...) {} 64 Result(E error)65 constexpr Result(E error) : error_(error) {} 66 67 constexpr Result(const Result&) = default; 68 constexpr Result& operator=(const Result&) = default; 69 70 constexpr Result(Result&&) = default; 71 constexpr Result& operator=(Result&&) = default; 72 error()73 [[nodiscard]] constexpr E error() const { 74 PW_ASSERT(!value_.has_value()); 75 return error_; 76 } ok()77 [[nodiscard]] constexpr bool ok() const { return value_.has_value(); } 78 value()79 constexpr T& value() & { 80 PW_ASSERT(value_.has_value()); 81 return value_.value(); 82 } 83 value()84 constexpr const T& value() const& { 85 PW_ASSERT(value_.has_value()); 86 return value_.value(); 87 } 88 value()89 constexpr T&& value() && { 90 PW_ASSERT(value_.has_value()); 91 return std::move(value_.value()); 92 } 93 94 private: 95 std::optional<T> value_; 96 // error_ is only initialized if value_ is empty. 97 E error_ = {}; 98 }; 99 100 } // namespace pw::bluetooth 101