avcpp 3.1.0
Wrapper for the FFmpeg that simplify usage from C++ projects.
Loading...
Searching...
No Matches
api2-dict-basic.cpp

Dictionary basics.

Dictionary basics.

api2-dict-basic <args>
// Exercises the dictionary API without touching any media.
//
// Shows: creating dictionaries, setting and reading keys, iteration, copying,
// serialising to a string and parsing one back.
//
// Usage: api2-dict-basic
#include <iostream>
#include "avcpp/av.h"
using namespace std;
using namespace av;
int main()
{
#if 0
{
}
#endif
#if 1
{
dict.set("test1", "test value");
dict.set("test2", 31337);
cout << dict.get("test1") << endl
<< dict.get("test2") << endl;
//<< dict.get("test3") << endl;
{
shared_ptr<char> sptr;
{
ptr = dict.toRawStringPtr('=', ';');
cout << ptr.get() << endl;
sptr = std::move(ptr);
}
cout << sptr.get() << endl;
}
cout << dict[1] << endl;
}
{
dict.set("test1", "test value");
dict.set("test2", 31337);
cout << endl;
cout << "ptr: " << (void*)dict.raw() << endl;
cout << dict.get("test1") << endl
<< dict.get("test2") << endl;
av::Dictionary dict2 = dict;
cout << endl;
cout << "ptr: " << (void*)dict.raw() << endl;
cout << dict.get("test1") << endl
<< dict.get("test2") << endl;
cout << "ptr: " << (void*)dict2.raw() << endl;
cout << dict2.get("test1") << endl
<< dict2.get("test2") << endl;
dict3 = dict;
cout << endl;
cout << "ptr: " << (void*)dict.raw() << endl;
cout << dict.get("test1") << endl
<< dict.get("test2") << endl;
cout << "ptr: " << (void*)dict3.raw() << endl;
cout << dict3.get("test1") << endl
<< dict3.get("test2") << endl;
av::Dictionary dict4 = std::move(dict2);
cout << endl;
cout << "ptr: " << (void*)dict2.raw() << endl;
cout << "ptr: " << (void*)dict4.raw() << endl;
cout << dict4.get("test1") << endl
<< dict4.get("test2") << endl;
dict5 = std::move(dict3);
cout << endl;
cout << "ptr: " << (void*)dict3.raw() << endl;
cout << "ptr: " << (void*)dict5.raw() << endl;
cout << dict5.get("test1") << endl
<< dict5.get("test2") << endl;
}
{
dict.set("test1", "test value");
dict.set("test2", 31337);
dict.set("test3", "other value");
for (auto const &i : dict)
{
cout << i.key() << " -> " << i.value() << endl;
}
auto it = dict.begin();
for (int i = 0; it != dict.end(); ++it, ++i)
{
cout << (*it).key() << " -> " << (*it).value() << endl;
cout << it->key() << " -> " << it->value() << endl;
it->set(to_string(i));
}
{
auto it = dict.cbegin();
for (int i = 0; it != dict.cend(); ++it, ++i)
{
cout << (*it).key() << " -> " << (*it).value() << endl;
cout << it->key() << " -> " << it->value() << endl;
//it->set(to_string(i)); // <-- compilation error
}
}
cout << "End\n";
}
{
av::Dictionary dict = {
{"init1", "value1"},
{"init2", "value2"}
};
for (auto const &i : dict)
{
cout << i.key() << " -> " << i.value() << endl;
}
dict = {
{"init3", "value3"},
{"init4", "value4"},
};
for (auto const &i : dict)
{
cout << i.key() << " -> " << i.value() << endl;
}
}
{
{
{"i1", "v1"},
{"i2", "v2"}
},
{
{"i3", "v3"},
{"i4", "v4"}
}
};
auto v = arr[0];
v.set("i1", "change1");
auto c = arr[0];
c.set("i1", "change2");
auto d = arr[0];
cout << v.get("i1") << ", " << c.get("i1") << ", " << d.get("i1") << endl;
auto& e = arr[0];
e.set("i1", "change3");
auto& f = arr[0];
cout << e.get("i1") << ", " << f.get("i1") << endl;
}
{
Dictionary dict;
dict.parseString("key1=val1;key2=val2", "=", ";");
dict.parseString("key3=val1;key4=val2", string("="), string(";"));
error_code ec;
cout << " str: " << dict.toString(':', ',', ec) << endl;
cout << " err: " << ec << ", " << ec.message() << endl;
cout << " str: " << dict.toString(':', '\0', ec) << endl;
cout << " err: " << ec << ", " << ec.message() << endl;
auto raw = dict.toRawStringPtr('=', ',', ec);
cout << " str: " << raw.get() << endl;
cout << " err: " << ec << ", " << ec.message() << endl;
}
#endif
#if 1
{
#if 0
{
AVDictionary *rdict = nullptr;
av_dict_set(&rdict, "rdict", "rvalue", 0);
av::Dictionary dict = {
{"dict", "val"}
};
dict.assign(rdict);
}
#endif
#if 0
{
Dictionary dict;
//dict.set("test")
dict["test"] = "value";
dict["test2"] = "fdsfdsfdsfdsfsdfsd";
auto ptr = dict.release();
auto ptr2 = dict.rawPtr();
//
// simulate external api
//
// {
av_freep(ptr2);
//av_freep(dict.rawPtr());
cout << (void*)dict.raw() << endl;
AVDictionary *rdict = nullptr;
av_dict_set(&rdict, "rdict", "rvalue", 0);
*ptr2 = rdict;
//
// }
dict.assign(*ptr2);
}
#endif
}
#endif
#if 0
{
{
{"key1", "val1"}
},
{
{"key2", "val2"}
},
};
auto arr2 = arr;
auto raw = arr2.raws();
for (size_t i = 0; i < arr2.size(); ++i)
{
auto ptr1 = raw[i];
auto ptr2 = arr2[i].raw();
auto ptr3 = arr[i].raw();
cout << (void*)ptr1 << " vs " << (void*)ptr2 << " vs " << (void*)ptr3 << endl;
}
}
#endif
return 0;
}
Library initialization.
Array of Dictinaries wrapper.
Definition dictionary.h:808
const AVDictionary *const * raws() const
The raw array, read-only.
Implements interface to access to the AVDictionary entity.
Definition dictionary.h:38
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...
AVDictionary ** rawPtr() noexcept
Address of the internal pointer, for FFmpeg calls that take AVDictionary**.
void assign(AVDictionary *dict, bool takeOwning=true) noexcept
assign - assign new resouces Old dictionary destroyes (if owning).
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)
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
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.
ConstIterator cbegin() const
First entry, read-only, for explicit const traversal.
Iterator end()
End of the iteration.
ConstIterator cend() const
End of the read-only traversal.
AVDictionary * release()
release - drops ownershipping
AVDictionary wrappers: Dictionary and DictionaryArray.
Root namespace of the library.
Definition audioresampler.h:19
STL namespace.
const T * raw() const
Read access to the wrapped object.
Definition ffmpeg.h:56
RAII holder for strings allocated by FFmpeg internals.
Definition dictionary.h:89