7 #ifndef __MERCURY_COROUTINE_ENGINE_BASE_CHANNEL__
8 #define __MERCURY_COROUTINE_ENGINE_BASE_CHANNEL__
75 virtual const std::type_info&
type_info()
const = 0;
84 virtual bool send(
const T& s)
const = 0;
87 virtual bool send(T&& s)
const = 0;
90 virtual bool recv(T& r)
const = 0;
125 struct iterator_context
127 iterator_context() : bc(NULL), good(
false) { }
128 iterator_context(
const base_channel<T>* rhs_bc) : bc(rhs_bc), good(
true) { }
130 inline T& dereference()
const {
return t; }
131 inline T* arrow()
const {
return &t; }
133 inline bool increment()
const
135 if(good && !bc->recv(t)) { good=
false; }
146 ctx = std::make_shared<iterator::iterator_context>(bc);
149 mutable std::shared_ptr<iterator_context> ctx;
153 typedef T value_type;
169 ctx = std::move(rhs.ctx);
176 return ctx == rhs.ctx;
189 inline T&
operator*()
const {
return ctx->dereference(); }
197 if(!ctx->increment()){ ctx.reset(); }
204 if(!ctx->increment()){ ctx.reset(); }
227 template <
typename T,
typename CHANNEL>
235 virtual void assign(
const CHANNEL& rhs)
const = 0;
236 virtual void assign(CHANNEL&& rhs)
const = 0;
238 template <
typename... As>
239 static CHANNEL make(As&&... as)
242 ch.construct(std::forward<As>(as)...);
247 inline explicit operator bool()
const
249 return ch->context() ? true :
false;
254 inline const CHANNEL&
operator=(
const CHANNEL& rhs)
const
263 ch->assign(std::move(rhs));
270 return ch->context() == rhs.context();
282 return !(*ch == rhs);
291 inline bool operator<(
const CHANNEL& rhs)
const
293 return ch->context() < rhs.context();
296 inline bool operator<(CHANNEL&& rhs)
const
298 return ch->context() < rhs.context();
301 inline bool operator<=(
const CHANNEL& rhs)
const
303 return ch->context() < rhs.context();
306 inline bool operator<=(CHANNEL&& rhs)
const
308 return ch->context() <= rhs.context();
311 inline bool operator>(
const CHANNEL& rhs)
const
313 return ch->context() > rhs.context();
316 inline bool operator>(CHANNEL&& rhs)
const
318 return ch->context() > rhs.context();
321 inline bool operator>=(
const CHANNEL& rhs)
const
323 return ch->context() > rhs.context();
326 inline bool operator>=(CHANNEL&& rhs)
const
328 return ch->context() >= rhs.context();
result
enum for channel operation results
Definition: base_channel.hpp:23
@ success
channel is closed, operation failed
Definition: base_channel.hpp:26
@ failure
blocking/nonblocking operation succeeded
Definition: base_channel.hpp:27
Definition: base_channel.hpp:123
T & operator*() const
retrieve reference to cached value T
Definition: base_channel.hpp:189
const iterator & operator=(iterator &&rhs) const
rvalue iterator assignment
Definition: base_channel.hpp:167
bool operator==(iterator &&rhs) const
rvalue iterator comparison
Definition: base_channel.hpp:180
const iterator & operator=(const iterator &rhs) const
lvalue iterator assignment
Definition: base_channel.hpp:160
bool operator!=(const iterator &rhs) const
lvalue iterator not comparison
Definition: base_channel.hpp:183
bool operator!=(iterator &&rhs) const
rvalue iterator not comparison
Definition: base_channel.hpp:186
T * operator->() const
retrieve pointer to cached value T
Definition: base_channel.hpp:192
const iterator & operator++() const
retrieve data from the channel iterator
Definition: base_channel.hpp:195
const iterator operator++(int) const
retrieve data from the channel iterator
Definition: base_channel.hpp:202
bool operator==(const iterator &rhs) const
lvalue iterator comparison
Definition: base_channel.hpp:174
Definition: base_channel.hpp:49
virtual bool recv(T &r) const =0
retrieve data from channel
iterator end() const
default iterator == end()
Definition: base_channel.hpp:219
virtual bool send(const T &s) const =0
send a copy of data through channel
virtual void * context() const =0
retrieve internal context pointer
base_channel(const base_channel< T > *rhs_bc)
iterator context constructor
Definition: base_channel.hpp:54
virtual bool send(T &&s) const =0
move data through channel
virtual bool closed() const =0
report if channel is closed
virtual result try_send(T &&s) const =0
attempt to move data through channel
virtual result try_recv(T &r) const =0
attempt to retrieve data from channel
virtual void construct() const =0
construct channel context
iterator begin() const
iterator to the current beginning of the channel
Definition: base_channel.hpp:210
virtual const std::type_info & type_info() const =0
retrieve type_info
virtual result try_send(const T &s) const =0
attempt to send a copy of data through channel
virtual void close() const =0
close channel
T value_type
template type for channel
Definition: base_channel.hpp:51
Definition: base_channel.hpp:229
bool operator==(const CHANNEL &rhs) const
lvalue channel context comparison
Definition: base_channel.hpp:268
const CHANNEL & operator=(const CHANNEL &rhs) const
lvalue assign channel context
Definition: base_channel.hpp:254
const CHANNEL & operator=(CHANNEL &&rhs) const
rvalue assign channel context
Definition: base_channel.hpp:261
bool operator==(CHANNEL &&rhs) const
rvalue channel context comparison
Definition: base_channel.hpp:274
bool operator!=(const CHANNEL &rhs) const
lvalue channel context not comparison
Definition: base_channel.hpp:280
channel_operators(CHANNEL *rhs_ch)
context constructor
Definition: base_channel.hpp:231
bool operator!=(CHANNEL &&rhs) const
rvalue channel context not comparison
Definition: base_channel.hpp:286