Falcon
Loading...
Searching...
No Matches
serializer.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 <cctype>
24#include <ostream>
25#include <string>
26
27#include "yaml-cpp/yaml.h"
28
29#include "datatype_generated.h"
30#include "idata.hpp"
31#include "serialization.hpp"
32
33namespace Serialization {
34
35class Serializer {
36 public:
37 Serializer(Format fmt = Format::FULL, std::string description = "", std::string extension = "")
38 : format_(fmt), description_(description), extension_(extension) {}
39
40 virtual bool Serialize(std::ostream& stream, typename AnyType::Data* data, uint16_t streamid,
41 uint64_t packetid, std::string processor, std::string port,
42 uint8_t slot) = 0;
43 Format format() const;
44 void set_format(Format fmt);
45
46 YAML::Node DataDescription(const typename AnyType::Data& data) const;
47
48 std::string description() const;
49 std::string extension() const;
50
51 protected:
52 Format format_;
53 std::string description_;
54 std::string extension_;
55};
56
57class BinarySerializer : public Serializer {
58 public:
59 BinarySerializer(Format fmt = Format::FULL) : Serializer(fmt, "Compact binary format", "bin") {}
60
61 bool Serialize(std::ostream& stream, typename AnyType::Data* data, uint16_t streamid,
62 uint64_t packetid, std::string processor, std::string port, uint8_t slot);
63};
64
65class FlatBufferSerializer : public Serializer {
66 public:
67 FlatBufferSerializer(Format fmt = Format::FULL)
68 : Serializer(fmt, "Flatbuffer format", "bin"), builder_(1024) {}
69
70 bool Serialize(std::ostream& stream, typename AnyType::Data* data, uint16_t streamid,
71 uint64_t packetid, std::string processor, std::string port, uint8_t slot);
72
73 private:
74 flatbuffers::FlatBufferBuilder builder_;
75 flexbuffers::Builder flex_builder_;
76};
77
78class YAMLSerializer : public Serializer {
79 public:
80 YAMLSerializer(Format fmt = Format::FULL)
81 : Serializer(fmt, "Human readable YAML format", "yaml") {}
82
83 bool Serialize(std::ostream& stream, typename AnyType::Data* data, uint16_t streamid,
84 uint64_t packetid, std::string processor, std::string port, uint8_t slot);
85};
86
87Serializer* serializer_from_string(std::string s, Serialization::Format fmt = Format::FULL);
88
89Serializer* serializer(Serialization::Encoding enc, Serialization::Format fmt = Format::FULL);
90
91} // namespace Serialization
Definition serializer.hpp:35