117 #ifndef TINYFORMAT_H_INCLUDED
118 #define TINYFORMAT_H_INCLUDED
120 namespace tinyformat {}
125 namespace tfm = tinyformat;
142 #ifndef TINYFORMAT_ERROR
143 # define TINYFORMAT_ERROR(reason) assert(0 && reason)
146 #if !defined(TINYFORMAT_USE_VARIADIC_TEMPLATES) && !defined(TINYFORMAT_NO_VARIADIC_TEMPLATES)
147 # ifdef __GXX_EXPERIMENTAL_CXX0X__
148 # define TINYFORMAT_USE_VARIADIC_TEMPLATES
152 #if defined(__GLIBCXX__) && __GLIBCXX__ < 20080201
155 # define TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
161 # define TINYFORMAT_HIDDEN __attribute__((visibility("hidden")))
163 # define TINYFORMAT_HIDDEN
166 namespace tinyformat {
172 template <
typename T1,
typename T2>
177 struct fail {
char dummy[2]; };
178 struct succeed {
char dummy; };
180 static fail tryConvert(...);
181 static succeed tryConvert(
const T2&);
182 static const T1& makeT1();
186 # pragma warning(push)
187 # pragma warning(disable:4244)
188 # pragma warning(disable:4267)
195 sizeof(tryConvert(makeT1())) ==
sizeof(succeed);
197 # pragma warning(pop)
215 static void invoke(std::ostream& ,
const T& ) { assert(0); }
219 template<
typename T,
typename fmtT>
223 { out << static_cast<fmtT>(
value); }
226 #ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
228 struct formatZeroIntegerWorkaround
230 static bool invoke(std::ostream& ,
const T& ) {
return false; }
233 struct formatZeroIntegerWorkaround<
T,true>
235 static bool invoke(std::ostream& out,
const T&
value)
237 if (static_cast<int>(value) == 0 && out.flags() & std::ios::showpos)
245 #endif // TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
255 "integer for use as variable width or precision");
263 static int invoke(
const T& value) {
return static_cast<int>(
value); }
270 std::ostringstream tmp;
271 tmp.imbue (out.getloc());
274 out.write(result.c_str(), (
std::min)(ntrunc, static_cast<int>(result.size())));
276 #define TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(type) \
277 inline void formatTruncated(std::ostream& out, type* value, int ntrunc) \
279 std::streamsize len = 0; \
280 while(len < ntrunc && value[len] != 0) \
282 out.write(value, len); \
288 #undef TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR
311 const char* fmtEnd,
int ntrunc,
const T& value)
313 #ifndef TINYFORMAT_ALLOW_WCHAR_STRINGS
326 if(canConvertToChar && *(fmtEnd-1) ==
'c')
328 else if(canConvertToVoidPtr && *(fmtEnd-1) ==
'p')
330 #ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
331 else if(detail::formatZeroIntegerWorkaround<T>::invoke(out, value)) ;
345 #define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType) \
346 inline void formatValue(std::ostream& out, const char* , \
347 const char* fmtEnd, int , charType value) \
349 switch(*(fmtEnd-1)) \
351 case 'u': case 'd': case 'i': case 'o': case 'X': case 'x': \
352 out << static_cast<int>(value); break; \
354 out << value; break; \
361 #undef TINYFORMAT_DEFINE_FORMATVALUE_CHAR
369 #define TINYFORMAT_ARGTYPES(n) TINYFORMAT_ARGTYPES_ ## n
370 #define TINYFORMAT_VARARGS(n) TINYFORMAT_VARARGS_ ## n
371 #define TINYFORMAT_PASSARGS(n) TINYFORMAT_PASSARGS_ ## n
372 #define TINYFORMAT_PASSARGS_TAIL(n) TINYFORMAT_PASSARGS_TAIL_ ## n
410 #define TINYFORMAT_ARGTYPES_1 class T1
411 #define TINYFORMAT_ARGTYPES_2 class T1, class T2
412 #define TINYFORMAT_ARGTYPES_3 class T1, class T2, class T3
413 #define TINYFORMAT_ARGTYPES_4 class T1, class T2, class T3, class T4
414 #define TINYFORMAT_ARGTYPES_5 class T1, class T2, class T3, class T4, class T5
415 #define TINYFORMAT_ARGTYPES_6 class T1, class T2, class T3, class T4, class T5, class T6
416 #define TINYFORMAT_ARGTYPES_7 class T1, class T2, class T3, class T4, class T5, class T6, class T7
417 #define TINYFORMAT_ARGTYPES_8 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8
418 #define TINYFORMAT_ARGTYPES_9 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9
419 #define TINYFORMAT_ARGTYPES_10 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10
420 #define TINYFORMAT_ARGTYPES_11 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11
421 #define TINYFORMAT_ARGTYPES_12 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12
422 #define TINYFORMAT_ARGTYPES_13 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13
423 #define TINYFORMAT_ARGTYPES_14 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14
424 #define TINYFORMAT_ARGTYPES_15 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15
425 #define TINYFORMAT_ARGTYPES_16 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10, class T11, class T12, class T13, class T14, class T15, class T16
427 #define TINYFORMAT_VARARGS_1 const T1& v1
428 #define TINYFORMAT_VARARGS_2 const T1& v1, const T2& v2
429 #define TINYFORMAT_VARARGS_3 const T1& v1, const T2& v2, const T3& v3
430 #define TINYFORMAT_VARARGS_4 const T1& v1, const T2& v2, const T3& v3, const T4& v4
431 #define TINYFORMAT_VARARGS_5 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5
432 #define TINYFORMAT_VARARGS_6 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6
433 #define TINYFORMAT_VARARGS_7 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7
434 #define TINYFORMAT_VARARGS_8 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8
435 #define TINYFORMAT_VARARGS_9 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9
436 #define TINYFORMAT_VARARGS_10 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10
437 #define TINYFORMAT_VARARGS_11 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11
438 #define TINYFORMAT_VARARGS_12 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12
439 #define TINYFORMAT_VARARGS_13 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13
440 #define TINYFORMAT_VARARGS_14 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13, const T14& v14
441 #define TINYFORMAT_VARARGS_15 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13, const T14& v14, const T15& v15
442 #define TINYFORMAT_VARARGS_16 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7, const T8& v8, const T9& v9, const T10& v10, const T11& v11, const T12& v12, const T13& v13, const T14& v14, const T15& v15, const T16& v16
444 #define TINYFORMAT_PASSARGS_1 v1
445 #define TINYFORMAT_PASSARGS_2 v1, v2
446 #define TINYFORMAT_PASSARGS_3 v1, v2, v3
447 #define TINYFORMAT_PASSARGS_4 v1, v2, v3, v4
448 #define TINYFORMAT_PASSARGS_5 v1, v2, v3, v4, v5
449 #define TINYFORMAT_PASSARGS_6 v1, v2, v3, v4, v5, v6
450 #define TINYFORMAT_PASSARGS_7 v1, v2, v3, v4, v5, v6, v7
451 #define TINYFORMAT_PASSARGS_8 v1, v2, v3, v4, v5, v6, v7, v8
452 #define TINYFORMAT_PASSARGS_9 v1, v2, v3, v4, v5, v6, v7, v8, v9
453 #define TINYFORMAT_PASSARGS_10 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10
454 #define TINYFORMAT_PASSARGS_11 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
455 #define TINYFORMAT_PASSARGS_12 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
456 #define TINYFORMAT_PASSARGS_13 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13
457 #define TINYFORMAT_PASSARGS_14 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14
458 #define TINYFORMAT_PASSARGS_15 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15
459 #define TINYFORMAT_PASSARGS_16 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16
461 #define TINYFORMAT_PASSARGS_TAIL_1
462 #define TINYFORMAT_PASSARGS_TAIL_2 , v2
463 #define TINYFORMAT_PASSARGS_TAIL_3 , v2, v3
464 #define TINYFORMAT_PASSARGS_TAIL_4 , v2, v3, v4
465 #define TINYFORMAT_PASSARGS_TAIL_5 , v2, v3, v4, v5
466 #define TINYFORMAT_PASSARGS_TAIL_6 , v2, v3, v4, v5, v6
467 #define TINYFORMAT_PASSARGS_TAIL_7 , v2, v3, v4, v5, v6, v7
468 #define TINYFORMAT_PASSARGS_TAIL_8 , v2, v3, v4, v5, v6, v7, v8
469 #define TINYFORMAT_PASSARGS_TAIL_9 , v2, v3, v4, v5, v6, v7, v8, v9
470 #define TINYFORMAT_PASSARGS_TAIL_10 , v2, v3, v4, v5, v6, v7, v8, v9, v10
471 #define TINYFORMAT_PASSARGS_TAIL_11 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
472 #define TINYFORMAT_PASSARGS_TAIL_12 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
473 #define TINYFORMAT_PASSARGS_TAIL_13 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13
474 #define TINYFORMAT_PASSARGS_TAIL_14 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14
475 #define TINYFORMAT_PASSARGS_TAIL_15 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15
476 #define TINYFORMAT_PASSARGS_TAIL_16 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16
478 #define TINYFORMAT_FOREACH_ARGNUM(m) \
479 m(1) m(2) m(3) m(4) m(5) m(6) m(7) m(8) m(9) m(10) m(11) m(12) m(13) m(14) m(15) m(16)
497 : m_value(static_cast<
const void*>(&value)),
498 m_formatImpl(&formatImpl<
T>),
499 m_toIntImpl(&toIntImpl<
T>)
502 void format(std::ostream& out,
const char* fmtBegin,
503 const char* fmtEnd,
int ntrunc)
const
505 m_formatImpl(out, fmtBegin, fmtEnd, ntrunc, m_value);
510 return m_toIntImpl(m_value);
515 TINYFORMAT_HIDDEN static void formatImpl(std::ostream& out,
const char* fmtBegin,
516 const char* fmtEnd,
int ntrunc,
const void* value)
518 formatValue(out, fmtBegin, fmtEnd, ntrunc, *static_cast<const T*>(value));
528 void (*m_formatImpl)(std::ostream& out,
const char* fmtBegin,
529 const char* fmtEnd,
int ntrunc,
const void*
value);
530 int (*m_toIntImpl)(
const void*
value);
539 for(;*c >=
'0' && *c <=
'9'; ++
c)
540 i = 10*i + (*c -
'0');
558 out.write(fmt, c - fmt);
561 out.write(fmt, c - fmt);
585 int& ntrunc,
const char* fmtStart,
587 int& argIndex,
int numFormatters)
591 TINYFORMAT_ERROR(
"tinyformat: Not enough conversion specifiers in format string");
599 out.unsetf(std::ios::adjustfield | std::ios::basefield |
600 std::ios::floatfield | std::ios::showbase | std::ios::boolalpha |
601 std::ios::showpoint | std::ios::showpos | std::ios::uppercase);
602 bool precisionSet =
false;
603 bool widthSet =
false;
605 const char*
c = fmtStart + 1;
612 out.setf(std::ios::showpoint | std::ios::showbase);
621 out.setf(std::ios::internal, std::ios::adjustfield);
630 if(!(out.flags() & std::ios::showpos))
631 spacePadPositive =
true;
634 out.setf(std::ios::showpos);
635 spacePadPositive =
false;
644 if(*c >=
'0' && *c <=
'9')
653 if(argIndex < numFormatters)
654 width = formatters[argIndex++].
toInt();
656 TINYFORMAT_ERROR(
"tinyformat: Not enough arguments to read variable width");
675 if(argIndex < numFormatters)
676 precision = formatters[argIndex++].
toInt();
678 TINYFORMAT_ERROR(
"tinyformat: Not enough arguments to read variable precision");
682 if(*c >=
'0' && *c <=
'9')
687 out.precision(precision);
691 while(*c ==
'l' || *c ==
'h' || *c ==
'L' ||
692 *c ==
'j' || *c ==
'z' || *c ==
't')
697 bool intConversion =
false;
700 case 'u':
case 'd':
case 'i':
701 out.setf(std::ios::dec, std::ios::basefield);
702 intConversion =
true;
705 out.setf(std::ios::oct, std::ios::basefield);
706 intConversion =
true;
709 out.setf(std::ios::uppercase);
711 out.setf(std::ios::hex, std::ios::basefield);
712 intConversion =
true;
715 out.setf(std::ios::uppercase);
717 out.setf(std::ios::scientific, std::ios::floatfield);
718 out.setf(std::ios::dec, std::ios::basefield);
721 out.setf(std::ios::uppercase);
723 out.setf(std::ios::fixed, std::ios::floatfield);
726 out.setf(std::ios::uppercase);
728 out.setf(std::ios::dec, std::ios::basefield);
730 out.flags(out.flags() & ~std::ios::floatfield);
734 "are not supported");
741 ntrunc =
static_cast<int>(out.precision());
743 out.setf(std::ios::boolalpha);
751 "terminated by end of string");
756 if(intConversion && precisionSet && !widthSet)
762 out.width(out.precision() + widthExtra);
763 out.setf(std::ios::internal, std::ios::adjustfield);
776 std::streamsize origWidth = out.width();
777 std::streamsize origPrecision = out.precision();
778 std::ios::fmtflags origFlags = out.flags();
779 char origFill = out.fill();
781 for (
int argIndex = 0; argIndex < numFormatters; ++argIndex)
785 bool spacePadPositive =
false;
788 formatters, argIndex, numFormatters);
789 if (argIndex >= numFormatters)
797 if(!spacePadPositive)
798 arg.
format(out, fmt, fmtEnd, ntrunc);
805 std::ostringstream tmpStream;
806 tmpStream.imbue (out.getloc());
807 tmpStream.copyfmt(out);
808 tmpStream.setf(std::ios::showpos);
809 arg.
format(tmpStream, fmt, fmtEnd, ntrunc);
811 for(
size_t i = 0, iend = result.size(); i < iend; ++i)
812 if(result[i] ==
'+') result[i] =
' ';
821 TINYFORMAT_ERROR(
"tinyformat: Too many conversion specifiers in format string");
824 out.width(origWidth);
825 out.precision(origPrecision);
826 out.flags(origFlags);
843 : m_formatters(formatters), m_N(N) { }
845 friend void vformat(std::ostream& out,
const char* fmt,
864 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
865 template<
typename... Args>
869 { static_assert(
sizeof...(args) ==
N,
"Number of args must be N"); }
870 #else // C++98 version
872 # define TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR(n) \
874 template<TINYFORMAT_ARGTYPES(n)> \
875 FormatListN(TINYFORMAT_VARARGS(n)) \
876 : FormatList(&m_formatterStore[0], n) \
877 { assert(n == N); init(0, TINYFORMAT_PASSARGS(n)); } \
879 template<TINYFORMAT_ARGTYPES(n)> \
880 void init(int i, TINYFORMAT_VARARGS(n)) \
882 m_formatterStore[i] = FormatArg(v1); \
883 init(i+1 TINYFORMAT_PASSARGS_TAIL(n)); \
887 # undef TINYFORMAT_MAKE_FORMATLIST_CONSTRUCTOR
891 FormatArg m_formatterStore[
N];
906 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
914 template<
typename... Args>
917 return detail::FormatListN<
sizeof...(args)>(args...);
920 #else // C++98 version
926 #define TINYFORMAT_MAKE_MAKEFORMATLIST(n) \
927 template<TINYFORMAT_ARGTYPES(n)> \
928 detail::FormatListN<n> makeFormatList(TINYFORMAT_VARARGS(n)) \
930 return detail::FormatListN<n>(TINYFORMAT_PASSARGS(n)); \
933 #undef TINYFORMAT_MAKE_MAKEFORMATLIST
948 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
952 template<
typename... Args>
953 void format(std::ostream& out,
const char* fmt,
const Args&... args)
961 template<
typename... Args>
964 std::ostringstream oss;
965 oss.imbue (std::locale::classic());
966 format(oss, fmt, args...);
974 template<
typename... Args>
977 std::ostringstream oss;
979 format(oss, fmt, args...);
985 template<
typename... Args>
986 void printf(
const char* fmt,
const Args&... args)
988 format(std::cout, fmt, args...);
991 template<
typename... Args>
992 void printfln(
const char* fmt,
const Args&... args)
994 format(std::cout, fmt, args...);
999 #else // C++98 version
1001 inline void format(std::ostream& out,
const char* fmt)
1008 std::ostringstream oss;
1024 #define TINYFORMAT_MAKE_FORMAT_FUNCS(n) \
1026 template<TINYFORMAT_ARGTYPES(n)> \
1027 void format(std::ostream& out, const char* fmt, TINYFORMAT_VARARGS(n)) \
1029 vformat(out, fmt, makeFormatList(TINYFORMAT_PASSARGS(n))); \
1032 template<TINYFORMAT_ARGTYPES(n)> \
1033 std::string format(const char* fmt, TINYFORMAT_VARARGS(n)) \
1035 std::ostringstream oss; \
1036 oss.imbue (std::locale::classic()); \
1037 format(oss, fmt, TINYFORMAT_PASSARGS(n)); \
1041 template<TINYFORMAT_ARGTYPES(n)> \
1042 std::string format(const std::locale& loc, const char* fmt, TINYFORMAT_VARARGS(n)) \
1044 std::ostringstream oss; \
1046 format(oss, fmt, TINYFORMAT_PASSARGS(n)); \
1050 template<TINYFORMAT_ARGTYPES(n)> \
1051 void printf(const char* fmt, TINYFORMAT_VARARGS(n)) \
1053 format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
1056 template<TINYFORMAT_ARGTYPES(n)> \
1057 void printfln(const char* fmt, TINYFORMAT_VARARGS(n)) \
1059 format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
1060 std::cout << '\n'; \
1064 #undef TINYFORMAT_MAKE_FORMAT_FUNCS
1071 #endif // TINYFORMAT_H_INCLUDED
GLint GLint GLint GLint GLint GLint GLsizei width
internal::named_arg< T, char > arg(string_view name, const T &arg)
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
GLsizei const GLchar *const * string
GA_API const UT_StringHolder N
vint4 min(const vint4 &a, const vint4 &b)
GLenum GLint GLint * precision
GLsizei const GLfloat * value