Web Wispers 1.2.0 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/utility - StringTo.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.0 C++ Unit Tests Lines: 10 11 90.9 %
Date: 2010-05-27 Functions: 6 6 100.0 %
Branches: 4 8 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     http://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the StringTo class.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_UTILITY_STRING_TO_H
      23                 :            : #define __SSRC_WSPR_UTILITY_STRING_TO_H
      24                 :            : 
      25                 :            : #include <ssrc/wispers-packages.h>
      26                 :            : 
      27                 :            : #include <sstream>
      28                 :            : 
      29                 :            : __BEGIN_NS_SSRC_WSPR_UTILITY
      30                 :            : 
      31                 :            : using std::string;
      32                 :            : using std::istringstream;
      33                 :            : 
      34                 :            : #if defined(__GNUC__) && (__GNUC__ >= 4)
      35                 :            : #define __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
      36                 :            : #endif
      37                 :            : 
      38                 :            : /**
      39                 :            :  *
      40                 :            :  */
      41                 :          4 : class StringTo {
      42                 :            :   istringstream _istream;
      43                 :            : 
      44                 :            : public:
      45                 :            :   typedef istringstream::fmtflags fmtflags;
      46                 :            :   typedef istringstream::iostate iostate;
      47                 :            : 
      48                 :            :   StringTo(const StringTo &) { }
      49                 :            : 
      50                 :          4 :   StringTo() { }
      51                 :            : 
      52                 :            :   /**
      53                 :            :    * Converts a string to an object.
      54                 :            :    *
      55                 :            :    * 
      56                 :            :    * 
      57                 :            :    * 
      58                 :            :    * 
      59                 :            :    */
      60                 :            :   template<typename T>  T cast(const string & str)
      61                 :            :     SSRC_DECL_THROW(std::bad_cast)
      62                 :            :   {
      63                 :            :     T result;
      64                 :            : 
      65                 :            : #ifdef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
      66                 :            :     _istream.clear();
      67                 :            :     _istream.rdbuf()->pubsetbuf(const_cast<char *>(str.c_str()), str.size());
      68                 :            :     //    _istream.seekg(0);
      69                 :            : #else
      70                 :            :     _istream.str(str);
      71                 :            : #endif
      72                 :            : 
      73                 :            :     if(!(_istream >> result))
      74                 :            :       throw std::bad_cast();
      75                 :            : 
      76                 :            :     return result;
      77                 :            :   }
      78                 :            : 
      79                 :          6 :   template<typename T>  T cast(const char *str)
      80                 :            :     SSRC_DECL_THROW(std::bad_cast)
      81                 :            :   {
      82                 :            :     T result;
      83                 :            : 
      84                 :            : #ifdef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
      85                 :          6 :     _istream.clear();
      86                 :          6 :     _istream.rdbuf()->pubsetbuf(const_cast<char *>(str), std::strlen(str));
      87                 :            :     //    _istream.seekg(0);
      88                 :            : #else
      89                 :            :     _istream.str(str);
      90                 :            : #endif
      91                 :            : 
      92   [ -  +  -  + ]:          6 :     if(!(_istream >> result))
      93                 :          0 :       throw std::bad_cast();
      94                 :            : 
      95                 :          6 :     return result;
      96                 :            :   }
      97                 :            : 
      98                 :            :   fmtflags flags() const {
      99                 :            :     return _istream.flags();
     100                 :            :   }
     101                 :            : 
     102                 :            :   fmtflags flags(const fmtflags flags) {
     103                 :            :     return _istream.flags(flags);
     104                 :            :   }
     105                 :            : 
     106                 :            :   fmtflags setf(const fmtflags flags) {
     107                 :            :     return _istream.setf(flags);
     108                 :            :   }
     109                 :            : 
     110                 :            :   fmtflags setf(const fmtflags flags,
     111                 :            :                                const fmtflags mask)
     112                 :            :   {
     113                 :            :     return _istream.setf(flags, mask);
     114                 :            :   }
     115                 :            : 
     116                 :            :   void unsetf(const fmtflags flags) {
     117                 :            :     _istream.unsetf(flags);
     118                 :            :   }
     119                 :            : 
     120                 :            :   iostate rdstate() const {
     121                 :            :     return _istream.rdstate();
     122                 :            :   }
     123                 :            : 
     124                 :            :   void clear(iostate state = istringstream::goodbit) {
     125                 :            :     _istream.clear(state);
     126                 :            :   }
     127                 :            : 
     128                 :            :   void setstate(iostate state) {
     129                 :            :     _istream.setstate(state);
     130                 :            :   }
     131                 :            : 
     132                 :            : };
     133                 :            : 
     134                 :            : #ifdef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
     135                 :            : #undef __WSPR_LEXICAL_CAST_USE_STATIC_BUFFER
     136                 :            : #endif
     137                 :            : 
     138                 :            : template<typename T>
     139                 :            : inline T string_to(const string & str)
     140                 :            :   SSRC_DECL_THROW(std::bad_cast)
     141                 :            : {
     142                 :            :   StringTo number_cast;
     143                 :            :   return number_cast.cast<T>(str);
     144                 :            : }
     145                 :            : 
     146                 :            : template<>
     147                 :            : inline string string_to<string>(const string & str)
     148                 :            :   SSRC_DECL_THROW(std::bad_cast)
     149                 :            : {
     150                 :            :   return str;
     151                 :            : }
     152                 :            : 
     153                 :            : template<>
     154                 :            : inline const char * string_to<const char *>(const string & str)
     155                 :            :   SSRC_DECL_THROW(std::bad_cast)
     156                 :            : {
     157                 :            :   return str.c_str();
     158                 :            : }
     159                 :            : 
     160                 :            : template<typename T>
     161                 :          3 : inline T string_to(const char *str)
     162                 :            :   SSRC_DECL_THROW(std::bad_cast)
     163                 :            : {
     164                 :          6 :   StringTo number_cast;
     165   [ +  -  +  - ]:          3 :   return number_cast.cast<T>(str);
     166                 :            : }
     167                 :            : 
     168                 :            : template<>
     169                 :            : inline string string_to<string>(const char *str)
     170                 :            :   SSRC_DECL_THROW(std::bad_cast)
     171                 :            : {
     172                 :            :   return string(str);
     173                 :            : }
     174                 :            : 
     175                 :            : template<>
     176                 :            : inline const char * string_to<const char *>(const char *str)
     177                 :            :   SSRC_DECL_THROW(std::bad_cast)
     178                 :            : {
     179                 :            :   return str;
     180                 :            : }
     181                 :            : 
     182                 :            : __END_NS_SSRC_WSPR_UTILITY
     183                 :            : 
     184                 :            : #endif