-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpin.hpp
75 lines (75 loc) · 2.04 KB
/
pin.hpp
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
#pragma once
#include "concurrentqueue.h"
#include <tuple>
namespace Pin
{
template<typename T>
class Base;
template<typename T>
class Rx;
template<typename T>
class Tx;
template<typename T>
std::tuple<Rx<T> ,Tx<T>> getRxTx()
{
std::shared_ptr<moodycamel::ConcurrentQueue<T>> sp_lockfree_q =std::make_shared<moodycamel::ConcurrentQueue<T>>() ;
std::shared_ptr<bool> isConnecting =std::make_shared<bool>(true);
Rx<T> rx;
Tx<T> tx;
rx.setSource(sp_lockfree_q,isConnecting);
tx.setSource(sp_lockfree_q,isConnecting);
return std::make_tuple(std::move(rx),std::move(tx));
}
template<typename T>
void connect(Rx<T> &rx,Tx<T> &tx,std::shared_ptr<moodycamel::ConcurrentQueue<T>> sp_q)
{
std::shared_ptr<moodycamel::ConcurrentQueue<T>> sp_lockfree_q =std::make_shared<moodycamel::ConcurrentQueue<T>>();
std::shared_ptr<bool> isConnecting =std::make_shared<bool>(true);
rx.setSource(sp_lockfree_q,isConnecting);
tx.setSource(sp_lockfree_q,isConnecting);
}
}
template<typename T>
class Pin::Base{
protected:
std::shared_ptr<moodycamel::ConcurrentQueue<T>> msp_lockfree_q;
std::shared_ptr<bool> m_isConnecting;
public:
void setSource(std::shared_ptr<moodycamel::ConcurrentQueue<T>> p_lockfree_q,std::shared_ptr<bool> p_bool)
{
msp_lockfree_q.reset();
msp_lockfree_q = p_lockfree_q;
m_isConnecting = p_bool;
}
};
template<typename T>
class Pin::Rx:public Pin::Base<T>{
public:
bool read(T & item)
{
return msp_lockfree_q->try_dequeue(item);
}
int getBufferedNum()
{
return msp_lockfree_q->size_approx();
}
void disconnect()
{
*m_isConnecting = false;
}
};
template<typename T>
class Pin::Tx:public Pin::Base<T>{
public:
bool send(T && item)
{
if(*m_isConnecting==true)
{
return msp_lockfree_q->try_enqueue(std::forward<T>(item));
}
else
{
return false;
}
}
};