18#include <libavutil/dict.h>
82 using RawStringDeleter = AvStringDeleter;
88 struct AvStringPtr :
public std::unique_ptr<char, AvStringDeleter>
91 using base_class = std::unique_ptr<char, AvStringDeleter>;
92 using base_class::unique_ptr;
117 return strlen(
c_str());
130 return c_str()[index];
134 static_assert (
sizeof(AvStringPtr) ==
sizeof(
char*),
"Size must be same. Check and try again");
136 using RawStringPtr = AvStringPtr;
140 template<
bool constIterator>
141 class DictionaryIterator;
151 friend class DictionaryIterator;
152 friend class Dictionary;
158 const char*
key() const noexcept;
177 void set(const
char *
value,
int flags = 0) noexcept;
216 operator
bool() const;
243 AVDictionaryEntry *m_entry =
nullptr;
250 template<
bool constIterator = false>
258 using value_type =
typename std::conditional<constIterator, const Entry, Entry>::type;
267 using DictType =
typename std::conditional<constIterator, const Dictionary, Dictionary>::type;
268 using EntryType =
typename std::conditional<constIterator, const Entry, Entry>::type;
304 m_entry.m_entry = av_dict_get(m_dict.raw(),
"", m_entry.m_entry,
FlagIgnoreSuffix);
330 return lhs.m_entry == rhs.m_entry;
343 return lhs.m_entry != rhs.m_entry;
390 explicit Dictionary(AVDictionary *dict,
bool takeOwning =
true);
447 Dictionary(std::initializer_list<std::pair<const char*, const char*>> list,
int flags = 0);
525 void assign(AVDictionary *dict,
bool takeOwning = true) noexcept;
544 const
char* operator[](
size_t index) const;
574 const
char* operator[](const
char*
key) const;
627 const
char *
get(const
char*
key,
int flags = 0) const noexcept;
636 const
char *
get(const
std::
string&
key,
int flags = 0) const noexcept;
652 template<typename Key, typename Value = Key>
654 typename
std::enable_if
656 (
std::is_same<Key,
std::
string>::
value ||
std::is_same<typename
std::remove_cv<typename
std::decay<Key>::type>::type,
char*>::
value) &&
657 (
std::is_same<Value,
std::
string>::
value ||
std::is_same<typename
std::remove_cv<typename
std::decay<Value>::type>::type,
char*>::
value ||
std::is_integral<Value>::
value)
663 if ((sts = set_priv(
key,
value, flags)) < 0) {
685 template<
typename Str,
typename Sep1,
typename Sep2>
689 _to_const_char_ptr(str),
690 _to_const_char_ptr(keyValSep),
691 _to_const_char_ptr(pairsSep),
775 int set_priv(const
char*
key, const
char *
value,
int flags = 0) noexcept;
776 int set_priv(const
std::
string&
key, const
std::
string&
value,
int flags = 0) noexcept;
777 int set_priv(const
char*
key, int64_t
value,
int flags = 0) noexcept;
778 int set_priv(const
std::
string&
key, int64_t
value,
int flags = 0) noexcept;
780 const
char* _to_const_char_ptr(const
char *str)
785 const char* _to_const_char_ptr(
const std::string &str)
790 int parseString_priv(
const char* str,
const char* keyvalSep,
const char* pairsSep,
int flags);
791 void parseString_priv(OptionalErrorCode ec,
const char* str,
const char* keyvalSep,
const char* pairsSep,
int flags);
795 bool m_owning =
true;
811 DictionaryArray() =
default;
851 DictionaryArray&
operator=(DictionaryArray&&) =
default;
860 void assign(AVDictionary **dicts,
size_t count,
bool takeOwning =
true);
934 const AVDictionary*
const*
raws()
const;
955 void swap(DictionaryArray &rhs);
957 std::vector<AVDictionary*> m_raws;
958 std::vector<Dictionary> m_dicts;
Error codes, error categories and the OptionalErrorCode convention.
DictionaryArray(AVDictionary **dicts, size_t count, bool takeOwning=true)
Wrap an array of raw dictionaries.
void assign(AVDictionary **dicts, size_t count, bool takeOwning=true)
Replace the contents with an array of raw dictionaries.
AVDictionary ** release()
Give up ownership of the dictionaries.
void swap(DictionaryArray &rhs)
Exchange the contents of two arrays.
DictionaryArray & operator=(DictionaryArray &&)=default
Move assignment; the source is left empty.
void resize(size_t size)
Resize the array, adding empty dictionaries at the end.
void pushBack(const Dictionary &dict)
Append a copy of a dictionary.
DictionaryArray(const DictionaryArray &rhs)
Copy the array; each dictionary is copied deeply.
Dictionary & operator[](size_t index)
Dictionary by index, mutable.
const Dictionary & at(size_t index) const
Dictionary by index with bounds checking.
DictionaryArray(std::initializer_list< Dictionary > dicts)
Build an array from a list of dictionaries.
const AVDictionary *const * raws() const
The raw array, read-only.
void pushBack(Dictionary &&dict)
Append a dictionary, moving it.
Dictionary & at(size_t index)
Dictionary by index with bounds checking.
size_t size() const
Number of dictionaries in the array.
const Dictionary & operator[](size_t index) const
Dictionary by index.
AVDictionary ** raws()
The raw array, mutable.
DictionaryArray(DictionaryArray &&)=default
Move the array; the source is left empty.
void reserve(size_t size)
Reserve storage for the given number of dictionaries.
DictionaryArray & operator=(const DictionaryArray &rhs)
Assign from another array; each dictionary is copied deeply.
Base dictionary iterator implementation.
Definition dictionary.h:252
DictionaryIterator & operator++() noexcept
Definition dictionary.h:302
std::forward_iterator_tag iterator_category
Standard iterator category tag.
Definition dictionary.h:256
ptrdiff_t difference_type
Distance type required by the iterator traits.
Definition dictionary.h:260
value_type * pointer
Pointer to the entry.
Definition dictionary.h:262
EntryType * operator->() noexcept
Entry at the current position.
Definition dictionary.h:361
typename std::conditional< constIterator, const Entry, Entry >::type value_type
Type produced by dereferencing.
Definition dictionary.h:258
friend bool operator!=(const DictionaryIterator &lhs, const DictionaryIterator &rhs) noexcept
Compare two iterators.
Definition dictionary.h:341
DictionaryIterator operator++(int) noexcept
Advance, returning the previous position.
Definition dictionary.h:313
EntryType & operator*() noexcept
Entry at the current position.
Definition dictionary.h:351
value_type & reference
Reference to the entry.
Definition dictionary.h:264
friend bool operator==(const DictionaryIterator &lhs, const DictionaryIterator &rhs) noexcept
Compare two iterators.
Definition dictionary.h:328
DictionaryIterator(DictType &dict) noexcept
Start the iteration at the first entry.
Definition dictionary.h:291
DictionaryIterator(DictType &dict, const Entry &entry) noexcept
Continue the iteration from the given entry.
Definition dictionary.h:280
Dictionary key and value holder and accessor.
Definition dictionary.h:147
void set(const char *value, int flags=0) noexcept
Change the value of this entry.
const char * value() const noexcept
value - item value accessor
const char * key() const noexcept
key - item key accessor
bool isNull() const
isNull - checks that entry invalid (null)
Implements interface to access to the AVDictionary entity.
Definition dictionary.h:38
Dictionary(Dictionary &&other)
Move ctor Takes resources.
AvStringPtr toRawStringPtr(const char keyValSep, const char pairsSep, OptionalErrorCode ec=throws()) const
toRawStringPtr - converts dictionary to the raw string (char*) and protect it with smart pointer (std...
void swap(Dictionary &other) noexcept
swap - swaps resouces between objects
AVDictionary ** rawPtr() noexcept
Address of the internal pointer, for FFmpeg calls that take AVDictionary**.
Dictionary & operator=(Dictionary &&rhs)
Move assign operator Takes resources.
void assign(AVDictionary *dict, bool takeOwning=true) noexcept
assign - assign new resouces Old dictionary destroyes (if owning).
size_t count() const noexcept
Number of entries.
Iterator begin()
First entry.
std::string toString(const char keyValSep, const char pairsSep, OptionalErrorCode ec=throws()) const
toString - converts dictionary to the string representation (serialize)
ConstIterator begin() const
First entry, read-only.
Flags
AVDictionary flags mapping.
Definition dictionary.h:44
@ FlagMatchCase
Do not ignore case.
Definition dictionary.h:46
@ FlagDontStrdupVal
Do not duplicate string value.
Definition dictionary.h:55
@ FlagAppend
Append value to the existing one (string concat).
Definition dictionary.h:59
@ FlagIgnoreSuffix
Special case: Iterate via dictionary.
Definition dictionary.h:48
@ FlagDontOverwrite
Keep existing value if key already exists.
Definition dictionary.h:57
@ FlagDontStrdupKey
Do not duplicate string key.
Definition dictionary.h:51
auto set(const Key &key, const Value &value, OptionalErrorCode ec=throws(), int flags=0) -> typename std::enable_if<(std::is_same< Key, std::string >::value||std::is_same< typename std::remove_cv< typename std::decay< Key >::type >::type, char * >::value) &&(std::is_same< Value, std::string >::value||std::is_same< typename std::remove_cv< typename std::decay< Value >::type >::type, char * >::value||std::is_integral< Value >::value), void >::type
set - set new value for key or add new dictionary item (or create dictionary) If dictionary empty (nu...
Definition dictionary.h:653
Dictionary(AVDictionary *dict, bool takeOwning=true)
Assign ctor Wrap around raw dictionary.
Dictionary(std::initializer_list< std::pair< const char *, const char * > > list, int flags=0)
Initializer ctor Allows create dictionaries in array manier:
void copyFrom(const Dictionary &other, int flags=0) noexcept
copyFrom - copy data from other dictionary.
DictionaryIterator< true > ConstIterator
Const iterator.
Definition dictionary.h:375
DictionaryIterator< false > Iterator
Non-const iterator.
Definition dictionary.h:371
size_t size() const noexcept
Number of entries.
void parseString(const Str &str, const Sep1 &keyValSep, const Sep2 &pairsSep, int flags=0, OptionalErrorCode ec=throws())
parseString - process string with options and fill dictionary String examples:
Definition dictionary.h:686
const char * get(const char *key, int flags=0) const noexcept
Value for a key.
Dictionary()
Default ctor do nothig, null dictionary creates.
Dictionary & operator=(const Dictionary &rhs)
Copy assign operator Make deep copy of dictionary.
ConstIterator end() const
End of the read-only iteration.
ConstIterator cbegin() const
First entry, read-only, for explicit const traversal.
bool isOwning() const noexcept
isOwning - checks resources owning status
Iterator end()
End of the iteration.
~Dictionary()
Dtor If Dictionary takes ownershipping on AVDictionary, it free allocated resources.
ConstIterator cend() const
End of the read-only traversal.
Dictionary & operator=(std::initializer_list< std::pair< const char *, const char * > > list)
Initializer assign operator Allows assign dictionaries in array manier:
AVDictionary * release()
release - drops ownershipping
Dictionary(const Dictionary &other)
Copy ctor Make deep copy of dictionary.
Carries the caller's choice between exceptions and error codes.
Definition averror.h:96
FFmpeg types re-exported for the public API and small value wrappers.
Root namespace of the library.
Definition audioresampler.h:19
void clear_if(OptionalErrorCode ec)
clear_if - clear error code if it is not av::throws()
Definition averror.h:368
OptionalErrorCode throws()
Helper to construct null OptionalErrorCode object.
Definition averror.h:310
FfmpegCategory & ffmpeg_category()
Error category that wraps FFmpeg return codes.
Definition averror.h:196
void throws_if(OptionalErrorCode ec, int errcode, const Category &cat)
Throw or store an error, depending on what the caller passed.
Definition averror.h:323
void reset(AVDictionary *raw=nullptr)
Definition ffmpeg.h:71
Deleter for raw string.
Definition dictionary.h:68
void operator()(char *ptr)
Free a string allocated by FFmpeg.
Definition dictionary.h:74
RAII holder for strings allocated by FFmpeg internals.
Definition dictionary.h:89
char * c_str() const noexcept
Access to the holded string with std::string interface.
Definition dictionary.h:98
std::unique_ptr< char, AvStringDeleter > base_class
The unique_ptr instantiation this holder builds on.
Definition dictionary.h:91
char & operator[](size_t index) const noexcept
Array-like char access.
Definition dictionary.h:128
size_t length() const noexcept
RAW string length in bytes.
Definition dictionary.h:115