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 
43 
48 
51 
54 
57 
61 };
62 
64 {
66 
67 public:
68  OptionalErrorCode(std::error_code &ec);
69 
70  static OptionalErrorCode null();
71 
72  operator bool() const;
73 
74  std::error_code& operator*();
75 
76 private:
77  std::error_code *m_ec = nullptr;
78 };
79 
86 class Exception : public std::system_error
87 {
88 public:
89  using std::system_error::system_error;
90 };
91 
96 class AvcppCategory : public std::error_category
97 {
98 public:
99 
100  virtual const char *name() const noexcept override;
101  virtual std::string message(int ev) const override;
102 };
103 
108 class FfmpegCategory : public std::error_category
109 {
110  virtual const char *name() const noexcept override;
111  virtual std::string message(int ev) const override;
112 };
113 
114 inline
116 {
117  static AvcppCategory res;
118  return res;
119 }
120 
122 {
123  static FfmpegCategory res;
124  return res;
125 }
126 
127 inline
128 std::error_condition make_error_condition(av::Errors errc) noexcept
129 {
130  return std::error_condition(static_cast<int>(errc), av::avcpp_category());
131 }
132 
133 inline
134 std::error_code make_error_code(av::Errors errc) noexcept
135 {
136  return std::error_code(static_cast<int>(errc), av::avcpp_category());
137 }
138 
139 inline
140 std::error_code make_avcpp_error(Errors code)
141 {
142  return std::error_code(static_cast<int>(code), avcpp_category());
143 }
144 
145 inline
146 std::error_condition make_avcpp_condition(Errors code)
147 {
148  return std::error_condition(static_cast<int>(code), avcpp_category());
149 }
150 
151 inline
152 std::error_code make_ffmpeg_error(int code)
153 {
154  return std::error_code(code, ffmpeg_category());
155 }
156 
157 inline
158 std::error_condition make_ffmpeg_condition(int code)
159 {
160  return std::error_condition(code, ffmpeg_category());
161 }
162 
163 template<typename Exception = av::Exception>
164 void throw_error_code(const std::error_code& ec)
165 {
166  throw Exception(ec);
167 }
168 
169 template<typename Exception = av::Exception>
171 {
172  throw Exception(make_error_code(errc));
173 }
174 
179 inline OptionalErrorCode throws()
180 {
181  return OptionalErrorCode::null();
182 }
183 
187 template<typename Category, typename Exception = av::Exception>
188 void throws_if(OptionalErrorCode ec, int errcode, const Category &cat)
189 {
190  if (ec)
191  *ec = std::error_code(errcode, cat);
192  else
193  throw Exception(std::error_code(errcode, cat));
194 }
195 
196 template<typename T, typename Exception = av::Exception>
197 void throws_if(OptionalErrorCode ec, T errcode)
198 {
199  if (ec)
200  *ec = make_error_code(errcode);
201  else
202  throw Exception(make_error_code(errcode));
203 }
204 
205 
210 inline
212 {
213  if (ec)
214  (*ec).clear();
215 }
216 
217 inline
219 {
220  if (ec)
221  return static_cast<bool>(*ec);
222 
223  return false;
224 }
225 
226 } // ::av
227 
228 namespace std {
229 template<> struct is_error_condition_enum<av::Errors> : public true_type {};
230 // Commented out for future invertigations. Note, for correct comparation, users error enum must
231 // declared only as is_error_condition_enum<> or is_error_code_enum<>
232 //template<> struct is_error_code_enum<av::AvError> : public true_type {};
233 }
234 
The AvcppCategory class Describes internal AvCpp errors.
Definition: averror.h:97
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:87
The FfmpegCategory class Describers FFmpeg internal errors.
Definition: averror.h:109
Definition: averror.h:64
static OptionalErrorCode null()
Definition: averror.cpp:83
OptionalErrorCode(std::error_code &ec)
std::error_code & operator*()
Definition: averror.cpp:88
Definition: audioresampler.cpp:8
const AvcppCategory & avcpp_category()
Definition: averror.h:115
void clear_if(OptionalErrorCode ec)
clear_if - clear error code if it is not av::throws()
Definition: averror.h:211
void throw_error_code(const std::error_code &ec)
Definition: averror.h:164
bool is_error(OptionalErrorCode ec)
Definition: averror.h:218
std::error_code make_avcpp_error(Errors code)
Definition: averror.h:140
std::error_code make_error_code(av::Errors errc) noexcept
Definition: averror.h:134
std::error_condition make_ffmpeg_condition(int code)
Definition: averror.h:158
std::error_code make_ffmpeg_error(int code)
Definition: averror.h:152
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:128
void throws_if(OptionalErrorCode ec, int errcode, const Category &cat)
Throws exception if ec is av::throws() or fill error code.
Definition: averror.h:188
std::error_condition make_avcpp_condition(Errors code)
Definition: averror.h:146
FfmpegCategory & ffmpeg_category()
Definition: averror.h:121
Definition: averror.h:228