Simbody  3.5
Serialize.h
Go to the documentation of this file.
1 #ifndef SimTK_SimTKCOMMON_SERIALIZE_H_
2 #define SimTK_SimTKCOMMON_SERIALIZE_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKcommon *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2012 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
44 
45 #include <iostream>
46 
47 namespace SimTK {
48 
49 //------------------------------------------------------------------------------
50 // WRITE UNFORMATTED
51 //------------------------------------------------------------------------------
74 template <class T> inline void
75 writeUnformatted(std::ostream& o, const T& v) {
76  o << String(v);
77 }
78 
80 template <class T> inline void
81 writeUnformatted(std::ostream& o, const float& v)
82 { writeUnformatted<float>(o,v); }
84 template <class T> inline void
85 writeUnformatted(std::ostream& o, const double& v)
86 { writeUnformatted<double>(o,v); }
89 template <class T> inline void
90 writeUnformatted(std::ostream& o, const long double& v)
91 { writeUnformatted<long double>(o,v); }
92 
94 template <class T> inline void
95 writeUnformatted(std::ostream& o, const negator<T>& v)
96 { writeUnformatted(o, T(v)); }
97 
100 template <class T> inline void
101 writeUnformatted(std::ostream& o, const std::complex<T>& v)
102 { writeUnformatted(o, v.real()); o << " "; writeUnformatted(o, v.imag()); }
103 
105 template <class T> inline void
106 writeUnformatted(std::ostream& o, const conjugate<T>& v)
107 { writeUnformatted(o, std::complex<T>(v)); }
108 
109 
113 template <int M, class E, int S> inline void
114 writeUnformatted(std::ostream& o, const Vec<M,E,S>& v) {
115  for (int i=0; i < M; ++i) {
116  if (i != 0) o << " ";
117  writeUnformatted(o, v[i]);
118  }
119 }
123 template <int N, class E, int S> inline void
124 writeUnformatted(std::ostream& o, const Row<N,E,S>& v)
125 { writeUnformatted(o, ~v); }
126 
130 template <int M, int N, class E, int CS, int RS> inline void
131 writeUnformatted(std::ostream& o, const Mat<M,N,E,CS,RS>& v) {
132  for (int i=0; i < M; ++i) {
133  if (i != 0) o << std::endl;
134  writeUnformatted(o, v[i]);
135  }
136 }
137 
141 template <int M, class E, int RS> inline void
142 writeUnformatted(std::ostream& o, const SymMat<M,E,RS>& v) {
143  for (int i=0; i < M; ++i) {
144  if (i != 0) o << std::endl;
145  writeUnformatted(o, v[i]);
146  }
147 }
151 //------------------------------------------------------------------------------
152 // READ UNFORMATTED
153 //------------------------------------------------------------------------------
170 inline bool
171 readOneTokenUnformatted(std::istream& in, String& token) {
172  // If the stream is already bad or at eof, we fail.
173  if (!in.good()) {in.setstate(std::ios::failbit); return false;}
174  // Skip whitespace. Failure or eof here means no token.
175  std::ws(in);
176  if (!in.good()) {in.setstate(std::ios::failbit); return false;}
177  in >> token; if (in.fail()) return false;
178  if (token.empty()) {in.setstate(std::ios_base::failbit); return false;}
179  return true;
180 }
181 
187 template <class T> inline bool
188 readUnformatted(std::istream& in, T& v) {
189  String token;
190  if (!readOneTokenUnformatted(in, token)) return false;
191  if (!token.tryConvertTo<T>(v))
192  { in.setstate(std::ios::failbit); return false; }
193  return true;
194 }
195 
197 template <class T> inline bool
198 readUnformatted(std::istream& in, float& v)
199 { return readUnformatted<float>(in,v); }
201 template <class T> inline bool
202 readUnformatted(std::istream& in, double& v)
203 { return readUnformatted<double>(in,v); }
206 template <class T> inline bool
207 readUnformatted(std::istream& in, long double& v)
208 { return readUnformatted<long double>(in,v); }
209 
211 template <class T> inline bool
212 readUnformatted(std::istream& in, negator<T>& v) {
213  T nonneg; if (!readUnformatted(in, nonneg)) return false;
214  v = nonneg; // Changes representation, not value.
215  return true;
216 }
217 
219 template <class T> inline bool
220 readUnformatted(std::istream& in, std::complex<T>& v) {
221  T real, imag;
222  if (!readUnformatted(in, real)) return false;
223  if (!readUnformatted(in, imag)) return false;
224  v = std::complex<T>(real,imag);
225  return true;
226 }
227 
229 template <class T> inline bool
230 readUnformatted(std::istream& in, conjugate<T>& v) {
231  std::complex<T> cmplx; if (!readUnformatted(in, cmplx)) return false;
232  v = cmplx; // Changes representation, not value.
233  return true;
234 }
235 
237 template <> inline bool
238 readUnformatted<String>(std::istream& in, String& v)
239 { return readOneTokenUnformatted(in,v); }
240 
241 
245 template <int M, class E, int S> inline bool
246 readUnformatted(std::istream& in, Vec<M,E,S>& v) {
247  for (int i=0; i < M; ++i)
248  if (!readUnformatted(in, v[i])) return false;
249  return true;
250 }
251 
255 template <int N, class E, int S> inline bool
256 readUnformatted(std::istream& in, Row<N,E,S>& v)
257 { return readUnformatted(in, ~v); }
258 
261 template <int M, int N, class E, int CS, int RS> inline bool
262 readUnformatted(std::istream& in, Mat<M,N,E,CS,RS>& v) {
263  for (int i=0; i < M; ++i)
264  if (!readUnformatted(in, v[i])) return false;
265  return true;
266 }
267 
274 template <int M, class E, int RS> inline bool
275 readUnformatted(std::istream& in, SymMat<M,E,RS>& v) {
276  Mat<M,M,E> m; if (!readUnformatted(in, m)) return false;
277  if (!m.isNumericallySymmetric()) {
278  in.setstate(std::ios::failbit);
279  return false;
280  }
281  v.setFromSymmetric(m);
282  return true;
283 }
286 //------------------------------------------------------------------------------
287 // WRITE FORMATTED
288 //------------------------------------------------------------------------------
311 template <class T> inline void
312 writeFormatted(std::ostream& o, const T& v) {
313  o << String(v);
314 }
317 //------------------------------------------------------------------------------
318 // READ FORMATTED
319 //------------------------------------------------------------------------------
334 template <class T> inline bool
335 readFormatted(std::istream& in, T& v) {
336  return readUnformatted(in, v);
337 }
338 
339 // TODO: need specializations for complex to support (real,imag) where the
340 // numbers can be NaN, Inf, -Inf.
345 } // namespace SimTK
346 #endif // SimTK_SimTKCOMMON_SERIALIZE_H_
bool isNumericallySymmetric(double tol=getDefaultTolerance()) const
A Matrix is symmetric (actually Hermitian) if it is square and each element (i,j) is the Hermitian tr...
Definition: Mat.h:1172
This is a small, fixed-size symmetric or Hermitian matrix designed for no-overhead inline computation...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:608
void writeUnformatted(std::ostream &o, const Mat< M, N, E, CS, RS > &v)
Specialize for Mat<M,N,E,CS,RS> delegating to Row<N,E,RS> with newlines separating the rows...
Definition: Serialize.h:131
bool readUnformatted(std::istream &in, T &v)
The default implementation of readUnformatted<T> reads in the next whitespace-separated token and the...
Definition: Serialize.h:188
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
SimTK::conjugate<R> should be instantiated only for float, double, long double.
Definition: String.h:45
bool tryConvertTo(T &out) const
Attempt to convert this String to an object of type T, returning a status value to indicate success o...
Definition: String.h:421
void writeUnformatted(std::ostream &o, const Row< N, E, S > &v)
Specialize for Row<N,E,S> to delegate to element type E, with spaces separating the elements; raw out...
Definition: Serialize.h:124
bool readUnformatted(std::istream &in, Vec< M, E, S > &v)
Specialize for Vec<M,E,S> to delegate to element type E, with spaces separating the elements...
Definition: Serialize.h:246
SymMat & setFromSymmetric(const Mat< M, M, EE, CSS, RSS > &m)
Create a new SymMat of this type from a square Mat of the right size, that is expected to be symmetri...
Definition: SymMat.h:317
negator<N>, where N is a number type (real, complex, conjugate), is represented in memory identically...
Definition: String.h:44
void writeUnformatted(std::ostream &o, const SymMat< M, E, RS > &v)
Specialize for SymMat<M,E,RS> delegating to Row<M,E,RS> with newlines separating the rows...
Definition: Serialize.h:142
void writeUnformatted(std::ostream &o, const Vec< M, E, S > &v)
Specialize for Vec<M,E,S> to delegate to element type E, with spaces separating the elements...
Definition: Serialize.h:114
This file contains macros which are convenient to use for sprinkling error checking around liberally ...
bool readUnformatted(std::istream &in, SymMat< M, E, RS > &v)
Specialize for SymMat<M,E,RS>.
Definition: Serialize.h:275
This is a fixed-length column vector designed for no-overhead inline computation. ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:605
bool readOneTokenUnformatted(std::istream &in, String &token)
Read in the next whitespace-delimited token as a String, ignoring leading whitespace.
Definition: Serialize.h:171
bool readFormatted(std::istream &in, T &v)
The default implementation of readFormatted<T>() uses readUnformatted<T>().
Definition: Serialize.h:335
bool readUnformatted(std::istream &in, Mat< M, N, E, CS, RS > &v)
Specialize for Mat<M,N,E,CS,RS> delegating to Row<N,E,RS>.
Definition: Serialize.h:262
const float & real(const conjugate< float > &c)
Definition: conjugate.h:771
SimTK::String is a plug-compatible std::string replacement (plus some additional functionality) inten...
Definition: String.h:62
This is a fixed-length row vector designed for no-overhead inline computation.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:606
Mandatory first inclusion for any Simbody source or header file.
const negator< float > & imag(const conjugate< float > &c)
Definition: conjugate.h:772
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:607
bool readUnformatted(std::istream &in, Row< N, E, S > &v)
Specialize for Row<N,E,S> to delegate to element type E, with spaces separating the elements; format ...
Definition: Serialize.h:256
bool readUnformatted< String >(std::istream &in, String &v)
Specialization for SimTK::String (just read token).
Definition: Serialize.h:238
void writeFormatted(std::ostream &o, const T &v)
The default implementation of writeFormatted<T> converts the object to a String using the templatized...
Definition: Serialize.h:312
void writeUnformatted(std::ostream &o, const T &v)
The default implementation of writeUnformatted<T> converts the object to a String using the templatiz...
Definition: Serialize.h:75