-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.hpp
42 lines (36 loc) · 882 Bytes
/
queue.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once
#include "entities.hpp"
#include <experimental/optional>
#include <mutex>
using std::mutex;
using std::vector;
using std::lock_guard;
using std::find_if;
using std::experimental::optional;
using std::experimental::nullopt;
using entities::Page;
class Queue
{
public:
auto push_back(Page const &page) -> void
{
lock_guard<mutex> lock{_mutex};
_pages.push_back(page);
}
auto get(size_t index) -> optional<Page>
{
lock_guard<mutex> lock{_mutex};
auto const it
= find_if(cbegin(_pages), cend(_pages), [index](auto const &p) {
return p.pagination.currentPage == index;
});
if (it == cend(_pages))
return nullopt;
auto const page = *it;
_pages.erase(it);
return page;
}
private:
vector<Page> _pages;
mutex _mutex;
};