forked from ChunelFeng/CGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGMessage.h
More file actions
103 lines (87 loc) · 2.38 KB
/
GMessage.h
File metadata and controls
103 lines (87 loc) · 2.38 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/***************************
@Author: Chunel
@Contact: chunel@foxmail.com
@File: GMessage.h
@Time: 2022/10/30 20:38
@Desc:
***************************/
#ifndef CGRAPH_GMESSAGE_H
#define CGRAPH_GMESSAGE_H
#include <memory>
#include "GMessageObject.h"
#include "GMessageDefine.h"
CGRAPH_NAMESPACE_BEGIN
template<typename T, CUint capacity = CGRAPH_DEFAULT_RINGBUFFER_SIZE,
c_enable_if_t<std::is_base_of<GMessageParam, T>::value, int> = 0>
class GMessage : public GMessageObject {
public:
explicit GMessage(CUint size = capacity) {
queue_.setCapacity(size);
}
/**
* 析构函数。释放前,要先释放队列中所有的信息
*/
~GMessage() override {
queue_.clear();
}
/**
* 写入参数
* @tparam TImpl
* @param value
* @param strategy
* @return
*/
template<class TImpl,
c_enable_if_t<std::is_base_of<T, TImpl>::value, int> = 0>
CVoid send(const TImpl& value, GMessagePushStrategy strategy) {
queue_.push(value, strategy);
}
/**
* 写入智能指针类型的参数
* @tparam TImpl
* @param value
* @param strategy
* @return
*/
template<class TImpl,
c_enable_if_t<std::is_base_of<T, TImpl>::value, int> = 0>
CVoid send(std::unique_ptr<TImpl>& value, GMessagePushStrategy strategy) {
queue_.push(value, strategy);
}
/**
* 获取参数
* @param value
* @param timeout
* @return
*/
template<class TImpl,
c_enable_if_t<std::is_base_of<T, TImpl>::value, int> = 0>
CStatus recv(TImpl& value, CMSec timeout) {
return queue_.waitPopWithTimeout(value, timeout);
}
/**
* 通过智能指针的方式传递
* @tparam TImpl
* @param value
* @param timeout
* @return
*/
template<class TImpl,
c_enable_if_t<std::is_base_of<T, TImpl>::value, int> = 0>
CStatus recv(std::unique_ptr<TImpl>& value, CMSec timeout) {
return queue_.waitPopWithTimeout(value, timeout);
}
/**
* 获取容量大小
* @return
*/
CUint getCapacity() const {
return queue_.getCapacity();
}
private:
UAtomicRingBufferQueue<T, capacity> queue_;
};
template<typename T, CUint capacity = CGRAPH_DEFAULT_RINGBUFFER_SIZE>
using GMessagePtr = GMessage<T, capacity> *;
CGRAPH_NAMESPACE_END
#endif //CGRAPH_GMESSAGE_H