forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closed_docs.h
64 lines (51 loc) · 1.61 KB
/
closed_docs.h
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Aseprite
// Copyright (C) 2019-2020 Igara Studio S.A.
//
// This program is distributed under the terms of
// the End-User License Agreement for Aseprite.
#ifndef APP_CLOSED_DOCS_H_INCLUDED
#define APP_CLOSED_DOCS_H_INCLUDED
#pragma once
#include "base/time.h"
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
namespace app {
class Doc;
class Preferences;
// Handle the list of closed docs:
// * When a document is closed, we keep it for some time so the user
// can undo the close command without losing the undo history.
// * For the first closed document, a thread is launched to wait
// until we can definitely delete the doc after X minutes (like a
// garbage collector).
// * If the document was not restore, we delete it from memory, if
// the document was restore, we remove it from the m_docs.
class ClosedDocs {
public:
ClosedDocs(const Preferences& pref);
~ClosedDocs();
bool hasClosedDocs();
void addClosedDoc(Doc* doc);
Doc* reopenLastClosedDoc();
// Called at the very end to get all closed docs, remove them from
// the list of closed docs, and stop the thread.
std::vector<Doc*> getAndRemoveAllClosedDocs();
private:
void backgroundThread();
struct ClosedDoc {
Doc* doc;
base::tick_t timestamp;
};
std::atomic<bool> m_done;
base::tick_t m_dataRecoveryPeriodMSecs;
base::tick_t m_keepClosedDocAliveForMSecs;
std::vector<ClosedDoc> m_docs;
std::mutex m_mutex;
std::condition_variable m_cv;
std::thread m_thread;
};
} // namespace app
#endif