Repository navigation
Expand file tree
/
Copy pathcascade.cpp
More file actions
137 lines (120 loc) · 3.76 KB
/
Copy pathcascade.cpp
File metadata and controls
137 lines (120 loc) · 3.76 KB
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
/*############################################################################
# Copyright (c) 2020 Source Simian : https://github.com/sourcesimian/uICAL #
############################################################################*/
#include "uICAL/cppstl.h"
#include "uICAL/types.h"
#include "uICAL/util.h"
#include "uICAL/error.h"
#include "uICAL/counter.h"
#include "uICAL/logging.h"
#include "uICAL/cascade.h"
namespace uICAL {
// Cascade::Cascade() {}
void Cascade::add(Counter_ptr counter) {
this->counters.push_back(counter);
}
void Cascade::add(std::vector<Counter_ptr> counters) {
for (Counter_ptr counter : counters) {
this->counters.push_back(counter);
}
}
size_t Cascade::size() const {
return this->counters.size();
}
bool Cascade::reset(const DateStamp& base) {
std::ignore = base; // TODO
return false;
}
DateStamp Cascade::value() const {
return this->counters.front()->value();
}
bool Cascade::syncLock(const DateStamp& from, const DateStamp& now) const {
std::ignore = from; // TODO
std::ignore = now; // TODO
return true;
};
const string Cascade::name() const {
Joiner names(',');
for (auto counter : this->counters) {
counter->str(names.out());
names.next();
}
ostream out;
names.write(out);
return out.str();
}
void Cascade::wrap() {
}
bool Cascade::initCounters(const DateStamp& base, const DateStamp& begin) {
counters_t::iterator it = this->counters.end();
-- it;
if(!(*it)->reset(base)) {
return false;
}
return this->resetCascade(it, [&](counters_t::iterator it) {
while (!(*it)->syncLock(begin, (*it)->value())) {
if (!(*it)->next()) {
ostream out;
out << "Can not seek " << (*it)->name() << " (" << "base: " << base << " from: " << begin << ")";
throw ParseError(out);
}
}
});
}
bool Cascade::resetCascade(counters_t::iterator it, sync_f sync) {
DateStamp base = (*it)->value();
for (;;) {
if (it == this->counters.begin())
break;
-- it;
if (!(*it)->reset(base)) {
++ it;
if(!(*it)->next()) {
++ it;
if (it != this->counters.end()) {
throw ImplementationError("No next available from upper level counter");
}
-- it;
}
}
if (sync != nullptr) {
sync(it);
}
if (it == this->counters.begin())
break;
base = (*it)->value();
// if (this->expired(base)) {
// return false;
// }
}
return true;
}
bool Cascade::next() {
counters_t::iterator it = this->counters.begin();
if(!(*it)->next()) {
for (;;) {
++ it;
if (it == this->counters.end()) {
-- it;
if (it == this->counters.begin())
return true;
break;
}
if ((*it)->next())
break;
}
if (!this->resetCascade(it, nullptr)) {
return false;
}
}
return true;
}
void Cascade::str(ostream& out) const {
Joiner counters('+');
for (auto counter : this->counters) {
counter->str(counters.out());
counters.next();
}
counters.str(out);
}
}