Clipboard.cpp 2.85 KB
Newer Older
1
#include "Clipboard.hpp"
2
#include "exceptions.h"
3
#include "objects/Object.hpp"
4
5
6

using namespace corryvreckan;

7
void Clipboard::putPersistentData(std::string name, double value) {
8
9
10
    m_persistent_data[name] = value;
}

11
double Clipboard::getPersistentData(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
20
21
22
bool Clipboard::hasPersistentData(std::string name) const {
    return m_persistent_data.find(name) != m_persistent_data.end();
}

23
bool Clipboard::event_defined() const {
24
25
26
27
28
29
    return (m_event != nullptr);
}

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

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

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<ObjectVector> collection = std::static_pointer_cast<ObjectVector>(set->second);
51
            // Loop over all objects and delete them
52
            for(ObjectVector::iterator it = collection->begin(); it != collection->end(); ++it) {
53
54
55
56
57
                // All objects are destroyed together in this clear function at the end of the event. To avoid costly
                // reverse-iterations through the TRef dependency hash lists, we just tell ROOT not to care about possible
                // TRef-dependants and to just destroy the object directly by resetting the `kMustCleanup` bit.
                (*it)->ResetBit(kMustCleanup);
                // Delete the object itself:
58
59
60
                delete(*it);
            }
            set = collections.erase(set);
61
        }
62
        block = m_data.erase(block);
63
    }
64
65
66

    // Resetting the event definition:
    m_event.reset();
67
68
}

69
std::vector<std::string> Clipboard::list_collections() const {
70
    std::vector<std::string> collections;
71
72
73
74
75

    for(const auto& block : m_data) {
        std::string line(corryvreckan::demangle(block.first.name()));
        line += ": ";
        for(const auto& set : block.second) {
76
            std::shared_ptr<ObjectVector> collection = std::static_pointer_cast<ObjectVector>(set.second);
77
            line += set.first + " (" + collection->size() + ") ";
78
79
80
        }
        line += "\n";
        collections.push_back(line);
81
82
    }
    return collections;
83
}
84

85
const ClipboardData& Clipboard::get_all() const {
86
87
    return m_data;
}