当前位置:网站首页>template<typename MAP, typename LIST, typename First, typename ... Keytypes > recursive call with indefinite parameters - beauty of Pan China
template<typename MAP, typename LIST, typename First, typename ... Keytypes > recursive call with indefinite parameters - beauty of Pan China
2022-07-04 11:20:00 【Ascetic monk runnercai】
In explaining what we want to talk about in this chapter , Let's start with an example :
( The example comes from zlmediakit Medium MediaSource.cpp)
This example shows recursive search map An example of template meta programming , We mainly followfor_each_media_l
This function is called before and after .
// First of all, a nested layer map, Every key All are std::string, hinder find Functions are nested layer by layer from here map Find what we want MediaSource, Entire nested 4 layer , It's temporary schema,vhost,app,streama_id.
using Ptr = std::shared_ptr<MediaSource>;
using StreamMap = unordered_map<string/*strema_id*/, weak_ptr<MediaSource> >;
using AppStreamMap = unordered_map<string/*app*/, StreamMap>;
using VhostAppStreamMap = unordered_map<string/*vhost*/, AppStreamMap>;
using SchemaVhostAppStreamMap = unordered_map<string/*schema*/, VhostAppStreamMap>;
static recursive_mutex s_media_source_mtx;
static MediaSource::SchemaVhostAppStreamMap s_media_source_map;
MediaSource::Ptr MediaSource::find(const string &schema, const string &vhost, const string &app, const string &id, bool from_mp4) {
return find_l(schema, vhost, app, id, from_mp4);
}
static MediaSource::Ptr find_l(const string &schema, const string &vhost_in, const string &app, const string &id, bool from_mp4) {
MediaSource::Ptr ret;
MediaSource::for_each_media([&](const MediaSource::Ptr &src) {
ret = std::move(const_cast<MediaSource::Ptr &>(src)); }, schema, vhost, app, id);
return ret;
}
void MediaSource::for_each_media(const function<void(const Ptr &src)> &cb,
const string &schema,
const string &vhost,
const string &app,
const string &stream) {
deque<Ptr> src_list;
{
lock_guard<recursive_mutex> lock(s_media_source_mtx);
//printf("schema:%s,vhost:%s,app:%s,stream:%s\n", schema.c_str(), vhost.c_str(), app.c_str(), stream.c_str());
for_each_media_l(s_media_source_map, src_list, schema, vhost, app, stream);
}
for (auto &src : src_list) {
cb(src);
}
}
template<typename MAP, typename LIST, typename First, typename ...KeyTypes>
static void for_each_media_l(const MAP &map, LIST &list, const First &first, const KeyTypes &...keys) {
// If key It's empty , This means that you need to traverse all map.
if (first.empty()) {
for (auto &pr : map) {
for_each_media_l(pr.second, list, keys...);
}
return;
}
auto it = map.find(first);
if (it != map.end()) {
for_each_media_l(it->second, list, keys...);
}
}
// This is a recursive termination function
template<typename MAP, typename LIST, typename First>
static void for_each_media_l(const MAP &map, LIST &list, const First &first) {
if (first.empty()) {
for (auto &pr : map) {
emplace_back(list, pr.second);
}
return;
}
auto it = map.find(first);
if (it != map.end()) {
// Recursion finds it and puts it in list in
emplace_back(list, it->second);
}
}
template<typename LIST, typename Ptr>
static void emplace_back(LIST &list, const Ptr &ptr) {
auto src = ptr.lock();
if (src) {
list.emplace_back(std::move(src));
}
}
This example has a very detailed explanation :
link : The beauty of generalization –C++11 The magic of variable template parameters
边栏推荐
- Canoe-the second simulation project-xvehicle-1 bus database design (idea)
- Jemeter script recording
- Performance test process
- Canoe: distinguish VT, VN and vteststudio from their development history
- QQ group administrators
- Daemon xinted and logging syslogd
- Take advantage of the world's sleeping gap to improve and surpass yourself -- get up early
- Performance test method
- Usage of case when then else end statement
- Fundamentals of software testing
猜你喜欢
Foreach (system.out:: println) usage
QQ group collection
Summary of collection: (to be updated)
Canoe - description of common database attributes
2021-08-09
Postman interface test
Appscan installation steps
Introduction to canoe automatic test system
Canoe test: two ways to create CAPL test module
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
随机推荐
array_ The contains() function uses
First article
三立期货安全么?期货开户怎么开?目前期货手续费怎么降低?
SSH原理和公钥认证
Heartbeat报错 attempted replay attack
Canoe - the second simulation engineering - xvehicle - 2 panel design (operation)
Open the neural network "black box"! Unveil the mystery of machine learning system with natural language
Detailed array expansion analysis --- take you step by step analysis
Take advantage of the world's sleeping gap to improve and surpass yourself -- get up early
Digital simulation beauty match preparation -matlab basic operation No. 6
os. Path built-in module
Fundamentals of software testing
R built in data set
Canoe - the third simulation project - bus simulation - 3-2 project implementation
OSI model notes
Install freeradius3 in the latest version of openwrt
Jemeter script recording
Ten key performance indicators of software applications
Canoe-the second simulation project-xvehicle-1 bus database design (idea)
Performance test method