1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <string>
20 #include <utility>
21 #include <variant>
22 
23 #include <android-base/logging.h>
24 
25 namespace android {
26 namespace jsonpb {
27 
28 template <typename T>
29 struct ErrorOr {
30   template <class... Args>
ErrorOrErrorOr31   explicit ErrorOr(Args&&... args) : data_(kIndex1, std::forward<Args>(args)...) {}
32   T& operator*() {
33     CHECK(ok());
34     return *std::get_if<1u>(&data_);
35   }
36   const T& operator*() const {
37     CHECK(ok());
38     return *std::get_if<1u>(&data_);
39   }
40   T* operator->() {
41     CHECK(ok());
42     return std::get_if<1u>(&data_);
43   }
44   const T* operator->() const {
45     CHECK(ok());
46     return std::get_if<1u>(&data_);
47   }
errorErrorOr48   const std::string& error() const {
49     CHECK(!ok());
50     return *std::get_if<0u>(&data_);
51   }
okErrorOr52   bool ok() const { return data_.index() != 0; }
MakeErrorErrorOr53   static ErrorOr<T> MakeError(std::string message) {
54     return ErrorOr<T>(std::move(message), Tag::kDummy);
55   }
56 
57  private:
58   enum class Tag { kDummy };
59   static constexpr std::in_place_index_t<0> kIndex0{};
60   static constexpr std::in_place_index_t<1> kIndex1{};
ErrorOrErrorOr61   ErrorOr(std::string msg, Tag) : data_(kIndex0, std::move(msg)) {}
62 
63   std::variant<std::string, T> data_;
64 };
65 
66 template <typename T>
MakeError(std::string message)67 inline ErrorOr<T> MakeError(std::string message) {
68   return ErrorOr<T>::MakeError(std::move(message));
69 }
70 
71 }  // namespace jsonpb
72 }  // namespace android
73