/3rd_party/llvm/include/llvm/Support/Format.h

https://code.google.com/p/softart/ · C++ Header · 236 lines · 122 code · 28 blank · 86 comment · 3 complexity · bf2e6ec12853d45d6f2c7a5ded7a65be MD5 · raw file

  1. //===- Format.h - Efficient printf-style formatting for streams -*- C++ -*-===//
  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. #ifndef LLVM_SUPPORT_FORMAT_H
  23. #define LLVM_SUPPORT_FORMAT_H
  24. #include <cassert>
  25. #include <cstdio>
  26. #ifdef _MSC_VER
  27. // FIXME: This define is wrong:
  28. // - _snprintf does not guarantee that trailing null is always added - if
  29. // there is no space for null, it does not report any error.
  30. // - According to C++ standard, snprintf should be visible in the 'std'
  31. // namespace - this define makes this impossible.
  32. #define snprintf _snprintf
  33. #endif
  34. namespace llvm {
  35. /// format_object_base - This is a helper class used for handling formatted
  36. /// output. It is the abstract base class of a templated derived class.
  37. class format_object_base {
  38. protected:
  39. const char *Fmt;
  40. virtual void home(); // Out of line virtual method.
  41. /// snprint - Call snprintf() for this object, on the given buffer and size.
  42. virtual int snprint(char *Buffer, unsigned BufferSize) const = 0;
  43. public:
  44. format_object_base(const char *fmt) : Fmt(fmt) {}
  45. virtual ~format_object_base() {}
  46. /// print - Format the object into the specified buffer. On success, this
  47. /// returns the length of the formatted string. If the buffer is too small,
  48. /// this returns a length to retry with, which will be larger than BufferSize.
  49. unsigned print(char *Buffer, unsigned BufferSize) const {
  50. assert(BufferSize && "Invalid buffer size!");
  51. // Print the string, leaving room for the terminating null.
  52. int N = snprint(Buffer, BufferSize);
  53. // VC++ and old GlibC return negative on overflow, just double the size.
  54. if (N < 0)
  55. return BufferSize*2;
  56. // Other impls yield number of bytes needed, not including the final '\0'.
  57. if (unsigned(N) >= BufferSize)
  58. return N+1;
  59. // Otherwise N is the length of output (not including the final '\0').
  60. return N;
  61. }
  62. };
  63. /// format_object1 - This is a templated helper class used by the format
  64. /// function that captures the object to be formated and the format string. When
  65. /// actually printed, this synthesizes the string into a temporary buffer
  66. /// provided and returns whether or not it is big enough.
  67. template <typename T>
  68. class format_object1 : public format_object_base {
  69. T Val;
  70. public:
  71. format_object1(const char *fmt, const T &val)
  72. : format_object_base(fmt), Val(val) {
  73. }
  74. virtual int snprint(char *Buffer, unsigned BufferSize) const {
  75. return snprintf(Buffer, BufferSize, Fmt, Val);
  76. }
  77. };
  78. /// format_object2 - This is a templated helper class used by the format
  79. /// function that captures the object to be formated and the format string. When
  80. /// actually printed, this synthesizes the string into a temporary buffer
  81. /// provided and returns whether or not it is big enough.
  82. template <typename T1, typename T2>
  83. class format_object2 : public format_object_base {
  84. T1 Val1;
  85. T2 Val2;
  86. public:
  87. format_object2(const char *fmt, const T1 &val1, const T2 &val2)
  88. : format_object_base(fmt), Val1(val1), Val2(val2) {
  89. }
  90. virtual int snprint(char *Buffer, unsigned BufferSize) const {
  91. return snprintf(Buffer, BufferSize, Fmt, Val1, Val2);
  92. }
  93. };
  94. /// format_object3 - This is a templated helper class used by the format
  95. /// function that captures the object to be formated and the format string. When
  96. /// actually printed, this synthesizes the string into a temporary buffer
  97. /// provided and returns whether or not it is big enough.
  98. template <typename T1, typename T2, typename T3>
  99. class format_object3 : public format_object_base {
  100. T1 Val1;
  101. T2 Val2;
  102. T3 Val3;
  103. public:
  104. format_object3(const char *fmt, const T1 &val1, const T2 &val2,const T3 &val3)
  105. : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3) {
  106. }
  107. virtual int snprint(char *Buffer, unsigned BufferSize) const {
  108. return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3);
  109. }
  110. };
  111. /// format_object4 - This is a templated helper class used by the format
  112. /// function that captures the object to be formated and the format string. When
  113. /// actually printed, this synthesizes the string into a temporary buffer
  114. /// provided and returns whether or not it is big enough.
  115. template <typename T1, typename T2, typename T3, typename T4>
  116. class format_object4 : public format_object_base {
  117. T1 Val1;
  118. T2 Val2;
  119. T3 Val3;
  120. T4 Val4;
  121. public:
  122. format_object4(const char *fmt, const T1 &val1, const T2 &val2,
  123. const T3 &val3, const T4 &val4)
  124. : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4) {
  125. }
  126. virtual int snprint(char *Buffer, unsigned BufferSize) const {
  127. return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4);
  128. }
  129. };
  130. /// format_object5 - This is a templated helper class used by the format
  131. /// function that captures the object to be formated and the format string. When
  132. /// actually printed, this synthesizes the string into a temporary buffer
  133. /// provided and returns whether or not it is big enough.
  134. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  135. class format_object5 : public format_object_base {
  136. T1 Val1;
  137. T2 Val2;
  138. T3 Val3;
  139. T4 Val4;
  140. T5 Val5;
  141. public:
  142. format_object5(const char *fmt, const T1 &val1, const T2 &val2,
  143. const T3 &val3, const T4 &val4, const T5 &val5)
  144. : format_object_base(fmt), Val1(val1), Val2(val2), Val3(val3), Val4(val4),
  145. Val5(val5) {
  146. }
  147. virtual int snprint(char *Buffer, unsigned BufferSize) const {
  148. return snprintf(Buffer, BufferSize, Fmt, Val1, Val2, Val3, Val4, Val5);
  149. }
  150. };
  151. /// This is a helper function that is used to produce formatted output.
  152. ///
  153. /// This is typically used like:
  154. /// \code
  155. /// OS << format("%0.4f", myfloat) << '\n';
  156. /// \endcode
  157. template <typename T>
  158. inline format_object1<T> format(const char *Fmt, const T &Val) {
  159. return format_object1<T>(Fmt, Val);
  160. }
  161. /// This is a helper function that is used to produce formatted output.
  162. ///
  163. /// This is typically used like:
  164. /// \code
  165. /// OS << format("%0.4f", myfloat) << '\n';
  166. /// \endcode
  167. template <typename T1, typename T2>
  168. inline format_object2<T1, T2> format(const char *Fmt, const T1 &Val1,
  169. const T2 &Val2) {
  170. return format_object2<T1, T2>(Fmt, Val1, Val2);
  171. }
  172. /// This is a helper function that is used to produce formatted output.
  173. ///
  174. /// This is typically used like:
  175. /// \code
  176. /// OS << format("%0.4f", myfloat) << '\n';
  177. /// \endcode
  178. template <typename T1, typename T2, typename T3>
  179. inline format_object3<T1, T2, T3> format(const char *Fmt, const T1 &Val1,
  180. const T2 &Val2, const T3 &Val3) {
  181. return format_object3<T1, T2, T3>(Fmt, Val1, Val2, Val3);
  182. }
  183. /// This is a helper function that is used to produce formatted output.
  184. ///
  185. /// This is typically used like:
  186. /// \code
  187. /// OS << format("%0.4f", myfloat) << '\n';
  188. /// \endcode
  189. template <typename T1, typename T2, typename T3, typename T4>
  190. inline format_object4<T1, T2, T3, T4> format(const char *Fmt, const T1 &Val1,
  191. const T2 &Val2, const T3 &Val3,
  192. const T4 &Val4) {
  193. return format_object4<T1, T2, T3, T4>(Fmt, Val1, Val2, Val3, Val4);
  194. }
  195. /// This is a helper function that is used to produce formatted output.
  196. ///
  197. /// This is typically used like:
  198. /// \code
  199. /// OS << format("%0.4f", myfloat) << '\n';
  200. /// \endcode
  201. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  202. inline format_object5<T1, T2, T3, T4, T5> format(const char *Fmt,const T1 &Val1,
  203. const T2 &Val2, const T3 &Val3,
  204. const T4 &Val4, const T5 &Val5) {
  205. return format_object5<T1, T2, T3, T4, T5>(Fmt, Val1, Val2, Val3, Val4, Val5);
  206. }
  207. } // end namespace llvm
  208. #endif