Clipboard.cpp 2.38 KB
Newer Older
1
#include "Clipboard.hpp"
2
#include "exceptions.h"
3
#include "objects/Object.hpp"
4
5
6
7
8
9
10

using namespace corryvreckan;

void Clipboard::put_persistent(std::string name, double value) {
    m_persistent_data[name] = value;
}

11
double Clipboard::get_persistent(std::string name) const {
12
13
14
    try {
        return m_persistent_data.at(name);
    } catch(std::out_of_range&) {
Simon Spannagel's avatar
Simon Spannagel committed
15
        throw MissingDataError(name);
16
17
18
    }
}

19
bool Clipboard::event_defined() const {
20
21
22
23
24
25
    return (m_event != nullptr);
}

void Clipboard::put_event(std::shared_ptr<Event> event) {
    // Already defined:
    if(m_event) {
26
        throw InvalidDataError("Event already defined. Only one module can place an event definition");
27
28
29
30
31
    } else {
        m_event = event;
    }
}

32
std::shared_ptr<Event> Clipboard::get_event() const {
33
    if(!m_event) {
34
        throw InvalidDataError("Event not defined. Add Metronome module or Event reader defining the event");
35
36
37
38
    }
    return m_event;
}

39
bool Clipboard::has_persistent(std::string name) const {
40
    return m_persistent_data.find(name) != m_persistent_data.end();
41
42
43
}

void Clipboard::clear() {
44
45
46
47
48
49
    // Loop over all data types
    for(auto block = m_data.cbegin(); block != m_data.cend();) {
        auto collections = block->second;

        // Loop over all stored collections of this type
        for(auto set = collections.cbegin(); set != collections.cend();) {
50
            std::shared_ptr<Objects> collection = std::static_pointer_cast<Objects>(set->second);
51
52
53
54
55
            // Loop over all objects and delete them
            for(Objects::iterator it = collection->begin(); it != collection->end(); ++it) {
                delete(*it);
            }
            set = collections.erase(set);
56
        }
57
        block = m_data.erase(block);
58
    }
59
60
61

    // Resetting the event definition:
    m_event.reset();
62
63
}

64
std::vector<std::string> Clipboard::list_collections() const {
65
    std::vector<std::string> collections;
66
67
68
69
70

    for(const auto& block : m_data) {
        std::string line(corryvreckan::demangle(block.first.name()));
        line += ": ";
        for(const auto& set : block.second) {
71
72
            std::shared_ptr<Objects> collection = std::static_pointer_cast<Objects>(set.second);
            line += set.first + " (" + collection->size() + ") ";
73
74
75
        }
        line += "\n";
        collections.push_back(line);
76
77
    }
    return collections;
78
}
79

80
const ClipboardData& Clipboard::get_all() const {
81
82
    return m_data;
}