lib/mdflib/docs/manual/html/isampleobserver_8h_source.html
| MDF Lib 2.2
Interface against MDF 3/4 files |
Loading...
Searching...
No Matches
isampleobserver.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Ingemar Hedvall
3 * SPDX-License-Identifier: MIT
4 */
5
9#pragma once
10#include <cstdint>
11#include <vector>
12#include <set>
13#include <functional>
14
15#include "mdf/ichannelgroup.h"
16#include "mdf/idatagroup.h"
17namespace mdf {
18
19
20
23class ISampleObserver {
24 public:
25ISampleObserver() = delete;
26
28explicit ISampleObserver(const IDataGroup& data_group);
29
30virtual ~ISampleObserver();
31
37virtual void AttachObserver();
38
46virtual void DetachObserver();
47
65virtual bool OnSample(uint64_t sample, uint64_t record_id,
66const std::vector<uint8_t>& record);
67
75 [[nodiscard]] bool IsRecordIdNeeded(uint64_t record_id) const {
76return record_id_list_.find(record_id) != record_id_list_.cend();
77 }
78
84 std::function<bool(uint64_t sample, uint64_t record_id,
85const std::vector<uint8_t>& record)> DoOnSample;
86
100template <typename V>
101bool GetChannelValue(const IChannel& channel,
102 uint64_t sample,
103const std::vector<uint8_t>& record,
104 V& value,
105 uint64_t array_index = 0) const {
106bool valid = false;
107 value = {};
108
109switch (channel.Type()) {
110case ChannelType::VirtualMaster:
111case ChannelType::VirtualData:
112 valid = channel.GetVirtualSample(sample, value);
113break;
114
115// This channel may reference a SD/CG blocks
116case ChannelType::VariableLength:
117if (channel.VlsdRecordId() == 0) {
118// If variable length, the value is an index into an SD block.
119// Value should be in the channels data list (SD). The channels
120// GetChannelValue handle this situation
121 valid = channel.GetChannelValue(record, value, array_index);
122 }
123// VLSD CG records cannot be handled without some sort of cache.
124break;
125
126case ChannelType::MaxLength:
127case ChannelType::Sync:
128case ChannelType::Master:
129case ChannelType::FixedLength:
130default:
131 valid = channel.GetChannelValue(record, value, array_index);
132break;
133 }
134return valid;
135 }
136
154template <typename V>
155bool GetEngValue(const IChannel& channel,
156 uint64_t sample,
157const std::vector<uint8_t>& record,
158 V& value,
159 uint64_t array_index = 0) const {
160
161const auto* conversion = channel.ChannelConversion();
162if (conversion == nullptr) {
163return GetChannelValue(channel,sample, record,
164 value, array_index);
165 }
166
167bool valid;
168 value = {};
169switch (channel.DataType()) {
170case ChannelDataType::UnsignedIntegerLe:
171case ChannelDataType::UnsignedIntegerBe: {
172 uint64_t v = 0;
173 valid = GetChannelValue(channel,sample, record, v,
174 array_index) && conversion->Convert(v, value);
175break;
176 }
177
178case ChannelDataType::SignedIntegerLe:
179case ChannelDataType::SignedIntegerBe: {
180 int64_t v = 0;
181 valid = GetChannelValue(channel,sample, record, v,
182 array_index) && conversion->Convert(v, value);
183break;
184 }
185
186case ChannelDataType::FloatLe:
187case ChannelDataType::FloatBe: {
188double v = 0.0;
189 valid = GetChannelValue(channel,sample, record, v,
190 array_index) && conversion->Convert(v, value);
191break;
192 }
193
194case ChannelDataType::StringAscii:
195case ChannelDataType::StringUTF16Be:
196case ChannelDataType::StringUTF16Le:
197case ChannelDataType::StringUTF8: {
198 std::string v;
199 valid = GetChannelValue(channel,sample, record, v,
200 array_index) && conversion->Convert(v, value);
201break;
202 }
203
204default:
205 valid = GetChannelValue(channel,sample, record, value,
206 array_index);
207break;
208 }
209return valid;
210 }
211
212 protected:
213 std::set<uint64_t> record_id_list_;
214const IDataGroup& data_group_;
215 private:
216bool attached_ = false;
217};
218
219
220
221} // namespace mdf
Defines a MDF channel (CN) block.
Definition ichannel.h:126
virtual void Type(ChannelType type)=0
Sets the type of channel.
mdf::IChannel::ChannelConversion
virtual IChannelConversion * ChannelConversion() const =0
Returns the conversion block, if any.
virtual void DataType(ChannelDataType type)=0
Sets the data type.
mdf::IChannel::GetVirtualSample
static bool GetVirtualSample(uint64_t sample, V &value)
Returns the value for a virtual sample.
Definition ichannel.h:480
void VlsdRecordId(uint64_t record_id) const
Sets the VLSD record id.
Definition ichannel.h:322
mdf::IChannel::GetChannelValue
bool GetChannelValue(const std::vector< uint8_t > &record_buffer, T &dest, uint64_t array_index=0) const
Parse out the channel value from a data record.
Definition ichannel.h:595
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
mdf::ISampleObserver::data_group_
const IDataGroup & data_group_
Reference to the data group (DG) block.
Definition isampleobserver.h:214
mdf::ISampleObserver::~ISampleObserver
virtual ~ISampleObserver()
Destructor.
mdf::ISampleObserver::AttachObserver
virtual void AttachObserver()
Attach the observer to an observer list (publisher).
mdf::ISampleObserver::GetChannelValue
bool GetChannelValue(const IChannel &channel, uint64_t sample, const std::vector< uint8_t > &record, V &value, uint64_t array_index=0) const
The function returns a channel value.
Definition isampleobserver.h:101
mdf::ISampleObserver::record_id_list_
std::set< uint64_t > record_id_list_
List of subscribed channel groups.
Definition isampleobserver.h:213
mdf::ISampleObserver::OnSample
virtual bool OnSample(uint64_t sample, uint64_t record_id, const std::vector< uint8_t > &record)
Observer function that receives the sample record and parse out a channel value.
mdf::ISampleObserver::DoOnSample
std::function< bool(uint64_t sample, uint64_t record_id, const std::vector< uint8_t > &record)> DoOnSample
Function object that is called if assigned.
Definition isampleobserver.h:85
mdf::ISampleObserver::DetachObserver
virtual void DetachObserver()
Detach the observer from an observer list.
mdf::ISampleObserver::ISampleObserver
ISampleObserver(const IDataGroup &data_group)
Sample observer constructor.
mdf::ISampleObserver::IsRecordIdNeeded
bool IsRecordIdNeeded(uint64_t record_id) const
Function that test if this observer needs to read a specific record.
Definition isampleobserver.h:75
mdf::ISampleObserver::GetEngValue
bool GetEngValue(const IChannel &channel, uint64_t sample, const std::vector< uint8_t > &record, V &value, uint64_t array_index=0) const
Returns the scaled sample value for the channel with the record bytes as input. This function is main...
Definition isampleobserver.h:155
Defines an interface against a channel group (CG) block.
Interface to a data group (DG) block.
Main namespace for the MDF library.
Definition canmessage.h:17
mdf::ChannelType::VirtualMaster
@ VirtualMaster
Virtual master channel.
@ FixedLength
Fixed length data (default type)
@ MaxLength
Max length channel.
@ VirtualData
Virtual data channel.
mdf::ChannelType::VariableLength
@ VariableLength
Variable length data.
@ Sync
Synchronize channel.
@ Master
Master channel.
mdf::ChannelDataType::StringUTF16Le
@ StringUTF16Le
Text, UTF16 coded little endian.
mdf::ChannelDataType::StringUTF8
@ StringUTF8
Text, UTF8 coded.
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::StringAscii
@ StringAscii
Text, ISO-8859-1 coded.
@ FloatBe
Float, big endian.
mdf::ChannelDataType::SignedIntegerLe
@ SignedIntegerLe
Signed integer, little endian.
mdf::ChannelDataType::UnsignedIntegerBe
@ UnsignedIntegerBe
Unsigned integer, big endian.