UnitTemplate.h 6.89 KB
Newer Older
Alessio Netti's avatar
Alessio Netti committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//
// Created by Netti, Alessio on 11.12.18.
//

#ifndef PROJECT_UNITTEMPLATE_H
#define PROJECT_UNITTEMPLATE_H

#include "UnitInterface.h"

/**
 * Template that implements features needed to use Units in Analyzers.
 *
 * The template accepts any class derived from SensorBase, allowing users to add their own attributes to input and output
 * sensors. Users should employ this template in their plugins if possible, and change it only if absolutely necessary.
 *
 */
Alessio Netti's avatar
Alessio Netti committed
17
template <class S=SensorBase>
Alessio Netti's avatar
Alessio Netti committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class UnitTemplate : public UnitInterface {
    // The template shall only be instantiated for classes which derive from SensorBase
    static_assert(std::is_base_of<SensorBase, S>::value, "S must derive from SensorBase!");

protected:

    // For readability
    using S_Ptr = std::shared_ptr<S>;

public:

    /**
    * @brief            Class constructor
    *
    * @param name       The name of this unit
    * @param inputs     The vector of input sensors associated to this unit
    * @param outputs    The vector of outputs sensors associated to this unit
    */
    UnitTemplate(const std::string& name, const std::vector<S_Ptr>& inputs, const std::vector<S_Ptr>& outputs) :
Alessio Netti's avatar
Alessio Netti committed
37
            UnitInterface(),
38
            _name(name),
Alessio Netti's avatar
Alessio Netti committed
39
            _inputMode(SELECTIVE),
40
41
            _inputs(inputs),
            _outputs(outputs) {
Alessio Netti's avatar
Alessio Netti committed
42
43
44
45
46
47
48
49
50
51

        // base inputs and outputs vectors are constructed using iterators
        _baseInputs = std::vector<SBasePtr>(_inputs.begin(), _inputs.end());
        _baseOutputs = std::vector<SBasePtr>(_outputs.begin(), _outputs.end());
    }

    /**
    * @brief            Copy constructor
    */
    UnitTemplate(const UnitTemplate& other) :
Alessio Netti's avatar
Alessio Netti committed
52
53
            _name(other._name),
            _inputMode(other._inputMode) {
Alessio Netti's avatar
Alessio Netti committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

        for(auto s : other._inputs) {
            _inputs.push_back(s);
            _baseInputs.push_back(s);
        }

        for(auto s : other._outputs) {
            _outputs.push_back(s);
            _baseOutputs.push_back(s);
        }
    }

    /**
    * @brief            Assignment operator
    */
    UnitTemplate& operator=(const UnitTemplate& other) {
        _name = other._name;
Alessio Netti's avatar
Alessio Netti committed
71
        _inputMode = other._inputMode;
Alessio Netti's avatar
Alessio Netti committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

        _inputs.clear();
        _baseInputs.clear();
        for(auto s : other._inputs) {
            _inputs.push_back(s);
            _baseInputs.push_back(s);
        }

        _outputs.clear();
        _baseOutputs.clear();
        for(auto s : other._outputs) {
            _outputs.push_back(s);
            _baseOutputs.push_back(s);
        }

        return *this;
    }

Alessio Netti's avatar
Alessio Netti committed
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    /**
    * @brief            Sets the name of this unit
    *
    * @param name       The name of this unit
    */
    void setName(const std::string& name) override { _name = name; }

    /**
    * @brief            Get the name of this unit
    *
    *                   A unit's name points to the logical entity that it represents; for example, it could be
    *                   "hpcsystem1.node44", or "node44.cpu10". All the outputs of the unit are then associated to its
    *                   entity, and all of the input are related to it as well.
    *
    * @return           The unit's name
    */
    std::string& getName() override                     { return _name; }

    /**
    * @brief            Sets the input mode of this unit
    *
    * @param iMode      The input mode that was used for this unit
    */
    void setInputMode(const inputMode_t iMode) override { _inputMode=iMode; }

    /**
    * @brief            Get the input mode of this unit
    *
    * @return           The unit's input mode
    */
    inputMode_t getInputMode()  override              { return _inputMode; }

Alessio Netti's avatar
Alessio Netti committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    /**
    * @brief            Get the (base) input sensors of this unit
    *
    * @return           A vector of pointers to SensorBase objects that constitute this unit's input
    */
    std::vector<SBasePtr>& getBaseInputs() override  { return _baseInputs; }

    /**
    * @brief            Get the derived input sensors of this unit
    *
    * @return           A vector of pointers to objects derived from SensorBase that constitute this unit's input
    */
    std::vector<S_Ptr>& getInputs()                  { return _inputs; }

    /**
    * @brief            Get the (base) output sensors of this unit
    *
    * @return           A vector of pointers to SensorBase objects that constitute this unit's output
    */
    std::vector<SBasePtr>& getBaseOutputs() override { return _baseOutputs; }

    /**
    * @brief            Get the derived output sensors of this unit
    *
    * @return           A vector of pointers to objects derived from SensorBase that constitute this unit's output
    */
    std::vector<S_Ptr>& getOutputs()                 { return _outputs; }

    /**
    * @brief            Set the inputs of this unit
    *
    *                   Note that these setter methods will replicate the vectors given as input, but the copies
    *                   are shallow (i.e., vectors of pointers will refer to the same objects). That is intended, in
    *                   order to save memory space, and does not lead to race conditions.
    *
    * @param inputs     A vector of pointers to objects derived from SensorBase that will be this unit's input
    */
    void setInputs(const std::vector<S_Ptr>& inputs) {
        _inputs = inputs;
        _baseInputs = std::vector<SBasePtr>(_inputs.begin(), _inputs.end());
    }

    /**
    * @brief            Set the outputs of this unit
    *
    *                   The same considerations done for setInputs apply here as well.
    *
    * @param outputs    A vector of pointers to objects derived from SensorBase that will be this unit's output
    */
    void setOutputs(const std::vector<S_Ptr>& outputs) {
        _outputs = outputs;
        _baseOutputs = std::vector<SBasePtr>(_outputs.begin(), _outputs.end());
    }

    /**
    * @brief            Add a single input sensor to this unit
    *
    * @param input      A pointer to an object derived from SensorBase that will be added to this unit's inputs
    */
    void addInput(const S_Ptr input) { _inputs.push_back(input); _baseInputs.push_back(input); }

    /**
    * @brief            Add a single output sensor to this unit
    *
    * @param output     A pointer to an object derived from SensorBase that will be added to this unit's outputs
    */
    void addOutput(const S_Ptr output) { _outputs.push_back(output); _baseOutputs.push_back(output); }

protected:

    // Name corresponds to the output unit we are addressing
    std::string _name;
Alessio Netti's avatar
Alessio Netti committed
194
195
    // Input mode associated to this unit
    inputMode_t _inputMode;
Alessio Netti's avatar
Alessio Netti committed
196
197
198
199
200
201
202
203
204
205
206
    // Vector of sensor objects that make up inputs
    std::vector<S_Ptr> _inputs;
    // Base version that uses SensorBase is needed to expose units externally
    std::vector<SBasePtr> _baseInputs;
    // Vector of Sensor objects that make up outputs
    std::vector<S_Ptr> _outputs;
    // Same as baseInputs
    std::vector<SBasePtr> _baseOutputs;
};

#endif //PROJECT_UNITTEMPLATE_H