1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci
3141cc406Sopenharmony_ci   Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4141cc406Sopenharmony_ci
5141cc406Sopenharmony_ci   This file is part of the SANE package.
6141cc406Sopenharmony_ci
7141cc406Sopenharmony_ci   This program is free software; you can redistribute it and/or
8141cc406Sopenharmony_ci   modify it under the terms of the GNU General Public License as
9141cc406Sopenharmony_ci   published by the Free Software Foundation; either version 2 of the
10141cc406Sopenharmony_ci   License, or (at your option) any later version.
11141cc406Sopenharmony_ci
12141cc406Sopenharmony_ci   This program is distributed in the hope that it will be useful, but
13141cc406Sopenharmony_ci   WITHOUT ANY WARRANTY; without even the implied warranty of
14141cc406Sopenharmony_ci   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15141cc406Sopenharmony_ci   General Public License for more details.
16141cc406Sopenharmony_ci
17141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
18141cc406Sopenharmony_ci   along with this program.  If not, see <https://www.gnu.org/licenses/>.
19141cc406Sopenharmony_ci*/
20141cc406Sopenharmony_ci
21141cc406Sopenharmony_ci#ifndef BACKEND_GENESYS_SERIALIZE_H
22141cc406Sopenharmony_ci#define BACKEND_GENESYS_SERIALIZE_H
23141cc406Sopenharmony_ci
24141cc406Sopenharmony_ci#include "error.h"
25141cc406Sopenharmony_ci#include <array>
26141cc406Sopenharmony_ci#include <iostream>
27141cc406Sopenharmony_ci#include <limits>
28141cc406Sopenharmony_ci#include <string>
29141cc406Sopenharmony_ci#include <vector>
30141cc406Sopenharmony_ci
31141cc406Sopenharmony_cinamespace genesys {
32141cc406Sopenharmony_ci
33141cc406Sopenharmony_ci// it would be best to use something like boost.serialization
34141cc406Sopenharmony_ci
35141cc406Sopenharmony_ciinline void serialize_newline(std::ostream& str) { str << '\n'; }
36141cc406Sopenharmony_ciinline void serialize_newline(std::istream& str) { (void) str; }
37141cc406Sopenharmony_ci
38141cc406Sopenharmony_ciinline void serialize(std::ostream& str, bool x) { str << static_cast<unsigned>(x) << " "; }
39141cc406Sopenharmony_ciinline void serialize(std::istream& str, bool& x) { unsigned v; str >> v; x = v; }
40141cc406Sopenharmony_ciinline void serialize(std::ostream& str, char x) { str << static_cast<int>(x) << " "; }
41141cc406Sopenharmony_ciinline void serialize(std::istream& str, char& x) { int v; str >> v; x = v; }
42141cc406Sopenharmony_ciinline void serialize(std::ostream& str, unsigned char x) { str << static_cast<unsigned>(x) << " "; }
43141cc406Sopenharmony_ciinline void serialize(std::istream& str, unsigned char& x) { unsigned v; str >> v; x = v; }
44141cc406Sopenharmony_ciinline void serialize(std::ostream& str, signed char x) { str << static_cast<int>(x) << " "; }
45141cc406Sopenharmony_ciinline void serialize(std::istream& str, signed char& x) { int v; str >> v; x = v; }
46141cc406Sopenharmony_ciinline void serialize(std::ostream& str, short x) { str << x << " "; }
47141cc406Sopenharmony_ciinline void serialize(std::istream& str, short& x) { str >> x; }
48141cc406Sopenharmony_ciinline void serialize(std::ostream& str, unsigned short x) { str << x << " "; }
49141cc406Sopenharmony_ciinline void serialize(std::istream& str, unsigned short& x) { str >> x; }
50141cc406Sopenharmony_ciinline void serialize(std::ostream& str, int x) { str << x << " "; }
51141cc406Sopenharmony_ciinline void serialize(std::istream& str, int& x) { str >> x; }
52141cc406Sopenharmony_ciinline void serialize(std::ostream& str, unsigned int x) { str << x << " "; }
53141cc406Sopenharmony_ciinline void serialize(std::istream& str, unsigned int& x) { str >> x; }
54141cc406Sopenharmony_ciinline void serialize(std::ostream& str, long x) { str << x << " "; }
55141cc406Sopenharmony_ciinline void serialize(std::istream& str, long& x) { str >> x; }
56141cc406Sopenharmony_ciinline void serialize(std::ostream& str, unsigned long x) { str << x << " "; }
57141cc406Sopenharmony_ciinline void serialize(std::istream& str, unsigned long& x) { str >> x; }
58141cc406Sopenharmony_ciinline void serialize(std::ostream& str, long long x) { str << x << " "; }
59141cc406Sopenharmony_ciinline void serialize(std::istream& str, long long& x) { str >> x; }
60141cc406Sopenharmony_ciinline void serialize(std::ostream& str, unsigned long long x) { str << x << " "; }
61141cc406Sopenharmony_ciinline void serialize(std::istream& str, unsigned long long& x) { str >> x; }
62141cc406Sopenharmony_ciinline void serialize(std::ostream& str, float x) { str << x << " "; }
63141cc406Sopenharmony_ciinline void serialize(std::istream& str, float& x) { str >> x; }
64141cc406Sopenharmony_ciinline void serialize(std::ostream& str, double x) { str << x << " "; }
65141cc406Sopenharmony_ciinline void serialize(std::istream& str, double& x) { str >> x; }
66141cc406Sopenharmony_ciinline void serialize(std::ostream& str, const std::string& x) { str << x << " "; }
67141cc406Sopenharmony_ciinline void serialize(std::istream& str, std::string& x) { str >> x; }
68141cc406Sopenharmony_ci
69141cc406Sopenharmony_citemplate<class T>
70141cc406Sopenharmony_civoid serialize(std::ostream& str, std::vector<T>& x)
71141cc406Sopenharmony_ci{
72141cc406Sopenharmony_ci    serialize(str, x.size());
73141cc406Sopenharmony_ci    serialize_newline(str);
74141cc406Sopenharmony_ci
75141cc406Sopenharmony_ci    for (auto& item : x) {
76141cc406Sopenharmony_ci        serialize(str, item);
77141cc406Sopenharmony_ci        serialize_newline(str);
78141cc406Sopenharmony_ci    }
79141cc406Sopenharmony_ci}
80141cc406Sopenharmony_ci
81141cc406Sopenharmony_citemplate<class T>
82141cc406Sopenharmony_civoid serialize(std::istream& str, std::vector<T>& x,
83141cc406Sopenharmony_ci               size_t max_size = std::numeric_limits<size_t>::max())
84141cc406Sopenharmony_ci{
85141cc406Sopenharmony_ci    size_t new_size;
86141cc406Sopenharmony_ci    serialize(str, new_size);
87141cc406Sopenharmony_ci
88141cc406Sopenharmony_ci    if (new_size > max_size) {
89141cc406Sopenharmony_ci        throw SaneException("Too large std::vector to deserialize");
90141cc406Sopenharmony_ci    }
91141cc406Sopenharmony_ci    x.reserve(new_size);
92141cc406Sopenharmony_ci    for (size_t i = 0; i < new_size; ++i) {
93141cc406Sopenharmony_ci        T item;
94141cc406Sopenharmony_ci        serialize(str, item);
95141cc406Sopenharmony_ci        x.push_back(item);
96141cc406Sopenharmony_ci    }
97141cc406Sopenharmony_ci}
98141cc406Sopenharmony_ci
99141cc406Sopenharmony_citemplate<class T, size_t Size>
100141cc406Sopenharmony_civoid serialize(std::ostream& str, std::array<T, Size>& x)
101141cc406Sopenharmony_ci{
102141cc406Sopenharmony_ci    serialize(str, x.size());
103141cc406Sopenharmony_ci    serialize_newline(str);
104141cc406Sopenharmony_ci
105141cc406Sopenharmony_ci    for (auto& item : x) {
106141cc406Sopenharmony_ci        serialize(str, item);
107141cc406Sopenharmony_ci        serialize_newline(str);
108141cc406Sopenharmony_ci    }
109141cc406Sopenharmony_ci}
110141cc406Sopenharmony_ci
111141cc406Sopenharmony_citemplate<class T, size_t Size>
112141cc406Sopenharmony_civoid serialize(std::istream& str, std::array<T, Size>& x)
113141cc406Sopenharmony_ci{
114141cc406Sopenharmony_ci    size_t new_size;
115141cc406Sopenharmony_ci    serialize(str, new_size);
116141cc406Sopenharmony_ci
117141cc406Sopenharmony_ci    if (new_size > Size) {
118141cc406Sopenharmony_ci        throw SaneException("Incorrect std::array size to deserialize");
119141cc406Sopenharmony_ci    }
120141cc406Sopenharmony_ci    for (auto& item : x) {
121141cc406Sopenharmony_ci        serialize(str, item);
122141cc406Sopenharmony_ci    }
123141cc406Sopenharmony_ci}
124141cc406Sopenharmony_ci
125141cc406Sopenharmony_ci} // namespace genesys
126141cc406Sopenharmony_ci
127141cc406Sopenharmony_ci#endif
128