Tulip  5.2.1
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 #ifndef SERIALIZABLETYPE_H
22 #define SERIALIZABLETYPE_H
23 
24 #include <set>
25 #include <vector>
26 #include <string>
27 #include <list>
28 #include <iostream>
29 #include <sstream>
30 #include <cfloat>
31 
32 #include <tulip/TypeInterface.h>
33 
34 #define FORWARD_TOSTRING(T) \
35  static std::string toString(const T::RealType &v) { \
36  std::ostringstream oss; \
37  write(oss, v); \
38  return oss.str(); \
39  }
40 #define FORWARD_FROMSTRING(T) \
41  static bool fromString(T::RealType &v, const std::string &s) { \
42  std::istringstream iss(s); \
43  return read(iss, v); \
44  }
45 #define FORWARD_STRING_METHODS(T) FORWARD_FROMSTRING(T) FORWARD_TOSTRING(T)
46 
47 namespace tlp {
48 template <typename T>
49 class TLP_SCOPE SerializableType : public TypeInterface<T> {
50 public:
51  static void write(std::ostream &oss, const typename TypeInterface<T>::RealType &v) {
52  oss << v;
53  }
54  static bool read(std::istream &iss, typename TypeInterface<T>::RealType &v) {
55  return bool(iss >> v);
56  }
57  FORWARD_STRING_METHODS(typename TypeInterface<T>)
58 };
59 
60 template <typename ELT_TYPE, typename ELT_READER, int openParen>
61 class TLP_SCOPE SerializableVectorType : public TypeInterface<std::vector<ELT_TYPE>> {
62  static bool readVector(std::istream &is, std::vector<ELT_TYPE> &v, char openChar, char sepChar,
63  char closeChar) {
64  v.clear();
65 
66  char c = ' ';
67  ELT_TYPE val;
68  bool firstVal = true;
69  bool sepFound = false;
70 
71  // go to first non space char
72  while ((is >> c) && isspace(c)) {
73  }
74 
75  if (openChar) {
76  if (c != openChar)
77  return false;
78  } else
79  is.unget();
80 
81  for (;;) {
82  if (!(is >> c))
83  return (!sepFound && !closeChar);
84 
85  if (isspace(c))
86  continue;
87 
88  if (c == closeChar) {
89  if (!openChar || sepFound)
90  return false;
91 
92  return true;
93  }
94 
95  if (c == sepChar) {
96  if (firstVal || sepFound)
97  return false;
98 
99  sepFound = true;
100  } else {
101  if (firstVal || sepFound) {
102  if (openParen && c != '(')
103  return false;
104 
105  is.unget();
106 
107  if (!ELT_READER::read(is, val))
108  return false;
109 
110  v.push_back(val);
111  firstVal = false;
112  sepFound = false;
113  } else
114  return false;
115  }
116  }
117  }
118  static void writeVector(std::ostream &os, const std::vector<ELT_TYPE> &v) {
119  os << '(';
120 
121  for (unsigned int i = 0; i < v.size(); i++) {
122  if (i)
123  os << ", ";
124 
125  os << v[i];
126  }
127 
128  os << ')';
129  }
130 
131 public:
132  static void write(std::ostream &oss,
133  const typename TypeInterface<std::vector<ELT_TYPE>>::RealType &v) {
134  writeVector(oss, v);
135  }
136  static void writeb(std::ostream &oss,
137  const typename TypeInterface<std::vector<ELT_TYPE>>::RealType &v) {
138  unsigned int vSize = v.size();
139  oss.write(reinterpret_cast<const char *>(&vSize), sizeof(vSize));
140  oss.write(reinterpret_cast<const char *>(v.data()), vSize * sizeof(ELT_TYPE));
141  }
142  static bool read(std::istream &iss, typename TypeInterface<std::vector<ELT_TYPE>>::RealType &v,
143  char openChar = '(', char sepChar = ',', char closeChar = ')') {
144  return readVector(iss, v, openChar, sepChar, closeChar);
145  }
146  static bool readb(std::istream &iss, typename TypeInterface<std::vector<ELT_TYPE>>::RealType &v) {
147  unsigned int vSize;
148 
149  if (bool(iss.read(reinterpret_cast<char *>(&vSize), sizeof(vSize)))) {
150  v.resize(vSize);
151  return bool(iss.read(reinterpret_cast<char *>(v.data()), vSize * sizeof(ELT_TYPE)));
152  }
153  return false;
154  }
155  static bool read(const std::vector<std::string> &vs,
156  typename TypeInterface<std::vector<ELT_TYPE>>::RealType &v) {
157  v.clear();
158  v.reserve(vs.size());
159 
160  for (const std::string &s : vs) {
161  ELT_TYPE val;
162  std::istringstream is(s);
163  if (!ELT_READER::read(is, val))
164  return false;
165 
166  v.push_back(val);
167  }
168  return true;
169  }
170  static bool tokenize(const std::string &s, std::vector<std::string> &v, char openChar,
171  char sepChar, char closeChar) {
172  v.clear();
173 
174  std::istringstream is(s);
175  char c = ' ';
176  ELT_TYPE val;
177  bool firstVal = true;
178  bool sepFound = false;
179 
180  // go to first non space char
181  while ((is >> c) && isspace(c)) {
182  }
183 
184  if (openChar) {
185  if (c != openChar)
186  return false;
187  } else
188  is.unget();
189 
190  for (;;) {
191  if (!(is >> c))
192  return (!sepFound && !closeChar);
193 
194  if (isspace(c))
195  continue;
196 
197  if (c == closeChar) {
198  if (!openChar || sepFound)
199  return false;
200 
201  return true;
202  }
203 
204  if (c == sepChar) {
205  if (firstVal || sepFound)
206  return false;
207 
208  sepFound = true;
209  } else {
210  if (firstVal || sepFound) {
211  if (openParen && c != '(')
212  return false;
213 
214  is.unget();
215 
216  auto pos = is.tellg();
217  if (!ELT_READER::read(is, val))
218  return false;
219 
220  v.push_back(s.substr(pos, is.tellg() - pos)),
221 
222  firstVal = false;
223  sepFound = false;
224  } else
225  return false;
226  }
227  }
228  }
229  static unsigned int valueSize() {
230  return 0; // means is not fixed
231  }
232  FORWARD_STRING_METHODS(typename TypeInterface<std::vector<ELT_TYPE>>)
233 };
234 } // namespace tlp
235 
236 #endif // SERIALIZABLETYPE_H
237 ///@endcond