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 
av::Errors::IncorrectBufferSinkMediaType
@ IncorrectBufferSinkMediaType
av::Errors::FormatInvalidStreamIndex
@ FormatInvalidStreamIndex
av::FfmpegCategory
The FfmpegCategory class Describers FFmpeg internal errors.
Definition: averror.h:108
av::is_error
bool is_error(OptionalErrorCode ec)
Definition: averror.h:218
av::ffmpeg_category
FfmpegCategory & ffmpeg_category()
Definition: averror.h:121
av::Errors::CodecIsNotOpened
@ CodecIsNotOpened
av::make_ffmpeg_error
std::error_code make_ffmpeg_error(int code)
Definition: averror.h:152
av::Errors::IncorrectBufferSrcMediaType
@ IncorrectBufferSrcMediaType
av::Errors::CodecInvalidForEncode
@ CodecInvalidForEncode
av::OptionalErrorCode::operator*
std::error_code & operator*()
Definition: averror.cpp:88
av::Errors::IncorrectBufferSinkFilter
@ IncorrectBufferSinkFilter
av::Errors::Unallocated
@ Unallocated
av::Errors::FormatNullOutputFormat
@ FormatNullOutputFormat
av::Errors::CantAllocateFrame
@ CantAllocateFrame
av::avcpp_category
const AvcppCategory & avcpp_category()
Definition: averror.h:115
av::Errors::FormatNoStreams
@ FormatNoStreams
av::Errors::IncorrectBufferSrcFilter
@ IncorrectBufferSrcFilter
av::Errors::FilterNotInFilterGraph
@ FilterNotInFilterGraph
av::Errors::FormatCantAddStream
@ FormatCantAddStream
av::Errors::RescalerInternalSwsError
@ RescalerInternalSwsError
av::Errors::CodecNotOpened
@ CodecNotOpened
av::throw_error_code
void throw_error_code(const std::error_code &ec)
Definition: averror.h:164
av::Errors::CodecInvalidEncodeProc
@ CodecInvalidEncodeProc
av::make_error_condition
std::error_condition make_error_condition(av::Errors errc) noexcept
Definition: averror.h:128
av::clear_if
void clear_if(OptionalErrorCode ec)
clear_if - clear error code if it is not av::throws()
Definition: averror.h:211
av::OptionalErrorCode
Definition: averror.h:63
av::Errors::CodecInvalid
@ CodecInvalid
av::Errors::CodecStreamInvalid
@ CodecStreamInvalid
av::Errors::NoError
@ NoError
av::make_ffmpeg_condition
std::error_condition make_ffmpeg_condition(int code)
Definition: averror.h:158
av::Errors::FormatInvalidDirection
@ FormatInvalidDirection
av::Errors::OutOfRange
@ OutOfRange
av::throws_if
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
av::Errors::FormatAlreadyOpened
@ FormatAlreadyOpened
av::Errors::CodecDecodingOffsetToLarge
@ CodecDecodingOffsetToLarge
av::Errors
Errors
Definition: averror.h:9
av::OptionalErrorCode::null
static OptionalErrorCode null()
Definition: averror.cpp:83
av::Errors::MixBufferSinkAccess
@ MixBufferSinkAccess
av::Errors::ResamplerInputChanges
@ ResamplerInputChanges
av::Errors::FrameInvalid
@ FrameInvalid
av::make_avcpp_condition
std::error_condition make_avcpp_condition(Errors code)
Definition: averror.h:146
av::AvcppCategory
The AvcppCategory class Describes internal AvCpp errors.
Definition: averror.h:96
av::AvcppCategory::name
virtual const char * name() const noexcept override
Definition: averror.cpp:9
av::Exception
The AvException class Default exception that thows from function that does not accept error code stor...
Definition: averror.h:86
av::make_error_code
std::error_code make_error_code(av::Errors errc) noexcept
Definition: averror.h:134
av
Definition: audioresampler.cpp:8
std
Definition: averror.h:228
av::Errors::Generic
@ Generic
av::Errors::FormatWrongCountOfStreamOptions
@ FormatWrongCountOfStreamOptions
av::Errors::CodecInvalidForDecoce
@ CodecInvalidForDecoce
av::AvcppCategory::message
virtual std::string message(int ev) const override
Definition: averror.cpp:14
av::Errors::FormatHeaderNotWriten
@ FormatHeaderNotWriten
av::Errors::CodecAlreadyOpened
@ CodecAlreadyOpened
av::Errors::FilterGraphDescriptionEmpty
@ FilterGraphDescriptionEmpty
av::Errors::ResamplerNotInited
@ ResamplerNotInited
av::Errors::ResamplerInvalidParameters
@ ResamplerInvalidParameters
av::Errors::CodecInvalidMediaType
@ CodecInvalidMediaType
av::Errors::RescalerInvalidParameters
@ RescalerInvalidParameters
av::Errors::ResamplerOutputChanges
@ ResamplerOutputChanges
av::Errors::InvalidArgument
@ InvalidArgument
av::Errors::FormatCodecUnsupported
@ FormatCodecUnsupported
av::Errors::CodecInvalidDecodeProc
@ CodecInvalidDecodeProc
av::Errors::FormatNotOpened
@ FormatNotOpened
av::make_avcpp_error
std::error_code make_avcpp_error(Errors code)
Definition: averror.h:140
av::Errors::DictOutOfRage
@ DictOutOfRage
av::Errors::DictNoKey
@ DictNoKey
av::Errors::CodecInvalidDirection
@ CodecInvalidDirection