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