Simbody  3.6
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) 2015 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 //------------------------------------------------------------------------------
75 template <class T> inline void
76 writeUnformatted(std::ostream& o, const T& v) {
77  o << String(v);
78 }
79 
81 template <class T> inline void
82 writeUnformatted(std::ostream& o, const negator<T>& v)
83 { writeUnformatted(o, T(v)); }
84 
87 template <class T> inline void
88 writeUnformatted(std::ostream& o, const std::complex<T>& v)
89 { writeUnformatted(o, v.real()); o << " "; writeUnformatted(o, v.imag()); }
90 
93 template <class T> inline void
94 writeUnformatted(std::ostream& o, const conjugate<T>& v)
95 { writeUnformatted(o, std::complex<T>(v)); }
96 
97 
101 template <int M, class E, int S> inline void
102 writeUnformatted(std::ostream& o, const Vec<M,E,S>& v) {
103  for (int i=0; i < M; ++i) {
104  if (i != 0) o << " ";
105  writeUnformatted(o, v[i]);
106  }
107 }
111 template <int N, class E, int S> inline void
112 writeUnformatted(std::ostream& o, const Row<N,E,S>& v)
113 { writeUnformatted(o, ~v); }
114 
118 template <int M, int N, class E, int CS, int RS> inline void
119 writeUnformatted(std::ostream& o, const Mat<M,N,E,CS,RS>& v) {
120  for (int i=0; i < M; ++i) {
121  if (i != 0) o << std::endl;
122  writeUnformatted(o, v[i]);
123  }
124 }
125 
129 template <int M, class E, int RS> inline void
130 writeUnformatted(std::ostream& o, const SymMat<M,E,RS>& v) {
131  for (int i=0; i < M; ++i) {
132  if (i != 0) o << std::endl;
133  writeUnformatted(o, v[i]);
134  }
135 }
139 //------------------------------------------------------------------------------
140 // READ UNFORMATTED
141 //------------------------------------------------------------------------------
158 inline bool
159 readOneTokenUnformatted(std::istream& in, String& token) {
160  // If the stream is already bad or at eof, we fail.
161  if (!in.good()) {in.setstate(std::ios::failbit); return false;}
162  // Skip whitespace. Failure or eof here means no token.
163  std::ws(in);
164  if (!in.good()) {in.setstate(std::ios::failbit); return false;}
165  in >> token; if (in.fail()) return false;
166  if (token.empty()) {in.setstate(std::ios_base::failbit); return false;}
167  return true;
168 }
169 
175 template <class T> inline bool
176 readUnformatted(std::istream& in, T& v) {
177  String token;
178  if (!readOneTokenUnformatted(in, token)) return false;
179  if (!token.tryConvertTo<T>(v))
180  { in.setstate(std::ios::failbit); return false; }
181  return true;
182 }
183 
185 template <class T> inline bool
186 readUnformatted(std::istream& in, negator<T>& v) {
187  T nonneg; if (!readUnformatted(in, nonneg)) return false;
188  v = nonneg; // Changes representation, not value.
189  return true;
190 }
191 
193 template <class T> inline bool
194 readUnformatted(std::istream& in, std::complex<T>& v) {
195  T real, imag;
196  if (!readUnformatted(in, real)) return false;
197  if (!readUnformatted(in, imag)) return false;
198  v = std::complex<T>(real,imag);
199  return true;
200 }
201 
203 template <class T> inline bool
204 readUnformatted(std::istream& in, conjugate<T>& v) {
205  std::complex<T> cmplx; if (!readUnformatted(in, cmplx)) return false;
206  v = cmplx; // Changes representation, not value.
207  return true;
208 }
209 
211 template <> inline bool
212 readUnformatted<String>(std::istream& in, String& v)
213 { return readOneTokenUnformatted(in,v); }
214 
215 
219 template <int M, class E, int S> inline bool
220 readUnformatted(std::istream& in, Vec<M,E,S>& v) {
221  for (int i=0; i < M; ++i)
222  if (!readUnformatted(in, v[i])) return false;
223  return true;
224 }
225 
229 template <int N, class E, int S> inline bool
230 readUnformatted(std::istream& in, Row<N,E,S>& v)
231 { return readUnformatted(in, ~v); }
232 
235 template <int M, int N, class E, int CS, int RS> inline bool
236 readUnformatted(std::istream& in, Mat<M,N,E,CS,RS>& v) {
237  for (int i=0; i < M; ++i)
238  if (!readUnformatted(in, v[i])) return false;
239  return true;
240 }
241 
248 template <int M, class E, int RS> inline bool
249 readUnformatted(std::istream& in, SymMat<M,E,RS>& v) {
250  Mat<M,M,E> m; if (!readUnformatted(in, m)) return false;
251  if (!m.isNumericallySymmetric()) {
252  in.setstate(std::ios::failbit);
253  return false;
254  }
255  v.setFromSymmetric(m);
256  return true;
257 }
260 //------------------------------------------------------------------------------
261 // WRITE FORMATTED
262 //------------------------------------------------------------------------------
285 template <class T> inline void
286 writeFormatted(std::ostream& o, const T& v) {
287  o << String(v);
288 }
291 //------------------------------------------------------------------------------
292 // READ FORMATTED
293 //------------------------------------------------------------------------------
308 template <class T> inline bool
309 readFormatted(std::istream& in, T& v) {
310  return readUnformatted(in, v);
311 }
312 
313 // TODO: need specializations for complex to support (real,imag) where the
314 // numbers can be NaN, Inf, -Inf.
319 } // namespace SimTK
320 #endif // SimTK_SimTKCOMMON_SERIALIZE_H_
This is a small, fixed-size symmetric or Hermitian matrix designed for no-overhead inline computation...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:621
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:119
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:176
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.
Definition: String.h:45
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:112
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:220
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
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:445
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:130
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:102
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:249
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:1174
This is a fixed-length column vector designed for no-overhead inline computation. ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:618
bool readOneTokenUnformatted(std::istream &in, String &token)
Read in the next whitespace-delimited token as a String, ignoring leading whitespace.
Definition: Serialize.h:159
bool readFormatted(std::istream &in, T &v)
The default implementation of readFormatted<T>() uses readUnformatted<T>().
Definition: Serialize.h:309
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:236
const float & real(const conjugate< float > &c)
Definition: conjugate.h:482
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:619
Mandatory first inclusion for any Simbody source or header file.
const negator< float > & imag(const conjugate< float > &c)
Definition: conjugate.h:483
This class represents a small matrix whose size is known at compile time, containing elements of any ...
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:620
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:230
bool readUnformatted< String >(std::istream &in, String &v)
Specialization for SimTK::String (just read token).
Definition: Serialize.h:212
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:286
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:76