Tulip  5.1.0
Large graphs analysis and drawing
SerializableType.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 ///@cond DOXYGEN_HIDDEN
20 
21 
22 #ifndef SERIALIZABLETYPE_H
23 #define SERIALIZABLETYPE_H
24 
25 #include <set>
26 #include <vector>
27 #include <string>
28 #include <list>
29 #include <iostream>
30 #include <sstream>
31 #include <float.h>
32 
33 #include <tulip/TypeInterface.h>
34 
35 #define FORWARD_TOSTRING(T) static std::string toString(const T::RealType &v) { std::ostringstream oss; write(oss, v); return oss.str(); }
36 #define FORWARD_FROMSTRING(T) static bool fromString(T::RealType &v, const std::string &s) { std::istringstream iss(s); return read(iss, v); }
37 #define FORWARD_STRING_METHODS(T) FORWARD_FROMSTRING(T) FORWARD_TOSTRING(T)
38 
39 namespace tlp {
40 template<typename T>
41 class TLP_SCOPE SerializableType: public TypeInterface<T> {
42 public:
43  static void write(std::ostream& oss, const typename TypeInterface<T>::RealType& v) {
44  oss << v;
45  }
46  static bool read(std::istream& iss, typename TypeInterface<T>::RealType& v) {
47  return bool(iss >> v);
48  }
49  FORWARD_STRING_METHODS(typename TypeInterface<T>)
50 };
51 
52 template<typename ELT_TYPE, typename ELT_READER, int openParen>
53 class TLP_SCOPE SerializableVectorType: public TypeInterface<std::vector<ELT_TYPE> > {
54  static bool readVector(std::istream& is, std::vector<ELT_TYPE>& v,
55  char openChar, char sepChar, char closeChar) {
56  v.clear();
57 
58  char c =' ';
59  ELT_TYPE val;
60  bool firstVal = true;
61  bool sepFound = false;
62 
63  // go to first non space char
64  while((is >> c) && isspace(c)) {}
65 
66  if (openChar) {
67  if (c != openChar)
68  return false;
69  }
70  else
71  is.unget();
72 
73  for(;;) {
74  if( !(is >> c) )
75  return (!sepFound && !closeChar);
76 
77  if (isspace(c))
78  continue;
79 
80  if(c == closeChar) {
81  if (!openChar || sepFound)
82  return false;
83 
84  return true;
85  }
86 
87  if (c == sepChar) {
88  if (firstVal || sepFound)
89  return false;
90 
91  sepFound = true;
92  }
93  else {
94  if (firstVal || sepFound) {
95  if (openParen && c != '(')
96  return false;
97 
98  is.unget();
99 
100  if( !ELT_READER::read(is, val) )
101  return false;
102 
103  v.push_back(val);
104  firstVal = false;
105  sepFound = false;
106  }
107  else
108  return false;
109  }
110  }
111  }
112  static void writeVector(std::ostream& os, const std::vector<ELT_TYPE>& v) {
113  os << '(';
114 
115  for( unsigned int i = 0 ; i < v.size() ; i++ ) {
116  if (i)
117  os << ", ";
118 
119  os << v[i];
120  }
121 
122  os << ')';
123  }
124 
125 public:
126  static void write(std::ostream& oss, const typename TypeInterface<std::vector<ELT_TYPE> >::RealType& v) {
127  writeVector(oss, v);
128  }
129  static void writeb(std::ostream& oss, const typename TypeInterface<std::vector<ELT_TYPE> >::RealType& v) {
130  unsigned int vSize = v.size();
131  oss.write((char *) &vSize, sizeof(vSize));
132  oss.write((char *) v.data(), vSize * sizeof(ELT_TYPE));
133  }
134  static bool read(std::istream& iss, typename TypeInterface<std::vector<ELT_TYPE> >::RealType& v, char openChar = '(', char sepChar = ',', char closeChar = ')') {
135  return readVector(iss, v, openChar, sepChar, closeChar);
136  }
137  static bool readb(std::istream& iss, typename TypeInterface<std::vector<ELT_TYPE> >::RealType& v) {
138  unsigned int vSize;
139 
140  if (bool(iss.read((char *) &vSize, sizeof(vSize)))) {
141  v.resize(vSize);
142  return bool(iss.read((char *) v.data(), vSize * sizeof(ELT_TYPE)));
143  }
144 
145  return false;
146  }
147  static unsigned int valueSize() {
148  return 0; // means is not fixed
149  }
150  FORWARD_STRING_METHODS(typename TypeInterface<std::vector<ELT_TYPE> >)
151 };
152 }
153 
154 #endif // SERIALIZABLETYPE_H
155 ///@endcond