xref: /aosp_15_r20/system/teeui/libteeui/include/teeui/error.h (revision 20bfefbe1966c142a35ae1ab84a8af250b3fd403)
1*20bfefbeSAndroid Build Coastguard Worker /*
2*20bfefbeSAndroid Build Coastguard Worker  *
3*20bfefbeSAndroid Build Coastguard Worker  * Copyright 2019, The Android Open Source Project
4*20bfefbeSAndroid Build Coastguard Worker  *
5*20bfefbeSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
6*20bfefbeSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
7*20bfefbeSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
8*20bfefbeSAndroid Build Coastguard Worker  *
9*20bfefbeSAndroid Build Coastguard Worker  *     http://www.apache.org/licenses/LICENSE-2.0
10*20bfefbeSAndroid Build Coastguard Worker  *
11*20bfefbeSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
12*20bfefbeSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
13*20bfefbeSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*20bfefbeSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
15*20bfefbeSAndroid Build Coastguard Worker  * limitations under the License.
16*20bfefbeSAndroid Build Coastguard Worker  */
17*20bfefbeSAndroid Build Coastguard Worker 
18*20bfefbeSAndroid Build Coastguard Worker #ifndef LIBTEEUI_INCLUDE_TEEUI_ERROR_H_
19*20bfefbeSAndroid Build Coastguard Worker #define LIBTEEUI_INCLUDE_TEEUI_ERROR_H_
20*20bfefbeSAndroid Build Coastguard Worker 
21*20bfefbeSAndroid Build Coastguard Worker #include "log.h"
22*20bfefbeSAndroid Build Coastguard Worker #include <stdint.h>
23*20bfefbeSAndroid Build Coastguard Worker 
24*20bfefbeSAndroid Build Coastguard Worker namespace teeui {
25*20bfefbeSAndroid Build Coastguard Worker 
26*20bfefbeSAndroid Build Coastguard Worker class Error {
27*20bfefbeSAndroid Build Coastguard Worker   public:
28*20bfefbeSAndroid Build Coastguard Worker     enum error_e : uint32_t {
29*20bfefbeSAndroid Build Coastguard Worker         OK,
30*20bfefbeSAndroid Build Coastguard Worker         NotInitialized,
31*20bfefbeSAndroid Build Coastguard Worker         FaceNotLoaded,
32*20bfefbeSAndroid Build Coastguard Worker         CharSizeNotSet,
33*20bfefbeSAndroid Build Coastguard Worker         GlyphNotLoaded,
34*20bfefbeSAndroid Build Coastguard Worker         GlyphNotRendered,
35*20bfefbeSAndroid Build Coastguard Worker         GlyphNotExtracted,
36*20bfefbeSAndroid Build Coastguard Worker         UnsupportedPixelFormat,
37*20bfefbeSAndroid Build Coastguard Worker         OutOfBoundsDrawing,
38*20bfefbeSAndroid Build Coastguard Worker         BBoxComputation,
39*20bfefbeSAndroid Build Coastguard Worker         OutOfMemory,
40*20bfefbeSAndroid Build Coastguard Worker         Localization,
41*20bfefbeSAndroid Build Coastguard Worker     };
42*20bfefbeSAndroid Build Coastguard Worker 
Error()43*20bfefbeSAndroid Build Coastguard Worker     constexpr Error() noexcept : v_(OK) {}
Error(error_e v)44*20bfefbeSAndroid Build Coastguard Worker     constexpr Error(error_e v) noexcept : v_(v) {}
45*20bfefbeSAndroid Build Coastguard Worker     constexpr Error(Error&&) noexcept = default;
46*20bfefbeSAndroid Build Coastguard Worker     constexpr Error(const Error&) noexcept = default;
47*20bfefbeSAndroid Build Coastguard Worker 
48*20bfefbeSAndroid Build Coastguard Worker     Error& operator=(error_e v) {
49*20bfefbeSAndroid Build Coastguard Worker         v_ = v;
50*20bfefbeSAndroid Build Coastguard Worker         return *this;
51*20bfefbeSAndroid Build Coastguard Worker     }
52*20bfefbeSAndroid Build Coastguard Worker     Error& operator=(const Error& other) {
53*20bfefbeSAndroid Build Coastguard Worker         v_ = other.v_;
54*20bfefbeSAndroid Build Coastguard Worker         return *this;
55*20bfefbeSAndroid Build Coastguard Worker     }
56*20bfefbeSAndroid Build Coastguard Worker 
57*20bfefbeSAndroid Build Coastguard Worker     /**
58*20bfefbeSAndroid Build Coastguard Worker      * Evaluates to true if this represents an error condition.
59*20bfefbeSAndroid Build Coastguard Worker      * Consider a function Error foo(), use the following pattern to handle the error.
60*20bfefbeSAndroid Build Coastguard Worker      * if (auto error = foo()) {  <handle error here> }
61*20bfefbeSAndroid Build Coastguard Worker      */
62*20bfefbeSAndroid Build Coastguard Worker     operator bool() const { return v_ != OK; }
63*20bfefbeSAndroid Build Coastguard Worker 
64*20bfefbeSAndroid Build Coastguard Worker     Error operator||(const Error& rhs) const { return *this ? *this : rhs; }
65*20bfefbeSAndroid Build Coastguard Worker 
66*20bfefbeSAndroid Build Coastguard Worker     inline bool operator==(error_e v) const { return v_ == v; };
67*20bfefbeSAndroid Build Coastguard Worker     inline bool operator!=(error_e v) const { return v_ != v; };
68*20bfefbeSAndroid Build Coastguard Worker 
code()69*20bfefbeSAndroid Build Coastguard Worker     constexpr error_e code() const { return v_; }
70*20bfefbeSAndroid Build Coastguard Worker 
71*20bfefbeSAndroid Build Coastguard Worker   private:
72*20bfefbeSAndroid Build Coastguard Worker     error_e v_;
73*20bfefbeSAndroid Build Coastguard Worker };
74*20bfefbeSAndroid Build Coastguard Worker 
75*20bfefbeSAndroid Build Coastguard Worker #ifdef TEEUI_DO_LOG_DEBUG
76*20bfefbeSAndroid Build Coastguard Worker // keep this in the header, so the test can switch it on without switching it on in the static
77*20bfefbeSAndroid Build Coastguard Worker // library
78*20bfefbeSAndroid Build Coastguard Worker [[maybe_unused]] static std::ostream& operator<<(std::ostream& out, Error e) {
79*20bfefbeSAndroid Build Coastguard Worker     switch (e.code()) {
80*20bfefbeSAndroid Build Coastguard Worker     case Error::OK:
81*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::OK";
82*20bfefbeSAndroid Build Coastguard Worker     case Error::NotInitialized:
83*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::NotInitialized";
84*20bfefbeSAndroid Build Coastguard Worker     case Error::FaceNotLoaded:
85*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::FaceNotLoaded";
86*20bfefbeSAndroid Build Coastguard Worker     case Error::CharSizeNotSet:
87*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::CharSizeNotSet";
88*20bfefbeSAndroid Build Coastguard Worker     case Error::GlyphNotLoaded:
89*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::GlyphNotLoaded";
90*20bfefbeSAndroid Build Coastguard Worker     case Error::GlyphNotRendered:
91*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::GlyphNotRendered";
92*20bfefbeSAndroid Build Coastguard Worker     case Error::GlyphNotExtracted:
93*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::GlyphNotExtracted";
94*20bfefbeSAndroid Build Coastguard Worker     case Error::UnsupportedPixelFormat:
95*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::UnsupportedPixelFormat";
96*20bfefbeSAndroid Build Coastguard Worker     case Error::OutOfBoundsDrawing:
97*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::OutOfBoundsDrawing";
98*20bfefbeSAndroid Build Coastguard Worker     case Error::BBoxComputation:
99*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::BBoxComputation";
100*20bfefbeSAndroid Build Coastguard Worker     case Error::OutOfMemory:
101*20bfefbeSAndroid Build Coastguard Worker         return out << "teeui::Error::OutOfMemory";
102*20bfefbeSAndroid Build Coastguard Worker     default:
103*20bfefbeSAndroid Build Coastguard Worker         return out << "Invalid teeui::Error Code";
104*20bfefbeSAndroid Build Coastguard Worker     }
105*20bfefbeSAndroid Build Coastguard Worker }
106*20bfefbeSAndroid Build Coastguard Worker #endif
107*20bfefbeSAndroid Build Coastguard Worker 
108*20bfefbeSAndroid Build Coastguard Worker }  // namespace teeui
109*20bfefbeSAndroid Build Coastguard Worker 
110*20bfefbeSAndroid Build Coastguard Worker #endif  // LIBTEEUI_INCLUDE_TEEUI_ERROR_H_
111