当前位置:网站首页>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_lThis 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
边栏推荐
猜你喜欢

2021 annual summary - it seems that I have done everything except studying hard

Introduction to Lichuang EDA

Canoe - the third simulation project - bus simulation - 3-2 project implementation

How to create a new virtual machine

Postman interface test

Notes on writing test points in mind mapping

Introduction of network security research direction of Shanghai Jiaotong University

Discussion | has large AI become autonomous? Lecun, chief scientist of openai

JMeter Foundation

Canoe - the third simulation project - bus simulation - 2 function introduction, network topology
随机推荐
Xiaobing · beauty appraisal
20 kinds of hardware engineers must be aware of basic components | the latest update to 8.13
thread
2021 annual summary - it seems that I have done everything except studying hard
Software testing related resources
LVS+Keepalived实现四层负载及高可用
Aike AI frontier promotion (2.14)
Video analysis
Sys module
Configure SSH certificate login
Summary of automated testing framework
Canoe: what is vtsystem
Basic function exercises
Canoe - description of common database attributes
Digital simulation beauty match preparation -matlab basic operation No. 6
Foreach (system.out:: println) usage
2022 AAAI fellow release! Yan Shuicheng, chief scientist of sail, and Feng Yan, Professor of Hong Kong University of science and technology, were selected
Dos and path
Four sorts: bubble, select, insert, count
Summary of collection: (to be updated)