Simbody  3.4 (development)
Serialize.h
Go to the documentation of this file.
00001 #ifndef SimTK_SimTKCOMMON_SERIALIZE_H_
00002 #define SimTK_SimTKCOMMON_SERIALIZE_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                       Simbody(tm): SimTKcommon                             *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK biosimulation toolkit originating from           *
00008  * Simbios, the NIH National Center for Physics-Based Simulation of           *
00009  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
00010  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody.  *
00011  *                                                                            *
00012  * Portions copyright (c) 2012 Stanford University and the Authors.           *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
00015  *                                                                            *
00016  * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
00017  * not use this file except in compliance with the License. You may obtain a  *
00018  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0.         *
00019  *                                                                            *
00020  * Unless required by applicable law or agreed to in writing, software        *
00021  * distributed under the License is distributed on an "AS IS" BASIS,          *
00022  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
00023  * See the License for the specific language governing permissions and        *
00024  * limitations under the License.                                             *
00025  * -------------------------------------------------------------------------- */
00026 
00041 #include "SimTKcommon/internal/common.h"
00042 #include "SimTKcommon/internal/ExceptionMacros.h"
00043 #include "SimTKcommon/internal/String.h"
00044 
00045 #include <iostream>
00046 
00047 namespace SimTK {
00048 
00049 //------------------------------------------------------------------------------
00050 //                           WRITE UNFORMATTED
00051 //------------------------------------------------------------------------------
00074 template <class T> inline void
00075 writeUnformatted(std::ostream& o, const T& v) {
00076     o << String(v);
00077 }
00078 
00080 template <class T> inline void
00081 writeUnformatted(std::ostream& o, const float& v)  
00082 {   writeUnformatted<float>(o,v); }
00084 template <class T> inline void
00085 writeUnformatted(std::ostream& o, const double& v) 
00086 {   writeUnformatted<double>(o,v); }
00089 template <class T> inline void
00090 writeUnformatted(std::ostream& o, const long double& v) 
00091 {   writeUnformatted<long double>(o,v); }
00092 
00094 template <class T> inline void
00095 writeUnformatted(std::ostream& o, const negator<T>& v) 
00096 {   writeUnformatted(o, T(v)); }
00097 
00100 template <class T> inline void
00101 writeUnformatted(std::ostream& o, const std::complex<T>& v) 
00102 {   writeUnformatted(o, v.real()); o << " "; writeUnformatted(o, v.imag()); }
00103 
00105 template <class T> inline void
00106 writeUnformatted(std::ostream& o, const conjugate<T>& v) 
00107 {   writeUnformatted(o, std::complex<T>(v)); }
00108 
00109 
00113 template <int M, class E, int S> inline void
00114 writeUnformatted(std::ostream& o, const Vec<M,E,S>& v) {
00115     for (int i=0; i < M; ++i) {
00116         if (i != 0) o << " ";
00117         writeUnformatted(o, v[i]);
00118     }
00119 }   
00123 template <int N, class E, int S> inline void
00124 writeUnformatted(std::ostream& o, const Row<N,E,S>& v) 
00125 {   writeUnformatted(o, ~v); }
00126 
00130 template <int M, int N, class E, int CS, int RS> inline void
00131 writeUnformatted(std::ostream& o, const Mat<M,N,E,CS,RS>& v) {
00132     for (int i=0; i < M; ++i) {
00133         if (i != 0) o << std::endl;
00134         writeUnformatted(o, v[i]);
00135     }
00136 } 
00137 
00141 template <int M, class E, int RS> inline void
00142 writeUnformatted(std::ostream& o, const SymMat<M,E,RS>& v) {
00143     for (int i=0; i < M; ++i) {
00144         if (i != 0) o << std::endl;
00145         writeUnformatted(o, v[i]);
00146     }
00147 } 
00151 //------------------------------------------------------------------------------
00152 //                             READ UNFORMATTED
00153 //------------------------------------------------------------------------------
00170 inline bool
00171 readOneTokenUnformatted(std::istream& in, String& token) {
00172     // If the stream is already bad or at eof, we fail.
00173     if (!in.good()) {in.setstate(std::ios::failbit); return false;}
00174     // Skip whitespace. Failure or eof here means no token.
00175     std::ws(in);
00176     if (!in.good()) {in.setstate(std::ios::failbit); return false;}
00177     in >> token; if (in.fail()) return false;
00178     if (token.empty()) {in.setstate(std::ios_base::failbit); return false;}
00179     return true;
00180 } 
00181 
00187 template <class T> inline bool
00188 readUnformatted(std::istream& in, T& v) {
00189     String token;
00190     if (!readOneTokenUnformatted(in, token)) return false;
00191     if (!token.tryConvertTo<T>(v)) 
00192     {   in.setstate(std::ios::failbit); return false; }
00193     return true;
00194 }
00195 
00197 template <class T> inline bool
00198 readUnformatted(std::istream& in, float& v) 
00199 {   return readUnformatted<float>(in,v); }
00201 template <class T> inline bool
00202 readUnformatted(std::istream& in, double& v) 
00203 {   return readUnformatted<double>(in,v); }
00206 template <class T> inline bool
00207 readUnformatted(std::istream& in, long double& v) 
00208 {   return readUnformatted<long double>(in,v); }
00209 
00211 template <class T> inline bool
00212 readUnformatted(std::istream& in, negator<T>& v) {
00213     T nonneg; if (!readUnformatted(in, nonneg)) return false;
00214     v = nonneg; // Changes representation, not value.
00215     return true;
00216 }
00217 
00219 template <class T> inline bool
00220 readUnformatted(std::istream& in, std::complex<T>& v) {
00221     T real, imag;
00222     if (!readUnformatted(in, real)) return false;
00223     if (!readUnformatted(in, imag)) return false;
00224     v = std::complex<T>(real,imag);
00225     return true;
00226 }
00227 
00229 template <class T> inline bool
00230 readUnformatted(std::istream& in, conjugate<T>& v) {
00231     std::complex<T> cmplx; if (!readUnformatted(in, cmplx)) return false;
00232     v = cmplx; // Changes representation, not value.
00233     return true;
00234 }
00235 
00237 template <> inline bool 
00238 readUnformatted<String>(std::istream& in, String& v)
00239 {   return readOneTokenUnformatted(in,v); }
00240 
00241 
00245 template <int M, class E, int S> inline bool
00246 readUnformatted(std::istream& in, Vec<M,E,S>& v) {
00247     for (int i=0; i < M; ++i)
00248         if (!readUnformatted(in, v[i])) return false;
00249     return true;
00250 }   
00251 
00255 template <int N, class E, int S> inline bool
00256 readUnformatted(std::istream& in, Row<N,E,S>& v) 
00257 {   return readUnformatted(in, ~v); }
00258 
00261 template <int M, int N, class E, int CS, int RS> inline bool
00262 readUnformatted(std::istream& in, Mat<M,N,E,CS,RS>& v) {
00263     for (int i=0; i < M; ++i)
00264         if (!readUnformatted(in, v[i])) return false;
00265     return true;
00266 } 
00267 
00274 template <int M, class E, int RS> inline bool
00275 readUnformatted(std::istream& in, SymMat<M,E,RS>& v) {
00276     Mat<M,M,E> m; if (!readUnformatted(in, m)) return false;
00277     if (!m.isNumericallySymmetric()) {
00278         in.setstate(std::ios::failbit); 
00279         return false;
00280     }
00281     v.setFromSymmetric(m);
00282     return true;
00283 } 
00286 //------------------------------------------------------------------------------
00287 //                             WRITE FORMATTED
00288 //------------------------------------------------------------------------------
00311 template <class T> inline void
00312 writeFormatted(std::ostream& o, const T& v) {
00313     o << String(v);
00314 }
00317 //------------------------------------------------------------------------------
00318 //                             READ FORMATTED
00319 //------------------------------------------------------------------------------
00334 template <class T> inline bool
00335 readFormatted(std::istream& in, T& v) {
00336     return readUnformatted(in, v);
00337 }
00338 
00339 // TODO: need specializations for complex to support (real,imag) where the
00340 // numbers can be NaN, Inf, -Inf.
00345 } // namespace SimTK
00346 #endif // SimTK_SimTKCOMMON_SERIALIZE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines