-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHCPPInit.cpp
More file actions
70 lines (66 loc) · 1.77 KB
/
HCPPInit.cpp
File metadata and controls
70 lines (66 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/***************************************************************
* Name: HCPPInit.cpp
* Purpose: HCPPInit接口实现。
* Author: HYH (hyhsystem.cn)
* Created: 2024-11-15
* Copyright: HYH (hyhsystem.cn)
* License: MIT
**************************************************************/
#define HCPPINIT_IMPLEMENTATION 1
#include "HCPPInit.h"
#include <mutex>
#include <string>
#include <queue>
#include <functional>
#include <map>
namespace HCPPInitGlobal
{
extern std::recursive_mutex m_lock;
extern std::map<std::string,std::queue<std::function<void(void)>>> *m_map;
}
void HCPPInit(void)
{
std::lock_guard<std::recursive_mutex> lock(HCPPInitGlobal::m_lock);
if(HCPPInitGlobal::m_map==NULL)
{
return;
}
for(auto it=HCPPInitGlobal::m_map->begin(); it!=HCPPInitGlobal::m_map->end(); it++)
{
std::queue<std::function<void(void)>> &queue=it->second;
while(!queue.empty())
{
auto init_fn=queue.front();
if(init_fn!=NULL)
{
init_fn();
}
queue.pop();
}
}
{
// 删除,节省空间
delete HCPPInitGlobal::m_map;
HCPPInitGlobal::m_map=NULL;
}
}
void HCPPInitRegister(std::string tag,std::function<void (void)> init_fn)
{
std::lock_guard<std::recursive_mutex> lock(HCPPInitGlobal::m_lock);
if(HCPPInitGlobal::m_map==NULL)
{
HCPPInitGlobal::m_map=new std::map<std::string,std::queue<std::function<void(void)>>>();
}
(*HCPPInitGlobal::m_map)[tag].push(init_fn);
};
void HCPPInitRegister(const char * tag,void (*init_fn)(void))
{
if(tag==NULL)
{
tag="";
}
if(init_fn!=NULL)
{
HCPPInitRegister(std::string(tag),std::function<void (void)>(*init_fn));
}
}