LLVM API Documentation
00001 //===- Format.h - Efficient printf-style formatting for streams -*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the format() function, which can be used with other 00011 // LLVM subsystems to provide printf-style formatting. This gives all the power 00012 // and risk of printf. This can be used like this (with raw_ostreams as an 00013 // example): 00014 // 00015 // OS << "mynumber: " << format("%4.5f", 1234.412) << '\n'; 00016 // 00017 // Or if you prefer: 00018 // 00019 // OS << format("mynumber: %4.5f\n", 1234.412); 00020 // 00021 //===----------------------------------------------------------------------===// 00022 00023 #ifndef LLVM_SUPPORT_FORMAT_H 00024 #define LLVM_SUPPORT_FORMAT_H 00025 00026 #include <cassert> 00027 #include <cstdio> 00028 #ifdef _MSC_VER 00029 // FIXME: This define is wrong: 00030 // - _snprintf does not guarantee that trailing null is always added - if 00031 // there is no space for null, it does not report any error. 00032 // - According to C++ standard, snprintf should be visible in the 'std' 00033 // namespace - this define makes this impossible. 00034 #define snprintf _snprintf 00035 #endif 00036 00037 namespace llvm { 00038 00039 /// format_object_base - This is a helper class used for handling formatted 00040 /// output. It is the abstract base class of a templated derived class. 00041 class format_object_base { 00042 protected: 00043 const char *Fmt; 00044 virtual void home(); // Out of line virtual method. 00045 00046 /// snprint - Call snprintf() for this object, on the given buffer and size. 00047 virtual int snprint(char *Buffer, unsigned BufferSize) const = 0; 00048 00049 public: 00050 format_object_base(const char *fmt) : Fmt(fmt) {} 00051 virtual ~format_object_base() {} 00052 00053 /// print - Format the object into the specified buffer. On success, this 00054 /// returns the length of the formatted string. If the buffer is too small, 00055 /// this returns a length to retry with, which will be larger than BufferSize. 00056 unsigned print(char *Buffer, unsigned BufferSize) const { 00057 assert(BufferSize && "Invalid buffer size!"); 00058 00059 // Print the string, leaving room for the terminating null. 00060 int N = snprint(Buffer, BufferSize); 00061 00062 // VC++ and old GlibC return negative on overflow, just double the size. 00063 if (N < 0) 00064 return BufferSize*2; 00065 00066 // Other impls yield number of bytes needed, not including the final '\0'. 00067 if (unsigned(N) >= BufferSize) 00068 return N+1; 00069 00070 // Otherwise N is the length of output (not including the final '\0'). 00071 return N; 00072 } 00073 }; 00074 00075 /// format_object1 - This is a templated helper class used by the format 00076 /// function that captures the object to be formated and the format string. When 00077 /// actually printed, this synthesizes the string into a temporary buffer 00078 /// provided and returns whether or not it is big enough. 00079 template <typename T> 00080 class format_object1 : public format_object_base { 00081 T Val; 00082 public: 00083 format_object1(const char *fmt, const T &val) 00084 : format_object_base(fmt), Val(val) { 00085 } 00086 00087 virtual int snprint(char *Buffer, unsigned BufferSize) const { 00088 return snprintf(Buffer, BufferSize, Fmt, Val); 00089 } 00090 }; 00091 00092 /// format_object2 - This is a templated helper class used by the format 00093 /// function that captures the object to be formated and the format string. When 00094 /// actually printed, this synthesizes the string into a temporary buffer 00095 /// provided and returns whether or not it is big enough. 00096 template <typename T1, typename T2> 00097 class format_object2 : public format_object_base { 00098 T1 Val1; 00099 T2 Val2; 00100 public: 00101 format_object2(const char *fmt, const T1 &val1, const T2 &val2) 00102 : format_object_base(fmt), Val1(val1), Val2(val2) { 00103 } 00104 00105 virtual int snprint(char *Buffer, unsigned BufferSize) const { 00106 return snprintf(Buffer, BufferSize, Fmt, Val1, Val2); 00107 } 00108 }; 00109 00110 /// format_object3 - This is a templated helper class used by the format 00111 /// function that captures the object to be formated and the format string. When 00112 /// actually printed, this synthesizes the string into a temporary buffer 00113 /// provided and returns whether or not it is big enough. 00114 template <typename T1, typename T2, typename T3> 00115 class format_object3 : public format_object_base { 00116 T1 Val1; 00117 T2 Val2; 00118 T3 Val3; 00119 public: 00120 format_object3(const char *fmt, const T1 &val1, const T2 &val2,const T3 &val3) 00121 : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3) { 00122 } 00123 00124 virtual int snprint(char *Buffer, unsigned BufferSize) const { 00125 return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3); 00126 } 00127 }; 00128 00129 /// format_object4 - This is a templated helper class used by the format 00130 /// function that captures the object to be formated and the format string. When 00131 /// actually printed, this synthesizes the string into a temporary buffer 00132 /// provided and returns whether or not it is big enough. 00133 template <typename T1, typename T2, typename T3, typename T4> 00134 class format_object4 : public format_object_base { 00135 T1 Val1; 00136 T2 Val2; 00137 T3 Val3; 00138 T4 Val4; 00139 public: 00140 format_object4(const char *fmt, const T1 &val1, const T2 &val2, 00141 const T3 &val3, const T4 &val4) 00142 : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4) { 00143 } 00144 00145 virtual int snprint(char *Buffer, unsigned BufferSize) const { 00146 return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4); 00147 } 00148 }; 00149 00150 /// format_object5 - This is a templated helper class used by the format 00151 /// function that captures the object to be formated and the format string. When 00152 /// actually printed, this synthesizes the string into a temporary buffer 00153 /// provided and returns whether or not it is big enough. 00154 template <typename T1, typename T2, typename T3, typename T4, typename T5> 00155 class format_object5 : public format_object_base { 00156 T1 Val1; 00157 T2 Val2; 00158 T3 Val3; 00159 T4 Val4; 00160 T5 Val5; 00161 public: 00162 format_object5(const char *fmt, const T1 &val1, const T2 &val2, 00163 const T3 &val3, const T4 &val4, const T5 &val5) 00164 : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4), 00165 Val5(val5) { 00166 } 00167 00168 virtual int snprint(char *Buffer, unsigned BufferSize) const { 00169 return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4, Val5); 00170 } 00171 }; 00172 00173 /// This is a helper function that is used to produce formatted output. 00174 /// 00175 /// This is typically used like: 00176 /// \code 00177 /// OS << format("%0.4f", myfloat) << '\n'; 00178 /// \endcode 00179 template <typename T> 00180 inline format_object1<T> format(const char *Fmt, const T &Val) { 00181 return format_object1<T>(Fmt, Val); 00182 } 00183 00184 /// This is a helper function that is used to produce formatted output. 00185 /// 00186 /// This is typically used like: 00187 /// \code 00188 /// OS << format("%0.4f", myfloat) << '\n'; 00189 /// \endcode 00190 template <typename T1, typename T2> 00191 inline format_object2<T1, T2> format(const char *Fmt, const T1 &Val1, 00192 const T2 &Val2) { 00193 return format_object2<T1, T2>(Fmt, Val1, Val2); 00194 } 00195 00196 /// This is a helper function that is used to produce formatted output. 00197 /// 00198 /// This is typically used like: 00199 /// \code 00200 /// OS << format("%0.4f", myfloat) << '\n'; 00201 /// \endcode 00202 template <typename T1, typename T2, typename T3> 00203 inline format_object3<T1, T2, T3> format(const char *Fmt, const T1 &Val1, 00204 const T2 &Val2, const T3 &Val3) { 00205 return format_object3<T1, T2, T3>(Fmt, Val1, Val2, Val3); 00206 } 00207 00208 /// This is a helper function that is used to produce formatted output. 00209 /// 00210 /// This is typically used like: 00211 /// \code 00212 /// OS << format("%0.4f", myfloat) << '\n'; 00213 /// \endcode 00214 template <typename T1, typename T2, typename T3, typename T4> 00215 inline format_object4<T1, T2, T3, T4> format(const char *Fmt, const T1 &Val1, 00216 const T2 &Val2, const T3 &Val3, 00217 const T4 &Val4) { 00218 return format_object4<T1, T2, T3, T4>(Fmt, Val1, Val2, Val3, Val4); 00219 } 00220 00221 /// This is a helper function that is used to produce formatted output. 00222 /// 00223 /// This is typically used like: 00224 /// \code 00225 /// OS << format("%0.4f", myfloat) << '\n'; 00226 /// \endcode 00227 template <typename T1, typename T2, typename T3, typename T4, typename T5> 00228 inline format_object5<T1, T2, T3, T4, T5> format(const char *Fmt,const T1 &Val1, 00229 const T2 &Val2, const T3 &Val3, 00230 const T4 &Val4, const T5 &Val5) { 00231 return format_object5<T1, T2, T3, T4, T5>(Fmt, Val1, Val2, Val3, Val4, Val5); 00232 } 00233 00234 } // end namespace llvm 00235 00236 #endif