Removed trailing whitespace.
[oota-llvm.git] / include / llvm / Support / Format.h
1 //===--- Format.h - Efficient printf-style formatting for streams etc -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the format() function, which can be used with other
11 // LLVM subsystems to provide printf-style formatting.  This gives all the power
12 // and risk of printf.  This can be used like this (with raw_ostreams as an
13 // example):
14 //
15 //    OS << "mynumber: " << format("%4.5f", 1234.412) << '\n';
16 //
17 // Or if you prefer:
18 //
19 //  OS << format("mynumber: %4.5f\n", 1234.412);
20 //
21 //===----------------------------------------------------------------------===//
22
23 #ifndef LLVM_SUPPORT_FORMAT_H
24 #define LLVM_SUPPORT_FORMAT_H
25
26 #include <cstdio>
27 #ifdef WIN32
28 #define snprintf _snprintf
29 #endif
30
31 namespace llvm {
32
33 /// format_object_base - This is a helper class used for handling formatted
34 /// output.  It is the abstract base class of a templated derived class.
35 class format_object_base {
36 protected:
37   const char *Fmt;
38   virtual void home(); // Out of line virtual method.
39 public:
40   format_object_base(const char *fmt) : Fmt(fmt) {}
41   virtual ~format_object_base() {}
42
43   /// print - Format the object into the specified buffer.  On success, this
44   /// returns the length of the formatted string.  If the buffer is too small,
45   /// this returns a length to retry with, which will be larger than BufferSize.
46   virtual unsigned print(char *Buffer, unsigned BufferSize) const = 0;
47 };
48
49 /// format_object1 - This is a templated helper class used by the format
50 /// function that captures the object to be formated and the format string. When
51 /// actually printed, this synthesizes the string into a temporary buffer
52 /// provided and returns whether or not it is big enough.
53 template <typename T>
54 class format_object1 : public format_object_base {
55   T Val;
56 public:
57   format_object1(const char *fmt, const T &val)
58     : format_object_base(fmt), Val(val) {
59   }
60
61   /// print - Format the object into the specified buffer.  On success, this
62   /// returns the length of the formatted string.  If the buffer is too small,
63   /// this returns a length to retry with, which will be larger than BufferSize.
64   virtual unsigned print(char *Buffer, unsigned BufferSize) const {
65     int N = snprintf(Buffer, BufferSize-1, Fmt, Val);
66     if (N < 0)             // VC++ and old GlibC return negative on overflow.
67       return BufferSize*2;
68     if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
69       return N+1;
70     // If N is positive and <= BufferSize-1, then the string fit, yay.
71     return N;
72   }
73 };
74
75 /// format_object2 - This is a templated helper class used by the format
76 /// function that captures the object to be formated and the format string. When
77 /// actually printed, this synthesizes the string into a temporary buffer
78 /// provided and returns whether or not it is big enough.
79 template <typename T1, typename T2>
80 class format_object2 : public format_object_base {
81   T1 Val1;
82   T2 Val2;
83 public:
84   format_object2(const char *fmt, const T1 &val1, const T2 &val2)
85   : format_object_base(fmt), Val1(val1), Val2(val2) {
86   }
87
88   /// print - Format the object into the specified buffer.  On success, this
89   /// returns the length of the formatted string.  If the buffer is too small,
90   /// this returns a length to retry with, which will be larger than BufferSize.
91   virtual unsigned print(char *Buffer, unsigned BufferSize) const {
92     int N = snprintf(Buffer, BufferSize-1, Fmt, Val1, Val2);
93     if (N < 0)             // VC++ and old GlibC return negative on overflow.
94       return BufferSize*2;
95     if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
96       return N+1;
97     // If N is positive and <= BufferSize-1, then the string fit, yay.
98     return N;
99   }
100 };
101
102 /// format_object3 - This is a templated helper class used by the format
103 /// function that captures the object to be formated and the format string. When
104 /// actually printed, this synthesizes the string into a temporary buffer
105 /// provided and returns whether or not it is big enough.
106 template <typename T1, typename T2, typename T3>
107 class format_object3 : public format_object_base {
108   T1 Val1;
109   T2 Val2;
110   T3 Val3;
111 public:
112   format_object3(const char *fmt, const T1 &val1, const T2 &val2,const T3 &val3)
113     : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3) {
114   }
115
116   /// print - Format the object into the specified buffer.  On success, this
117   /// returns the length of the formatted string.  If the buffer is too small,
118   /// this returns a length to retry with, which will be larger than BufferSize.
119   virtual unsigned print(char *Buffer, unsigned BufferSize) const {
120     int N = snprintf(Buffer, BufferSize-1, Fmt, Val1, Val2, Val3);
121     if (N < 0)             // VC++ and old GlibC return negative on overflow.
122       return BufferSize*2;
123     if (unsigned(N) >= BufferSize-1)// Other impls yield number of bytes needed.
124       return N+1;
125     // If N is positive and <= BufferSize-1, then the string fit, yay.
126     return N;
127   }
128 };
129
130 /// format - This is a helper function that is used to produce formatted output.
131 /// This is typically used like:  OS << format("%0.4f", myfloat) << '\n';
132 template <typename T>
133 inline format_object1<T> format(const char *Fmt, const T &Val) {
134   return format_object1<T>(Fmt, Val);
135 }
136
137 /// format - This is a helper function that is used to produce formatted output.
138 /// This is typically used like:  OS << format("%0.4f", myfloat) << '\n';
139 template <typename T1, typename T2>
140 inline format_object2<T1, T2> format(const char *Fmt, const T1 &Val1,
141                                      const T2 &Val2) {
142   return format_object2<T1, T2>(Fmt, Val1, Val2);
143 }
144
145 /// format - This is a helper function that is used to produce formatted output.
146 /// This is typically used like:  OS << format("%0.4f", myfloat) << '\n';
147 template <typename T1, typename T2, typename T3>
148   inline format_object3<T1, T2, T3> format(const char *Fmt, const T1 &Val1,
149                                            const T2 &Val2, const T3 &Val3) {
150   return format_object3<T1, T2, T3>(Fmt, Val1, Val2, Val3);
151 }
152
153 } // end namespace llvm
154
155 #endif