Clipboard.hpp 4.69 KB
Newer Older
1
2
3
/**
 * @file
 * @brief Store objects for exachange between modules on the clipboard
4
 * @copyright Copyright (c) 2017 CERN and the Corryvreckan authors.
5
6
7
8
9
10
11
 * This software is distributed under the terms of the MIT License, copied verbatim in the file "LICENSE.md".
 * In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an
 * Intergovernmental Organization or submit itself to any jurisdiction.
 */

#ifndef CORRYVRECKAN_CLIPBOARD_H
#define CORRYVRECKAN_CLIPBOARD_H
12

Simon Spannagel's avatar
Simon Spannagel committed
13
#include <iostream>
14
#include <memory>
15
#include <string>
16
#include <typeindex>
Simon Spannagel's avatar
Simon Spannagel committed
17
#include <unordered_map>
18

19
#include "core/utils/log.h"
20
#include "objects/Event.hpp"
21
#include "objects/Object.hpp"
22

23
namespace corryvreckan {
24
    typedef std::map<std::type_index, std::map<std::string, std::shared_ptr<void>>> ClipboardData;
25

26
27
28
29
30
31
32
33
34
35
    /**
     * @brief Class for temporary data storage for exachange between modules
     *
     * The Clipboard class is used to transfer information between modules during the event processing. \ref Objects can be
     * placed on the clipboard, and retrieved by their name. At the end of each event, the clipboard is
     * wiped clean.
     *
     * In addition, a permanent clipboard storage area for variables of type double is provided, which allow to exchange
     * information which should outlast a single event. This is dubbed the "persistent storage"
     */
36
    class Clipboard {
37
        friend class ModuleManager;
Simon Spannagel's avatar
Simon Spannagel committed
38

39
    public:
40
41
42
        /**
         * @brief Construct the clipboard
         */
43
        Clipboard() {}
44
45
46
        /**
         * @brief Required virtual destructor
         */
47
        virtual ~Clipboard() {}
Simon Spannagel's avatar
Simon Spannagel committed
48

49
        /**
50
51
52
         * @brief Method to add a vector of objects to the clipboard
         * @param objects Shared pointer to vector of objects to be stored
         * @param key     Identifying key for this set of objects. Defaults to empty key
53
         */
54
        template <typename T> void putData(std::shared_ptr<std::vector<T*>> objects, const std::string& key = "");
Simon Spannagel's avatar
Simon Spannagel committed
55

56
        /**
57
58
         * @brief Method to retrieve objects from the clipboard
         * @param key Identifying key of objects to be fetched. Defaults to empty key
59
         */
60
        template <typename T> std::shared_ptr<std::vector<T*>> getData(const std::string& key = "") const;
Simon Spannagel's avatar
Simon Spannagel committed
61

62
63
64
65
        /**
         * @brief Check whether an event has been defined
         * @return true if an event has been defined, false otherwise
         */
66
        bool isEventDefined() const;
67
68
69
70
71
72

        /**
         * @brief Store the event object
         * @param event The event object to be stored
         * @thorws InvalidDataError in case an event exist already
         */
73
        void putEvent(std::shared_ptr<Event> event);
74
75
76
77
78
79

        /**
         * @brief Retrieve the event object
         * @returnShared pointer to the event
         * @throws MissingDataError in case no event is available.
         */
80
        std::shared_ptr<Event> getEvent() const;
81

82
83
84
85
86
        /**
         * @brief Store or update variable on the persistent clipboard storage
         * @param name Name of the variable
         * @param value Value to be stored
         */
87
        void putPersistentData(std::string name, double value);
88
89
90
91
92

        /**
         * @brief Retrieve variable from the persistent clipboard storage
         * @param name Name of the variable
         * @return Stored value from the persistent clipboard storage
93
         * @throws MissingDataError in case the key is not found.
94
         */
95
        double getPersistentData(std::string name) const;
96

97
98
99
100
101
        /**
         * @brief Check if variable exists on the persistent clipboard storage
         * @param name Name of the variable
         * @return True if value exists, false if it does not exist.
         */
102
        bool hasPersistentData(std::string name) const;
103

104
105
106
107
        /**
         * @brief Get a list of currently held collections on the clipboard event storage
         * @return Vector of collections names currently stored on the clipboard
         */
108
        std::vector<std::string> listCollections() const;
Simon Spannagel's avatar
Simon Spannagel committed
109

110
111
112
113
        /**
         * @brief Retrieve all currently stored clipboard data
         * @return All clipboard data
         */
114
        const ClipboardData& getAll() const;
115

116
    private:
117
118
119
120
121
        /**
         * @brief Clear the event storage of the clipboard
         */
        void clear();

122
123
        // Container for data, list of all data held
        ClipboardData m_data;
124
125

        // Persistent clipboard storage
Simon Spannagel's avatar
Simon Spannagel committed
126
        std::unordered_map<std::string, double> m_persistent_data;
127
128
129

        // Store the current time slice:
        std::shared_ptr<Event> m_event{};
130
131
    };
} // namespace corryvreckan
132

133
134
135
// Include template members
#include "Clipboard.tpp"

136
#endif // CORRYVRECKAN_CLIPBOARD_H