From 6eefba812ede29549af3633c490f2e85a4805524 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期三, 31 三月 2021 11:24:20 +0800 Subject: [PATCH] format code style. --- src/shm_queue.h | 184 ++++++++++++++++++++++++--------------------- 1 files changed, 99 insertions(+), 85 deletions(-) diff --git a/src/shm_queue.h b/src/shm_queue.h index 60b1862..9064f55 100644 --- a/src/shm_queue.h +++ b/src/shm_queue.h @@ -19,111 +19,125 @@ #ifndef SHM_QUEUE_JE0OEUP3 #define SHM_QUEUE_JE0OEUP3 -#include "shm.h" #include "msg.h" +#include "shm.h" #include <boost/circular_buffer.hpp> #include <boost/date_time/posix_time/posix_time.hpp> -namespace bhome_shm { - -template <class D> using Circular = boost::circular_buffer<D, Allocator<D> >; +namespace bhome_shm +{ + +template <class D> +using Circular = boost::circular_buffer<D, Allocator<D>>; typedef boost::uuids::uuid MQId; template <class D> class SharedQueue : private Circular<D> { - typedef Circular<D> Super; - Mutex mutex_; - Cond cond_read_; - Cond cond_write_; - Mutex & mutex() { return mutex_; } + typedef Circular<D> Super; + Mutex mutex_; + Cond cond_read_; + Cond cond_write_; + Mutex &mutex() { return mutex_; } - static boost::posix_time::ptime MSFromNow(const int ms) - { - using namespace boost::posix_time; - ptime cur = boost::posix_time::microsec_clock::universal_time(); - return cur + millisec(ms); - } + static boost::posix_time::ptime MSFromNow(const int ms) + { + using namespace boost::posix_time; + ptime cur = boost::posix_time::microsec_clock::universal_time(); + return cur + millisec(ms); + } public: - SharedQueue(const uint32_t len, Allocator<D> const& alloc):Super(len, alloc) {} - using Super::size; - using Super::capacity; - template <class Iter, class OnWrite> - int Write(Iter begin, Iter end, const int timeout_ms, const OnWrite &onWrite) { - int n = 0; - if (begin != end) { - auto endtime = MSFromNow(timeout_ms); - Guard lock(mutex()); - while (cond_write_.timed_wait(lock, endtime, [&]() { return !this->full(); })) { - onWrite(*begin); - this->push_back(*begin); - ++n; - cond_read_.notify_one(); - if (++begin == end) { - break; - } - } - } - return n; - } - template <class OnWrite> - bool Write(const D &buf, const int timeout_ms, const OnWrite &onWrite) { - return Write(&buf, (&buf)+1, timeout_ms, onWrite); - } - bool Write(const D &buf, const int timeout_ms) { return Write(buf, timeout_ms, [](const D &buf){}); } + SharedQueue(const uint32_t len, Allocator<D> const &alloc) : + Super(len, alloc) {} + using Super::capacity; + using Super::size; + template <class Iter, class OnWrite> + int Write(Iter begin, Iter end, const int timeout_ms, const OnWrite &onWrite) + { + int n = 0; + if (begin != end) { + auto endtime = MSFromNow(timeout_ms); + Guard lock(mutex()); + while (cond_write_.timed_wait(lock, endtime, [&]() { return !this->full(); })) { + onWrite(*begin); + this->push_back(*begin); + ++n; + cond_read_.notify_one(); + if (++begin == end) { + break; + } + } + } + return n; + } - template <class OnData> - bool Read(const int timeout_ms, OnData onData){ - int n = 0; - auto endtime = MSFromNow(timeout_ms); - Guard lock(mutex()); - while (cond_read_.timed_wait(lock, endtime, [&]() { return !this->empty(); })) { - const bool more = onData(this->front()); - this->pop_front(); - cond_write_.notify_one(); - ++n; - if (!more) { - break; - } - } - return n; - } - bool Read(D &buf, const int timeout_ms){ - auto read1 = [&](D &d) { - using std::swap; - swap(buf, d); - return false; - }; - return Read(timeout_ms, read1) == 1; - } + template <class OnWrite> + bool Write(const D &buf, const int timeout_ms, const OnWrite &onWrite) + { + return Write(&buf, (&buf) + 1, timeout_ms, onWrite); + } + bool Write(const D &buf, const int timeout_ms) + { + return Write(buf, timeout_ms, [](const D &buf) {}); + } + + template <class OnData> + bool Read(const int timeout_ms, OnData onData) + { + int n = 0; + auto endtime = MSFromNow(timeout_ms); + Guard lock(mutex()); + while (cond_read_.timed_wait(lock, endtime, [&]() { return !this->empty(); })) { + const bool more = onData(this->front()); + this->pop_front(); + cond_write_.notify_one(); + ++n; + if (!more) { + break; + } + } + return n; + } + + bool Read(D &buf, const int timeout_ms) + { + auto read1 = [&](D &d) { + using std::swap; + swap(buf, d); + return false; + }; + return Read(timeout_ms, read1) == 1; + } }; using namespace bhome_msg; -class ShmMsgQueue : private ShmObject<SharedQueue<MsgI> > +class ShmMsgQueue : private ShmObject<SharedQueue<MsgI>> { - typedef ShmObject<SharedQueue<MsgI> > Super; - typedef Super::Data Queue; - bool Write(const MsgI &buf, const int timeout_ms) { return data()->Write(buf, timeout_ms); } - bool Read(MsgI &buf, const int timeout_ms) { return data()->Read(buf, timeout_ms); } - MQId id_; -protected: - ShmMsgQueue(const std::string &raw_name, ShmType &segment, const int len); // internal use. -public: - ShmMsgQueue(const MQId &id, ShmType &segment, const int len); - ShmMsgQueue(ShmType &segment, const int len); - ~ShmMsgQueue(); - const MQId &Id() const { return id_; } + typedef ShmObject<SharedQueue<MsgI>> Super; + typedef Super::Data Queue; + bool Write(const MsgI &buf, const int timeout_ms) { return data()->Write(buf, timeout_ms); } + bool Read(MsgI &buf, const int timeout_ms) { return data()->Read(buf, timeout_ms); } + MQId id_; - bool Recv(BHMsg &msg, const int timeout_ms); - bool Recv(MsgI &msg, const int timeout_ms) { return Read(msg, timeout_ms); } - bool Send(const MQId &remote_id, const BHMsg &msg, const int timeout_ms); - static bool Send(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, const int timeout_ms); - bool Send(const MQId &remote_id, const MsgI &msg, const int timeout_ms) { - return Send(shm(), remote_id, msg, timeout_ms); - } +protected: + ShmMsgQueue(const std::string &raw_name, ShmType &segment, const int len); // internal use. +public: + ShmMsgQueue(const MQId &id, ShmType &segment, const int len); + ShmMsgQueue(ShmType &segment, const int len); + ~ShmMsgQueue(); + const MQId &Id() const { return id_; } + + bool Recv(BHMsg &msg, const int timeout_ms); + bool Recv(MsgI &msg, const int timeout_ms) { return Read(msg, timeout_ms); } + bool Send(const MQId &remote_id, const BHMsg &msg, const int timeout_ms); + static bool Send(SharedMemory &shm, const MQId &remote_id, const MsgI &msg, const int timeout_ms); + bool Send(const MQId &remote_id, const MsgI &msg, const int timeout_ms) + { + return Send(shm(), remote_id, msg, timeout_ms); + } }; } // namespace bhome_shm -- Gitblit v1.8.0