2015-11-09 04:36:37 -06:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef INCLUDED_MESSAGEQUEUE_HPP
|
|
|
|
#define INCLUDED_MESSAGEQUEUE_HPP
|
|
|
|
|
2016-09-23 03:37:33 -05:00
|
|
|
#include <algorithm>
|
2015-11-09 04:36:37 -06:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <deque>
|
2016-09-14 16:42:50 -05:00
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
2016-03-26 21:32:29 -05:00
|
|
|
#include <vector>
|
2015-11-09 04:36:37 -06:00
|
|
|
|
|
|
|
/** Thread-safe message queue (FIFO).
|
|
|
|
*/
|
|
|
|
class MessageQueue
|
|
|
|
{
|
|
|
|
public:
|
2016-03-26 21:32:29 -05:00
|
|
|
|
2016-03-26 21:56:10 -05:00
|
|
|
typedef std::vector<char> Payload;
|
2016-03-26 21:32:29 -05:00
|
|
|
|
2015-11-24 02:41:59 -06:00
|
|
|
MessageQueue()
|
|
|
|
{
|
|
|
|
}
|
2016-03-26 21:32:29 -05:00
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
virtual ~MessageQueue();
|
|
|
|
|
|
|
|
MessageQueue(const MessageQueue&) = delete;
|
|
|
|
MessageQueue& operator=(const MessageQueue&) = delete;
|
|
|
|
|
|
|
|
/// Thread safe insert the message.
|
2016-03-26 21:32:29 -05:00
|
|
|
void put(const Payload& value);
|
2016-03-26 21:56:10 -05:00
|
|
|
void put(const std::string& value)
|
|
|
|
{
|
|
|
|
put(Payload(value.data(), value.data() + value.size()));
|
|
|
|
}
|
2015-11-09 04:36:37 -06:00
|
|
|
|
|
|
|
/// Thread safe obtaining of the message.
|
2016-03-26 21:32:29 -05:00
|
|
|
Payload get();
|
2015-11-09 04:36:37 -06:00
|
|
|
|
|
|
|
/// Thread safe removal of all the pending messages.
|
|
|
|
void clear();
|
|
|
|
|
2015-12-13 22:36:46 -06:00
|
|
|
/// Thread safe remove_if.
|
2016-04-27 07:58:33 -05:00
|
|
|
void remove_if(const std::function<bool(const Payload&)>& pred);
|
2015-12-13 22:36:46 -06:00
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
private:
|
|
|
|
std::mutex _mutex;
|
|
|
|
std::condition_variable _cv;
|
|
|
|
|
|
|
|
protected:
|
2016-03-26 21:32:29 -05:00
|
|
|
virtual void put_impl(const Payload& value);
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2016-04-01 07:33:02 -05:00
|
|
|
bool wait_impl() const;
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2016-09-15 07:40:26 -05:00
|
|
|
virtual Payload get_impl();
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2016-04-01 07:33:02 -05:00
|
|
|
void clear_impl();
|
2015-11-09 04:36:37 -06:00
|
|
|
|
2016-03-26 21:32:29 -05:00
|
|
|
std::deque<Payload> _queue;
|
2015-11-09 04:36:37 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/** MessageQueue specialized for priority handling of tiles.
|
|
|
|
*/
|
2016-09-25 10:50:01 -05:00
|
|
|
class TileQueue : public MessageQueue
|
2015-11-09 04:36:37 -06:00
|
|
|
{
|
2016-09-14 16:42:50 -05:00
|
|
|
private:
|
|
|
|
|
|
|
|
class CursorPosition
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int Part;
|
|
|
|
int X;
|
|
|
|
int Y;
|
|
|
|
int Width;
|
|
|
|
int Height;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
void updateCursorPosition(int viewId, int part, int x, int y, int width, int height)
|
|
|
|
{
|
|
|
|
auto cursorPosition = CursorPosition({part, x, y, width, height});
|
|
|
|
auto it = _cursorPositions.find(viewId);
|
|
|
|
if (it != _cursorPositions.end())
|
|
|
|
{
|
|
|
|
it->second = cursorPosition;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_cursorPositions[viewId] = cursorPosition;
|
|
|
|
}
|
2016-09-23 03:37:33 -05:00
|
|
|
|
2016-09-25 11:38:17 -05:00
|
|
|
// Move to front, so the current front view
|
|
|
|
// becomes the second.
|
|
|
|
const auto view = std::find(_viewOrder.begin(), _viewOrder.end(), viewId);
|
2016-09-23 03:37:33 -05:00
|
|
|
if (view != _viewOrder.end())
|
|
|
|
{
|
2016-09-25 11:38:17 -05:00
|
|
|
_viewOrder.erase(view);
|
2016-09-23 03:37:33 -05:00
|
|
|
}
|
2016-09-25 11:38:17 -05:00
|
|
|
|
|
|
|
_viewOrder.push_front(viewId);
|
2016-09-14 16:42:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void removeCursorPosition(int viewId)
|
|
|
|
{
|
2016-09-25 11:38:17 -05:00
|
|
|
const auto view = std::find(_viewOrder.begin(), _viewOrder.end(), viewId);
|
|
|
|
if (view != _viewOrder.end())
|
|
|
|
{
|
|
|
|
_viewOrder.erase(view);
|
|
|
|
}
|
2016-09-23 03:37:33 -05:00
|
|
|
|
2016-09-14 16:42:50 -05:00
|
|
|
_cursorPositions.erase(viewId);
|
|
|
|
}
|
|
|
|
|
2015-11-09 04:36:37 -06:00
|
|
|
protected:
|
2016-04-01 07:31:26 -05:00
|
|
|
virtual void put_impl(const Payload& value) override;
|
2016-09-14 16:42:50 -05:00
|
|
|
|
2016-09-15 07:40:26 -05:00
|
|
|
virtual Payload get_impl() override;
|
|
|
|
|
2016-09-14 16:42:50 -05:00
|
|
|
private:
|
|
|
|
|
|
|
|
/// Check if the given tile msg underlies a cursor.
|
|
|
|
bool priority(const std::string& tileMsg);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<int, CursorPosition> _cursorPositions;
|
2016-09-23 03:37:33 -05:00
|
|
|
|
|
|
|
/// Check the views in the order of how the editing (cursor movement) has
|
|
|
|
/// been happening.
|
|
|
|
std::deque<int> _viewOrder;
|
2015-11-09 04:36:37 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|