Falcon
Loading...
Searching...
No Matches
idata.hpp
1// ---------------------------------------------------------------------
2// This file is part of falcon-core.
3//
4// Copyright (C) 2015, 2016, 2017 Neuro-Electronics Research Flanders
5//
6// Falcon-server is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Falcon-server is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with falcon-core. If not, see <http://www.gnu.org/licenses/>.
18// ---------------------------------------------------------------------
19
20#pragma once
21
22#include <cassert>
23#include <chrono>
24#include <deque>
25#include <limits>
26#include <memory>
27#include <string>
28#include <string_view>
29
30#include "ringbuffer.hpp"
31
32#include "datatype_generated.h"
33#include "flatbuffers/flatbuffers.h"
34#include "flatbuffers/flexbuffers.h"
35#include "logging/log.hpp"
36#include "serialization.hpp"
37#include "utilities/string.hpp"
38#include "utilities/time.hpp"
39#include "yaml-cpp/yaml.h"
40
44class BaseData {};
45
49class BaseType {
50 public:
51 static bool ispure() { return true; }
52 using Data = BaseData;
53};
54
73template <typename DATA, typename BASETYPE, bool pure = false,
74 typename CAPS = typename DATA::Capabilities, typename PARAMS = typename DATA::Parameters>
76 public:
77 static const std::string datatype() { return DATA::static_datatype(); }
78 static const std::string dataname() { return DATA::static_dataname(); }
79
80 static bool ispure() { return pure && BASETYPE::ispure(); }
81
82 using Data = DATA;
83 using Parameters = PARAMS;
84 using Capabilities = CAPS;
85 using Base = BASETYPE;
86};
87
94template <typename T, typename BASETYPE>
95class IData : public BASETYPE::Data {
96 public:
97 using Base = typename BASETYPE::Data;
98 using Base::Base;
99
100 public:
101 virtual std::string datatype() const { return T::static_datatype(); }
102 virtual std::string dataname() const { return T::static_dataname(); }
103};
104
105namespace nsAnyType {
106
119struct Parameters {};
120
129class Data : public IData<Data, BaseType> {
130 public:
131 Data() : hardware_timestamp_(0), serial_number_(0) { set_ingestion_tsc(); }
132 virtual ~Data() {}
133
138 static const std::string static_datatype() { return "any"; }
143 static const std::string static_dataname() { return "data"; }
144
150 virtual void ClearData() {}
151
152 uint64_t serial_number() const;
153 void set_serial_number(uint64_t n);
154
161 int64_t ingestion_tsc() const;
162
167 void set_ingestion_tsc();
168
173 void forward_ingestion_tsc(const Data& data);
174
184 void set_source_timestamp(TimePoint t);
185
192 TimePoint source_timestamp() const;
193
199 template <typename DURATION = std::chrono::microseconds>
200 DURATION time_passed() const {
201 return std::chrono::duration_cast<DURATION>(Clock::now() - source_timestamp_);
202 }
203
211 template <typename DURATION = std::chrono::microseconds>
212 DURATION time_since(TimePoint reference) const {
213 return std::chrono::duration_cast<DURATION>(Clock::now() - reference);
214 }
215
221 uint64_t hardware_timestamp() const;
222 void set_hardware_timestamp(uint64_t t);
223
228 void CloneTimestamps(const Data& data);
229
239 virtual void SerializeBinary(std::ostream& stream, Serialization::Format format) const;
240
245 virtual std::string_view serialized_type_name() const { return "Unknown"; }
246
256 virtual void SerializeYAML(YAML::Node& node, Serialization::Format format) const;
257
269 virtual void YAMLDescription(YAML::Node& node, Serialization::Format format) const;
270
280 virtual void SerializeFlatBuffer(flexbuffers::Builder& flex_builder);
281
282 protected:
288 TimePoint source_timestamp_;
289 uint64_t hardware_timestamp_; // e.g. from Neuralynx
290 uint64_t serial_number_;
291};
292
297
299 public:
307 void Validate(const Data& _) const {}
308};
309
310} // namespace nsAnyType
311
328using AnyType =
The base class of AnyType::Data.
Definition idata.hpp:44
The parent data type of AnyType.
Definition idata.hpp:49
Convenient template class to define data types.
Definition idata.hpp:75
Base class of all data object classes.
Definition idata.hpp:95
The datatype capabilities is used to restrict the data object parameterizations that an input port on...
Definition idata.hpp:298
void Validate(const Data &_) const
Validate method is used to validate incoming data objects against the capabilities....
Definition idata.hpp:307
Definition idata.hpp:129
int64_t ingestion_tsc_
CPU timestamp counter when the data packet was first ingested by Falcon.
Definition idata.hpp:287
virtual void YAMLDescription(YAML::Node &node, Serialization::Format format) const
YAMLDescription - Add the datatype metadata (list of data with type and size) to the header used for ...
Definition idata.cpp:102
void set_source_timestamp()
set_source_timestamp set the timepoint based on the internal Falcon clock.
Definition idata.cpp:44
virtual std::string_view serialized_type_name() const
Identifies the underlying dynamic type for binary serialization.
Definition idata.hpp:245
static const std::string static_datatype()
datatype
Definition idata.hpp:138
uint64_t hardware_timestamp() const
hardware_timestamp timestamp given by the hardware. The unit depends on the hardware.
Definition idata.cpp:56
virtual void SerializeYAML(YAML::Node &node, Serialization::Format format) const
SerializeYAML - Serialize data specific for the data type in yaml form according to the layout descri...
Definition idata.cpp:89
DURATION time_since(TimePoint reference) const
Definition idata.hpp:212
DURATION time_passed() const
Definition idata.hpp:200
void CloneTimestamps(const Data &data)
CloneTimestamps - copy timestamps from one packet to the other.
Definition idata.cpp:64
void forward_ingestion_tsc(const Data &data)
Forward the ingestion timestamp from an upstream data packet to this data packet.
Definition idata.cpp:40
static const std::string static_dataname()
dataname
Definition idata.hpp:143
virtual void ClearData()
ClearData class is called each time a data packet is claimed on the output port.
Definition idata.hpp:150
TimePoint source_timestamp() const
source_timestamp getter for the source_timestamp_. This timestamp is set by the internal Falcon clock...
Definition idata.cpp:52
virtual void SerializeBinary(std::ostream &stream, Serialization::Format format) const
SerializeBinary - Serialize data specific for the data type in binary form according to the layout de...
Definition idata.cpp:69
int64_t ingestion_tsc() const
This timestamp is set when the data packet is first ingested by Falcon.
Definition idata.cpp:32
void set_ingestion_tsc()
Set the ingestion timestamp to now. This should only be called by a "source" processor when the data ...
Definition idata.cpp:36
virtual void SerializeFlatBuffer(flexbuffers::Builder &flex_builder)
SerializeFlatBuffer - Serialize with a flexbuffer the data specific for each datatype.
Definition idata.cpp:112
The Parameters structure is a container for parameters used to initialize data object and provide an ...
Definition idata.hpp:119