Falcon
Loading...
Searching...
No Matches
streaminfo.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 <any>
23#include <limits>
24#include <stdexcept>
25
26#include "idata.hpp"
27
28const double IRREGULARSTREAM = std::numeric_limits<double>::min();
29
30// Represents the data stream generated by an output port
31class IStreamInfo {
32 public:
33 IStreamInfo(double stream_rate = IRREGULARSTREAM, std::string datatype = "none",
34 std::string stream_name = "data")
35 : stream_rate_(stream_rate), datatype_(datatype), stream_name_(stream_name) {}
36
37 bool finalized() const;
38 virtual void Finalize() = 0;
39 double stream_rate() const;
40 void set_stream_rate(double stream_rate);
41 void set_stream_rate(const IStreamInfo& info);
42 std::string datatype() const;
43
44 std::string stream_name() const { return stream_name_; };
45 void set_stream_name(const std::string& stream_name);
46 void set_stream_name(const IStreamInfo& info);
47
48 void set_stream_parameters(const IStreamInfo& info);
49 void set_stream_parameters(double stream_rate, const std::string& stream_name);
50
51 template <typename T = typename AnyType::Data>
52 const T& getDataPrototype() const {
53 // todo: throw exception when not finalized
54 return dynamic_cast<const T&>(*prototype_);
55 }
56
57 template <typename T>
58 const T& parameters() const {
59 return std::any_cast<const T&>(parameters_);
60 }
61
62 protected:
63 double stream_rate_ = 1.0;
64 std::string datatype_ = "none";
65 std::string stream_name_ = "data";
66 bool finalized_ = false;
67 std::unique_ptr<AnyType::Data> prototype_;
68 std::any parameters_;
69};
70
71// Data type specific implementation of stream info
72template <typename DATATYPE>
73class StreamInfo : public IStreamInfo {
74 public:
75 StreamInfo(const typename DATATYPE::Parameters& parameters,
76 double stream_rate = IRREGULARSTREAM, std::string name = "data")
77 : IStreamInfo(stream_rate, DATATYPE::datatype(), name) {
78 parameters_ = parameters;
79 }
80
81 const typename DATATYPE::Parameters parameters() const {
82 return std::any_cast<const typename DATATYPE::Parameters&>(parameters_);
83 }
84
85 void Finalize() override {
86 finalized_ = true;
87
88 auto prototype = std::make_unique<typename DATATYPE::Data>(this->parameters());
89
90 prototype_ = std::move(prototype);
91 }
92
93 void set_parameters(const typename DATATYPE::Parameters& parameters) {
94 if (finalized()) {
95 throw std::runtime_error("Stream information is finalized. Cannot change parameters.");
96 }
97 parameters_ = parameters;
98 }
99
100 void set_parameters(const StreamInfo<DATATYPE>& info) { set_parameters(info.parameters()); }
101};