Savarese Software Research Corporation
StringTo.h
Go to the documentation of this file.
00001 /* Copyright 2006-2009 Savarese Software Research Corporation
00002  *
00003  * Licensed under the Apache License, Version 2.0 (the "License");
00004  * you may not use this file except in compliance with the License.
00005  * You may obtain a copy of the License at
00006  *
00007  *     https://www.savarese.com/software/ApacheLicense-2.0
00008  *
00009  * Unless required by applicable law or agreed to in writing, software
00010  * distributed under the License is distributed on an "AS IS" BASIS,
00011  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012  * See the License for the specific language governing permissions and
00013  * limitations under the License.
00014  */
00015 
00021 #ifndef __SSRC_WSPR_UTILITY_STRING_TO_H
00022 #define __SSRC_WSPR_UTILITY_STRING_TO_H
00023 
00024 #include <ssrc/wispers-packages.h>
00025 
00026 #include <typeinfo>
00027 #include <sstream>
00028 #include <cstring>
00029 
00030 __BEGIN_NS_SSRC_WSPR_UTILITY
00031 
00032 using std::string;
00033 using std::istringstream;
00034 
00035 #if defined(__GNUC__) && (__GNUC__ >= 4)
00036 #define __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
00037 #endif
00038 
00042 class StringTo {
00043   istringstream _istream;
00044 
00045 public:
00046   typedef istringstream::fmtflags fmtflags;
00047   typedef istringstream::iostate iostate;
00048 
00049   StringTo(const StringTo &) { }
00050 
00051   StringTo() { }
00052 
00061   template<typename T>  T cast(const string & str)
00062     SSRC_DECL_THROW(std::bad_cast)
00063   {
00064     T result;
00065 
00066 #ifdef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
00067     _istream.clear();
00068     _istream.rdbuf()->pubsetbuf(const_cast<char *>(str.c_str()), str.size());
00069     //    _istream.seekg(0);
00070 #else
00071     _istream.str(str);
00072 #endif
00073 
00074     if(!(_istream >> result))
00075       throw std::bad_cast();
00076 
00077     return result;
00078   }
00079 
00080   template<typename T>  T cast(const char *str)
00081     SSRC_DECL_THROW(std::bad_cast)
00082   {
00083     T result;
00084 
00085 #ifdef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
00086     _istream.clear();
00087     _istream.rdbuf()->pubsetbuf(const_cast<char *>(str), std::strlen(str));
00088     //    _istream.seekg(0);
00089 #else
00090     _istream.str(str);
00091 #endif
00092 
00093     if(!(_istream >> result))
00094       throw std::bad_cast();
00095 
00096     return result;
00097   }
00098 
00099   fmtflags flags() const {
00100     return _istream.flags();
00101   }
00102 
00103   fmtflags flags(const fmtflags flags) {
00104     return _istream.flags(flags);
00105   }
00106 
00107   fmtflags setf(const fmtflags flags) {
00108     return _istream.setf(flags);
00109   }
00110 
00111   fmtflags setf(const fmtflags flags,
00112                                const fmtflags mask)
00113   {
00114     return _istream.setf(flags, mask);
00115   }
00116 
00117   void unsetf(const fmtflags flags) {
00118     _istream.unsetf(flags);
00119   }
00120 
00121   iostate rdstate() const {
00122     return _istream.rdstate();
00123   }
00124 
00125   void clear(iostate state = istringstream::goodbit) {
00126     _istream.clear(state);
00127   }
00128 
00129   void setstate(iostate state) {
00130     _istream.setstate(state);
00131   }
00132 
00133 };
00134 
00135 #ifdef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
00136 #undef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
00137 #endif
00138 
00139 template<typename T>
00140 inline T string_to(const string & str)
00141   SSRC_DECL_THROW(std::bad_cast)
00142 {
00143   StringTo number_cast;
00144   return number_cast.cast<T>(str);
00145 }
00146 
00147 template<>
00148 inline string string_to<string>(const string & str)
00149   SSRC_DECL_THROW(std::bad_cast)
00150 {
00151   return str;
00152 }
00153 
00154 template<>
00155 inline const char * string_to<const char *>(const string & str)
00156   SSRC_DECL_THROW(std::bad_cast)
00157 {
00158   return str.c_str();
00159 }
00160 
00161 template<typename T>
00162 inline T string_to(const char *str)
00163   SSRC_DECL_THROW(std::bad_cast)
00164 {
00165   StringTo number_cast;
00166   return number_cast.cast<T>(str);
00167 }
00168 
00169 template<>
00170 inline string string_to<string>(const char *str)
00171   SSRC_DECL_THROW(std::bad_cast)
00172 {
00173   return string(str);
00174 }
00175 
00176 template<>
00177 inline const char * string_to<const char *>(const char *str)
00178   SSRC_DECL_THROW(std::bad_cast)
00179 {
00180   return str;
00181 }
00182 
00183 __END_NS_SSRC_WSPR_UTILITY
00184 
00185 #endif

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.