UnitTemplate.h 7.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// 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.
 *
 */
template <class S=SensorBase>
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) :
            UnitInterface(),
            _name(name),
            _inputMode(SELECTIVE),
            _inputs(inputs),
            _outputs(outputs) {

        // 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) :
            _name(other._name),
            _inputMode(other._inputMode) {

        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;
        _inputMode = other._inputMode;

        _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;
    }

    /**
    * @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; }

    /**
    * @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); }


    /**
    * @brief            Prints the current unit configuration
    *
    * @param ll         Logging level at which the configuration is printed
    * @param lg         Logger object to be used
    */
    virtual void printConfig(LOG_LEVEL ll, LOGGER& lg) override {
        LOG_VAR(ll) << "                Unit: " << _name;
        LOG_VAR(ll) << "                Inputs: ";
        for (const auto &i : _inputs)
            LOG_VAR(ll) << "                " << i->getName();
        LOG_VAR(ll) << "                Outputs: ";
        for (const auto &o : _outputs)
            o->printConfig(ll, lg, 20);
    }

protected:

    // Name corresponds to the output unit we are addressing
    std::string _name;
    // Input mode associated to this unit
    inputMode_t _inputMode;
    // 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