当前位置:网站首页>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
边栏推荐
- Climb Phoenix Mountain on December 19, 2021
- Get the data of the top 100 headlines today with Tianxing data
- Customized version of cacti host template
- About the use of URL, href, SRC attributes
- Function parameters (positional parameters, default value parameters, variable parameters, named keyword parameters, keyword parameters)
- Xiaobing · beauty appraisal
- Regular expression
- Take advantage of the world's sleeping gap to improve and surpass yourself -- get up early
- Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
- Common built-in modules
猜你喜欢
JMeter correlation technology
Automatic translation between Chinese and English
Canoe - the second simulation project -xvihicle1 bus database design (operation)
QQ group collection
Canoe: what is vtsystem
Notes on writing test points in mind mapping
Attributes and methods in math library
Discussion | has large AI become autonomous? Lecun, chief scientist of openai
Summary of Shanghai Jiaotong University postgraduate entrance examination module firewall technology
2020 Summary - Magic year, magic me
随机推荐
Simple understanding of seesion, cookies, tokens
Canoe - description of common database attributes
Usage of case when then else end statement
The most ideal automated testing model, how to achieve layering of automated testing
LVS+Keepalived实现四层负载及高可用
QQ get group link, QR code
OSI seven layer reference model
Terms related to hacker technology
JMeter correlation technology
unit testing
SSH原理和公钥认证
2021-11-02
QQ get group member operation time
Process communication and thread explanation
JMeter Foundation
Number and math classes
Capl: timer event
Polymorphic system summary
Understanding of object
I What is security testing