Falcon
Loading...
Searching...
No Matches
scalardata.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 <limits>
24#include <string>
25
26#include "idata.hpp"
27#include "utilities/string.hpp"
28
29#define DEFAULT_SCALAR_VALUE 0 // can be used in a template
30
31namespace nsScalarType {
32
33using Base = AnyType;
34
35template <typename TYPE>
36struct Parameters {
37 Parameters(TYPE value) : default_value(value) {}
38 TYPE default_value;
39};
40
41template <typename TYPE>
42class Data : public IData<Data<TYPE>, Base> {
43 public:
44 ScalarData(TYPE data = DEFAULT_SCALAR_VALUE) : default_value_(data), data_(data) {}
45 ScalarData(const Parameters<TYPE>& parameters) : ScalarData(parameters.default_value) {}
46
47 static const std::string static_datatype() {
48 return "scalar [" + get_type_string<TYPE>() + "]";
49 }
50 static const std::string static_dataname() { return "data"; }
51
52 Parameters parameters() const { return Parameters(default_value_); }
53
54 void ClearData() override { data_ = default_value_; }
55
56 TYPE const& default_value() const { return default_value_; }
57 TYPE const& data() const { return data_; }
58
59 void set_data(const TYPE& data) { data_ = data; }
60
61 void set_data(const Data<TYPE>& source) { data_ = source.data(); }
62
63 friend bool operator==(Data<TYPE>& a, Data<TYPE>& b) { return a.data == b.data; }
64
65 friend bool operator!=(Data<TYPE>& a, Data<TYPE>& b) { return a.data != b.data; }
66
67 void SerializeBinary(std::ostream& stream, Serialization::Format format =
68 Serialization::Format::FULL) const override {
69 Base::SerializeBinary(stream, format);
70 if (format == Serialization::Format::FULL || format == Serialization::Format::COMPACT) {
71 stream.write(reinterpret_cast<const char*>(&data_), sizeof(TYPE));
72 }
73 }
74
75 void SerializeYAML(YAML::Node& node,
76 Serialization::Format format = Serialization::Format::FULL) const override {
77 Base::SerializeYAML(node, format);
78 if (format == Serialization::Format::FULL || format == Serialization::Format::COMPACT) {
79 node["scalar_data"] = data_;
80 }
81 }
82
83 void YAMLDescription(YAML::Node& node, Serialization::Format format =
84 Serialization::Format::FULL) const override {
85 Base::YAMLDescription(node, format);
86 if (format == Serialization::Format::FULL || format == Serialization::Format::COMPACT) {
87 node.push_back("scalar_data " + get_type_string<TYPE>() + " (1)");
88 }
89 }
90
91 protected:
92 TYPE default_value_;
93 TYPE data_;
94};
95
96using Capabilities = Base::Capabilities;
97
98} // namespace nsScalarType
99
100template <typename T>
101using ScalarType = DefineType<nsScalarType::Data<T>, AnyType, true, nsScalarType::Capabilities,
Convenient template class to define data types.
Definition idata.hpp:75
Base class of all data object classes.
Definition idata.hpp:95
Definition scalardata.hpp:42
Definition scalardata.hpp:36