acl/lib_fiber/cpp/include/fiber/channel.hpp

49 lines
645 B
C++
Raw Normal View History

2017-09-22 12:27:21 +08:00
#pragma once
#include "fiber_cpp_define.hpp"
2017-09-22 12:27:21 +08:00
struct ACL_CHANNEL;
namespace acl {
ACL_CHANNEL *channel_create(int elemsize, int bufsize);
void channel_free(ACL_CHANNEL *c);
int channel_send(ACL_CHANNEL *c, void *v);
int channel_recv(ACL_CHANNEL *c, void *v);
2017-09-22 12:27:21 +08:00
template <typename T>
class channel
{
public:
channel(void)
{
chan_ = channel_create(sizeof(T), 100);
2017-09-22 12:27:21 +08:00
}
~channel(void)
{
channel_free(chan_);
2017-09-22 12:27:21 +08:00
}
channel& operator << (T& t)
{
return put(t);
}
channel& put(T& t)
{
channel_send(chan_, &t);
2017-09-22 12:27:21 +08:00
return *this;
}
void pop(T& t)
{
channel_recv(chan_, &t);
2017-09-22 12:27:21 +08:00
}
private:
ACL_CHANNEL* chan_;
};
} // namespace acl