00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef Tanl_POS_serialization_H
00025 #define Tanl_POS_serialization_H
00026
00027 #include <iostream>
00028 #include <map>
00029 #include <string>
00030 #include <vector>
00031
00032 namespace Tanl { namespace POS {
00033
00034
00035
00036
00037
00038
00039 template<class _T>
00040 void serialize(const _T& x, std::ostream& out)
00041 {
00042 # ifdef DEBUG
00043 std::cerr << x << std::endl;
00044 # endif
00045 out.write((char*)&x, sizeof(_T));
00046 }
00047
00048
00049 template<class _T>
00050 void serialize(_T& x, std::istream& in)
00051 {
00052 in.read((char*)&x, sizeof(_T));
00053 }
00054
00055 template<>
00056 void serialize<std::pair<int, int> >(const std::pair<int, int>& x, std::ostream& out);
00057
00058 template<>
00059 void serialize<std::pair<int, int> >(std::pair<int, int>& x, std::istream& in);
00060
00061 template<>
00062 void serialize<std::string>(const std::string& x, std::ostream& out);
00063
00064 template<>
00065 void serialize<std::string>(std::string& x, std::istream& in);
00066
00067 template<class _K, class _T>
00068 void serialize(const std::map<_K, _T>& x, std::ostream& out)
00069 {
00070 std::size_t size = x.size();
00071 out.write((char*)&size, sizeof(size));
00072 typename std::map<_K, _T>::const_iterator it = x.begin();
00073 for (; it != x.end(); it++) {
00074 serialize(it->first, out);
00075 serialize(it->second, out);
00076 }
00077 }
00078
00079 template<class _K, class _T>
00080 void serialize(std::map<_K, _T>& x, std::istream& in)
00081 {
00082 std::size_t size = 0;
00083 in.read((char*)&size, sizeof(size));
00084 while (size-- > 0) {
00085 _K first;
00086 serialize(first, in);
00087 _T second;
00088 serialize(second, in);
00089 x[first] = second;
00090 }
00091 }
00092
00093 template<class _T>
00094 void serialize(const std::vector<_T>& x, std::ostream& out)
00095 {
00096 std::size_t size = x.size();
00097 out.write((char *)(&size), sizeof(std::size_t));
00098 typename std::vector<_T>::const_iterator it = x.begin();
00099 for (; it != x.end(); it++)
00100 serialize(*it, out);
00101 }
00102
00103 template<class _T>
00104 void serialize(std::vector<_T>& x, std::istream& in)
00105 {
00106 std::size_t size = 0;
00107 in.read((char *)(&size), sizeof(std::size_t));
00108 while (size-- > 0) {
00109 _T t;
00110 serialize(t, in);
00111 x.push_back(t);
00112 }
00113 }
00114
00115 }
00116 }
00117
00118 #endif // Tanl_POS_serialization_H
00119