MDF Lib 2.2
Interface against MDF 3/4 files
Loading...
Searching...
No Matches
ichannelobserver.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
5
10#pragma once
11#include <string>
12#include <vector>
13
14#include "mdf/ichannel.h"
15#include "mdf/isampleobserver.h"
16#include "mdf/mdfhelper.h"
17
18namespace mdf {
19
26 protected:
28 bool read_vlsd_data_ = true;
29
30 std::vector<uint64_t> offset_list_;
31 std::vector<bool> valid_list_;
32
33 virtual bool GetSampleUnsigned(uint64_t sample, uint64_t& value, uint64_t array_index)
34 const = 0;
35 virtual bool GetSampleSigned(uint64_t sample, int64_t& value, uint64_t array_index)
36 const = 0;
37 virtual bool GetSampleFloat(uint64_t sample, double& value, uint64_t array_index)
38 const = 0;
39 virtual bool GetSampleText(uint64_t sample, std::string& value, uint64_t array_index)
40 const = 0;
41 virtual bool GetSampleByteArray(uint64_t sample, std::vector<uint8_t>& value)
42 const = 0;
43
44 public:
45 explicit IChannelObserver(const IDataGroup& dataGroup, const IChannel& channel);
46
47 ~IChannelObserver() override = default;
48
49 IChannelObserver() = delete;
50 IChannelObserver(const IChannelObserver&) = delete;
52 IChannelObserver& operator=(const IChannelObserver&) = delete;
53 IChannelObserver& operator=(IChannelObserver&&) = delete;
54
55 [[nodiscard]] virtual uint64_t NofSamples()
56 const = 0;
57
58 [[nodiscard]] std::string Name() const;
59
60 [[nodiscard]] std::string Unit() const;
61
69 [[nodiscard]] const IChannel& Channel() const;
70
71 [[nodiscard]] bool IsMaster() const;
72
73 [[nodiscard]] bool IsArray() const;
74
82 [[nodiscard]] std::vector<uint64_t> Shape() const;
83
95 void ReadVlsdData(bool read_vlsd_data);
96
98 [[nodiscard]] bool ReadVlsdData() const { return read_vlsd_data_; }
99
106 [[nodiscard]] uint64_t ArraySize() const;
107
117 template <typename V>
118 bool GetChannelValue(uint64_t sample, V& value, uint64_t array_index = 0 ) const;
119
129 template<typename V>
130 std::vector<bool> GetChannelSamples(std::vector<V>& values) const;
131
146 template<typename V>
147 std::vector<bool> GetChannelValues(uint64_t sample,
148 std::vector<V>& values) const;
149
159 template <typename V>
160 bool GetEngValue(uint64_t sample, V& value, uint64_t array_index = 0) const;
161
171 template<typename V>
172 std::vector<bool> GetEngSamples(std::vector<V>& values) const;
173
186 template<typename V>
187 std::vector<bool> GetEngValues(uint64_t sample, std::vector<V>& values) const;
188
197 [[nodiscard]] std::string EngValueToString(uint64_t sample) const;
198
207 [[nodiscard]] const std::vector<uint64_t>& GetOffsetList() const {
208 return offset_list_;
209 }
210
212 [[nodiscard]] const std::vector<bool>& GetValidList() const {
213 return valid_list_;
214 }
215
216};
217
218template <typename V>
219bool IChannelObserver::GetChannelValue(uint64_t sample, V& value, uint64_t array_index) const {
220 bool valid = false;
221 value = {};
222 switch (channel_.DataType()) {
225 // All times are stored as ns since 1970 (uint64_t)
226 uint64_t v = 0; // ns since 1970
227 valid = GetSampleUnsigned(sample, v, array_index);
228 value = static_cast<V>(v);
229 break;
230 }
231
234 uint64_t v = 0;
235 valid = GetSampleUnsigned(sample, v, array_index);
236 value = static_cast<V>(v);
237 break;
238 }
239
242 int64_t v = 0;
243 valid = GetSampleSigned(sample, v, array_index);
244 value = static_cast<V>(v);
245 break;
246 }
247
250 double v = 0.0;
251 valid = GetSampleFloat(sample, v, array_index);
252 value = static_cast<V>(v);
253 break;
254 }
255
260 std::string v;
261 valid = GetSampleText(sample, v, array_index);
262 std::istringstream s(v);
263 s >> value;
264 break;
265 }
266
270 std::vector<uint8_t> v;
271 valid = GetSampleByteArray(sample, v);
272 value = static_cast<V>(v.empty() ? V{} : v[0]);
273 break;
274 }
275
276 default:
277 break;
278 }
279 return valid;
280}
281
283template <>
285 std::string& value, uint64_t array_index) const;
286
288template <>
290 std::vector<uint8_t>& value, uint64_t array_index) const;
291
292template <typename V>
294 std::vector<V>& values) const {
295 const uint64_t nof_samples = NofSamples();
296 std::vector<bool> valid_array(nof_samples, false);
297 values.resize(nof_samples, {});
298 uint64_t sample = 0;
299 for (auto& value : values) {
300 valid_array[sample] = GetChannelValue(sample, value, 0);
301 ++sample;
302 }
303 return valid_array;
304}
305
306template <typename V>
307std::vector<bool> IChannelObserver::GetChannelValues(uint64_t sample,
308 std::vector<V>& values) const {
309 const auto array_size = ArraySize();
310 std::vector<bool> valid_array(array_size, false);
311 values.resize(array_size, {});
312 uint64_t index = 0;
313 for (auto& value : values) {
314 valid_array[index] = GetChannelValue(sample, value, index);
315 ++index;
316 }
317 return valid_array;
318}
319
320template <typename V>
321bool IChannelObserver::GetEngValue(uint64_t sample, V& value, uint64_t array_index) const {
322 const auto* conversion = channel_.ChannelConversion();
323 if (conversion == nullptr) {
324 return GetChannelValue(sample, value, array_index);
325 }
326
327 bool valid = false;
328 value = {};
329 switch (channel_.DataType()) {
332 uint64_t v = 0;
333 valid = GetSampleUnsigned(sample, v, array_index)
334 && conversion->Convert(v, value);
335 break;
336 }
337
340 int64_t v = 0;
341 valid = GetSampleSigned(sample, v, array_index)
342 && conversion->Convert(v, value);
343 break;
344 }
345
348 double v = 0.0;
349 valid = GetSampleFloat(sample, v, array_index)
350 && conversion->Convert(v, value);
351 break;
352 }
353
358 std::string v;
359 valid = GetSampleText(sample, v, array_index);
360 std::string temp;
361 conversion->Convert(v, temp);
362 std::istringstream temp1(temp);
363 temp1 >> value;
364 break;
365 }
366
367 default:
368 valid = GetChannelValue(sample, value); // No conversion is allowed;
369 break;
370 }
371 return valid;
372}
373
375template <>
376bool IChannelObserver::GetEngValue(uint64_t sample,
377 std::vector<uint8_t>& value,
378 uint64_t array_index) const;
379template <typename V>
381 std::vector<V>& values) const {
382 const uint64_t nof_samples = NofSamples();
383 std::vector<bool> valid_array(nof_samples, false);
384 values.resize(nof_samples, {});
385 uint64_t sample = 0;
386 for (auto& value : values) {
387 valid_array[sample] = GetEngValue(sample, value, 0);
388 ++sample;
389 }
390 return valid_array;
391}
392
393template <typename V>
394std::vector<bool> IChannelObserver::GetEngValues(uint64_t sample,
395 std::vector<V>& values) const {
396 const auto array_size = ArraySize();
397 std::vector<bool> valid_array(array_size, false);
398 values.resize(array_size, {});
399 uint64_t index = 0;
400 for (auto& value : values) {
401 valid_array[index] = GetEngValue(sample, value, index);
402 ++index;
403 }
404 return valid_array;
405}
406
407} // namespace mdf
Defines a MDF channel (CN) block.
Definition ichannel.h:126
virtual IChannelConversion * ChannelConversion() const =0
Returns the conversion block, if any.
virtual void DataType(ChannelDataType type)=0
Sets the data type.
The channel observer object shall hold all samples for a channel.
Definition ichannelobserver.h:25
const std::vector< bool > & GetValidList() const
Returns the sample to valid list.
Definition ichannelobserver.h:212
std::vector< bool > valid_list_
List of valid samples.
Definition ichannelobserver.h:31
bool IsArray() const
True if this channel is an array channel.
const IChannel & channel_
Reference to the channel (CN) block.
Definition ichannelobserver.h:27
virtual bool GetSampleFloat(uint64_t sample, double &value, uint64_t array_index) const =0
Returns a float sample value.
virtual bool GetSampleUnsigned(uint64_t sample, uint64_t &value, uint64_t array_index) const =0
Returns a unsigned sample value.
const IChannel & Channel() const
Returns the channel object.
bool GetEngValue(uint64_t sample, V &value, uint64_t array_index=0) const
Returns the engineering value for a specific value.
Definition ichannelobserver.h:321
std::string Name() const
Channel name.
virtual bool GetSampleText(uint64_t sample, std::string &value, uint64_t array_index) const =0
Returns a string sample value.
bool GetChannelValue(uint64_t sample, V &value, uint64_t array_index=0) const
Returns the channel value for a sample.
Definition ichannelobserver.h:219
~IChannelObserver() override=default
Default destructor.
bool IsMaster() const
True if this is the master channel.
virtual bool GetSampleByteArray(uint64_t sample, std::vector< uint8_t > &value) const =0
Returns a byte array sample value.
virtual bool GetSampleSigned(uint64_t sample, int64_t &value, uint64_t array_index) const =0
Returns a signed sample value.
IChannelObserver(const IDataGroup &dataGroup, const IChannel &channel)
Constructor.
std::string EngValueToString(uint64_t sample) const
Support function that convert a sample value to a user friendly string.
std::string Unit() const
Channel unit.
void ReadVlsdData(bool read_vlsd_data)
Property interface that defines if the VLSD raw data should be read or not.
bool ReadVlsdData() const
Returns the read VLSD bytes property.
Definition ichannelobserver.h:98
std::vector< bool > GetEngSamples(std::vector< V > &values) const
Simple function that returns all scaled samples.
Definition ichannelobserver.h:380
const std::vector< uint64_t > & GetOffsetList() const
Returns the sample to VLSD offset list.
Definition ichannelobserver.h:207
bool read_vlsd_data_
Defines if the VLSD bytes should be read.
Definition ichannelobserver.h:28
std::vector< uint64_t > Shape() const
Returns a shape vector that describes an array dimension.
virtual uint64_t NofSamples() const =0
Returns number of samples.
std::vector< bool > GetChannelSamples(std::vector< V > &values) const
Simple function that returns all non-scaled samples.
Definition ichannelobserver.h:293
std::vector< bool > GetEngValues(uint64_t sample, std::vector< V > &values) const
Returns a vector of array values for a specific sample.
Definition ichannelobserver.h:394
std::vector< uint64_t > offset_list_
Only used for VLSD channels.
Definition ichannelobserver.h:30
uint64_t ArraySize() const
If this is an array channel, this function returns the array size.
std::vector< bool > GetChannelValues(uint64_t sample, std::vector< V > &values) const
Returns a vector of array channel values for a specific sample.
Definition ichannelobserver.h:307
Interface to a data group (DG) block.
Definition idatagroup.h:42
Interface to a sample observer that handle incoming samples events.
Definition isampleobserver.h:23
The define an interface against a channel block (CN).
Interface class to a sample observer. This class is used internally.
Support class for the MDF library.
Main namespace for the MDF library.
Definition canmessage.h:17
@ StringUTF16Le
Text, UTF16 coded little endian.
@ StringUTF8
Text, UTF8 coded.
@ ByteArray
Byte array.
@ CanOpenDate
7-byte CANOpen date.
@ SignedIntegerBe
Signed integer, big endian.
@ UnsignedIntegerLe
Unsigned integer, little endian.
@ StringUTF16Be
Text, UTF16 coded big endian.
@ FloatLe
Float, little endian.
@ MimeSample
MIME sample byte array.
@ StringAscii
Text, ISO-8859-1 coded.
@ FloatBe
Float, big endian.
@ SignedIntegerLe
Signed integer, little endian.
@ CanOpenTime
6-byte CANOpen time.
@ UnsignedIntegerBe
Unsigned integer, big endian.
@ MimeStream
MIME stream byte array.