inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/tos
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_tos
00005 #define inlib_tos
00006 
00007 // Used in BatchLab/MemoryTuple and Rio_Tuple.
00008 // We need something different than the to<T>
00009 // to handle std::vector<> and bool.
00010 
00011 #include "sprintf"
00012 #include "charmanip"
00013 #include "typedefs"
00014 
00015 #include <vector>
00016 
00017 namespace inlib {
00018 
00019 inline std::string tos(unsigned char a_value){
00020   std::string s;
00021   if(is_printable(a_value)) 
00022     sprintf(s,32,"%c",a_value);
00023   else
00024     sprintf(s,32,"%d",a_value);
00025   return s;
00026 }
00027 
00028 inline std::string tos(char a_value){
00029   std::string s;
00030   if(is_printable(a_value)) 
00031     sprintf(s,32,"%c",a_value);
00032   else
00033     sprintf(s,32,"%d",a_value);
00034   return s;
00035 }
00036 
00037 inline std::string tos(unsigned short a_value) {
00038   std::string s;
00039   sprintf(s,32,"%d",a_value);
00040   return s;
00041 }
00042 
00043 inline std::string tos(short a_value){
00044   std::string s;
00045   sprintf(s,32,"%d",a_value);
00046   return s;
00047 }
00048 
00049 inline std::string tos(unsigned int a_value) {
00050   std::string s;
00051   sprintf(s,32,"%u",a_value);
00052   return s;
00053 }
00054 
00055 inline std::string tosx(unsigned int a_value) {
00056   std::string s;
00057   sprintf(s,32,"%x",a_value);
00058   return s;
00059 }
00060 
00061 inline std::string tos(int a_value){
00062   std::string s;
00063   sprintf(s,32,"%d",a_value);
00064   return s;
00065 }
00066 
00067 inline std::string tos(uint64 a_value) {
00068   std::string s;
00069   sprintf(s,32,uint64_format(),a_value);
00070   return s;
00071 }
00072 
00073 inline std::string tos(int64 a_value){
00074   std::string s;
00075   sprintf(s,32,int64_format(),a_value);
00076   return s;
00077 }
00078 
00079 inline std::string tos(float a_value){
00080   std::string s;
00081   sprintf(s,32,"%g",a_value);
00082   return s;
00083 }
00084 
00085 inline std::string tos(double a_value){
00086   std::string s;
00087   sprintf(s,32,"%g",a_value);
00088   return s;
00089 }
00090 
00091 inline std::string tos(bool a_value){
00092   return std::string(a_value?"true":"false");
00093 }
00094 
00095 inline std::string tos(const std::string& a_value){return a_value;}
00096 
00097 template <class T>  
00098 inline std::string tos(const std::vector<T>& a_value,
00099                        const std::string& a_sep = "\n" ) {
00100   unsigned int number = a_value.size();
00101   if(number<=0) return "";
00102   std::string result;
00103   number--;
00104   for(unsigned int index=0;index<number;index++) {
00105     result += tos(a_value[index]);
00106     result += a_sep;
00107   }
00108   result += tos(a_value[number]);
00109   return result;
00110 }
00111 
00112 }
00113 
00114 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines