avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
dictionary.h
Go to the documentation of this file.
1#pragma once
2
9
10#include <tuple>
11#include <memory>
12#include <iterator>
13#include <vector>
14#include <type_traits>
15#include <cassert>
16
17extern "C" {
18#include <libavutil/dict.h>
19}
20
21#include "averror.h"
22
23#include "ffmpeg.h"
24
25namespace av {
26
37class Dictionary : public FFWrapperPtr<AVDictionary>
38{
39public:
43 enum Flags
44 {
46 FlagMatchCase = AV_DICT_MATCH_CASE,
48 FlagIgnoreSuffix = AV_DICT_IGNORE_SUFFIX,
51 FlagDontStrdupKey = AV_DICT_DONT_STRDUP_KEY,
55 FlagDontStrdupVal = AV_DICT_DONT_STRDUP_VAL,
57 FlagDontOverwrite = AV_DICT_DONT_OVERWRITE,
59 FlagAppend = AV_DICT_APPEND,
60 };
61
68 {
74 void operator()(char *ptr)
75 {
76 av_freep(&ptr);
77 }
78 };
79
80 // Back compatible
82 using RawStringDeleter = AvStringDeleter;
84
88 struct AvStringPtr : public std::unique_ptr<char, AvStringDeleter>
89 {
91 using base_class = std::unique_ptr<char, AvStringDeleter>;
92 using base_class::unique_ptr;
93
98 char* c_str() const noexcept
99 {
100 return get();
101 }
102
115 size_t length() const noexcept
116 {
117 return strlen(c_str());
118 }
119
128 char& operator[](size_t index) const noexcept
129 {
130 return c_str()[index];
131 }
132 };
133 // Our contract
134 static_assert (sizeof(AvStringPtr) == sizeof(char*), "Size must be same. Check and try again");
136 using RawStringPtr = AvStringPtr;
138
139 // Fwd
140 template<bool constIterator>
141 class DictionaryIterator;
142
146 class Entry
147 {
148 public:
149
150 template<bool>
151 friend class DictionaryIterator;
152 friend class Dictionary;
153
158 const char* key() const noexcept;
163 const char* value() const noexcept;
171
177 void set(const char *value, int flags = 0) noexcept;
184 void set(const std::string& value, int flags = 0) noexcept;
186
194
201 Entry& operator=(const char *value) noexcept;
209 Entry& operator=(const std::string &value) noexcept;
211
216 operator bool() const;
221 bool isNull() const;
222
231 friend bool operator==(const Entry& lhs, const Entry& rhs);
240 friend bool operator!=(const Entry& lhs, const Entry& rhs);
241
242 private:
243 AVDictionaryEntry *m_entry = nullptr;
244 };
245
246
250 template<bool constIterator = false>
252 {
253 public:
254 // Iterator interface
256 using iterator_category = std::forward_iterator_tag;
258 using value_type = typename std::conditional<constIterator, const Entry, Entry>::type;
260 using difference_type = ptrdiff_t;
265
266 private:
267 using DictType = typename std::conditional<constIterator, const Dictionary, Dictionary>::type;
268 using EntryType = typename std::conditional<constIterator, const Entry, Entry>::type;
269
270 DictType &m_dict;
271 Entry m_entry;
272
273 public:
280 DictionaryIterator(DictType &dict, const Entry& entry) noexcept
281 : m_dict(dict),
282 m_entry(entry)
283 {
284 }
285
291 DictionaryIterator(DictType &dict) noexcept
292 : m_dict(dict)
293 {
294 operator++();
295 }
296
303 {
304 m_entry.m_entry = av_dict_get(m_dict.raw(), "", m_entry.m_entry, FlagIgnoreSuffix);
305 return *this;
306 }
307
314 {
315 DictionaryIterator tmp(*this);
316 operator++();
317 return tmp;
318 }
319
328 friend bool operator==(const DictionaryIterator& lhs, const DictionaryIterator& rhs) noexcept
329 {
330 return lhs.m_entry == rhs.m_entry;
331 }
332
341 friend bool operator!=(const DictionaryIterator& lhs, const DictionaryIterator& rhs) noexcept
342 {
343 return lhs.m_entry != rhs.m_entry;
344 }
345
351 EntryType& operator*() noexcept
352 {
353 return m_entry;
354 }
355
361 EntryType* operator->() noexcept
362 {
363 return &m_entry;
364 }
365
366 };
367
376
382
390 explicit Dictionary(AVDictionary *dict, bool takeOwning = true);
391
397
403 Dictionary(const Dictionary& other);
404
414
421
431
447 Dictionary(std::initializer_list<std::pair<const char*, const char*>> list, int flags = 0);
448
467 Dictionary& operator=(std::initializer_list<std::pair<const char*, const char*>> list);
468
473
511
516 bool isOwning() const noexcept;
517
525 void assign(AVDictionary *dict, bool takeOwning = true) noexcept;
526
535
544 const char* operator[](size_t index) const;
554 Entry operator[](size_t index);
556
565
574 const char* operator[](const char* key) const;
584 Entry operator[](const char* key) noexcept;
586
594
601 size_t count() const noexcept;
607 size_t size() const noexcept;
609
619
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;
638
639
652 template<typename Key, typename Value = Key>
653 auto set(const Key& key, const Value& value, OptionalErrorCode ec = throws(), int flags = 0) ->
654 typename std::enable_if
655 <
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)
658 ,void
659 >::type
660 {
661 clear_if(ec);
662 int sts;
663 if ((sts = set_priv(key, value, flags)) < 0) {
664 throws_if(ec, sts, ffmpeg_category());
665 }
666 }
667
685 template<typename Str, typename Sep1, typename Sep2>
686 void parseString(const Str& str, const Sep1& keyValSep, const Sep2& pairsSep, int flags = 0, OptionalErrorCode ec = throws())
687 {
688 parseString_priv(ec,
689 _to_const_char_ptr(str),
690 _to_const_char_ptr(keyValSep),
691 _to_const_char_ptr(pairsSep),
692 flags);
693 }
694
711 std::string toString(const char keyValSep, const char pairsSep, OptionalErrorCode ec = throws()) const;
712
729 AvStringPtr toRawStringPtr(const char keyValSep, const char pairsSep, OptionalErrorCode ec = throws()) const;
730
740 void copyFrom(const Dictionary& other, int flags = 0) noexcept;
741
746 void swap(Dictionary& other) noexcept;
747
758 AVDictionary* release();
759
760
769 AVDictionary **rawPtr() noexcept;
770
771private:
772 // Discard access to the reset() method
773 using FFWrapperPtr<AVDictionary>::reset;
774
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;
779
780 const char* _to_const_char_ptr(const char *str)
781 {
782 return str;
783 }
784
785 const char* _to_const_char_ptr(const std::string &str)
786 {
787 return str.c_str();
788 }
789
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);
792
793
794private:
795 bool m_owning = true;
796};
797
798
807class DictionaryArray
808{
809public:
810
811 DictionaryArray() = default;
817 DictionaryArray(std::initializer_list<Dictionary> dicts);
825 DictionaryArray(AVDictionary **dicts, size_t count, bool takeOwning = true);
826
832 DictionaryArray(const DictionaryArray& rhs);
840 DictionaryArray& operator=(const DictionaryArray& rhs);
841
845 DictionaryArray(DictionaryArray&&) = default;
851 DictionaryArray& operator=(DictionaryArray&&) = default;
852
860 void assign(AVDictionary **dicts, size_t count, bool takeOwning = true);
866 void reserve(size_t size);
872 void resize(size_t size);
873
879 size_t size() const;
880
886 void pushBack(const Dictionary &dict);
892 void pushBack(Dictionary &&dict);
893
901 const Dictionary& operator[](size_t index) const;
909 Dictionary& operator[](size_t index);
917 const Dictionary& at(size_t index) const;
925 Dictionary& at(size_t index);
926
934 const AVDictionary* const* raws() const;
942 AVDictionary** raws();
943
949 AVDictionary** release();
955 void swap(DictionaryArray &rhs);
956private:
957 std::vector<AVDictionary*> m_raws;
958 std::vector<Dictionary> m_dicts;
959};
960
961} // namespace av
962
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
STL namespace.
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