sprint 1-alpha
sprint/buffer_cast.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::buffer_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 _BUFFER_CAST_H
00023 #define _BUFFER_CAST_H
00024 
00028 #if defined (_MSC_VER)
00029 // avoid bogus warning....
00030 # define _CRT_SECURE_NO_WARNINGS
00031 #endif
00032 
00033 #include <cstdlib>
00034 #include <cstdio>
00035 #include <string>
00036 
00041 namespace sprint {
00042 
00043 
00044 namespace detail {
00046         char *cast(char *p, int buf_len, unsigned long un);
00048         char *cast(char *p, int buf_len, long un);
00049 
00050         inline char *cast(char *p, int buf_len, unsigned int un)
00051         {
00052                 return cast(p, buf_len, (unsigned long) un);
00053         }
00054 
00055         inline char *cast(char *p, int buf_len, int un)
00056         {
00057                 return cast(p, buf_len, (long) un);
00058         }
00059 
00060         inline char *cast(char *p, int buf_len, short unsigned int un)
00061         {
00062                 return cast(p, buf_len, (unsigned long) un);
00063         }
00064 
00065         inline char *cast(char *p, int buf_len, short int un)
00066         {
00067                 return cast(p, buf_len, (long) un);
00068         }
00069 
00070         inline char *cast(char *p, int buf_len, unsigned char un)
00071         {
00072                 return cast(p, buf_len, (unsigned long) un);
00073         }
00074 
00075         inline char *cast(char *p, int buf_len, signed char un)
00076         {
00077                 return cast(p, buf_len, (long) un);
00078         }
00079 
00080         inline char *cast(char *p, int buf_len, char un)
00081         {
00082                 p[0]=un;
00083                 p[1]=0;
00084                 return p;
00085         }
00086 
00088         inline const char * cast(char *buf, int buf_len, bool value)
00089         {
00090         return (value) ? "true" : "false";
00091         }
00092 
00094         inline char * cast(char *buf, int buf_len, double value)
00095         {
00096          sprintf(buf,"%f", value);
00097          return buf;
00098         }
00099 
00101         inline char * cast(char *buf, int buf_len, double value, int prec)
00102         {
00103          sprintf(buf,"%.*f", prec, value);
00104          return buf;
00105         }
00106 
00108         inline char * cast(char *buf, int buf_len, float value)
00109         {
00110          sprintf(buf,"%f", value);
00111          return buf;
00112         }
00113 
00115         inline char * cast(char *buf, int buf_len, float value, int prec)
00116         {
00117          sprintf(buf,"%.*f", prec, value);
00118          return buf;
00119         }
00120 
00121 };
00122 
00124 class buffer_cast {
00125       static const int max_size = 23; // up to 64 bit number
00126 
00128       char buf[max_size + 1];
00130       const char *ptr;
00131       
00132       public:
00133              
00135       template<class T>
00136       buffer_cast(const T & t)
00137           {
00138                   ptr = detail::cast(buf, max_size, t);
00139           }
00140 
00142       template<class T, class P>
00143       buffer_cast(T t, P p)
00144           {
00145                   ptr = detail::cast(buf, max_size, t, p);
00146           }
00147       
00149       operator const char *() const { return ptr; }
00150              
00152       const char *c_str() const { return ptr; }
00153       
00154       };
00155 
00156 } // namespace sprint
00157 
00158 // #endif // NO MSC
00159 
00160 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines