Tamer
C++ language extensions for event-driven programming
Loading...
Searching...
No Matches
channel.hh
1// -*- mode: c++ -*-
2#ifndef TAMER_CHANNEL_HH
3#define TAMER_CHANNEL_HH 1
4#include <deque>
5#include <tamer/event.hh>
6namespace tamer {
7
8template <typename T>
9class channel {
10 public:
11 typedef typename std::deque<T>::size_type size_type;
12
13 inline channel();
14
15 inline size_type size() const;
16 inline bool empty() const;
17
18 inline size_type wait_size() const;
19 inline bool wait_empty() const;
20
21 inline void push_front(T x);
22 inline void push_back(T x);
23 inline void pop_front(tamer::event<T> e);
24 inline void pop_back(tamer::event<T> e);
25
26 private:
27 std::deque<T> q_;
28 std::deque<tamer::event<T> > waitq_;
29
30};
31
32template <typename T>
33inline channel<T>::channel() {
34}
35
36template <typename T>
37inline typename channel<T>::size_type channel<T>::size() const {
38 return q_.size();
39}
40
41template <typename T>
42inline bool channel<T>::empty() const {
43 return q_.empty();
44}
45
46template <typename T>
47inline typename channel<T>::size_type channel<T>::wait_size() const {
48 return waitq_.size();
49}
50
51template <typename T>
52inline bool channel<T>::wait_empty() const {
53 return waitq_.empty();
54}
55
56template <typename T>
57inline void channel<T>::push_front(T x) {
58 while (!waitq_.empty() && !waitq_.front())
59 waitq_.pop_front();
60 if (!waitq_.empty()) {
61 waitq_.front()(TAMER_MOVE(x));
62 waitq_.pop_front();
63 } else
64 q_.push_front(TAMER_MOVE(x));
65}
66
67template <typename T>
68inline void channel<T>::push_back(T x) {
69 while (!waitq_.empty() && !waitq_.front())
70 waitq_.pop_front();
71 if (!waitq_.empty()) {
72 waitq_.front()(TAMER_MOVE(x));
73 waitq_.pop_front();
74 } else
75 q_.push_back(TAMER_MOVE(x));
76}
77
78template <typename T>
79inline void channel<T>::pop_front(tamer::event<T> e) {
80 if (!q_.empty()) {
81 e(q_.front());
82 q_.pop_front();
83 } else
84 waitq_.push_back(TAMER_MOVE(e));
85}
86
87template <typename T>
88inline void channel<T>::pop_back(tamer::event<T> e) {
89 if (!q_.empty()) {
90 e(q_.back());
91 q_.pop_back();
92 } else
93 waitq_.push_back(TAMER_MOVE(e));
94}
95
96}
97#endif
The event template classes and helper functions.
Namespace containing public Tamer classes and functions for the Tamer core.
Definition adapter.hh:17