Line data Source code
1 : /*******************************************************************************
2 : * *
3 : * Cosmos:(C)oncept et (O)utils (S)tatistique pour les (Mo)deles *
4 : * (S)tochastiques *
5 : * *
6 : * Copyright (C) 2009-2012 LSV & LACL *
7 : * Authors: Paolo Ballarini BenoƮt Barbot & Hilal Djafri *
8 : * Website: http://www.lsv.ens-cachan.fr/Software/cosmos *
9 : * *
10 : * This program is free software; you can redistribute it and/or modify *
11 : * it under the terms of the GNU General Public License as published by *
12 : * the Free Software Foundation; either version 3 of the License, or *
13 : * (at your option) any later version. *
14 : * *
15 : * This program is distributed in the hope that it will be useful, *
16 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 : * GNU General Public License for more details. *
19 : * *
20 : * You should have received a copy of the GNU General Public License along *
21 : * with this program; if not, write to the Free Software Foundation, Inc., *
22 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
23 : * file SimpleSerializer.hpp *
24 : * Created by Benoit Barbot on 04/005/2018. *
25 : *******************************************************************************
26 : */
27 :
28 : #ifndef SimpleSerializer_hpp
29 : #define SimpleSerializer_hpp
30 :
31 : #include <stdio.h>
32 : #include <iostream>
33 : #include <map>
34 :
35 : class Serializer{
36 : private:
37 : std::ostream &of;
38 : public:
39 34 : Serializer(std::ostream &f):of(f){};
40 : Serializer operator&(bool d);
41 : Serializer operator&(double d);
42 : Serializer operator&(int d);
43 : Serializer operator&(unsigned int d);
44 : Serializer operator&(long d);
45 : Serializer operator&(unsigned long d);
46 : Serializer operator&(const std::string &d);
47 : template <class T, class Q>
48 68 : Serializer operator&(const std::map<T,Q> &m){
49 68 : of << m.size() << " ";
50 104 : for( const auto & key : m ){
51 36 : *this & key.first;
52 36 : *this & key.second;
53 : }
54 68 : return *this;
55 : };
56 : };
57 :
58 : class HumanReadable{
59 : private:
60 : std::ostream &of;
61 : public:
62 : HumanReadable(std::ostream &f):of(f){};
63 : HumanReadable operator&(bool d);
64 : HumanReadable operator&(double d);
65 : HumanReadable operator&(int d);
66 : HumanReadable operator&(unsigned int d);
67 : HumanReadable operator&(long d);
68 : HumanReadable operator&(unsigned long d);
69 : HumanReadable operator&(const std::string &d);
70 : template <class T, class Q>
71 : HumanReadable operator&(const std::map<T,Q> &m){
72 : of << "dictionary:{";
73 : for( const auto & key : m ){
74 : *this & key.first;
75 : of << " : ";
76 : *this & key.second;
77 : of << "; ";
78 : }
79 : of << "}";
80 : return *this;
81 : };
82 : };
83 :
84 : class Unserializer{
85 : private:
86 : std::istream &inf;
87 : public:
88 29 : Unserializer(std::istream &f):inf(f){};
89 : Unserializer operator&(bool &d);
90 : Unserializer operator&(double &d);
91 : Unserializer operator&(int &d);
92 : Unserializer operator&(unsigned int &d);
93 : Unserializer operator&(long &d);
94 : Unserializer operator&(unsigned long &d);
95 : Unserializer operator&(std::string &d);
96 : template <class T, class Q>
97 58 : Unserializer operator&(std::map<T,Q> &m){
98 : size_t length;
99 58 : inf >> length;
100 90 : for( size_t i=0; i< length; i++){
101 64 : T key;
102 6 : Q value;
103 32 : *this & key;
104 32 : *this & value;
105 32 : m.insert(std::pair<T,Q>(key,value));
106 : }
107 58 : return *this;
108 : };
109 : };
110 :
111 :
112 : #endif /* SimpleSerializer_hpp */
|