Falcon
Loading...
Searching...
No Matches
vectordata.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 <algorithm>
23#include <string>
24#include <vector>
25
26#include "idata.hpp"
27
28namespace nsVectorType {
29using ParentType = AnyType;
30
31struct Parameters {
32 Parameters(unsigned int n) : size(n) {}
33 unsigned int size;
34};
35
36template <typename T>
37class Data : public IData<Data<T>, ParentType> {
38 public:
39 using BaseClass = IData<Data<T>, ParentType>;
40
41 Data(unsigned int n) {
42 if (n == 0) {
43 throw std::runtime_error("Vector size cannot be zero.");
44 }
45 data_.resize(n);
46 }
47 Data(const Parameters& parameters) : Data(parameters.size) {}
48
49 static const std::string static_datatype() { return "vector [" + get_type_string<T>() + "]"; }
50 static const std::string static_dataname() { return "data"; }
51
52 Parameters parameters() const { return Parameters(data_.size()); }
53
54 void setData(const std::vector<T>& data) {
55 if (data.size() != data_.size()) {
56 throw std::runtime_error("Setting vector data from wrong size source vector");
57 }
58 data_ = data; // copy
59 }
60
61 void setData(const T* data, int len) {
62 if (len != data_.size()) {
63 throw std::runtime_error("Setting vector data from wrong size source data");
64 }
65 std::copy(data, data + len, data_.begin());
66 }
67
68 void setSample(int index, const T& data) { data_[index] = data; }
69
70 std::vector<T>& data() { return data_; }
71
72 void SerializeBinary(std::ostream& stream, Serialization::Format format =
73 Serialization::Format::FULL) const override {
74 BaseClass::SerializeBinary(stream, format);
75 if (format == Serialization::Format::FULL || format == Serialization::Format::COMPACT) {
76 stream.write(reinterpret_cast<const char*>(data_.data()), data_.size() * sizeof(T));
77 }
78 }
79
80 void SerializeYAML(YAML::Node& node,
81 Serialization::Format format = Serialization::Format::FULL) const override {
82 BaseClass::SerializeYAML(node, format);
83 if (format == Serialization::Format::FULL || format == Serialization::Format::COMPACT) {
84 node["data"] = data_;
85 }
86 }
87
88 void YAMLDescription(YAML::Node& node, Serialization::Format format =
89 Serialization::Format::FULL) const override {
90 BaseClass::YAMLDescription(node, format);
91 if (format == Serialization::Format::FULL || format == Serialization::Format::COMPACT) {
92 node.push_back("data " + get_type_string<T>() + " (" + std::to_string(data_.size()) +
93 ")");
94 }
95 }
96
97 protected:
98 std::vector<T> data_;
99};
100
101using Capabilities = ParentType::Capabilities;
102
103} // namespace nsVectorType
104
105template <typename T>
106using VectorType = DefineType<nsVectorType::Data<T>, AnyType, true, nsVectorType::Capabilities,
Convenient template class to define data types.
Definition idata.hpp:75
Base class of all data object classes.
Definition idata.hpp:95
Definition vectordata.hpp:31