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
24 public:
25 ISampleObserver() = delete;
26
28 explicit ISampleObserver(const IDataGroup& data_group);
29
30 virtual ~ISampleObserver();
31
37 virtual void AttachObserver();
38
46 virtual void DetachObserver();
47
65 virtual bool OnSample(uint64_t sample, uint64_t record_id,
66 const std::vector<uint8_t>& record);
67
75 [[nodiscard]] bool IsRecordIdNeeded(uint64_t record_id) const {
76 return record_id_list_.find(record_id) != record_id_list_.cend();
77 }
78
84 std::function<bool(uint64_t sample, uint64_t record_id,
85 const std::vector<uint8_t>& record)> DoOnSample;
86
100 template <typename V>
101 bool GetChannelValue(const IChannel& channel,
102 uint64_t sample,
103 const std::vector<uint8_t>& record,
104 V& value,
105 uint64_t array_index = 0) const {
106 bool valid = false;
107 value = {};
108
109 switch (channel.Type()) {
112 valid = channel.GetVirtualSample(sample, value);
113 break;
114
115 // This channel may reference a SD/CG blocks
117 if (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.
124 break;
125
130 default:
131 valid = channel.GetChannelValue(record, value, array_index);
132 break;
133 }
134 return valid;
135 }
136
154 template <typename V>
155 bool GetEngValue(const IChannel& channel,
156 uint64_t sample,
157 const std::vector<uint8_t>& record,
158 V& value,
159 uint64_t array_index = 0) const {
160
161 const auto* conversion = channel.ChannelConversion();
162 if (conversion == nullptr) {
163 return GetChannelValue(channel,sample, record,
164 value, array_index);
165 }
166
167 bool valid;
168 value = {};
169 switch (channel.DataType()) {
172 uint64_t v = 0;
173 valid = GetChannelValue(channel,sample, record, v,
174 array_index) && conversion->Convert(v, value);
175 break;
176 }
177
180 int64_t v = 0;
181 valid = GetChannelValue(channel,sample, record, v,
182 array_index) && conversion->Convert(v, value);
183 break;
184 }
185
188 double v = 0.0;
189 valid = GetChannelValue(channel,sample, record, v,
190 array_index) && conversion->Convert(v, value);
191 break;
192 }
193
198 std::string v;
199 valid = GetChannelValue(channel,sample, record, v,
200 array_index) && conversion->Convert(v, value);
201 break;
202 }
203
204 default:
205 valid = GetChannelValue(channel,sample, record, value,
206 array_index);
207 break;
208 }
209 return valid;
210 }
211
212 protected:
213 std::set<uint64_t> record_id_list_;
215 private:
216 bool 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.
virtual IChannelConversion * ChannelConversion() const =0
Returns the conversion block, if any.
virtual void DataType(ChannelDataType type)=0
Sets the data type.
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
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
const IDataGroup & data_group_
Reference to the data group (DG) block.
Definition isampleobserver.h:214
virtual ~ISampleObserver()
Destructor.
virtual void AttachObserver()
Attach the observer to an observer list (publisher).
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
std::set< uint64_t > record_id_list_
List of subscribed channel groups.
Definition isampleobserver.h:213
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.
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
virtual void DetachObserver()
Detach the observer from an observer list.
ISampleObserver(const IDataGroup &data_group)
Sample observer constructor.
bool IsRecordIdNeeded(uint64_t record_id) const
Function that test if this observer needs to read a specific record.
Definition isampleobserver.h:75
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
@ VirtualMaster
Virtual master channel.
@ FixedLength
Fixed length data (default type)
@ MaxLength
Max length channel.
@ VirtualData
Virtual data channel.
@ VariableLength
Variable length data.
@ Sync
Synchronize channel.
@ Master
Master channel.
@ StringUTF16Le
Text, UTF16 coded little endian.
@ StringUTF8
Text, UTF8 coded.
@ SignedIntegerBe
Signed integer, big endian.
@ UnsignedIntegerLe
Unsigned integer, little endian.
@ StringUTF16Be
Text, UTF16 coded big endian.
@ FloatLe
Float, little endian.
@ StringAscii
Text, ISO-8859-1 coded.
@ FloatBe
Float, big endian.
@ SignedIntegerLe
Signed integer, little endian.
@ UnsignedIntegerBe
Unsigned integer, big endian.