Clipboard.cpp 1.72 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::has_persistent(std::string name) const {
20
    return m_persistent_data.find(name) != m_persistent_data.end();
21
22
23
}

void Clipboard::clear() {
24
25
26
27
28
29
    // 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();) {
30
            std::shared_ptr<Objects> collection = std::static_pointer_cast<Objects>(set->second);
31
32
33
34
35
            // Loop over all objects and delete them
            for(Objects::iterator it = collection->begin(); it != collection->end(); ++it) {
                delete(*it);
            }
            set = collections.erase(set);
36
        }
37
        block = m_data.erase(block);
38
39
40
    }
}

41
42
std::vector<std::string> Clipboard::listCollections() {
    std::vector<std::string> collections;
43
44
45
46
47

    for(const auto& block : m_data) {
        std::string line(corryvreckan::demangle(block.first.name()));
        line += ": ";
        for(const auto& set : block.second) {
48
49
            std::shared_ptr<Objects> collection = std::static_pointer_cast<Objects>(set.second);
            line += set.first + " (" + collection->size() + ") ";
50
51
52
        }
        line += "\n";
        collections.push_back(line);
53
54
    }
    return collections;
55
}