Tulip  4.1.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
SerializableType.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux 1 and Inria Bordeaux - Sud Ouest
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 (iss >> v);
48  }
49  FORWARD_STRING_METHODS(typename TypeInterface<T>)
50 };
51 
52 template<typename VT, int openParen>
53 class TLP_SCOPE SerializableVectorType: public TypeInterface<std::vector<VT> > {
54  static bool readVector(std::istream& is, std::vector<VT>& v) {
55  v.clear();
56 
57  char c =' ';
58  VT val;
59  bool firstVal = true;
60  bool sepFound = false;
61 
62  // go to first '('
63  while((is >> c) && isspace(c)) {}
64 
65  if (c != '(')
66  return false;
67 
68  for(;;) {
69  if( !(is >> c) )
70  return false;
71 
72  if (isspace(c))
73  continue;
74 
75  if(c == ')') {
76  if (sepFound)
77  return false;
78 
79  return true;
80  }
81 
82  if (c == ',') {
83  if (firstVal || sepFound)
84  return false;
85 
86  sepFound = true;
87  }
88  else {
89  if (firstVal || sepFound) {
90  if (openParen && c != '(')
91  return false;
92 
93  is.unget();
94 
95  if( !(is >> val) )
96  return false;
97 
98  v.push_back(val);
99  firstVal = false;
100  sepFound = false;
101  }
102  else
103  return false;
104  }
105  }
106  }
107  static void writeVector(std::ostream& os, const std::vector<VT>& v) {
108  os << '(';
109 
110  for( unsigned int i = 0 ; i < v.size() ; i++ ) {
111  if (i)
112  os << ", ";
113 
114  os << v[i];
115  }
116 
117  os << ')';
118  }
119 
120 public:
121  static void write(std::ostream& oss, const typename TypeInterface<std::vector<VT> >::RealType& v) {
122  writeVector(oss, v);
123  }
124  static bool read(std::istream& iss, typename TypeInterface<std::vector<VT> >::RealType& v) {
125  return readVector(iss, v);
126  }
127  FORWARD_STRING_METHODS(typename TypeInterface<std::vector<VT> >)
128 };
129 }
130 
131 #endif // SERIALIZABLETYPE_H
132 ///@endcond