101 #ifndef TINYFORMAT_H_INCLUDED
102 #define TINYFORMAT_H_INCLUDED
112 #define TINYFORMAT_ERROR(reasonString) throw std::runtime_error(reasonString)
125 #ifndef TINYFORMAT_ERROR
126 # define TINYFORMAT_ERROR(reason) assert(0 && reason)
129 #if !defined(TINYFORMAT_USE_VARIADIC_TEMPLATES) && !defined(TINYFORMAT_NO_VARIADIC_TEMPLATES)
130 # ifdef __GXX_EXPERIMENTAL_CXX0X__
131 # define TINYFORMAT_USE_VARIADIC_TEMPLATES
136 # define TINYFORMAT_NOINLINE __attribute__((noinline))
137 #elif defined(_MSC_VER)
138 # define TINYFORMAT_NOINLINE __declspec(noinline)
140 # define TINYFORMAT_NOINLINE
143 #if defined(__GLIBCXX__) && __GLIBCXX__ < 20080201
146 # define TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
155 template <
typename T1,
typename T2>
165 static const T1&
makeT1();
169 # pragma warning(push)
170 # pragma warning(disable:4244)
171 # pragma warning(disable:4267)
180 # pragma warning(pop)
189 template<
int n>
struct is_wchar<const wchar_t[n]> {};
195 template<typename T, typename fmtT, bool convertible = is_convertible<T, fmtT>::value>
198 static void invoke(std::ostream& ,
const T& ) { assert(0); }
202 template<
typename T,
typename fmtT>
205 static void invoke(std::ostream& out,
const T& value)
206 { out << static_cast<fmtT>(value); }
209 #ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
210 template<typename T, bool convertible = is_convertible<T, int>::value>
211 struct formatZeroIntegerWorkaround
213 static bool invoke(std::ostream& ,
const T& ) {
return false; }
216 struct formatZeroIntegerWorkaround<T,true>
218 static bool invoke(std::ostream& out,
const T& value)
220 if (static_cast<int>(value) == 0 && out.flags() & std::ios::showpos)
228 #endif // TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
232 template<typename T, bool convertible = is_convertible<T,int>::value>
238 "integer for use as variable width or precision");
246 static int invoke(
const T& value) {
return static_cast<int>(value); }
268 const char* fmtEnd,
const T& value)
270 #ifndef TINYFORMAT_ALLOW_WCHAR_STRINGS
283 if(canConvertToChar && *(fmtEnd-1) ==
'c')
285 else if(canConvertToVoidPtr && *(fmtEnd-1) ==
'p')
287 #ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND
288 else if(detail::formatZeroIntegerWorkaround<T>::invoke(out, value)) ;
296 #define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType) \
297 inline void formatValue(std::ostream& out, const char* , \
298 const char* fmtEnd, charType value) \
300 switch(*(fmtEnd-1)) \
302 case 'u': case 'd': case 'i': case 'o': case 'X': case 'x': \
303 out << static_cast<int>(value); break; \
305 out << value; break; \
312 #undef TINYFORMAT_DEFINE_FORMATVALUE_CHAR
320 #define TINYFORMAT_ARGTYPES(n) TINYFORMAT_ARGTYPES_ ## n
321 #define TINYFORMAT_VARARGS(n) TINYFORMAT_VARARGS_ ## n
322 #define TINYFORMAT_PASSARGS(n) TINYFORMAT_PASSARGS_ ## n
323 #define TINYFORMAT_PASSARGS_TAIL(n) TINYFORMAT_PASSARGS_TAIL_ ## n
361 #define TINYFORMAT_ARGTYPES_1 class T1
362 #define TINYFORMAT_ARGTYPES_2 class T1, class T2
363 #define TINYFORMAT_ARGTYPES_3 class T1, class T2, class T3
364 #define TINYFORMAT_ARGTYPES_4 class T1, class T2, class T3, class T4
365 #define TINYFORMAT_ARGTYPES_5 class T1, class T2, class T3, class T4, class T5
366 #define TINYFORMAT_ARGTYPES_6 class T1, class T2, class T3, class T4, class T5, class T6
367 #define TINYFORMAT_ARGTYPES_7 class T1, class T2, class T3, class T4, class T5, class T6, class T7
368 #define TINYFORMAT_ARGTYPES_8 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8
369 #define TINYFORMAT_ARGTYPES_9 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9
370 #define TINYFORMAT_ARGTYPES_10 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10
371 #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
372 #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
373 #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
374 #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
375 #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
376 #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
378 #define TINYFORMAT_VARARGS_1 const T1& v1
379 #define TINYFORMAT_VARARGS_2 const T1& v1, const T2& v2
380 #define TINYFORMAT_VARARGS_3 const T1& v1, const T2& v2, const T3& v3
381 #define TINYFORMAT_VARARGS_4 const T1& v1, const T2& v2, const T3& v3, const T4& v4
382 #define TINYFORMAT_VARARGS_5 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5
383 #define TINYFORMAT_VARARGS_6 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6
384 #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
385 #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
386 #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
387 #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
388 #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
389 #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
390 #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
391 #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
392 #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
393 #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
395 #define TINYFORMAT_PASSARGS_1 v1
396 #define TINYFORMAT_PASSARGS_2 v1, v2
397 #define TINYFORMAT_PASSARGS_3 v1, v2, v3
398 #define TINYFORMAT_PASSARGS_4 v1, v2, v3, v4
399 #define TINYFORMAT_PASSARGS_5 v1, v2, v3, v4, v5
400 #define TINYFORMAT_PASSARGS_6 v1, v2, v3, v4, v5, v6
401 #define TINYFORMAT_PASSARGS_7 v1, v2, v3, v4, v5, v6, v7
402 #define TINYFORMAT_PASSARGS_8 v1, v2, v3, v4, v5, v6, v7, v8
403 #define TINYFORMAT_PASSARGS_9 v1, v2, v3, v4, v5, v6, v7, v8, v9
404 #define TINYFORMAT_PASSARGS_10 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10
405 #define TINYFORMAT_PASSARGS_11 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
406 #define TINYFORMAT_PASSARGS_12 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
407 #define TINYFORMAT_PASSARGS_13 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13
408 #define TINYFORMAT_PASSARGS_14 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14
409 #define TINYFORMAT_PASSARGS_15 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15
410 #define TINYFORMAT_PASSARGS_16 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16
412 #define TINYFORMAT_PASSARGS_TAIL_1
413 #define TINYFORMAT_PASSARGS_TAIL_2 , v2
414 #define TINYFORMAT_PASSARGS_TAIL_3 , v2, v3
415 #define TINYFORMAT_PASSARGS_TAIL_4 , v2, v3, v4
416 #define TINYFORMAT_PASSARGS_TAIL_5 , v2, v3, v4, v5
417 #define TINYFORMAT_PASSARGS_TAIL_6 , v2, v3, v4, v5, v6
418 #define TINYFORMAT_PASSARGS_TAIL_7 , v2, v3, v4, v5, v6, v7
419 #define TINYFORMAT_PASSARGS_TAIL_8 , v2, v3, v4, v5, v6, v7, v8
420 #define TINYFORMAT_PASSARGS_TAIL_9 , v2, v3, v4, v5, v6, v7, v8, v9
421 #define TINYFORMAT_PASSARGS_TAIL_10 , v2, v3, v4, v5, v6, v7, v8, v9, v10
422 #define TINYFORMAT_PASSARGS_TAIL_11 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11
423 #define TINYFORMAT_PASSARGS_TAIL_12 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12
424 #define TINYFORMAT_PASSARGS_TAIL_13 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13
425 #define TINYFORMAT_PASSARGS_TAIL_14 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14
426 #define TINYFORMAT_PASSARGS_TAIL_15 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15
427 #define TINYFORMAT_PASSARGS_TAIL_16 , v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16
429 #define TINYFORMAT_FOREACH_ARGNUM(m) \
430 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)
474 TINYFORMAT_ERROR(
"tinyformat: Too many conversion specifiers in format string");
487 void accept(
const T& value);
495 for(;*c >=
'0' && *c <=
'9'; ++c)
496 i = 10*i + (*c -
'0');
509 # define TINYFORMAT_DEFINE_FORMAT_C_STRING_TRUNCATE(type) \
510 static bool formatCStringTruncate(std::ostream& out, type* value, \
511 std::streamsize truncLen) \
513 std::streamsize len = 0; \
514 while(len < truncLen && value[len] != 0) \
516 out.write(value, len); \
524 # undef TINYFORMAT_DEFINE_FORMAT_C_STRING_TRUNCATE
541 out.write(fmt, static_cast<std::streamsize>(c - fmt));
544 out.write(fmt, static_cast<std::streamsize>(c - fmt));
555 unsigned int& extraFlags,
556 const char* fmtStart,
558 int variablePrecision);
587 const char* fmtEnd = 0;
628 std::ostringstream tmpStream;
629 tmpStream.copyfmt(
m_out);
631 tmpStream.setf(std::ios::showpos);
641 std::string result = tmpStream.str();
644 for(
size_t i = 0, iend = result.size(); i < iend; ++i)
650 m_out.write(result.c_str(),
m_out.precision());
668 unsigned int& extraFlags,
669 const char* fmtStart,
671 int variablePrecision)
675 TINYFORMAT_ERROR(
"tinyformat: Not enough conversion specifiers in format string");
683 out.unsetf(std::ios::adjustfield | std::ios::basefield |
684 std::ios::floatfield | std::ios::showbase | std::ios::boolalpha |
685 std::ios::showpoint | std::ios::showpos | std::ios::uppercase);
687 bool precisionSet =
false;
688 bool widthSet =
false;
689 const char* c = fmtStart + 1;
696 out.setf(std::ios::showpoint | std::ios::showbase);
700 if(!(out.flags() & std::ios::left))
705 out.setf(std::ios::internal, std::ios::adjustfield);
710 out.setf(std::ios::left, std::ios::adjustfield);
714 if(!(out.flags() & std::ios::showpos))
718 out.setf(std::ios::showpos);
725 if(*c >=
'0' && *c <=
'9')
733 if(variableWidth < 0)
737 out.setf(std::ios::left, std::ios::adjustfield);
738 variableWidth = -variableWidth;
740 out.width(variableWidth);
753 precision = variablePrecision;
757 if(*c >=
'0' && *c <=
'9')
762 out.precision(precision);
766 while(*c ==
'l' || *c ==
'h' || *c ==
'L' ||
767 *c ==
'j' || *c ==
'z' || *c ==
't')
772 bool intConversion =
false;
775 case 'u':
case 'd':
case 'i':
776 out.setf(std::ios::dec, std::ios::basefield);
777 intConversion =
true;
780 out.setf(std::ios::oct, std::ios::basefield);
781 intConversion =
true;
784 out.setf(std::ios::uppercase);
786 out.setf(std::ios::hex, std::ios::basefield);
787 intConversion =
true;
790 out.setf(std::ios::uppercase);
792 out.setf(std::ios::scientific, std::ios::floatfield);
793 out.setf(std::ios::dec, std::ios::basefield);
796 out.setf(std::ios::uppercase);
798 out.setf(std::ios::fixed, std::ios::floatfield);
801 out.setf(std::ios::uppercase);
803 out.setf(std::ios::dec, std::ios::basefield);
805 out.flags(out.flags() & ~
std::ios::floatfield);
809 "are not supported");
818 out.setf(std::ios::boolalpha);
826 "terminated by end of string");
829 if(intConversion && precisionSet && !widthSet)
835 out.width(out.precision());
836 out.setf(std::ios::internal, std::ios::adjustfield);
850 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
852 template<
typename T1>
860 template<
typename T1,
typename... Args>
861 void format(FormatIterator& fmtIter,
const T1& value1,
const Args&... args)
863 fmtIter.accept(value1);
875 #define TINYFORMAT_MAKE_FORMAT_DETAIL(n) \
876 template<TINYFORMAT_ARGTYPES(n)> \
877 void format(detail::FormatIterator& fmtIter, TINYFORMAT_VARARGS(n)) \
879 fmtIter.accept(v1); \
880 format(fmtIter TINYFORMAT_PASSARGS_TAIL(n)); \
884 #undef TINYFORMAT_MAKE_FORMAT_DETAIL
886 #endif // End C++98 variadic template emulation for format()
894 #ifdef TINYFORMAT_USE_VARIADIC_TEMPLATES
897 template<
typename T1,
typename... Args>
898 void format(std::ostream& out,
const char* fmt,
const T1& v1,
const Args&... args)
900 detail::FormatIterator fmtIter(out, fmt);
901 format(fmtIter, v1, args...);
904 template<
typename T1,
typename... Args>
905 std::string
format(
const char* fmt,
const T1& v1,
const Args&... args)
907 std::ostringstream oss;
908 format(oss, fmt, v1, args...);
912 template<
typename T1,
typename... Args>
913 std::string
format(
const std::string &fmt,
const T1& v1,
const Args&... args)
915 std::ostringstream oss;
916 format(oss, fmt.c_str(), v1, args...);
920 template<
typename T1,
typename... Args>
921 void printf(
const char* fmt,
const T1& v1,
const Args&... args)
923 format(std::cout, fmt, v1, args...);
929 #define TINYFORMAT_MAKE_FORMAT_FUNCS(n) \
931 template<TINYFORMAT_ARGTYPES(n)> \
932 void format(std::ostream& out, const char* fmt, TINYFORMAT_VARARGS(n)) \
934 tinyformat::detail::FormatIterator fmtIter(out, fmt); \
935 tinyformat::detail::format(fmtIter, TINYFORMAT_PASSARGS(n)); \
938 template<TINYFORMAT_ARGTYPES(n)> \
939 std::string format(const char* fmt, TINYFORMAT_VARARGS(n)) \
941 std::ostringstream oss; \
942 tinyformat::format(oss, fmt, TINYFORMAT_PASSARGS(n)); \
946 template<TINYFORMAT_ARGTYPES(n)> \
947 std::string format(const std::string &fmt, TINYFORMAT_VARARGS(n)) \
949 std::ostringstream oss; \
950 tinyformat::format(oss, fmt.c_str(), TINYFORMAT_PASSARGS(n)); \
954 template<TINYFORMAT_ARGTYPES(n)> \
955 void printf(const char* fmt, TINYFORMAT_VARARGS(n)) \
957 tinyformat::format(std::cout, fmt, TINYFORMAT_PASSARGS(n)); \
961 #undef TINYFORMAT_MAKE_FORMAT_FUNCS
968 #define TINYFORMAT_WRAP_FORMAT_EXTRA_ARGS
969 #define TINYFORMAT_WRAP_FORMAT_N(n, returnType, funcName, funcDeclSuffix, \
970 bodyPrefix, streamName, bodySuffix) \
971 template<TINYFORMAT_ARGTYPES(n)> \
972 returnType funcName(TINYFORMAT_WRAP_FORMAT_EXTRA_ARGS const char* fmt, \
973 TINYFORMAT_VARARGS(n)) funcDeclSuffix \
976 tinyformat::format(streamName, fmt, TINYFORMAT_PASSARGS(n)); \
980 #define TINYFORMAT_WRAP_FORMAT(returnType, funcName, funcDeclSuffix, \
981 bodyPrefix, streamName, bodySuffix) \
983 returnType funcName(TINYFORMAT_WRAP_FORMAT_EXTRA_ARGS const char* fmt \
987 tinyformat::detail::FormatIterator(streamName, fmt).finish(); \
990 TINYFORMAT_WRAP_FORMAT_N(1 , returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
991 TINYFORMAT_WRAP_FORMAT_N(2 , returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
992 TINYFORMAT_WRAP_FORMAT_N(3 , returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
993 TINYFORMAT_WRAP_FORMAT_N(4 , returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
994 TINYFORMAT_WRAP_FORMAT_N(5 , returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
995 TINYFORMAT_WRAP_FORMAT_N(6 , returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
996 TINYFORMAT_WRAP_FORMAT_N(7 , returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
997 TINYFORMAT_WRAP_FORMAT_N(8 , returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
998 TINYFORMAT_WRAP_FORMAT_N(9 , returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
999 TINYFORMAT_WRAP_FORMAT_N(10, returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
1000 TINYFORMAT_WRAP_FORMAT_N(11, returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
1001 TINYFORMAT_WRAP_FORMAT_N(12, returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
1002 TINYFORMAT_WRAP_FORMAT_N(13, returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
1003 TINYFORMAT_WRAP_FORMAT_N(14, returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
1004 TINYFORMAT_WRAP_FORMAT_N(15, returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
1005 TINYFORMAT_WRAP_FORMAT_N(16, returnType, funcName, funcDeclSuffix, bodyPrefix, streamName, bodySuffix) \
1010 #endif // TINYFORMAT_H_INCLUDED