1 /* sane - Scanner Access Now Easy.
2
3 Copyright (C) 2019 Povilas Kanapickas <povilas@radix.lt>
4
5 This file is part of the SANE package.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #ifndef BACKEND_GENESYS_SERIALIZE_H
22 #define BACKEND_GENESYS_SERIALIZE_H
23
24 #include "error.h"
25 #include <array>
26 #include <iostream>
27 #include <limits>
28 #include <string>
29 #include <vector>
30
31 namespace genesys {
32
33 // it would be best to use something like boost.serialization
34
serialize_newline(std::ostream & str)35 inline void serialize_newline(std::ostream& str) { str << '\n'; }
serialize_newline(std::istream & str)36 inline void serialize_newline(std::istream& str) { (void) str; }
37
serialize(std::ostream & str,bool x)38 inline void serialize(std::ostream& str, bool x) { str << static_cast<unsigned>(x) << " "; }
serialize(std::istream & str,bool & x)39 inline void serialize(std::istream& str, bool& x) { unsigned v; str >> v; x = v; }
serialize(std::ostream & str,char x)40 inline void serialize(std::ostream& str, char x) { str << static_cast<int>(x) << " "; }
serialize(std::istream & str,char & x)41 inline void serialize(std::istream& str, char& x) { int v; str >> v; x = v; }
serialize(std::ostream & str,unsigned char x)42 inline void serialize(std::ostream& str, unsigned char x) { str << static_cast<unsigned>(x) << " "; }
serialize(std::istream & str,unsigned char & x)43 inline void serialize(std::istream& str, unsigned char& x) { unsigned v; str >> v; x = v; }
serialize(std::ostream & str,signed char x)44 inline void serialize(std::ostream& str, signed char x) { str << static_cast<int>(x) << " "; }
serialize(std::istream & str,signed char & x)45 inline void serialize(std::istream& str, signed char& x) { int v; str >> v; x = v; }
serialize(std::ostream & str,short x)46 inline void serialize(std::ostream& str, short x) { str << x << " "; }
serialize(std::istream & str,short & x)47 inline void serialize(std::istream& str, short& x) { str >> x; }
serialize(std::ostream & str,unsigned short x)48 inline void serialize(std::ostream& str, unsigned short x) { str << x << " "; }
serialize(std::istream & str,unsigned short & x)49 inline void serialize(std::istream& str, unsigned short& x) { str >> x; }
serialize(std::ostream & str,int x)50 inline void serialize(std::ostream& str, int x) { str << x << " "; }
serialize(std::istream & str,int & x)51 inline void serialize(std::istream& str, int& x) { str >> x; }
serialize(std::ostream & str,unsigned int x)52 inline void serialize(std::ostream& str, unsigned int x) { str << x << " "; }
serialize(std::istream & str,unsigned int & x)53 inline void serialize(std::istream& str, unsigned int& x) { str >> x; }
serialize(std::ostream & str,long x)54 inline void serialize(std::ostream& str, long x) { str << x << " "; }
serialize(std::istream & str,long & x)55 inline void serialize(std::istream& str, long& x) { str >> x; }
serialize(std::ostream & str,unsigned long x)56 inline void serialize(std::ostream& str, unsigned long x) { str << x << " "; }
serialize(std::istream & str,unsigned long & x)57 inline void serialize(std::istream& str, unsigned long& x) { str >> x; }
serialize(std::ostream & str,long long x)58 inline void serialize(std::ostream& str, long long x) { str << x << " "; }
serialize(std::istream & str,long long & x)59 inline void serialize(std::istream& str, long long& x) { str >> x; }
serialize(std::ostream & str,unsigned long long x)60 inline void serialize(std::ostream& str, unsigned long long x) { str << x << " "; }
serialize(std::istream & str,unsigned long long & x)61 inline void serialize(std::istream& str, unsigned long long& x) { str >> x; }
serialize(std::ostream & str,float x)62 inline void serialize(std::ostream& str, float x) { str << x << " "; }
serialize(std::istream & str,float & x)63 inline void serialize(std::istream& str, float& x) { str >> x; }
serialize(std::ostream & str,double x)64 inline void serialize(std::ostream& str, double x) { str << x << " "; }
serialize(std::istream & str,double & x)65 inline void serialize(std::istream& str, double& x) { str >> x; }
serialize(std::ostream & str,const std::string & x)66 inline void serialize(std::ostream& str, const std::string& x) { str << x << " "; }
serialize(std::istream & str,std::string & x)67 inline void serialize(std::istream& str, std::string& x) { str >> x; }
68
69 template<class T>
serialize(std::ostream & str,std::vector<T> & x)70 void serialize(std::ostream& str, std::vector<T>& x)
71 {
72 serialize(str, x.size());
73 serialize_newline(str);
74
75 for (auto& item : x) {
76 serialize(str, item);
77 serialize_newline(str);
78 }
79 }
80
81 template<class T>
82 void serialize(std::istream& str, std::vector<T>& x,
83 size_t max_size = std::numeric_limits<size_t>::max())
84 {
85 size_t new_size;
86 serialize(str, new_size);
87
88 if (new_size > max_size) {
89 throw SaneException("Too large std::vector to deserialize");
90 }
91 x.reserve(new_size);
92 for (size_t i = 0; i < new_size; ++i) {
93 T item;
94 serialize(str, item);
95 x.push_back(item);
96 }
97 }
98
99 template<class T, size_t Size>
serialize(std::ostream & str,std::array<T,Size> & x)100 void serialize(std::ostream& str, std::array<T, Size>& x)
101 {
102 serialize(str, x.size());
103 serialize_newline(str);
104
105 for (auto& item : x) {
106 serialize(str, item);
107 serialize_newline(str);
108 }
109 }
110
111 template<class T, size_t Size>
serialize(std::istream & str,std::array<T,Size> & x)112 void serialize(std::istream& str, std::array<T, Size>& x)
113 {
114 size_t new_size;
115 serialize(str, new_size);
116
117 if (new_size > Size) {
118 throw SaneException("Incorrect std::array size to deserialize");
119 }
120 for (auto& item : x) {
121 serialize(str, item);
122 }
123 }
124
125 } // namespace genesys
126
127 #endif
128