acl/lib_acl_cpp/include/acl_cpp/stdlib/mbox.hpp

82 lines
1.3 KiB
C++
Raw Normal View History

2017-04-07 20:13:08 +08:00
#pragma once
2017-06-02 14:47:24 +08:00
#include "../acl_cpp_define.hpp"
#include "noncopyable.hpp"
2017-04-07 20:13:08 +08:00
namespace acl
{
2017-09-01 18:59:42 +08:00
template<typename T>
class mbox
2017-04-07 20:13:08 +08:00
{
public:
mbox(void)
{
2017-09-02 09:42:11 +08:00
mbox_ = mbox_create();
}
~mbox(void)
{
2017-09-02 09:42:11 +08:00
mbox_free(mbox_, mbox_free_fn);
}
2017-04-07 20:13:08 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2017-09-01 18:59:42 +08:00
* @param t {T*} <EFBFBD>ǿ<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2017-04-07 20:13:08 +08:00
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
*/
2017-09-01 18:59:42 +08:00
bool push(T* t)
{
2017-09-02 09:42:11 +08:00
return mbox_send(mbox_, t);
2017-09-01 18:59:42 +08:00
}
2017-04-07 20:13:08 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param timeout {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0 ʱ<EFBFBD><EFBFBD><EFBFBD>ö<EFBFBD><EFBFBD>ȴ<EFBFBD><EFBFBD><EFBFBD>ʱʱ<EFBFBD><EFBFBD>(<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD>Զ<EFBFBD>ȴ<EFBFBD>ֱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2017-04-07 20:13:08 +08:00
* @param success {bool*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڸ<EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
2017-09-01 18:59:42 +08:00
* @return {T*} <EFBFBD><EFBFBD> NULL <EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ NULL ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD>
2017-04-07 20:13:08 +08:00
* success <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD>ɹ<EFBFBD>
*/
2017-09-01 18:59:42 +08:00
T* pop(int timeout = 0, bool* success = NULL)
{
2017-09-02 09:42:11 +08:00
return (T*) mbox_read(mbox_, timeout, success);
2017-09-01 18:59:42 +08:00
}
2017-04-07 20:13:08 +08:00
/**
* ͳ<EFBFBD>Ƶ<EFBFBD>ǰ<EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>
* @return {size_t}
*/
size_t push_count(void) const
{
2017-09-02 09:42:11 +08:00
return mbox_nsend(mbox_);
}
2017-04-07 20:13:08 +08:00
/**
* ͳ<EFBFBD>Ƶ<EFBFBD>ǰ<EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>
* @return {size_t}
*/
size_t pop_count(void) const
{
2017-09-02 09:42:11 +08:00
return mbox_nread(mbox_);
}
2017-04-07 20:13:08 +08:00
private:
2017-09-02 09:42:11 +08:00
void* mbox_;
2017-09-01 18:59:42 +08:00
static void mbox_free_fn(void* o)
{
T* t = (T*) o;
delete t;
}
2017-04-07 20:13:08 +08:00
};
2017-09-02 09:42:11 +08:00
// internal functions being used
void* mbox_create(void);
void mbox_free(void*, void (*free_fn)(void*));
bool mbox_send(void*, void*);
void* mbox_read(void*, int, bool*);
size_t mbox_nsend(void*);
size_t mbox_nread(void*);
2017-04-07 20:13:08 +08:00
} // namespace acl