[Libreoffice-commits] online.git: loolwsd/MessageQueue.cpp loolwsd/MessageQueue.hpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Tue Mar 29 02:47:12 UTC 2016


 loolwsd/MessageQueue.cpp |   33 ++++++++++++++++++++-------------
 loolwsd/MessageQueue.hpp |   21 +++++++++++++--------
 2 files changed, 33 insertions(+), 21 deletions(-)

New commits:
commit 69b4f42d3ca91209202f39f4423fbf00dbb5aead
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sat Mar 26 22:32:29 2016 -0400

    loolwsd: preparing MessageQueue for vector<char> payload
    
    Change-Id: I30bf7b4c641f4363790587ddc8ba8fb2bf9d269a
    Reviewed-on: https://gerrit.libreoffice.org/23582
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/MessageQueue.cpp b/loolwsd/MessageQueue.cpp
index a4e01cf..cf2c480 100644
--- a/loolwsd/MessageQueue.cpp
+++ b/loolwsd/MessageQueue.cpp
@@ -16,7 +16,7 @@ MessageQueue::~MessageQueue()
     clear();
 }
 
-void MessageQueue::put(const std::string& value)
+void MessageQueue::put(const Payload& value)
 {
     std::unique_lock<std::mutex> lock(_mutex);
     put_impl(value);
@@ -24,7 +24,7 @@ void MessageQueue::put(const std::string& value)
     _cv.notify_one();
 }
 
-std::string MessageQueue::get()
+MessageQueue::Payload MessageQueue::get()
 {
     std::unique_lock<std::mutex> lock(_mutex);
     _cv.wait(lock, [this] { return wait_impl(); });
@@ -37,13 +37,13 @@ void MessageQueue::clear()
     clear_impl();
 }
 
-void MessageQueue::remove_if(std::function<bool(const std::string&)> pred)
+void MessageQueue::remove_if(std::function<bool(const Payload&)> pred)
 {
     std::unique_lock<std::mutex> lock(_mutex);
     std::remove_if(_queue.begin(), _queue.end(), pred);
 }
 
-void MessageQueue::put_impl(const std::string& value)
+void MessageQueue::put_impl(const Payload& value)
 {
     _queue.push_back(value);
 }
@@ -53,11 +53,11 @@ bool MessageQueue::wait_impl() const
     return _queue.size() > 0;
 }
 
-std::string MessageQueue::get_impl()
+MessageQueue::Payload MessageQueue::get_impl()
 {
-    std::string result = _queue.front();
+    auto result = _queue.front();
     _queue.pop_front();
-    return result;
+    return std::move(result);
 }
 
 void MessageQueue::clear_impl()
@@ -65,17 +65,19 @@ void MessageQueue::clear_impl()
     _queue.clear();
 }
 
-void BasicTileQueue::put_impl(const std::string& value)
+void BasicTileQueue::put_impl(const Payload& value)
 {
-    if (value == "canceltiles")
+    const auto msg = std::string(&value[0], value.size());
+    if (msg == "canceltiles")
     {
         // remove all the existing tiles from the queue
         _queue.erase(std::remove_if(_queue.begin(), _queue.end(),
-                    [](const std::string& v)
+                    [](const Payload& v)
                     {
                         // must not remove the tiles with 'id=', they are special, used
                         // eg. for previews etc.
-                        return (v.compare(0, 5, "tile ") == 0) && (v.find("id=") == std::string::npos);
+                        const auto msg = std::string(&v[0], v.size());
+                        return (msg.compare(0, 5, "tile ") == 0) && (msg.find("id=") == std::string::npos);
                     }
                     ),
                 _queue.end());
@@ -84,12 +86,15 @@ void BasicTileQueue::put_impl(const std::string& value)
         _queue.push_front(value);
     }
     else
+    {
         MessageQueue::put_impl(value);
+    }
 }
 
-void TileQueue::put_impl(const std::string& value)
+void TileQueue::put_impl(const Payload& value)
 {
-    if (value.compare(0, 5, "tile ") == 0)
+    const auto msg = std::string(&value[0], value.size());
+    if (msg.compare(0, 5, "tile ") == 0)
     {
         // TODO: implement a real re-ordering here, so that the tiles closest to
         // the cursor are returned first.
@@ -104,7 +109,9 @@ void TileQueue::put_impl(const std::string& value)
         for (auto it = _queue.cbegin(); it != _queue.cend(); ++it)
         {
             if (value == *it)
+            {
                 return;
+            }
         }
     }
 
diff --git a/loolwsd/MessageQueue.hpp b/loolwsd/MessageQueue.hpp
index 9777c59..2d62173 100644
--- a/loolwsd/MessageQueue.hpp
+++ b/loolwsd/MessageQueue.hpp
@@ -13,46 +13,51 @@
 #include <condition_variable>
 #include <mutex>
 #include <deque>
+#include <vector>
 
 /** Thread-safe message queue (FIFO).
 */
 class MessageQueue
 {
 public:
+
+    typedef std::string Payload;
+
     MessageQueue()
     {
     }
+
     virtual ~MessageQueue();
 
     MessageQueue(const MessageQueue&) = delete;
     MessageQueue& operator=(const MessageQueue&) = delete;
 
     /// Thread safe insert the message.
-    void put(const std::string& value);
+    void put(const Payload& value);
 
     /// Thread safe obtaining of the message.
-    std::string get();
+    Payload get();
 
     /// Thread safe removal of all the pending messages.
     void clear();
 
     /// Thread safe remove_if.
-    void remove_if(std::function<bool(const std::string&)> pred);
+    void remove_if(std::function<bool(const Payload&)> pred);
 
 private:
     std::mutex _mutex;
     std::condition_variable _cv;
 
 protected:
-    virtual void put_impl(const std::string& value);
+    virtual void put_impl(const Payload& value);
 
     virtual bool wait_impl() const;
 
-    virtual std::string get_impl();
+    virtual Payload get_impl();
 
     virtual void clear_impl();
 
-    std::deque<std::string> _queue;
+    std::deque<Payload> _queue;
 };
 
 /** MessageQueue specialized for handling of tiles.
@@ -63,7 +68,7 @@ gets a "canceltiles" command.
 class BasicTileQueue : public MessageQueue
 {
 protected:
-    virtual void put_impl(const std::string& value);
+    virtual void put_impl(const Payload& value);
 };
 
 /** MessageQueue specialized for priority handling of tiles.
@@ -77,7 +82,7 @@ that the ones closest to the cursor position are returned first.
 class TileQueue : public BasicTileQueue
 {
 protected:
-    virtual void put_impl(const std::string& value);
+    virtual void put_impl(const Payload& value);
 };
 
 #endif


More information about the Libreoffice-commits mailing list