22 #ifndef SERIALIZABLETYPE_H
23 #define SERIALIZABLETYPE_H
33 #include <tulip/TypeInterface.h>
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)
41 class TLP_SCOPE SerializableType:
public TypeInterface<T> {
43 static void write(std::ostream& oss,
const typename TypeInterface<T>::RealType& v) {
46 static bool read(std::istream& iss,
typename TypeInterface<T>::RealType& v) {
47 return bool(iss >> v);
49 FORWARD_STRING_METHODS(
typename TypeInterface<T>)
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 char openChar,
char sepChar,
char closeChar) {
61 bool sepFound =
false;
64 while((is >> c) && isspace(c)) {}
75 return (!sepFound && !closeChar);
81 if (!openChar || sepFound)
88 if (firstVal || sepFound)
94 if (firstVal || sepFound) {
95 if (openParen && c !=
'(')
112 static void writeVector(std::ostream& os,
const std::vector<VT>& v) {
115 for(
unsigned int i = 0 ; i < v.size() ; i++ ) {
126 static void write(std::ostream& oss,
const typename TypeInterface<std::vector<VT> >::RealType& v) {
129 static void writeb(std::ostream& oss,
const typename TypeInterface<std::vector<VT> >::RealType& v) {
130 unsigned int vSize = v.size();
131 oss.write((
char *) &vSize,
sizeof(vSize));
132 oss.write((
char *) v.data(), vSize *
sizeof(VT));
134 static bool read(std::istream& iss,
typename TypeInterface<std::vector<VT> >::RealType& v,
char openChar =
'(',
char sepChar =
',',
char closeChar =
')') {
135 return readVector(iss, v, openChar, sepChar, closeChar);
137 static bool readb(std::istream& iss,
typename TypeInterface<std::vector<VT> >::RealType& v) {
140 if (
bool(iss.read((
char *) &vSize,
sizeof(vSize)))) {
142 return bool(iss.read((
char *) v.data(), vSize *
sizeof(VT)));
147 static unsigned int valueSize() {
150 FORWARD_STRING_METHODS(
typename TypeInterface<std::vector<VT> >)
154 #endif // SERIALIZABLETYPE_H