avcpp  2.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
averror.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <system_error>
4 #include <exception>
5 
6 namespace av
7 {
8 
9 enum class Errors
10 {
11  NoError = 0,
12  Generic,
15  OutOfRange,
31  DictNoKey,
32 
34 
45 
50 
53 
56 
59 
63 };
64 
66 {
68 
69 public:
70  OptionalErrorCode(std::error_code &ec);
71 
72  static OptionalErrorCode null();
73 
74  operator bool() const;
75 
76  std::error_code& operator*();
77 
78 private:
79  std::error_code *m_ec = nullptr;
80 };
81 
88 class Exception : public std::system_error
89 {
90 public:
91  using std::system_error::system_error;
92 };
93 
98 class AvcppCategory : public std::error_category
99 {
100 public:
101 
102  virtual const char *name() const noexcept override;
103  virtual std::string message(int ev) const override;
104 };
105 
110 class FfmpegCategory : public std::error_category
111 {
112  virtual const char *name() const noexcept override;
113  virtual std::string message(int ev) const override;
114 };
115 
116 inline
118 {
119  static AvcppCategory res;
120  return res;
121 }
122 
124 {
125  static FfmpegCategory res;
126  return res;
127 }
128 
129 inline
130 std::error_condition make_error_condition(av::Errors errc) noexcept
131 {
132  return std::error_condition(static_cast<int>(errc), av::avcpp_category());
133 }
134 
135 inline
136 std::error_code make_error_code(av::Errors errc) noexcept
137 {
138  return std::error_code(static_cast<int>(errc), av::avcpp_category());
139 }
140 
141 inline
142 std::error_code make_avcpp_error(Errors code)
143 {
144  return std::error_code(static_cast<int>(code), avcpp_category());
145 }
146 
147 inline
148 std::error_condition make_avcpp_condition(Errors code)
149 {
150  return std::error_condition(static_cast<int>(code), avcpp_category());
151 }
152 
153 inline
154 std::error_code make_ffmpeg_error(int code)
155 {
156  return std::error_code(code, ffmpeg_category());
157 }
158 
159 inline
160 std::error_condition make_ffmpeg_condition(int code)
161 {
162  return std::error_condition(code, ffmpeg_category());
163 }
164 
165 template<typename Exception = av::Exception>
166 void throw_error_code(const std::error_code& ec)
167 {
168  throw Exception(ec);
169 }
170 
171 template<typename Exception = av::Exception>
173 {
174  throw Exception(make_error_code(errc));
175 }
176 
181 inline OptionalErrorCode throws()
182 {
183  return OptionalErrorCode::null();
184 }
185 
189 template<typename Category, typename Exception = av::Exception>
190 void throws_if(OptionalErrorCode ec, int errcode, const Category &cat)
191 {
192  if (ec)
193  *ec = std::error_code(errcode, cat);
194  else
195  throw Exception(std::error_code(errcode, cat));
196 }
197 
198 template<typename T, typename Exception = av::Exception>
199 void throws_if(OptionalErrorCode ec, T errcode)
200 {
201  if (ec)
202  *ec = make_error_code(errcode);
203  else
204  throw Exception(make_error_code(errcode));
205 }
206 
207 
212 inline
214 {
215  if (ec)
216  (*ec).clear();
217 }
218 
219 inline
221 {
222  if (ec)
223  return static_cast<bool>(*ec);
224 
225  return false;
226 }
227 
228 } // ::av
229 
230 namespace std {
231 template<> struct is_error_condition_enum<av::Errors> : public true_type {};
232 // Commented out for future invertigations. Note, for correct comparation, users error enum must
233 // declared only as is_error_condition_enum<> or is_error_code_enum<>
234 //template<> struct is_error_code_enum<av::AvError> : public true_type {};
235 }
236 
The AvcppCategory class Describes internal AvCpp errors.
Definition: averror.h:99
virtual std::string message(int ev) const override
Definition: averror.cpp:14
virtual const char * name() const noexcept override
Definition: averror.cpp:9
The AvException class Default exception that thows from function that does not accept error code stor...
Definition: averror.h:89
The FfmpegCategory class Describers FFmpeg internal errors.
Definition: averror.h:111
Definition: averror.h:66
static OptionalErrorCode null()
Definition: averror.cpp:84
OptionalErrorCode(std::error_code &ec)
std::error_code & operator*()
Definition: averror.cpp:89
Definition: audioresampler.cpp:8
const AvcppCategory & avcpp_category()
Definition: averror.h:117
void clear_if(OptionalErrorCode ec)
clear_if - clear error code if it is not av::throws()
Definition: averror.h:213
void throw_error_code(const std::error_code &ec)
Definition: averror.h:166
bool is_error(OptionalErrorCode ec)
Definition: averror.h:220
std::error_code make_avcpp_error(Errors code)
Definition: averror.h:142
std::error_code make_error_code(av::Errors errc) noexcept
Definition: averror.h:136
std::error_condition make_ffmpeg_condition(int code)
Definition: averror.h:160
std::error_code make_ffmpeg_error(int code)
Definition: averror.h:154
Errors
Definition: averror.h:10
@ FilterNotInFilterGraph
@ MixBufferSinkAccess
@ FormatCodecUnsupported
@ CodecInvalidForEncode
@ IncorrectBufferSinkMediaType
@ IncorrectBufferSrcFilter
@ CodecInvalidForDecoce
@ FilterGraphDescriptionEmpty
@ ResamplerInvalidParameters
@ IncorrectBufferSrcMediaType
@ CodecStreamInvalid
@ CodecInvalidDirection
@ ResamplerNotInited
@ FormatAlreadyOpened
@ CodecInvalidDecodeProc
@ IncorrectBufferSinkFilter
@ ResamplerOutputChanges
@ CodecInvalidMediaType
@ FormatInvalidDirection
@ FormatCantAddStream
@ RescalerInternalSwsError
@ FormatInvalidStreamIndex
@ CodecInvalidEncodeProc
@ CodecAlreadyOpened
@ RescalerInvalidParameters
@ FormatWrongCountOfStreamOptions
@ CodecDecodingOffsetToLarge
@ ResamplerInputChanges
@ FormatNullOutputFormat
@ FormatHeaderNotWriten
std::error_condition make_error_condition(av::Errors errc) noexcept
Definition: averror.h:130
void throws_if(OptionalErrorCode ec, int errcode, const Category &cat)
Throws exception if ec is av::throws() or fill error code.
Definition: averror.h:190
std::error_condition make_avcpp_condition(Errors code)
Definition: averror.h:148
FfmpegCategory & ffmpeg_category()
Definition: averror.h:123
Definition: averror.h:230