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    As a special exception, the authors of SANE give permission for
21    additional uses of the libraries contained in this release of SANE.
22 
23    The exception is that, if you link a SANE library with other files
24    to produce an executable, this does not by itself cause the
25    resulting executable to be covered by the GNU General Public
26    License.  Your use of that executable is in no way restricted on
27    account of linking the SANE library code into it.
28 
29    This exception does not, however, invalidate any other reasons why
30    the executable file might be covered by the GNU General Public
31    License.
32 
33    If you submit changes to SANE to the maintainers to be included in
34    a subsequent release, you agree by submitting the changes that
35    those changes may be distributed with this exception intact.
36 
37    If you write modifications of your own for SANE, it is your choice
38    whether to permit this exception to apply to your modifications.
39    If you do not wish that, delete this exception notice.
40 */
41 
42 #ifndef BACKEND_GENESYS_SERIALIZE_H
43 #define BACKEND_GENESYS_SERIALIZE_H
44 
45 #include "error.h"
46 #include <array>
47 #include <iostream>
48 #include <limits>
49 #include <string>
50 #include <vector>
51 
52 namespace genesys {
53 
54 // it would be best to use something like boost.serialization
55 
serialize_newline(std::ostream & str)56 inline void serialize_newline(std::ostream& str) { str << '\n'; }
serialize_newline(std::istream & str)57 inline void serialize_newline(std::istream& str) { (void) str; }
58 
serialize(std::ostream & str,bool x)59 inline void serialize(std::ostream& str, bool x) { str << static_cast<unsigned>(x) << " "; }
serialize(std::istream & str,bool & x)60 inline void serialize(std::istream& str, bool& x) { unsigned v; str >> v; x = v; }
serialize(std::ostream & str,char x)61 inline void serialize(std::ostream& str, char x) { str << static_cast<int>(x) << " "; }
serialize(std::istream & str,char & x)62 inline void serialize(std::istream& str, char& x) { int v; str >> v; x = v; }
serialize(std::ostream & str,unsigned char x)63 inline void serialize(std::ostream& str, unsigned char x) { str << static_cast<unsigned>(x) << " "; }
serialize(std::istream & str,unsigned char & x)64 inline void serialize(std::istream& str, unsigned char& x) { unsigned v; str >> v; x = v; }
serialize(std::ostream & str,signed char x)65 inline void serialize(std::ostream& str, signed char x) { str << static_cast<int>(x) << " "; }
serialize(std::istream & str,signed char & x)66 inline void serialize(std::istream& str, signed char& x) { int v; str >> v; x = v; }
serialize(std::ostream & str,short x)67 inline void serialize(std::ostream& str, short x) { str << x << " "; }
serialize(std::istream & str,short & x)68 inline void serialize(std::istream& str, short& x) { str >> x; }
serialize(std::ostream & str,unsigned short x)69 inline void serialize(std::ostream& str, unsigned short x) { str << x << " "; }
serialize(std::istream & str,unsigned short & x)70 inline void serialize(std::istream& str, unsigned short& x) { str >> x; }
serialize(std::ostream & str,int x)71 inline void serialize(std::ostream& str, int x) { str << x << " "; }
serialize(std::istream & str,int & x)72 inline void serialize(std::istream& str, int& x) { str >> x; }
serialize(std::ostream & str,unsigned int x)73 inline void serialize(std::ostream& str, unsigned int x) { str << x << " "; }
serialize(std::istream & str,unsigned int & x)74 inline void serialize(std::istream& str, unsigned int& x) { str >> x; }
serialize(std::ostream & str,long x)75 inline void serialize(std::ostream& str, long x) { str << x << " "; }
serialize(std::istream & str,long & x)76 inline void serialize(std::istream& str, long& x) { str >> x; }
serialize(std::ostream & str,unsigned long x)77 inline void serialize(std::ostream& str, unsigned long x) { str << x << " "; }
serialize(std::istream & str,unsigned long & x)78 inline void serialize(std::istream& str, unsigned long& x) { str >> x; }
serialize(std::ostream & str,long long x)79 inline void serialize(std::ostream& str, long long x) { str << x << " "; }
serialize(std::istream & str,long long & x)80 inline void serialize(std::istream& str, long long& x) { str >> x; }
serialize(std::ostream & str,unsigned long long x)81 inline void serialize(std::ostream& str, unsigned long long x) { str << x << " "; }
serialize(std::istream & str,unsigned long long & x)82 inline void serialize(std::istream& str, unsigned long long& x) { str >> x; }
serialize(std::ostream & str,float x)83 inline void serialize(std::ostream& str, float x) { str << x << " "; }
serialize(std::istream & str,float & x)84 inline void serialize(std::istream& str, float& x) { str >> x; }
serialize(std::ostream & str,double x)85 inline void serialize(std::ostream& str, double x) { str << x << " "; }
serialize(std::istream & str,double & x)86 inline void serialize(std::istream& str, double& x) { str >> x; }
serialize(std::ostream & str,const std::string & x)87 inline void serialize(std::ostream& str, const std::string& x) { str << x << " "; }
serialize(std::istream & str,std::string & x)88 inline void serialize(std::istream& str, std::string& x) { str >> x; }
89 
90 template<class T>
serialize(std::ostream & str,std::vector<T> & x)91 void serialize(std::ostream& str, std::vector<T>& x)
92 {
93     serialize(str, x.size());
94     serialize_newline(str);
95 
96     for (auto& item : x) {
97         serialize(str, item);
98         serialize_newline(str);
99     }
100 }
101 
102 template<class T>
103 void serialize(std::istream& str, std::vector<T>& x,
104                size_t max_size = std::numeric_limits<size_t>::max())
105 {
106     size_t new_size;
107     serialize(str, new_size);
108 
109     if (new_size > max_size) {
110         throw SaneException("Too large std::vector to deserialize");
111     }
112     x.reserve(new_size);
113     for (size_t i = 0; i < new_size; ++i) {
114         T item;
115         serialize(str, item);
116         x.push_back(item);
117     }
118 }
119 
120 template<class T, size_t Size>
serialize(std::ostream & str,std::array<T,Size> & x)121 void serialize(std::ostream& str, std::array<T, Size>& x)
122 {
123     serialize(str, x.size());
124     serialize_newline(str);
125 
126     for (auto& item : x) {
127         serialize(str, item);
128         serialize_newline(str);
129     }
130 }
131 
132 template<class T, size_t Size>
serialize(std::istream & str,std::array<T,Size> & x)133 void serialize(std::istream& str, std::array<T, Size>& x)
134 {
135     size_t new_size;
136     serialize(str, new_size);
137 
138     if (new_size > Size) {
139         throw SaneException("Incorrect std::array size to deserialize");
140     }
141     for (auto& item : x) {
142         serialize(str, item);
143     }
144 }
145 
146 } // namespace genesys
147 
148 #endif
149