/src/libheif/libheif/error.h
Line | Count | Source |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2017 Dirk Farin <dirk.farin@gmail.com> |
4 | | * |
5 | | * This file is part of libheif. |
6 | | * |
7 | | * libheif is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libheif is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #ifndef LIBHEIF_ERROR_H |
22 | | #define LIBHEIF_ERROR_H |
23 | | |
24 | | #include <cinttypes> |
25 | | #include <cstddef> |
26 | | |
27 | | #include <vector> |
28 | | #include <string> |
29 | | #include <memory> |
30 | | #include <limits> |
31 | | #include <istream> |
32 | | #include <ostream> |
33 | | #include <sstream> |
34 | | |
35 | | #include "libheif/heif.h" |
36 | | #include <cassert> |
37 | | #include <variant> |
38 | | |
39 | | |
40 | | extern const heif_error heif_error_null_pointer_argument; |
41 | | |
42 | | |
43 | | class ErrorBuffer |
44 | | { |
45 | | public: |
46 | 3.38k | ErrorBuffer() = default; |
47 | | |
48 | | void set_success() |
49 | 0 | { |
50 | 0 | m_error_message = c_success; |
51 | 0 | } |
52 | | |
53 | | void set_error(const std::string& err) |
54 | 0 | { |
55 | 0 | m_buffer = err; |
56 | 0 | m_error_message = m_buffer.c_str(); |
57 | 0 | } |
58 | | |
59 | | const char* get_error() const |
60 | 0 | { |
61 | 0 | return m_error_message; |
62 | 0 | } |
63 | | |
64 | | private: |
65 | | constexpr static const char* c_success = "Success"; |
66 | | std::string m_buffer; |
67 | | const char* m_error_message = c_success; |
68 | | }; |
69 | | |
70 | | |
71 | | class Error |
72 | | { |
73 | | public: |
74 | | heif_error_code error_code = heif_error_Ok; |
75 | | heif_suberror_code sub_error_code = heif_suberror_Unspecified; |
76 | | std::string message; |
77 | | |
78 | | Error(); |
79 | | |
80 | | Error(heif_error_code c, |
81 | | heif_suberror_code sc = heif_suberror_Unspecified, |
82 | | const std::string& msg = ""); |
83 | | |
84 | | static Error from_heif_error(const heif_error&); |
85 | | |
86 | | static const Error Ok; |
87 | | |
88 | | static const Error InternalError; |
89 | | |
90 | | static const char kSuccess[]; |
91 | | |
92 | 3.26M | bool operator==(const Error& other) const { return error_code == other.error_code; } |
93 | | |
94 | 2.86M | bool operator!=(const Error& other) const { return !(*this == other); } |
95 | | |
96 | 0 | Error operator||(const Error& other) const { |
97 | 0 | if (error_code != heif_error_Ok) { |
98 | 0 | return *this; |
99 | 0 | } |
100 | 0 | else { |
101 | 0 | return other; |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | 486k | operator bool() const { return error_code != heif_error_Ok; } |
106 | | |
107 | | static const char* get_error_string(heif_error_code err); |
108 | | |
109 | | static const char* get_error_string(heif_suberror_code err); |
110 | | |
111 | | heif_error error_struct(ErrorBuffer* error_buffer) const; |
112 | | }; |
113 | | |
114 | | |
115 | | inline std::ostream& operator<<(std::ostream& ostr, const Error& err) |
116 | 0 | { |
117 | 0 | ostr << err.error_code << "/" << err.sub_error_code; |
118 | 0 | return ostr; |
119 | 0 | } |
120 | | |
121 | | |
122 | | template <typename T> class Result |
123 | | { |
124 | | public: |
125 | 0 | Result() = default; Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage> >::Result() Unexecuted instantiation: Result<Encoder::CodedImageData>::Result() Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_uncompressed> >::Result() |
126 | | |
127 | 0 | Result(const T& v) : m_data(v) {} Unexecuted instantiation: Result<heif_tai_timestamp_packet>::Result(heif_tai_timestamp_packet const&) Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >::Result(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&) Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage> >::Result(std::__1::shared_ptr<HeifPixelImage> const&) Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem> >::Result(std::__1::shared_ptr<ImageItem> const&) Unexecuted instantiation: Result<unsigned int>::Result(unsigned int const&) Unexecuted instantiation: Result<std::__1::shared_ptr<Track> >::Result(std::__1::shared_ptr<Track> const&) Unexecuted instantiation: Result<std::__1::shared_ptr<Track const> >::Result(std::__1::shared_ptr<Track const> const&) Unexecuted instantiation: Result<std::__1::shared_ptr<Track_Visual> >::Result(std::__1::shared_ptr<Track_Visual> const&) Unexecuted instantiation: Result<std::__1::shared_ptr<Track_Metadata> >::Result(std::__1::shared_ptr<Track_Metadata> const&) Unexecuted instantiation: Result<unsigned long>::Result(unsigned long const&) Unexecuted instantiation: Result<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Result(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) Unexecuted instantiation: Result<std::__1::shared_ptr<SEIMessage> >::Result(std::__1::shared_ptr<SEIMessage> const&) Unexecuted instantiation: Result<Encoder::CodedImageData>::Result(Encoder::CodedImageData const&) Unexecuted instantiation: Result<std::__1::vector<std::__1::shared_ptr<Box>, std::__1::allocator<std::__1::shared_ptr<Box> > > >::Result(std::__1::vector<std::__1::shared_ptr<Box>, std::__1::allocator<std::__1::shared_ptr<Box> > > const&) Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_Grid> >::Result(std::__1::shared_ptr<ImageItem_Grid> const&) Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_Overlay> >::Result(std::__1::shared_ptr<ImageItem_Overlay> const&) Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_Tiled> >::Result(std::__1::shared_ptr<ImageItem_Tiled> const&) Unexecuted instantiation: Result<DataExtent>::Result(DataExtent const&) Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage const> >::Result(std::__1::shared_ptr<HeifPixelImage const> const&) Unexecuted instantiation: Result<heif_raw_sequence_sample*>::Result(heif_raw_sequence_sample* const&) Unexecuted instantiation: Result<unciHeaders>::Result(unciHeaders const&) Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_uncompressed> >::Result(std::__1::shared_ptr<ImageItem_uncompressed> const&) Unexecuted instantiation: Result<std::__1::shared_ptr<Decoder> >::Result(std::__1::shared_ptr<Decoder> const&) Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >*>::Result(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >* const&) |
128 | | |
129 | 0 | Result(const Error& e) : m_data(e) {} Unexecuted instantiation: Result<heif_tai_timestamp_packet>::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage> >::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<Decoder> >::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem> >::Result(Error const&) Unexecuted instantiation: Result<unsigned int>::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<Track> >::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<Track const> >::Result(Error const&) Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >::Result(Error const&) Unexecuted instantiation: Result<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<SEIMessage> >::Result(Error const&) Unexecuted instantiation: Result<Encoder::CodedImageData>::Result(Error const&) Unexecuted instantiation: Result<std::__1::vector<std::__1::shared_ptr<Box>, std::__1::allocator<std::__1::shared_ptr<Box> > > >::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_Grid> >::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_Overlay> >::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_Tiled> >::Result(Error const&) Unexecuted instantiation: Result<DataExtent>::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage const> >::Result(Error const&) Unexecuted instantiation: Result<heif_raw_sequence_sample*>::Result(Error const&) Unexecuted instantiation: Result<unciHeaders>::Result(Error const&) Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_uncompressed> >::Result(Error const&) Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >*>::Result(Error const&) |
130 | | |
131 | 0 | operator bool() const { return std::holds_alternative<T>(m_data); } Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >::operator bool() const Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage> >::operator bool() const Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem> >::operator bool() const Unexecuted instantiation: Result<std::__1::shared_ptr<Track> >::operator bool() const Unexecuted instantiation: Result<unsigned long>::operator bool() const Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage const> >::operator bool() const Unexecuted instantiation: Result<std::__1::shared_ptr<Track const> >::operator bool() const Unexecuted instantiation: Result<std::__1::shared_ptr<SEIMessage> >::operator bool() const Unexecuted instantiation: Result<Encoder::CodedImageData>::operator bool() const Unexecuted instantiation: Result<std::__1::shared_ptr<Decoder> >::operator bool() const Unexecuted instantiation: Result<std::__1::vector<std::__1::shared_ptr<Box>, std::__1::allocator<std::__1::shared_ptr<Box> > > >::operator bool() const Unexecuted instantiation: Result<DataExtent>::operator bool() const Unexecuted instantiation: Result<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::operator bool() const Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >*>::operator bool() const Unexecuted instantiation: Result<heif_tai_timestamp_packet>::operator bool() const Unexecuted instantiation: Result<unciHeaders>::operator bool() const Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_Overlay> >::operator bool() const Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_uncompressed> >::operator bool() const |
132 | | |
133 | | //void set(const T& v) { m_data = v; } |
134 | | |
135 | | // Pointer-like access for `r->member` |
136 | | T* operator->() |
137 | 0 | { |
138 | 0 | assert(*this); // Uses the operator bool() above |
139 | 0 | return &std::get<T>(m_data); |
140 | 0 | } Unexecuted instantiation: Result<Encoder::CodedImageData>::operator->() Unexecuted instantiation: Result<std::__1::vector<std::__1::shared_ptr<Box>, std::__1::allocator<std::__1::shared_ptr<Box> > > >::operator->() Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >::operator->() |
141 | | |
142 | | // Dereference access for `*r` |
143 | | T& operator*() |
144 | 0 | { |
145 | 0 | assert(*this); |
146 | 0 | return std::get<T>(m_data); |
147 | 0 | } Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >::operator*() Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage> >::operator*() Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem> >::operator*() Unexecuted instantiation: Result<std::__1::shared_ptr<Track> >::operator*() Unexecuted instantiation: Result<unsigned long>::operator*() Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage const> >::operator*() Unexecuted instantiation: Result<std::__1::shared_ptr<Track const> >::operator*() Unexecuted instantiation: Result<std::__1::shared_ptr<SEIMessage> >::operator*() Unexecuted instantiation: Result<Encoder::CodedImageData>::operator*() Unexecuted instantiation: Result<std::__1::shared_ptr<Decoder> >::operator*() Unexecuted instantiation: Result<std::__1::vector<std::__1::shared_ptr<Box>, std::__1::allocator<std::__1::shared_ptr<Box> > > >::operator*() Unexecuted instantiation: Result<DataExtent>::operator*() Unexecuted instantiation: Result<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::operator*() Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >*>::operator*() Unexecuted instantiation: Result<heif_tai_timestamp_packet>::operator*() Unexecuted instantiation: Result<unciHeaders>::operator*() Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_Overlay> >::operator*() Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_uncompressed> >::operator*() |
148 | | |
149 | | [[nodiscard]] bool is_error() const { |
150 | | return std::holds_alternative<Error>(m_data); |
151 | | } |
152 | | |
153 | | // Accessor for the error, if it exists |
154 | | [[nodiscard]] const Error& error() const |
155 | 0 | { |
156 | 0 | if (*this) { |
157 | 0 | return Error::Ok; |
158 | 0 | } |
159 | | |
160 | 0 | return std::get<Error>(m_data); |
161 | 0 | } Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >::error() const Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage> >::error() const Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem> >::error() const Unexecuted instantiation: Result<std::__1::shared_ptr<Track> >::error() const Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage const> >::error() const Unexecuted instantiation: Result<std::__1::shared_ptr<SEIMessage> >::error() const Unexecuted instantiation: Result<Encoder::CodedImageData>::error() const Unexecuted instantiation: Result<std::__1::shared_ptr<Decoder> >::error() const Unexecuted instantiation: Result<std::__1::vector<std::__1::shared_ptr<Box>, std::__1::allocator<std::__1::shared_ptr<Box> > > >::error() const Unexecuted instantiation: Result<DataExtent>::error() const Unexecuted instantiation: Result<std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >*>::error() const Unexecuted instantiation: Result<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::error() const Unexecuted instantiation: Result<heif_tai_timestamp_packet>::error() const Unexecuted instantiation: Result<unciHeaders>::error() const |
162 | | |
163 | | // Directly get the C error struct. |
164 | | heif_error error_struct(ErrorBuffer* error_buffer) const |
165 | 0 | { |
166 | 0 | if (*this) { |
167 | 0 | return heif_error_success; |
168 | 0 | } |
169 | | |
170 | 0 | return std::get<Error>(m_data).error_struct(error_buffer); |
171 | 0 | } Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem> >::error_struct(ErrorBuffer*) const Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_Overlay> >::error_struct(ErrorBuffer*) const Unexecuted instantiation: Result<std::__1::shared_ptr<ImageItem_uncompressed> >::error_struct(ErrorBuffer*) const Unexecuted instantiation: Result<std::__1::shared_ptr<HeifPixelImage> >::error_struct(ErrorBuffer*) const |
172 | | |
173 | | std::variant<T, Error> m_data; |
174 | | }; |
175 | | |
176 | | #endif |