-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgrabcommon.h
More file actions
executable file
·338 lines (302 loc) · 11.3 KB
/
grabcommon.h
File metadata and controls
executable file
·338 lines (302 loc) · 11.3 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/**
* @file grabcommon.h
* @author Simone Comari
* @date 08 May 2019
* @brief This file collects common utilities which are not specifically related to any of
* GRAB libraries.
*/
#ifndef GRABCOMMON_H
#define GRABCOMMON_H
#include <errno.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/wait.h>
#include <unistd.h>
#include <vector>
/*---------------------- DEFINES ------------------------*/
#define ANSI_COLOR_RED "\x1b[31m" /**< ANSI _red_ codex for colorful printings. */
#define ANSI_COLOR_GREEN "\x1b[32m" /**< ANSI _green_ codex for colorful printings. */
#define ANSI_COLOR_YELLOW "\x1b[33m" /**< ANSI _yellow_ codex for colorful printings. */
#define ANSI_COLOR_BLUE "\x1b[34m" /**< ANSI _blue_ codex for colorful printings. */
#define ANSI_COLOR_MAGENTA \
"\x1b[35m" /**< ANSI _magenta_ codex for colorful printings. \
*/
#define ANSI_COLOR_CYAN "\x1b[36m" /**< ANSI _cyan_ codex for colorful printings. */
#define ANSI_COLOR_RESET \
"\x1b[0m" /**< ANSI color reset codex after colorful printings. */
/*---------------------- MACROS ------------------------*/
// @cond DO_NOT_DOCUMENT
#define SETUP_ENUM_CLASS_ASSIGNMENTS(EnumStruct, EType) \
EType val; \
EnumStruct() {} \
EnumStruct(EType p_eVal) : val(p_eVal) {} \
operator EType() const { return val; } \
void operator=(EType p_eVal) { val = p_eVal; }
// @endcond
/**
* @brief An enum-like format to define an enum class.
* @todo example
*/
#define ENUM_CLASS(EName, ...) \
struct EName \
{ \
enum Type \
{ \
__VA_ARGS__ \
}; \
SETUP_ENUM_CLASS_ASSIGNMENTS(EName, Type) \
};
// Make a FOREACH macro
// @cond DO_NOT_DOCUMENT
#define FE_1(WHAT, X) WHAT(X)
#define FE_2(WHAT, X, ...) WHAT(X) FE_1(WHAT, __VA_ARGS__)
#define FE_3(WHAT, X, ...) WHAT(X) FE_2(WHAT, __VA_ARGS__)
#define FE_4(WHAT, X, ...) WHAT(X) FE_3(WHAT, __VA_ARGS__)
#define FE_5(WHAT, X, ...) WHAT(X) FE_4(WHAT, __VA_ARGS__)
#define FE_6(WHAT, X, ...) WHAT(X) FE_5(WHAT, __VA_ARGS__)
#define FE_7(WHAT, X, ...) WHAT(X) FE_6(WHAT, __VA_ARGS__)
#define FE_8(WHAT, X, ...) WHAT(X) FE_7(WHAT, __VA_ARGS__)
#define FE_9(WHAT, X, ...) WHAT(X) FE_8(WHAT, __VA_ARGS__)
//... repeat as needed
#define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, NAME, ...) NAME
// @endcond
/**
* @brief A for loop macro implementation.
* @warning It works up to 9 iterations, to add more you must extend the list above.
*/
#define FOR_EACH(action, ...) \
GET_MACRO(__VA_ARGS__, FE_9, FE_8, FE_7, FE_6, FE_5, FE_4, FE_3, FE_2, FE_1) \
(action, __VA_ARGS__)
//---------------------- Generic enums -----------------------------------------------//
/**
*@brief Return value enum
*/
enum RetVal : uint8_t
{
OK, /**< success */
ECONFIG, /**< configuration error */
EREG, /**< registration error */
EACTIVE, /**< activation error */
EINIT, /**< initialization error */
EINV, /**< invalid error */
EINT, /**< interrupt error */
ETIMEOUT, /**< time-out errro */
EFAIL /**< generic error */
};
//---------------------- Generic functions -------------------------------------------//
/**
* @brief Handle an error message accorging to @c errno convention and display a message.
* @note This function immediately terminates the process with an error.
* @param[in] en Error number.
* @param[in] msg Message to be displayed before error description.
* @note For further details about error number convention, click
* <a href="http://man7.org/linux/man-pages/man3/errno.3.html">here</a>.
*/
[[noreturn]] void HandleErrorEn(const int en, const char* msg);
/**
* @brief Print colored text to shell/console.
* @param[in] color A letter indicating the text color. Available options are:
* - 'r' : red
* - 'y' : yellow
* - 'g' : green
* - 'm' : magenta
* - 'c' : cyan
* - 'b' : blue
* - other : white (default)
* @param[in] text Text to be colored and printed. It can contain format specifiers to be
* added as variable arguments like in _printf()_.
*/
void PrintColor(const char color, const char* text, ...);
/**
* @brief Print colored text to shell/console.
* @param[in] color A letter indicating the text color. Available options are:
* - 'r' : red
* - 'y' : yellow
* - 'g' : green
* - 'm' : magenta
* - 'c' : cyan
* - 'b' : blue
* - other : white (default)
* @param[in] text Text to be colored and printed.
* @param[in] args Additional variable arguments in case text contains format specifiers.
*/
void PrintColor(const char color, const char* text, va_list args);
template <typename T>
/**
* @brief Return sign of a scalar value.
* @param value A scalar value.
* @return Sign of given scalar value.
*/
T sign(const T& value)
{
return value >= 0 ? static_cast<T>(1) : static_cast<T>(-1);
}
/**
* @brief Display a message colored depending on the returned value and append the value
* outcome at the end of it.
* @param[in] err The error of RetVal type.
* @param[in] msg The custom message to be prepend before the error identifier.
*/
void DispRetVal(const int err, const char* msg, ...);
/**
* @brief Get return value's description.
* @param[in] err The error of RetVal type.
* @return A string describing the returned value.
*/
std::string GetRetValStr(const int err);
/**
* @brief Run a Matlab script from shell.
* @param[in] script_location Filepath of Matlab script.
* @param[in] display If _true_, display flags will be enabled and Matlab plots will be
* visible during the execution, if present.
*/
void RunMatlabScript(const std::string& script_location, const bool display = false);
/*---------------------- Generic classes ------------------------*/
template <typename T>
/**
* @brief A simple implementation of a ring buffer.
*/
class RingBuffer
{
public:
/**
* @brief RingBuffer constructor.
* @param[in] buffer_size Ring buffer size.
*/
RingBuffer(const size_t buffer_size) { buffer_.resize(buffer_size); }
/**
* @brief RingBuffer constructor with scalar initializer.
* @param[in] buffer_size Ring buffer size.
* @param[in] element Scalar whose value will be used to fill the whole buffer.
*/
RingBuffer(const size_t buffer_size, const T& element)
{
buffer_.resize(buffer_size, element);
}
/**
* @brief RingBuffer constructor with vector initializer.
* @param[in] vector Vector used to inialize buffer content and size.
*/
RingBuffer(const std::vector<T> vector) : buffer_(vector) {}
/**
* @brief operator [] Dynamic indexing, that is if given index = k, then the k-th
* element starting from current ring's head is given.
* @param[in] index The index of the element from 0 to buffer's size - 1.
* @return A reference to the desired element.
*/
T& operator[](const uint64_t& index)
{
assert(index <= this->linear_idx_);
return buffer_[(linear_idx_ + index + 1) % buffer_.size()];
}
/**
* @brief operator [] Dynamic indexing, that is if given index = k, then the k-th
* element starting from current ring's head is given.
* @param[in] index The index of the element from 0 to buffer's size - 1.
* @return A constant reference to the desired element.
*/
const T& operator[](const uint64_t& index) const
{
assert(index <= this->linear_idx_);
return buffer_[(linear_idx_ + index + 1) % buffer_.size()];
}
/**
* @brief Returns a constant reference to the last element in the ring vector.
* @return A reference to the last element in the ring vector.
*/
T& Tail() { return buffer_[ring_idx_]; }
/**
* @brief Returns a constant reference to the last element in the ring vector.
* @return A reference to the last element in the ring vector.
*/
const T& Tail() const { return buffer_[ring_idx_]; }
/**
* @brief Returns a reference to the first element in the ring vector.
* @return A reference to the first element in the ring vector.
*/
T& Head()
{
return buffer_[(linear_idx_ == ring_idx_ | ring_idx_ == buffer_.size() - 1)
? buffer_.front()
: buffer_[ring_idx_ + 1]];
}
/**
* @brief Returns a constant reference to the first element in the ring vector.
* @return A constant reference to the first element in the ring vector.
*/
const T& Head() const
{
return buffer_[(linear_idx_ == ring_idx_ | ring_idx_ == buffer_.size() - 1)
? buffer_.front()
: buffer_[ring_idx_ + 1]];
}
/**
* @brief Return raw data content of the buffer.
* @return Raw data content of the buffer.
*/
std::vector<T>& Data() { return buffer_; }
/**
* @brief Return a constant reference to raw data content of the buffer.
* @return A constant reference to raw data content of the buffer.
*/
const std::vector<T>& Data() const { return buffer_; }
/**
* @brief Check if buffer is empty.
* @return _True_ if buffer is empty, _false_ otherwise.
*/
bool IsEmpty() const { return linear_idx_ == 0; }
/**
* @brief Check if buffer is full.
* @return _True_ if buffer is full, _false_ otherwise.
*/
bool IsFull() const { return linear_idx_ >= (buffer_.size() - 1); }
/**
* @brief Return buffer size.
* @return Buffer size.
*/
size_t Size() const { return buffer_.size(); }
/**
* @brief Get current linear index.
*
* Linear index is a growing index which increases by 1 for every new element pushed
* into the buffer.
* @return Current linear index.
*/
uint64_t GetLinearIdx() const { return linear_idx_; }
/**
* @brief Get ring index.
*
* This is a bounded index between 0 and _buffer size_ - 1, which denote the ring
* position of the latest element pushed into he ring buffer.
* @return Ring index.
*/
uint64_t GetRingIdx() const { return ring_idx_; }
/**
* @brief Add an element to the buffer.
* @param[in] element New element to be pushed into the buffer.
*/
void Add(const T& element)
{
ring_idx_ = linear_idx_ % buffer_.size();
buffer_[ring_idx_] = element;
linear_idx_++;
}
/**
* @brief Empty the buffer.
*/
void Clear()
{
linear_idx_ = 0UL;
ring_idx_ = 0UL;
buffer_.clear();
}
private:
std::vector<T> buffer_;
uint64_t linear_idx_ = 0UL;
uint64_t ring_idx_ = 0UL;
};
using RingBufferD = RingBuffer<double>; /**< Alias for double-typed ring buffer. */
#endif // GRABCOMMON_H