Android-cuttlefish cvd tool
error_type.h
Go to the documentation of this file.
1//
2// Copyright (C) 2022 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#pragma once
17
18#include <unistd.h>
19
20#include <ostream>
21#include <string>
22#include <utility>
23#include <vector>
24
25#include "tl/expected.hpp"
26
29
30namespace cuttlefish {
31
32std::string ResultErrorFormat(bool color);
33
34template <typename E>
36 public:
37 friend class StackTraceError<void>;
38 using ValueOrBool = std::conditional_t<std::is_void_v<E>, bool, E>;
39
40 explicit StackTraceError()
41 requires std::is_void_v<E>
42 {}
43 explicit StackTraceError(StackTraceEntry first_entry)
44 requires std::is_void_v<E>
45 {
46 stack_.emplace_back(std::move(first_entry));
47 }
48 explicit StackTraceError(std::vector<StackTraceEntry> stack)
49 requires std::is_void_v<E>
50 : stack_(std::move(stack)) {}
52 requires(!std::is_void_v<E>)
53 : value_(std::move(value)) {}
55 std::vector<StackTraceEntry> stack)
56 requires(!std::is_void_v<E>)
57 : value_(std::move(value)), stack_(std::move(stack)) {}
58 explicit StackTraceError(ValueOrBool value, StackTraceEntry first_entry)
59 requires(!std::is_void_v<E>)
60 : value_(std::move(value)) {
61 stack_.emplace_back(std::move(first_entry));
62 }
63 template <typename E2>
65 requires std::is_void_v<E>
66 : stack_(std::move(other.stack_)) {}
67
69 stack_.emplace_back(std::move(entry));
70 return *this;
71 }
73 return std::move(this->PushEntry(entry));
74 }
75 const std::vector<StackTraceEntry>& Stack() const { return stack_; }
76
77 std::string Message() const;
78
79 std::string Trace() const;
80
81 std::string FormatForEnv(bool color = ShouldColorStdout()) const;
82
83 template <typename T>
84 operator tl::expected<T, StackTraceError>() && {
85 return tl::unexpected(std::move(*this));
86 }
87
89 requires(!std::is_void_v<E>)
90 {
91 return value_;
92 }
93
94 const ValueOrBool& Value() const
95 requires(!std::is_void_v<E>)
96 {
97 return value_;
98 }
99
101 requires(!std::is_void_v<E>)
102 {
103 return value_;
104 }
105
106 const ValueOrBool& operator*() const
107 requires(!std::is_void_v<E>)
108 {
109 return value_;
110 }
111
112 template <typename E2>
114 return StackTraceError<E2>(std::move(value), stack_);
115 }
116
117 template <typename E2>
119 return StackTraceError<E2>(std::move(value), std::move(stack_));
120 }
121
123
124 operator StackTraceError<void>() && {
125 return StackTraceError<void>(std::move(stack_));
126 }
127
128 private:
130 std::vector<StackTraceEntry> stack_;
131};
132
133template <class E>
135
136template <typename E>
137std::ostream& operator<<(std::ostream& out, const StackTraceError<E>& error) {
138 return out << StackTraceError<void>(error).FormatForEnv();
139}
140
141} // namespace cuttlefish
Definition: stack_trace_entry.h:27
Definition: error_type.h:35
StackTraceError< E > PushEntry(StackTraceEntry entry) &&
Definition: error_type.h:72
std::conditional_t< std::is_void_v< E >, bool, E > ValueOrBool
Definition: error_type.h:38
const ValueOrBool & Value() const
Definition: error_type.h:94
std::string Message() const
const ValueOrBool & operator*() const
Definition: error_type.h:106
const std::vector< StackTraceEntry > & Stack() const
Definition: error_type.h:75
StackTraceError(ValueOrBool value, std::vector< StackTraceEntry > stack)
Definition: error_type.h:54
ValueOrBool & operator*()
Definition: error_type.h:100
ValueOrBool value_
Definition: error_type.h:129
ValueOrBool & Value()
Definition: error_type.h:88
friend class StackTraceError< void >
Definition: error_type.h:37
StackTraceError< E2 > WithValue(E2 value) &&
Definition: error_type.h:118
std::string FormatForEnv(bool color=ShouldColorStdout()) const
StackTraceError(StackTraceError< E2 > other)
Definition: error_type.h:64
StackTraceError(ValueOrBool value, StackTraceEntry first_entry)
Definition: error_type.h:58
StackTraceError()
Definition: error_type.h:40
StackTraceError(StackTraceEntry first_entry)
Definition: error_type.h:43
StackTraceError< E > & PushEntry(StackTraceEntry entry) &
Definition: error_type.h:68
std::string Trace() const
StackTraceError(std::vector< StackTraceEntry > stack)
Definition: error_type.h:48
StackTraceError< E2 > WithValue(E2 value) &
Definition: error_type.h:113
std::vector< StackTraceEntry > stack_
Definition: error_type.h:130
StackTraceError(ValueOrBool value)
Definition: error_type.h:51
#define error(format, args...)
Definition: fec_private.h:199
Definition: alloc_driver.h:21
std::string ResultErrorFormat(bool color)
Definition: error_type.cc:327
bool ShouldColorStdout()
Definition: should_color.cc:40
StackTraceError(E, StackTraceEntry) -> StackTraceError< E >
std::ostream & operator<<(std::ostream &out, DeviceType device_type)
Definition: device_type.cpp:73
unexpected(E) -> unexpected< E >