-
Notifications
You must be signed in to change notification settings - Fork 293
/
UAllocator.h
80 lines (66 loc) · 1.78 KB
/
UAllocator.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: UAllocator.h
@Time: 2021/10/28 9:15 下午
@Desc:
***************************/
#ifndef CGRAPH_UALLOCATOR_H
#define CGRAPH_UALLOCATOR_H
#include <mutex>
#include <memory>
#include "../CBasic/CBasicInclude.h"
CGRAPH_NAMESPACE_BEGIN
static std::mutex g_session_mtx;
/**
* 仅用于生成CObject类型的类
*/
class UAllocator : public CObject {
public:
/**
* 生成普通指针信息
* @tparam T
* @return
*/
template<typename T,
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static T* safeMallocCObject() {
T* ptr = nullptr;
while (!ptr) {
ptr = new(std::nothrow) T();
}
return ptr;
}
/**
* 生成带参数的普通指针
* @tparam T
* @tparam Args
* @param args
* @return
*/
template<typename T, typename ...Args,
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static T* safeMallocTemplateCObject(Args... args) {
T* ptr = nullptr;
while (!ptr) {
ptr = new T(std::forward<Args>(args)...);
}
return ptr;
}
/**
* 生成unique智能指针信息
* @tparam T
* @return
*/
template<typename T,
c_enable_if_t<std::is_base_of<CObject, T>::value, int> = 0>
static std::unique_ptr<T> makeUniqueCObject() {
return c_make_unique<T>();
}
};
#define CGRAPH_SAFE_MALLOC_COBJECT(Type) \
UAllocator::safeMallocCObject<Type>(); \
#define CGRAPH_MAKE_UNIQUE_COBJECT(Type) \
UAllocator::makeUniqueCObject<Type>(); \
CGRAPH_NAMESPACE_END
#endif //CGRAPH_UALLOCATOR_H