lib/mdflib/docs/manual/html/ichannelobserver_8h_source.html
| 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
25class IChannelObserver : public ISampleObserver {
26 protected:
28bool read_vlsd_data_ = true;
29
30 std::vector<uint64_t> offset_list_;
31 std::vector<bool> valid_list_;
32
33virtual bool GetSampleUnsigned(uint64_t sample, uint64_t& value, uint64_t array_index)
34const = 0;
35virtual bool GetSampleSigned(uint64_t sample, int64_t& value, uint64_t array_index)
36const = 0;
37virtual bool GetSampleFloat(uint64_t sample, double& value, uint64_t array_index)
38const = 0;
39virtual bool GetSampleText(uint64_t sample, std::string& value, uint64_t array_index)
40const = 0;
41virtual bool GetSampleByteArray(uint64_t sample, std::vector<uint8_t>& value)
42const = 0;
43
44 public:
45explicit IChannelObserver(const IDataGroup& dataGroup, const IChannel& channel);
46
47~IChannelObserver() override = default;
48
49IChannelObserver() = delete;
50IChannelObserver(const IChannelObserver&) = delete;
51IChannelObserver(IChannelObserver&&) = delete;
52IChannelObserver& operator=(const IChannelObserver&) = delete;
53IChannelObserver& operator=(IChannelObserver&&) = delete;
54
55 [[nodiscard]] virtual uint64_t NofSamples()
56const = 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
95void ReadVlsdData(bool read_vlsd_data);
96
98 [[nodiscard]] bool ReadVlsdData() const { return read_vlsd_data_; }
99
106 [[nodiscard]] uint64_t ArraySize() const;
107
117template <typename V>
118bool GetChannelValue(uint64_t sample, V& value, uint64_t array_index = 0 ) const;
119
129template<typename V>
130 std::vector<bool> GetChannelSamples(std::vector<V>& values) const;
131
146template<typename V>
147 std::vector<bool> GetChannelValues(uint64_t sample,
148 std::vector<V>& values) const;
149
159template <typename V>
160bool GetEngValue(uint64_t sample, V& value, uint64_t array_index = 0) const;
161
171template<typename V>
172 std::vector<bool> GetEngSamples(std::vector<V>& values) const;
173
186template<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 {
208return offset_list_;
209 }
210
212 [[nodiscard]] const std::vector<bool>& GetValidList() const {
213return valid_list_;
214 }
215
216};
217
218template <typename V>
219bool IChannelObserver::GetChannelValue(uint64_t sample, V& value, uint64_t array_index) const {
220bool valid = false;
221 value = {};
222switch (channel_.DataType()) {
223case ChannelDataType::CanOpenDate:
224case ChannelDataType::CanOpenTime: {
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);
229break;
230 }
231
232case ChannelDataType::UnsignedIntegerLe:
233case ChannelDataType::UnsignedIntegerBe: {
234 uint64_t v = 0;
235 valid = GetSampleUnsigned(sample, v, array_index);
236 value = static_cast<V>(v);
237break;
238 }
239
240case ChannelDataType::SignedIntegerLe:
241case ChannelDataType::SignedIntegerBe: {
242 int64_t v = 0;
243 valid = GetSampleSigned(sample, v, array_index);
244 value = static_cast<V>(v);
245break;
246 }
247
248case ChannelDataType::FloatLe:
249case ChannelDataType::FloatBe: {
250double v = 0.0;
251 valid = GetSampleFloat(sample, v, array_index);
252 value = static_cast<V>(v);
253break;
254 }
255
256case ChannelDataType::StringAscii:
257case ChannelDataType::StringUTF8:
258case ChannelDataType::StringUTF16Le:
259case ChannelDataType::StringUTF16Be: {
260 std::string v;
261 valid = GetSampleText(sample, v, array_index);
262 std::istringstream s(v);
263 s >> value;
264break;
265 }
266
267case ChannelDataType::MimeStream:
268case ChannelDataType::MimeSample:
269case ChannelDataType::ByteArray: {
270 std::vector<uint8_t> v;
271 valid = GetSampleByteArray(sample, v);
272 value = static_cast<V>(v.empty() ? V{} : v[0]);
273break;
274 }
275
276default:
277break;
278 }
279return valid;
280}
281
283template <>
284bool IChannelObserver::GetChannelValue(uint64_t sample,
285 std::string& value, uint64_t array_index) const;
286
288template <>
289bool IChannelObserver::GetChannelValue(uint64_t sample,
290 std::vector<uint8_t>& value, uint64_t array_index) const;
291
292template <typename V>
293std::vector<bool> IChannelObserver::GetChannelSamples(
294 std::vector<V>& values) const {
295const uint64_t nof_samples = NofSamples();
296 std::vector<bool> valid_array(nof_samples, false);
297 values.resize(nof_samples, {});
298 uint64_t sample = 0;
299for (auto& value : values) {
300 valid_array[sample] = GetChannelValue(sample, value, 0);
301 ++sample;
302 }
303return valid_array;
304}
305
306template <typename V>
307std::vector<bool> IChannelObserver::GetChannelValues(uint64_t sample,
308 std::vector<V>& values) const {
309const auto array_size = ArraySize();
310 std::vector<bool> valid_array(array_size, false);
311 values.resize(array_size, {});
312 uint64_t index = 0;
313for (auto& value : values) {
314 valid_array[index] = GetChannelValue(sample, value, index);
315 ++index;
316 }
317return valid_array;
318}
319
320template <typename V>
321bool IChannelObserver::GetEngValue(uint64_t sample, V& value, uint64_t array_index) const {
322const auto* conversion = channel_.ChannelConversion();
323if (conversion == nullptr) {
324return GetChannelValue(sample, value, array_index);
325 }
326
327bool valid = false;
328 value = {};
329switch (channel_.DataType()) {
330case ChannelDataType::UnsignedIntegerLe:
331case ChannelDataType::UnsignedIntegerBe: {
332 uint64_t v = 0;
333 valid = GetSampleUnsigned(sample, v, array_index)
334 && conversion->Convert(v, value);
335break;
336 }
337
338case ChannelDataType::SignedIntegerLe:
339case ChannelDataType::SignedIntegerBe: {
340 int64_t v = 0;
341 valid = GetSampleSigned(sample, v, array_index)
342 && conversion->Convert(v, value);
343break;
344 }
345
346case ChannelDataType::FloatLe:
347case ChannelDataType::FloatBe: {
348double v = 0.0;
349 valid = GetSampleFloat(sample, v, array_index)
350 && conversion->Convert(v, value);
351break;
352 }
353
354case ChannelDataType::StringAscii:
355case ChannelDataType::StringUTF16Be:
356case ChannelDataType::StringUTF16Le:
357case ChannelDataType::StringUTF8: {
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;
364break;
365 }
366
367default:
368 valid = GetChannelValue(sample, value); // No conversion is allowed;
369break;
370 }
371return 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>
380std::vector<bool> IChannelObserver::GetEngSamples(
381 std::vector<V>& values) const {
382const uint64_t nof_samples = NofSamples();
383 std::vector<bool> valid_array(nof_samples, false);
384 values.resize(nof_samples, {});
385 uint64_t sample = 0;
386for (auto& value : values) {
387 valid_array[sample] = GetEngValue(sample, value, 0);
388 ++sample;
389 }
390return valid_array;
391}
392
393template <typename V>
394std::vector<bool> IChannelObserver::GetEngValues(uint64_t sample,
395 std::vector<V>& values) const {
396const auto array_size = ArraySize();
397 std::vector<bool> valid_array(array_size, false);
398 values.resize(array_size, {});
399 uint64_t index = 0;
400for (auto& value : values) {
401 valid_array[index] = GetEngValue(sample, value, index);
402 ++index;
403 }
404return valid_array;
405}
406
407} // namespace mdf
Defines a MDF channel (CN) block.
Definition ichannel.h:126
mdf::IChannel::ChannelConversion
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
mdf::IChannelObserver::GetValidList
const std::vector< bool > & GetValidList() const
Returns the sample to valid list.
Definition ichannelobserver.h:212
mdf::IChannelObserver::valid_list_
std::vector< bool > valid_list_
List of valid samples.
Definition ichannelobserver.h:31
mdf::IChannelObserver::IsArray
bool IsArray() const
True if this channel is an array channel.
mdf::IChannelObserver::channel_
const IChannel & channel_
Reference to the channel (CN) block.
Definition ichannelobserver.h:27
mdf::IChannelObserver::GetSampleFloat
virtual bool GetSampleFloat(uint64_t sample, double &value, uint64_t array_index) const =0
Returns a float sample value.
mdf::IChannelObserver::GetSampleUnsigned
virtual bool GetSampleUnsigned(uint64_t sample, uint64_t &value, uint64_t array_index) const =0
Returns a unsigned sample value.
mdf::IChannelObserver::Channel
const IChannel & Channel() const
Returns the channel object.
mdf::IChannelObserver::GetEngValue
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.
mdf::IChannelObserver::GetSampleText
virtual bool GetSampleText(uint64_t sample, std::string &value, uint64_t array_index) const =0
Returns a string sample value.
mdf::IChannelObserver::GetChannelValue
bool GetChannelValue(uint64_t sample, V &value, uint64_t array_index=0) const
Returns the channel value for a sample.
Definition ichannelobserver.h:219
mdf::IChannelObserver::~IChannelObserver
~IChannelObserver() override=default
Default destructor.
mdf::IChannelObserver::IsMaster
bool IsMaster() const
True if this is the master channel.
mdf::IChannelObserver::GetSampleByteArray
virtual bool GetSampleByteArray(uint64_t sample, std::vector< uint8_t > &value) const =0
Returns a byte array sample value.
mdf::IChannelObserver::GetSampleSigned
virtual bool GetSampleSigned(uint64_t sample, int64_t &value, uint64_t array_index) const =0
Returns a signed sample value.
mdf::IChannelObserver::IChannelObserver
IChannelObserver(const IDataGroup &dataGroup, const IChannel &channel)
Constructor.
mdf::IChannelObserver::EngValueToString
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.
mdf::IChannelObserver::ReadVlsdData
void ReadVlsdData(bool read_vlsd_data)
Property interface that defines if the VLSD raw data should be read or not.
mdf::IChannelObserver::ReadVlsdData
bool ReadVlsdData() const
Returns the read VLSD bytes property.
Definition ichannelobserver.h:98
mdf::IChannelObserver::GetEngSamples
std::vector< bool > GetEngSamples(std::vector< V > &values) const
Simple function that returns all scaled samples.
Definition ichannelobserver.h:380
mdf::IChannelObserver::GetOffsetList
const std::vector< uint64_t > & GetOffsetList() const
Returns the sample to VLSD offset list.
Definition ichannelobserver.h:207
mdf::IChannelObserver::read_vlsd_data_
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.
mdf::IChannelObserver::NofSamples
virtual uint64_t NofSamples() const =0
Returns number of samples.
mdf::IChannelObserver::GetChannelSamples
std::vector< bool > GetChannelSamples(std::vector< V > &values) const
Simple function that returns all non-scaled samples.
Definition ichannelobserver.h:293
mdf::IChannelObserver::GetEngValues
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
mdf::IChannelObserver::offset_list_
std::vector< uint64_t > offset_list_
Only used for VLSD channels.
Definition ichannelobserver.h:30
mdf::IChannelObserver::ArraySize
uint64_t ArraySize() const
If this is an array channel, this function returns the array size.
mdf::IChannelObserver::GetChannelValues
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
mdf::ChannelDataType::StringUTF16Le
@ StringUTF16Le
Text, UTF16 coded little endian.
mdf::ChannelDataType::StringUTF8
@ StringUTF8
Text, UTF8 coded.
mdf::ChannelDataType::ByteArray
@ ByteArray
Byte array.
mdf::ChannelDataType::CanOpenDate
@ CanOpenDate
7-byte CANOpen date.
mdf::ChannelDataType::SignedIntegerBe
@ SignedIntegerBe
Signed integer, big endian.
mdf::ChannelDataType::UnsignedIntegerLe
@ UnsignedIntegerLe
Unsigned integer, little endian.
mdf::ChannelDataType::StringUTF16Be
@ StringUTF16Be
Text, UTF16 coded big endian.
@ FloatLe
Float, little endian.
mdf::ChannelDataType::MimeSample
@ MimeSample
MIME sample byte array.
mdf::ChannelDataType::StringAscii
@ StringAscii
Text, ISO-8859-1 coded.
@ FloatBe
Float, big endian.
mdf::ChannelDataType::SignedIntegerLe
@ SignedIntegerLe
Signed integer, little endian.
mdf::ChannelDataType::CanOpenTime
@ CanOpenTime
6-byte CANOpen time.
mdf::ChannelDataType::UnsignedIntegerBe
@ UnsignedIntegerBe
Unsigned integer, big endian.
mdf::ChannelDataType::MimeStream
@ MimeStream
MIME stream byte array.