-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathib_utils.h
42 lines (36 loc) · 1.2 KB
/
ib_utils.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
#ifndef IB_UTILS_H_
#define IB_UTILS_H_
#include <functional>
#include <memory>
#include <iostream>
template<typename T>
using VoidDeleter = std::function<void(T*)>;
template<typename T>
VoidDeleter<T> int_deleter_wrapper(int (*orig_deleter)(T*), std::string msg)
{
return [orig_deleter, msg_capture = std::move(msg)](T *obj) -> void {
// std::cout << " deleting: " << msg_capture << std::endl;
if(orig_deleter(obj)) {
// perror(msg_capture.c_str());
}
};
}
template<typename Res, typename ...Args>
using ResourceFactory= std::function<Res*(Args...)>;
template<typename Res, typename ...Args>
ResourceFactory<Res, Args...> factory_wrapper(Res *(*orig_factory)(Args...), std::string msg) {
return [orig_factory, msg_capture = std::move(msg)](Args&&... args) -> Res* {
Res *ret = orig_factory(std::forward<Args>(args)...);
// perror("before");
// std::cout << " ctoring: " << msg_capture << " : ";
if(ret == NULL) {
// std::cout << "failed!" << std::endl;
perror(msg_capture.c_str());
std::abort();
}
// std::cout << "success" << std::endl;
// perror("after");
return ret;
};
}
#endif // IB_UTILS_H_