-
Notifications
You must be signed in to change notification settings - Fork 0
/
impl-keyval-map-commattr.h
51 lines (48 loc) · 1.99 KB
/
impl-keyval-map-commattr.h
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
// SPDX-License-Identifier: MIT
int add_comm_keyval_callbacks(int keyval,
WRAP_Comm_copy_attr_function * comm_copy_attr_fn,
WRAP_Comm_delete_attr_function * comm_delete_attr_fn)
{
const std::lock_guard<std::mutex> lock(keyval_comm_attr_cb_mutex);
#if DEBUG
printf("%s: insert_or_assign(keyval=%d, comm_copy_attr_fn=%p, comm_delete_attr_fn=%p)\n",
__func__, keyval, comm_copy_attr_fn, comm_delete_attr_fn);
#endif
// insert_or_assign (C++17) inserts an element or assigns to the current element if the key already exists
auto [it,rc] = keyval_comm_attr_cb_map.insert_or_assign(keyval,
std::make_pair(comm_copy_attr_fn,
comm_delete_attr_fn));
return 1; // SUCCESS int{rc};
(void)it;
(void)rc;
}
int find_comm_keyval_callbacks(int keyval,
WRAP_Comm_copy_attr_function ** comm_copy_attr_fn,
WRAP_Comm_delete_attr_function ** comm_delete_attr_fn)
{
const std::lock_guard<std::mutex> lock(keyval_comm_attr_cb_mutex);
try {
auto [copy_fn,delete_fn] = keyval_comm_attr_cb_map.at(keyval);
#if DEBUG
printf("%s: lookup(keyval=%d) -> [comm_copy_attr_fn=%p, comm_delete_attr_fn=%p]\n",
__func__, keyval, copy_fn, delete_fn);
#endif
if (comm_copy_attr_fn != NULL) {
*comm_copy_attr_fn = copy_fn;
}
if (comm_delete_attr_fn != NULL) {
*comm_delete_attr_fn = delete_fn;
}
return 1;
}
catch (...) {
printf("%s: lookup(keyval=%d) failed\n", __func__, keyval);
return 0;
}
}
int remove_comm_keyval_callbacks(int keyval)
{
const std::lock_guard<std::mutex> lock(keyval_comm_attr_cb_mutex);
// returns the number of elements removed, so 0=failure and 1=success
return keyval_comm_attr_cb_map.erase(keyval);
}