LLVM API Documentation

Format.h

Go to the documentation of this file.
00001 //===--- Format.h - Efficient printf-style formatting for streams etc -----===//
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 <cstdio>
00027 #ifdef WIN32
00028 #define snprintf _snprintf
00029 #endif
00030 
00031 namespace llvm {
00032 
00033 /// format_object_base - This is a helper class used for handling formatted
00034 /// output.  It is the abstract base class of a templated derived class.
00035 class format_object_base {
00036 protected:
00037   const char *Fmt;
00038   virtual void home(); // Out of line virtual method.
00039 public:
00040   format_object_base(const char *fmt) : Fmt(fmt) {}
00041   virtual ~format_object_base() {}
00042   
00043   /// print - Format the object into the specified buffer.  On success, this
00044   /// returns the length of the formatted string.  If the buffer is too small,
00045   /// this returns a length to retry with, which will be larger than BufferSize.
00046   virtual unsigned print(char *Buffer, unsigned BufferSize) const = 0;
00047 };
00048 
00049 /// format_object1 - This is a templated helper class used by the format
00050 /// function that captures the object to be formated and the format string. When
00051 /// actually printed, this synthesizes the string into a temporary buffer
00052 /// provided and returns whether or not it is big enough.
00053 template <typename T>
00054 class format_object1 : public format_object_base {
00055   T Val;
00056 public:
00057   format_object1(const char *fmt, const T &val)
00058     : format_object_base(fmt), Val(val) {
00059   }
00060   
00061   /// print - Format the object into the specified buffer.  On success, this
00062   /// returns the length of the formatted string.  If the buffer is too small,
00063   /// this returns a length to retry with, which will be larger than BufferSize.
00064   virtual unsigned print(char *Buffer, unsigned BufferSize) const {
00065     int N = snprintf(Buffer, BufferSize-1, Fmt, Val);
00066     if (N < 0)             // VC++ and old GlibC return negative on overflow.
00067       return BufferSize*2;
00068     if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
00069       return N+1;
00070     // If N is positive and <= BufferSize-1, then the string fit, yay.
00071     return N;
00072   }
00073 };
00074   
00075 /// format_object2 - 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 T1, typename T2>
00080 class format_object2 : public format_object_base {
00081   T1 Val1;
00082   T2 Val2;
00083 public:
00084   format_object2(const char *fmt, const T1 &val1, const T2 &val2)
00085   : format_object_base(fmt), Val1(val1), Val2(val2) {
00086   }
00087   
00088   /// print - Format the object into the specified buffer.  On success, this
00089   /// returns the length of the formatted string.  If the buffer is too small,
00090   /// this returns a length to retry with, which will be larger than BufferSize.
00091   virtual unsigned print(char *Buffer, unsigned BufferSize) const {
00092     int N = snprintf(Buffer, BufferSize-1, Fmt, Val1, Val2);
00093     if (N < 0)             // VC++ and old GlibC return negative on overflow.
00094       return BufferSize*2;
00095     if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
00096       return N+1;
00097     // If N is positive and <= BufferSize-1, then the string fit, yay.
00098     return N;
00099   }
00100 };
00101 
00102 /// format_object3 - This is a templated helper class used by the format
00103 /// function that captures the object to be formated and the format string. When
00104 /// actually printed, this synthesizes the string into a temporary buffer
00105 /// provided and returns whether or not it is big enough.
00106 template <typename T1, typename T2, typename T3>
00107 class format_object3 : public format_object_base {
00108   T1 Val1;
00109   T2 Val2;
00110   T3 Val3;
00111 public:
00112   format_object3(const char *fmt, const T1 &val1, const T2 &val2,const T3 &val3)
00113     : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3) {
00114   }
00115   
00116   /// print - Format the object into the specified buffer.  On success, this
00117   /// returns the length of the formatted string.  If the buffer is too small,
00118   /// this returns a length to retry with, which will be larger than BufferSize.
00119   virtual unsigned print(char *Buffer, unsigned BufferSize) const {
00120     int N = snprintf(Buffer, BufferSize-1, Fmt, Val1, Val2, Val3);
00121     if (N < 0)             // VC++ and old GlibC return negative on overflow.
00122       return BufferSize*2;
00123     if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
00124       return N+1;
00125     // If N is positive and <= BufferSize-1, then the string fit, yay.
00126     return N;
00127   }
00128 };
00129 
00130 /// format - This is a helper function that is used to produce formatted output.
00131 /// This is typically used like:  OS << format("%0.4f", myfloat) << '\n';
00132 template <typename T>
00133 inline format_object1<T> format(const char *Fmt, const T &Val) {
00134   return format_object1<T>(Fmt, Val);
00135 }
00136 
00137 /// format - This is a helper function that is used to produce formatted output.
00138 /// This is typically used like:  OS << format("%0.4f", myfloat) << '\n';
00139 template <typename T1, typename T2>
00140 inline format_object2<T1, T2> format(const char *Fmt, const T1 &Val1,
00141                                      const T2 &Val2) {
00142   return format_object2<T1, T2>(Fmt, Val1, Val2);
00143 }
00144 
00145 /// format - This is a helper function that is used to produce formatted output.
00146 /// This is typically used like:  OS << format("%0.4f", myfloat) << '\n';
00147 template <typename T1, typename T2, typename T3>
00148   inline format_object3<T1, T2, T3> format(const char *Fmt, const T1 &Val1,
00149                                            const T2 &Val2, const T3 &Val3) {
00150   return format_object3<T1, T2, T3>(Fmt, Val1, Val2, Val3);
00151 }
00152   
00153 } // end namespace llvm
00154 
00155 #endif