-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.h
141 lines (111 loc) · 3.11 KB
/
sync.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//
// Created by Yegor Jbanov.
//
#ifndef BARISTA2_SYNC_H_H
#define BARISTA2_SYNC_H_H
#include "common.h"
#include "lib/json/src/json.hpp"
#include <string>
#include <vector>
#include <map>
#include <tuple>
#include <sstream>
using namespace std;
namespace barista {
class Move {
public:
Move(int insertionIndex, int moveFromIndex)
: _insertionIndex(insertionIndex),
_moveFromIndex(moveFromIndex) { };
int GetInsertionIndex() { return _insertionIndex; }
int GetMoveFromIndex() { return _moveFromIndex; }
private:
int _insertionIndex;
int _moveFromIndex;
};
class ElementUpdate {
public:
/// Appends the JSON representation of this update into [buffer].
bool Render(nlohmann::json& js);
/// Assumes that this element update is exlusively made of insertions and
/// renders it as a plain HTML into the given [buffer].
void PrintHtml(stringstream &buffer);
void RemoveChild(int index) { _removes.push_back(index); }
void MoveChild(int insertionIndex, int moveFrom) {
_moves.push_back({insertionIndex, moveFrom});
}
ElementUpdate& InsertChildElement(int insertionIndex) {
_childElementInsertions.push_back(ElementUpdate(insertionIndex));
return _childElementInsertions.back();
}
ElementUpdate& UpdateChildElement(int index) {
_childElementUpdates.push_back(ElementUpdate(index));
return _childElementUpdates.back();
}
void SetTag(string tag) { _tag = tag; }
void SetKey(string key) { _key = key; }
void SetText(string text) { _text = text; _updateText = true; }
void SetAttribute(string name, string value) {
_attributes.push_back({name, value});
}
void SetBaristaId(string bid) {
_bid = bid;
}
void AddClassName(string name) {
_classNames.push_back(name);
}
private:
ElementUpdate(int index) : _index(index) { };
// insert-before index if this is being inserted.
// child index if this is being updated.
int _index;
string _tag = "";
string _key = "";
string _bid = "";
bool _updateText = false;
string _text = "";
vector<int> _removes;
vector<Move> _moves;
vector<ElementUpdate> _childElementInsertions;
vector<ElementUpdate> _childElementUpdates;
vector<tuple<string, string>> _attributes;
vector<string> _classNames;
PRIVATE_COPY_AND_ASSIGN(ElementUpdate);
friend class allocator<ElementUpdate>;
friend class TreeUpdate;
};
class TreeUpdate {
public:
TreeUpdate() :
_rootUpdate(ElementUpdate(0)) { };
ElementUpdate& CreateRootElement() {
_createMode = true;
return _rootUpdate;
}
ElementUpdate& UpdateRootElement() {
_createMode = false;
return _rootUpdate;
}
string Render() {
return Render(0);
}
string Render(int indent) {
nlohmann::json js;
if (_createMode) {
stringstream html;
_rootUpdate.PrintHtml(html);
js["create"] = html.str();
} else {
nlohmann::json jsRootUpdate;
if (_rootUpdate.Render(jsRootUpdate)) {
js["update"] = jsRootUpdate;
}
}
return indent > 0 ? js.dump(indent) : js.dump();
}
private:
bool _createMode = false;
ElementUpdate _rootUpdate;
};
} // namespace barista
#endif // BARISTA2_SYNC_H_H