String.h

Go to the documentation of this file.
00001 #ifndef SimTK_SimTKCOMMON_STRING_H_
00002 #define SimTK_SimTKCOMMON_STRING_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                      SimTK Core: SimTKcommon                               *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK Core 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.               *
00011  *                                                                            *
00012  * Portions copyright (c) 2005-7 Stanford University and the Authors.         *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
00015  *                                                                            *
00016  * Permission is hereby granted, free of charge, to any person obtaining a    *
00017  * copy of this software and associated documentation files (the "Software"), *
00018  * to deal in the Software without restriction, including without limitation  *
00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
00020  * and/or sell copies of the Software, and to permit persons to whom the      *
00021  * Software is furnished to do so, subject to the following conditions:       *
00022  *                                                                            *
00023  * The above copyright notice and this permission notice shall be included in *
00024  * all copies or substantial portions of the Software.                        *
00025  *                                                                            *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
00029  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
00030  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
00031  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
00032  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
00033  * -------------------------------------------------------------------------- */
00034 
00035 // Keeps MS VC++ 8 quiet about sprintf, strcpy, etc.
00036 #ifdef _MSC_VER
00037 #pragma warning(disable:4996)
00038 #endif
00039 
00040 
00041 #include "SimTKcommon/internal/common.h"
00042 
00043 #include <cstdio>
00044 #include <string>
00045 #include <limits>
00046 #include <complex>
00047 
00048 namespace SimTK {
00049     
00065 class String : public std::string {
00066 public:
00068     String() { }
00069 
00070     // uses default copy constructor, copy assignment, and destructor
00071 
00073     String(const char* s) : std::string(s) { }
00074 
00076     String(const std::string& s) : std::string(s) { }
00077 
00080     String(const String& s, int start, int len) : std::string(s,start,len) { }
00081 
00084     operator const char*() const { return c_str(); }
00085 
00087     char& operator[](int i) {
00088         assert(i >= 0);
00089         return std::string::operator[]((std::string::size_type)i);
00090     }
00091 
00093     char operator[](int i) const {
00094         assert(i >= 0);
00095         return std::string::operator[]((std::string::size_type)i);
00096     }
00097 
00099     char& operator[](std::string::size_type i) {return std::string::operator[](i);}
00101     char operator[](std::string::size_type i) const {return std::string::operator[](i);}
00102 
00105 
00106     explicit String(int i) { char buf[32]; sprintf(buf,"%d",i); (*this)=buf; }
00107     explicit String(long i) { char buf[32]; sprintf(buf,"%ld",i); (*this)=buf; }
00108     explicit String(unsigned int s)  { char buf[32]; sprintf(buf,"%u",s); (*this)=buf; }
00109     explicit String(unsigned long s) { char buf[32]; sprintf(buf,"%lu",s); (*this)=buf; }
00110     explicit String(float r)    { char buf[64]; sprintf(buf,"%.8g",r); (*this)=buf; }
00111     explicit String(double r)   { char buf[64]; sprintf(buf,"%.16g",r); (*this)=buf; }
00112     explicit String(long double r)  { char buf[64]; sprintf(buf,"%.20Lg",r); (*this)=buf; }
00113     explicit String(std::complex<float> r)  
00114         { char buf[128]; sprintf(buf,"(%.8g,%.8g)",r.real(),r.imag()); (*this)=buf; }
00115     explicit String(std::complex<double> r) 
00116         { char buf[128]; sprintf(buf,"(%.16g,%.16g)",r.real(),r.imag()); (*this)=buf; }
00117     explicit String(std::complex<long double> r)    
00118         { char buf[128]; sprintf(buf,"(%.20Lg,%.20Lg)",r.real(),r.imag()); (*this)=buf; }
00119     explicit String(bool b) : std::string(b?"true":"false") { }
00121 };  
00122 
00123 }
00124 #endif // SimTK_SimTKCOMMON_STRING_H_

Generated on Thu Feb 28 01:34:32 2008 for SimTKcommon by  doxygen 1.4.7