IrregularityFile.cpp 2.36 KB
Newer Older
Matteo's avatar
update  
Matteo committed
1
2
#include <exception>
#include "IrregularityFile.h"
Matteo's avatar
Matteo committed
3
4
5
#include <algorithm>
#include <iterator>
#include <memory>
Matteo's avatar
update  
Matteo committed
6

Matteo's avatar
Matteo committed
7
IrregularityFile::IrregularityFile(std::optional<uint16_t> offset) : offset_(offset) {}
Matteo's avatar
update  
Matteo committed
8

Matteo's avatar
Matteo committed
9
10
11
12
13
14
15
IrregularityFile::IrregularityFile(const IrregularityFile &rhs) {
    std::transform(rhs.irregularities_.begin(), rhs.irregularities_.end(),
                   std::back_inserter(irregularities_),
                   [](const std::unique_ptr<Irregularity> &ptr) {
                     return std::make_unique<Irregularity>(*ptr);
                   });
}
Matteo's avatar
update  
Matteo committed
16

Matteo's avatar
Matteo committed
17
18
19
20
IrregularityFile& IrregularityFile::add(std::unique_ptr<Irregularity> irregularity) {
    irregularities_.push_back(std::move(irregularity));
    return *this;
}
Matteo's avatar
update  
Matteo committed
21

Matteo's avatar
Matteo committed
22
23
24
25
26
27
28
29
IrregularityFile& IrregularityFile::remove_by_id(const boost::uuids::uuid id) {
    auto it = std::find_if(irregularities_.begin(), irregularities_.end(), [&id](const std::unique_ptr<Irregularity>& irregularity) {
        return irregularity->get_id() == id;
    });
    if (it != irregularities_.end()) {
        irregularities_.erase(it);
    }
    return *this;
Matteo's avatar
update  
Matteo committed
30
31
}

Matteo's avatar
Matteo committed
32
33
34
35
36
IrregularityFile& IrregularityFile::sort() {
    std::sort(irregularities_.begin(), irregularities_.end(), [](const std::unique_ptr<Irregularity>& a, const std::unique_ptr<Irregularity>& b) {
        return a->get_time_label() < b->get_time_label();
    });
    return *this;
Matteo's avatar
update  
Matteo committed
37
38
}

Matteo's avatar
Matteo committed
39
40
std::optional<uint16_t> IrregularityFile::get_offset() const {
    return offset_;
Matteo's avatar
update  
Matteo committed
41
42
}

Matteo's avatar
Matteo committed
43
44
std::vector<std::unique_ptr<Irregularity>>::iterator IrregularityFile::begin() {
    return irregularities_.begin();
Matteo's avatar
update  
Matteo committed
45
46
}

Matteo's avatar
Matteo committed
47
48
std::vector<std::unique_ptr<Irregularity>>::iterator IrregularityFile::end() {
    return irregularities_.end();
Matteo's avatar
update  
Matteo committed
49
50
}

Matteo's avatar
Matteo committed
51
52
53
54
55
56
json IrregularityFile::toJSON() const {
    json j;
    j["Offset"] = offset_.value_or(0);
    j["Irregularities"] = json::array();
    for (const auto& irregularity : irregularities_) {
        j["Irregularities"].push_back(irregularity->to_JSON());
Matteo's avatar
update  
Matteo committed
57
    }
Matteo's avatar
Matteo committed
58
    return j;
Matteo's avatar
update  
Matteo committed
59
60
}

Matteo's avatar
Matteo committed
61
62
63
64
65
66
67
68
69
70
IrregularityFile IrregularityFile::fromJSON(const json j) {
    if (!j.contains("Offset") || !j.contains("Irregularities")) {
        throw std::invalid_argument("Invalid JSON");
    }
    IrregularityFile irregularity_file(j["Offset"].get<uint16_t>());
    for (const auto& irregularity : j["Irregularities"]) {
        irregularity_file.add(std::make_unique<Irregularity>(Irregularity::from_JSON(irregularity)));
    }
    return irregularity_file;
}