sprint 1-alpha
sprint/smart_cast.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::smart_cast
00003 *
00004 *   Copyright (C) 2005-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00022 #ifndef _SMART_CAST_H
00023 #define _SMART_CAST_H
00024 
00028 #include <cstdlib>
00029 #include <cstdio>
00030 #include <string>
00031 #include "buffer_cast.h"
00032 
00033 namespace sprint {
00034 
00035 /**** No OverHead per le conversioni const char * a numero 
00036     *  OverHead fisso per le conversioni numero a const char *
00037     */
00038 
00040 template<class D, class S>
00041 D smart_cast(S s) { return static_cast<D>(s); }
00042 
00044 template<class D, class S, class P>
00045 D smart_cast(S s, P p);
00046 
00047 template<>
00048 inline int smart_cast<int, const char *>(const char * str) { return atoi(str); }
00049 
00050 template<>
00051 inline int smart_cast<int, std::string>(std::string str) { return atoi(str.c_str() ); }
00052 
00053 template<>
00054 inline int smart_cast<int, char *>(char * str) { return atoi(str); }
00055 
00056 template<>
00057 inline unsigned int smart_cast<unsigned int, const char *>(const char * str) { return atoi(str); }
00058 
00059 template<>
00060 inline unsigned int smart_cast<unsigned int, std::string>(std::string str) { return atoi(str.c_str() ); }
00061 
00062 template<>
00063 inline unsigned int smart_cast<unsigned int, char *>(char * str) { return atoi(str); }
00064 
00065 template<>
00066 inline long smart_cast<long, const char *>(const char *str) { return strtol(str, NULL, 10); }
00067 
00068 template<>
00069 inline long smart_cast<long, std::string>(std::string str) { return strtol(str.c_str(), NULL, 10); }
00070 
00071 template<>
00072 inline long smart_cast<long, char *>(char *str) { return strtol(str, NULL, 10); }
00073 
00074 template<>
00075 inline unsigned long smart_cast<unsigned long, const char *>(const char *str) { return strtoul(str, NULL,10) ; }
00076 
00077 template<>
00078 inline unsigned long smart_cast<unsigned long, char *>(char *str) { return strtoul(str, NULL,10) ; }
00079 
00080 template<>
00081 inline float smart_cast<float, const char *>(const char *str) { return (float) atof(str); }
00082 
00083 template<>
00084 inline float smart_cast<float, std::string>(std::string str) { return (float) atof(str.c_str() ); }
00085 
00086 template<>
00087 inline float smart_cast<float, char *>(char *str) { return (float) atof(str); }
00088  
00089 template<>
00090 inline double smart_cast<double, const char *>(const char *str) { return strtod(str, NULL); }
00091 
00092 template<>
00093 inline double smart_cast<double, std::string>(std::string str) { return strtod(str.c_str(), NULL); }
00094 
00095 template<>
00096 inline bool smart_cast<bool, std::string>(std::string str) 
00097        {
00098        return (str=="true");
00099        }
00100 
00101 template<>
00102 inline const char *smart_cast<const char *, std::string>(std::string str) 
00103        {
00104        return str.c_str();
00105        }
00106 
00107 template<>
00108 inline std::string smart_cast<std::string, bool>(bool value) 
00109        {
00110        return (value) ? "true" : "false";
00111        }
00112 
00113 template<>
00114 inline const char * smart_cast<const char *, bool>(bool value) 
00115        {
00116        return (value) ? "true" : "false";
00117        }
00118        
00119 template<>
00120 inline double smart_cast<double, char *>(char *str) { return strtod(str, NULL); }
00121 
00122 template<>
00123 inline std::string smart_cast<std::string, int>(int n)
00124 {
00125  return std::string( buffer_cast(n) );
00126 }
00127 
00128 template<>
00129 inline std::string smart_cast<std::string, unsigned int>(unsigned int n)
00130 {
00131  return std::string( buffer_cast(n) );
00132 }
00133 
00134 template<>
00135 inline std::string smart_cast<std::string, long int>(long int n)
00136 {
00137  return std::string( buffer_cast(n) );
00138 }
00139 
00140 template<>
00141 inline std::string smart_cast<std::string, unsigned long int>(unsigned long int n)
00142 {
00143  return std::string( buffer_cast(n) );
00144 }
00145 
00146 template<>
00147 inline std::string smart_cast<std::string, double>(double n)
00148 {
00149  return std::string( buffer_cast(n) );
00150 }
00151 
00152 template<>
00153 inline std::string smart_cast<std::string, double>(double n, int d)
00154 {
00155 return std::string( buffer_cast(n, d) );
00156 }
00157 
00158 template<>
00159 inline std::string smart_cast<std::string, float>(float n)
00160 {
00161  return std::string( buffer_cast(n) );
00162 }
00163 
00164 template<>
00165 inline std::string smart_cast<std::string, float>(float n, int d)
00166 {
00167  return std::string( buffer_cast(n, d) );
00168 }
00169 
00170 } // namespace sprint
00171 
00172 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines