avcpp 3.2.1
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
av::Dictionary Class Reference

Implements interface to access to the AVDictionary entity. More...

#include <dictionary.h>

Inheritance diagram for av::Dictionary:

Classes

struct  AvStringDeleter
 Deleter for raw string. More...
struct  AvStringPtr
 RAII holder for strings allocated by FFmpeg internals. More...
class  DictionaryIterator
 Base dictionary iterator implementation. More...
class  Entry
 Dictionary key and value holder and accessor. More...

Public Types

enum  Flags {
  FlagMatchCase = AV_DICT_MATCH_CASE , FlagIgnoreSuffix = AV_DICT_IGNORE_SUFFIX , FlagDontStrdupKey = AV_DICT_DONT_STRDUP_KEY , FlagDontStrdupVal = AV_DICT_DONT_STRDUP_VAL ,
  FlagDontOverwrite = AV_DICT_DONT_OVERWRITE , FlagAppend = AV_DICT_APPEND
}
 AVDictionary flags mapping. More...
using Iterator = DictionaryIterator<false>
 Non-const iterator.
using ConstIterator = DictionaryIterator<true>
 Const iterator.

Public Member Functions

 Dictionary ()
 Default ctor do nothig, null dictionary creates.
 Dictionary (AVDictionary *dict, bool takeOwning=true)
 Assign ctor Wrap around raw dictionary.
 ~Dictionary ()
 Dtor If Dictionary takes ownershipping on AVDictionary, it free allocated resources.
 Dictionary (const Dictionary &other)
 Copy ctor Make deep copy of dictionary.
Dictionary & operator= (const Dictionary &rhs)
 Copy assign operator Make deep copy of dictionary.
 Dictionary (Dictionary &&other)
 Move ctor Takes resources.
Dictionary & operator= (Dictionary &&rhs)
 Move assign operator Takes resources.
 Dictionary (std::initializer_list< std::pair< const char *, const char * > > list, int flags=0)
 Initializer ctor Allows create dictionaries in array manier:
Dictionary & operator= (std::initializer_list< std::pair< const char *, const char * > > list)
 Initializer assign operator Allows assign dictionaries in array manier:
bool isOwning () const noexcept
 isOwning - checks resources owning status
void assign (AVDictionary *dict, bool takeOwning=true) noexcept
 assign - assign new resouces Old dictionary destroyes (if owning).
template<typename Key, typename Value = Key>
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 (null), it takes ownershipping too.
template<typename Str, typename Sep1, typename Sep2>
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:
std::string toString (const char keyValSep, const char pairsSep, OptionalErrorCode ec=throws()) const
 toString - converts dictionary to the string representation (serialize)
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::unique_ptr).
void copyFrom (const Dictionary &other, int flags=0) noexcept
 copyFrom - copy data from other dictionary.
void swap (Dictionary &other) noexcept
 swap - swaps resouces between objects
AVDictionary * release ()
 release - drops ownershipping
AVDictionary ** rawPtr () noexcept
 Address of the internal pointer, for FFmpeg calls that take AVDictionary**.
Iterator interface
Iterator begin ()
 First entry.
Iterator end ()
 End of the iteration.
ConstIterator begin () const
 First entry, read-only.
ConstIterator end () const
 End of the read-only iteration.
ConstIterator cbegin () const
 First entry, read-only, for explicit const traversal.
ConstIterator cend () const
 End of the read-only traversal.
Index based access operator

operator [] - access to the entry via index O(1) complexity.

Parameters
index
Returns
const char * operator[] (size_t index) const
 Entry value by position.
Entry operator[] (size_t index)
 Entry holder by position, for assignment.
Key based access operator

operator [] - access to the entry via key O(n) complexity.

Parameters
key
Returns
const char * operator[] (const char *key) const
 Entry value by key.
Entry operator[] (const char *key) noexcept
 Entry holder by key, for assignment.
Entries counting

count/size - returns count of dictionary entries.

O(1) complexity.

Returns
size_t count () const noexcept
 Number of entries.
size_t size () const noexcept
 Number of entries.
Key based getter interface

get - gets value by key O(n) complexity.

Parameters
keykey for entry access
flagssee Flags
Returns
nullptr if key does not present
const char * get (const char *key, int flags=0) const noexcept
 Value for a key.
const char * get (const std::string &key, int flags=0) const noexcept
 Value for a key.
Public Member Functions inherited from FFWrapperPtr< AVDictionary >
 FFWrapperPtr ()=default
 Create an empty wrapper; isNull() is true.
const AVDictionary * raw () const
 Read access to the wrapped object.
void reset (AVDictionary *raw=nullptr)
 Replace the stored pointer.
bool isNull () const
 Whether the wrapper is empty.
void _log (int level, const char *fmt) const
 Log through the FFmpeg log system, using this wrapper as the context.

Additional Inherited Members

Protected Attributes inherited from FFWrapperPtr< AVDictionary >
AVDictionary * m_raw
 The wrapped pointer; owned by the derived class, not by this template.

Detailed Description

It also add useful extensions, like:

  • Iterator interface: you can walk via dictionary entries via foreach cycles
  • Initialization list construction: you can initialize dictionary simple as structure or array

This class also provide way to controll owning: you can create entity that does not owning data but provide access to them. You can drop owning by release() call.

Examples
api2-dict-basic.cpp.

Member Enumeration Documentation

◆ Flags

Enumerator
FlagMatchCase 

Do not ignore case.

FlagIgnoreSuffix 

Special case: Iterate via dictionary.

FlagDontStrdupKey 

Do not duplicate string key.

String must be duplicate before with av_strdup() or allocates with av_malloc(). Do not use new[] for it! This flag ignores for std::string keys.

FlagDontStrdupVal 

Do not duplicate string value.

String must be duplicate before with av_strdup() or allocates with av_malloc(). Do not use new[] for it! This flag ignores for std::string values and for integer values

FlagDontOverwrite 

Keep existing value if key already exists.

FlagAppend 

Append value to the existing one (string concat).

Constructor & Destructor Documentation

◆ Dictionary() [1/4]

av::Dictionary::Dictionary ( AVDictionary * dict,
bool takeOwning = true )
explicit

Useful to iterate with low-level FFmpeg API

Parameters
dictdictionary to hold
takeOwningownershipping flag. If false, Dictionary only wraps access to the AVDictionary and do not free resources on destroy.

◆ Dictionary() [2/4]

av::Dictionary::Dictionary ( const Dictionary & other)

Takes ownershipping on newly created dict.

Parameters
other

◆ Dictionary() [3/4]

av::Dictionary::Dictionary ( Dictionary && other)

Other dictionary moves to uninited state. Ownershpping same to other.

Parameters
other

◆ Dictionary() [4/4]

av::Dictionary::Dictionary ( std::initializer_list< std::pair< const char *, const char * > > list,
int flags = 0 )
Dictionary dict = {
{"key1", "value1"},
{"key2", "value2"},
};
Dictionary()
Default ctor do nothig, null dictionary creates.

New dictionary owning resources.

Parameters
listinit list
flagssee Flags

Member Function Documentation

◆ assign()

void av::Dictionary::assign ( AVDictionary * dict,
bool takeOwning = true )
noexcept

If dict same to the olready holding one, but takeOwning is true and isOwning() false - takes owning. Otherwise do nothing.

Parameters
dictdictionary to hold
takeOwningowning flag
Examples
api2-dict-basic.cpp.

◆ begin() [1/2]

Iterator av::Dictionary::begin ( )
Returns
an iterator at the first entry, or end() when the dictionary is empty
Examples
api2-dict-basic.cpp.

◆ begin() [2/2]

ConstIterator av::Dictionary::begin ( ) const
Returns
a const iterator at the first entry

◆ cbegin()

ConstIterator av::Dictionary::cbegin ( ) const
Returns
a const iterator at the first entry
Examples
api2-dict-basic.cpp.

◆ cend()

ConstIterator av::Dictionary::cend ( ) const
Returns
the past-the-end const iterator
Examples
api2-dict-basic.cpp.

◆ copyFrom()

void av::Dictionary::copyFrom ( const Dictionary & other,
int flags = 0 )
noexcept

If dictionary already exists, new fields will be addred. Entries with same keys will be overrided according to flags.

Parameters
otherdict to copy from
flagssee Flags

◆ count()

size_t av::Dictionary::count ( ) const
noexcept
Returns
the entry count
Note
O(1).

◆ end() [1/2]

Iterator av::Dictionary::end ( )
Returns
the past-the-end iterator
Examples
api2-dict-basic.cpp.

◆ end() [2/2]

ConstIterator av::Dictionary::end ( ) const
Returns
the past-the-end const iterator

◆ get() [1/2]

const char * av::Dictionary::get ( const char * key,
int flags = 0 ) const
noexcept
Parameters
keythe key to look up
flagsa combination of AV_DICT_* lookup flags
Returns
the value, or nullptr when the key is absent
Examples
api2-dict-basic.cpp.

◆ get() [2/2]

const char * av::Dictionary::get ( const std::string & key,
int flags = 0 ) const
noexcept
Parameters
keythe key to look up
flagsa combination of AV_DICT_* lookup flags
Returns
the value, or nullptr when the key is absent

◆ isOwning()

bool av::Dictionary::isOwning ( ) const
noexcept
Returns
true if object owning resources

◆ operator=() [1/3]

Dictionary & av::Dictionary::operator= ( const Dictionary & rhs)

Takes ownershipping on newly created dict. Old dict destroyed (resouces freed if there is ownershipping)

Parameters
rhsthe dictionary to copy
Returns
this dictionary

◆ operator=() [2/3]

Dictionary & av::Dictionary::operator= ( Dictionary && rhs)

Rhs dictionary moves to uninited state. Ownershpping same to rhs. Old dict destroyed (resouces freed if there is ownershipping)

Parameters
rhsthe dictionary to move from
Returns
this dictionary

◆ operator=() [3/3]

Dictionary & av::Dictionary::operator= ( std::initializer_list< std::pair< const char *, const char * > > list)
...
dict = {
{"key1", "value1"},
{"key2", "value2"},
};

New dictionary owning resources. Old dictionary destroes (resource freed if owning)

Parameters
listinit list
Returns
this dictionary

◆ operator[]() [1/4]

const char * av::Dictionary::operator[] ( const char * key) const
Parameters
keythe key to look up
Returns
the value, or nullptr when the key is absent
Note
O(n): the dictionary is a linked list.

◆ operator[]() [2/4]

Entry av::Dictionary::operator[] ( const char * key)
noexcept
Parameters
keythe key to look up
Returns
an entry holder; assigning to it sets the value
Note
O(n).

◆ operator[]() [3/4]

Entry av::Dictionary::operator[] ( size_t index)
Parameters
indexzero-based position
Returns
an entry holder; assigning to it changes the value
Note
O(1).

◆ operator[]() [4/4]

const char * av::Dictionary::operator[] ( size_t index) const
Parameters
indexzero-based position; the order is the dictionary order
Returns
the value, or nullptr when the index is out of range
Note
O(1): the entries are counted from the head of the list.

◆ parseString()

template<typename Str, typename Sep1, typename Sep2>
void av::Dictionary::parseString ( const Str & str,
const Sep1 & keyValSep,
const Sep2 & pairsSep,
int flags = 0,
OptionalErrorCode ec = throws() )
inline
foo=bar;foo2=bar2
foo:bar&foo2:bar2
Parameters
strstring to process
keyValSepnull-terminated string with chars that interprets as key and value separators '=' and ':' in most cases.
pairsSepnull-terminates string with chars that interprets as pairs (key and value) separators. ';' and ',' in most cases.
flagsSee Flags. All flags that omit strdups ignores.
[in,out]ecthis represents the error status on exit, if this is pre-initialized to av::throws the function will throw on error instead
Examples
api2-dict-basic.cpp.

◆ rawPtr()

AVDictionary ** av::Dictionary::rawPtr ( )
noexcept
Returns
a pointer to the stored pointer
Note
Pass it to functions such as av_dict_set() and av_dict_copy(); they may allocate a dictionary when the stored pointer is null.
Examples
api2-dict-basic.cpp.

◆ release()

AVDictionary * av::Dictionary::release ( )
Returns
Give up ownership of the dictionary.
the raw dictionary, which the caller must free; the wrapper is left empty and no longer owning
Examples
api2-dict-basic.cpp.

◆ set()

template<typename Key, typename Value = Key>
auto av::Dictionary::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
inline
Parameters
[in]keykey to change (can be std::string or char*)
[in]valuekey value (can be std::string, char* or integer)
[in,out]ecthis represents the error status on exit, if this is pre-initialized to av::throws the function will throw on error instead
[in]flagssee Flags
Returns
nothing; errors are reported through ec or an exception
Examples
api2-dict-basic.cpp.

◆ size()

size_t av::Dictionary::size ( ) const
noexcept
Returns
the entry count

◆ swap()

void av::Dictionary::swap ( Dictionary & other)
noexcept
Parameters
other

◆ toRawStringPtr()

AvStringPtr av::Dictionary::toRawStringPtr ( const char keyValSep,
const char pairsSep,
OptionalErrorCode ec = throws() ) const

This method omit data copy and returns raw pointer that allocated by av_dict_get_string(). For more safety this block wrapped with no-overhead smart pointer (std::unique_ptr).

See also
toString()
Parameters
[in]keyValSepchar to separate key and value
[in]pairsSepchat to separate key-value pairs.
[in,out]ecthis represents the error status on exit, if this is pre-initialized to av::throws the function will throw on error instead
Returns
valid string, null on error (check ec)
Examples
api2-dict-basic.cpp.

◆ toString()

std::string av::Dictionary::toString ( const char keyValSep,
const char pairsSep,
OptionalErrorCode ec = throws() ) const

This line can be processed via parseString() later.

FFmpeg internaly allocated buffer copies to the string and freed.

Parameters
[in]keyValSepchar to separate key and value
[in]pairsSepchat to separate key-value pairs.
[in,out]ecthis represents the error status on exit, if this is pre-initialized to av::throws the function will throw on error instead
Returns
valid string, null-string (std::string()) on error
Note
\0 and same separator chars unapplicable.
Examples
api2-dict-basic.cpp.

The documentation for this class was generated from the following file: