-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.h
102 lines (80 loc) · 2.27 KB
/
document.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef M_DOCUMNT
#define M_DOCUMENT
#include <iostream>
#include <map>
#include <mutex>
#include <string>
#include <vector>
namespace Document {
struct Document {
std::vector<std::string> data;
Document();
// Document info
size_t lines_count() const;
size_t line_length(size_t line) const;
// Document modification
void insert_line(size_t line, const std::string& content);
void delete_line(size_t line);
void break_line(size_t line, size_t column);
void insert_char(size_t line, size_t column, char ch);
void delete_char(size_t line, size_t column);
};
struct Cursor {
size_t line, column;
Document* document;
void set(size_t _line, size_t _column);
void set(const Cursor& other);
Cursor();
Cursor(Document* document);
Cursor(Document* document, const Cursor& other);
Cursor(Document* document, size_t _line, size_t _column);
void sync_with_document();
void home();
void end();
void up();
void down();
void left();
void right();
void write(char ch);
void del();
void backspace();
void break_line();
void tab();
};
struct Cursor_image {
Cursor_image();
Cursor_image(size_t line, size_t column, size_t id);
bool operator<(const Cursor_image& other);
size_t line, column, id;
};
struct Document_image {
Document_image();
// Regular constructor, serializes the object
Document_image(const std::vector<std::string>& data,
const std::vector<Cursor_image>& cursors);
// Constructor from string, deserializes the object
Document_image(const std::string& serialized_object);
// Je prakticke aby cursory boli usortene
void sort_cursors();
std::vector<std::string> data;
std::vector<Cursor_image> cursors;
std::string serialized_object;
};
namespace Document_handler {
static Document document;
static std::map<int, Cursor> cursors;
static std::mutex mtx;
// API
bool process_message(int cursor_id, std::string message);
// Cursor handling
void add_new_cursor(int cursor_id);
void remove_cursor(int cursor_id);
Cursor* get_cursor(int cursor_id);
// Serialization
Document_image get_document_image();
std::string serialize();
// Dev features
void print();
}
}
#endif