Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251// Format the value by casting to type fmtT. This default implementation252// should never be called.253template<typename T, typename fmtT, bool convertible = is_convertible<T, fmtT>::value>254struct formatValueAsType255{256static void invoke(std::ostream& /*out*/, const T& /*value*/) { TINYFORMAT_ASSERT(0); }257};258// Specialized version for types that can actually be converted to fmtT, as259// indicated by the "convertible" template parameter.260template<typename T, typename fmtT>261struct formatValueAsType<T,fmtT,true>262{263static void invoke(std::ostream& out, const T& value)264{ out << static_cast<fmtT>(value); }265};267#ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND268template<typename T, bool convertible = is_convertible<T, int>::value>269struct formatZeroIntegerWorkaround270{271static bool invoke(std::ostream& /**/, const T& /**/) { return false; }272};273template<typename T>274struct formatZeroIntegerWorkaround<T,true>275{276static bool invoke(std::ostream& out, const T& value)277{278if (static_cast<int>(value) == 0 && out.flags() & std::ios::showpos) {279out << "+0";280return true;281}282return false;283}284};285#endif // TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND287// Convert an arbitrary type to integer. The version with convertible=false288// throws an error.289template<typename T, bool convertible = is_convertible<T,int>::value>290struct convertToInt291{292static int invoke(const T& /*value*/)293{294TINYFORMAT_ERROR("tinyformat: Cannot convert from argument type to "295"integer for use as variable width or precision");296return 0;297}298};299// Specialization for convertToInt when conversion is possible300template<typename T>301struct convertToInt<T,true>302{303static int invoke(const T& value) { return static_cast<int>(value); }304};306// Format at most ntrunc characters to the given stream.307template<typename T>308inline void formatTruncated(std::ostream& out, const T& value, int ntrunc)309{310std::ostringstream tmp;311tmp << value;312std::string result = tmp.str();313out.write(result.c_str(), (std::min)(ntrunc, static_cast<int>(result.size())));314}315#define TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(type) \316inline void formatTruncated(std::ostream& out, type* value, int ntrunc) \317{ \318std::streamsize len = 0; \319while (len < ntrunc && value[len] != 0) \320++len; \321out.write(value, len); \322}323// Overload for const char* and char*. Could overload for signed & unsigned324// char too, but these are technically unneeded for printf compatibility.325TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(const char)326TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR(char)327#undef TINYFORMAT_DEFINE_FORMAT_TRUNCATED_CSTR329} // namespace detail332//------------------------------------------------------------------------------333// Variable formatting functions. May be overridden for user-defined types if334// desired.337/// Format a value into a stream, delegating to operator<< by default.338///339/// Users may override this for their own types. When this function is called,340/// the stream flags will have been modified according to the format string.341/// The format specification is provided in the range [fmtBegin, fmtEnd). For342/// truncating conversions, ntrunc is set to the desired maximum number of343/// characters, for example "%.7s" calls formatValue with ntrunc = 7.344///345/// By default, formatValue() uses the usual stream insertion operator346/// operator<< to format the type T, with special cases for the %c and %p347/// conversions.348template<typename T>349inline void formatValue(std::ostream& out, const char* /*fmtBegin*/,350const char* fmtEnd, int ntrunc, const T& value)351{352#ifndef TINYFORMAT_ALLOW_WCHAR_STRINGS353// Since we don't support printing of wchar_t using "%ls", make it fail at354// compile time in preference to printing as a void* at runtime.355typedef typename detail::is_wchar<T>::tinyformat_wchar_is_not_supported DummyType;356(void) DummyType(); // avoid unused type warning with gcc-4.8357#endif358// The mess here is to support the %c and %p conversions: if these359// conversions are active we try to convert the type to a char or const360// void* respectively and format that instead of the value itself. For the361// %p conversion it's important to avoid dereferencing the pointer, which362// could otherwise lead to a crash when printing a dangling (const char*).363const bool canConvertToChar = detail::is_convertible<T,char>::value;364const bool canConvertToVoidPtr = detail::is_convertible<T, const void*>::value;365if (canConvertToChar && *(fmtEnd-1) == 'c')366detail::formatValueAsType<T, char>::invoke(out, value);367else if (canConvertToVoidPtr && *(fmtEnd-1) == 'p')368detail::formatValueAsType<T, const void*>::invoke(out, value);369#ifdef TINYFORMAT_OLD_LIBSTDCPLUSPLUS_WORKAROUND370else if (detail::formatZeroIntegerWorkaround<T>::invoke(out, value)) /**/;371#endif372else if (ntrunc >= 0) {373// Take care not to overread C strings in truncating conversions like374// "%.4s" where at most 4 characters may be read.375detail::formatTruncated(out, value, ntrunc);376}377else378out << value;379}382// Overloaded version for char types to support printing as an integer383#define TINYFORMAT_DEFINE_FORMATVALUE_CHAR(charType) \384inline void formatValue(std::ostream& out, const char* /*fmtBegin*/, \385const char* fmtEnd, int /**/, charType value) \386{ \387switch (*(fmtEnd-1)) { \388case 'u': case 'd': case 'i': case 'o': case 'X': case 'x': \389out << static_cast<int>(value); break; \390default: \391out << value; break; \392} \393}394// per 3.9.1: char, signed char and unsigned char are all distinct types395TINYFORMAT_DEFINE_FORMATVALUE_CHAR(char)396TINYFORMAT_DEFINE_FORMATVALUE_CHAR(signed char)397TINYFORMAT_DEFINE_FORMATVALUE_CHAR(unsigned char)398#undef TINYFORMAT_DEFINE_FORMATVALUE_CHAR401//------------------------------------------------------------------------------402// Tools for emulating variadic templates in C++98. The basic idea here is403// stolen from the boost preprocessor metaprogramming library and cut down to404// be just general enough for what we need.406#define TINYFORMAT_ARGTYPES(n) TINYFORMAT_ARGTYPES_ ## n407#define TINYFORMAT_VARARGS(n) TINYFORMAT_VARARGS_ ## n408#define TINYFORMAT_PASSARGS(n) TINYFORMAT_PASSARGS_ ## n409#define TINYFORMAT_PASSARGS_TAIL(n) TINYFORMAT_PASSARGS_TAIL_ ## n411// To keep it as transparent as possible, the macros below have been generated412// using python via the excellent cog.py code generation script. This avoids413// the need for a bunch of complex (but more general) preprocessor tricks as414// used in boost.preprocessor.415//416// To rerun the code generation in place, use `cog.py -r tinyformat.h`417// (see http://nedbatchelder.com/code/cog). Alternatively you can just create418// extra versions by hand.420/*[[[cog421maxParams = 16423def makeCommaSepLists(lineTemplate, elemTemplate, startInd=1):424for j in range(startInd,maxParams+1):425list = ', '.join([elemTemplate % {'i':i} for i in range(startInd,j+1)])426cog.outl(lineTemplate % {'j':j, 'list':list})428makeCommaSepLists('#define TINYFORMAT_ARGTYPES_%(j)d %(list)s',429'class T%(i)d')431cog.outl()432makeCommaSepLists('#define TINYFORMAT_VARARGS_%(j)d %(list)s',433'const T%(i)d& v%(i)d')435cog.outl()436makeCommaSepLists('#define TINYFORMAT_PASSARGS_%(j)d %(list)s', 'v%(i)d')438cog.outl()439cog.outl('#define TINYFORMAT_PASSARGS_TAIL_1')440makeCommaSepLists('#define TINYFORMAT_PASSARGS_TAIL_%(j)d , %(list)s',441'v%(i)d', startInd = 2)443cog.outl()444cog.outl('#define TINYFORMAT_FOREACH_ARGNUM(m) \\\n ' +445' '.join(['m(%d)' % (j,) for j in range(1,maxParams+1)]))446]]]*/447#define TINYFORMAT_ARGTYPES_1 class T1448#define TINYFORMAT_ARGTYPES_2 class T1, class T2449#define TINYFORMAT_ARGTYPES_3 class T1, class T2, class T3450#define TINYFORMAT_ARGTYPES_4 class T1, class T2, class T3, class T4451#define TINYFORMAT_ARGTYPES_5 class T1, class T2, class T3, class T4, class T5452#define TINYFORMAT_ARGTYPES_6 class T1, class T2, class T3, class T4, class T5, class T6453#define TINYFORMAT_ARGTYPES_7 class T1, class T2, class T3, class T4, class T5, class T6, class T7454#define TINYFORMAT_ARGTYPES_8 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8455#define TINYFORMAT_ARGTYPES_9 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9456#define TINYFORMAT_ARGTYPES_10 class T1, class T2, class T3, class T4, class T5, class T6, class T7, class T8, class T9, class T10457#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 T11458#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 T12459#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 T13460#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 T14461#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 T15462#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 T16464#define TINYFORMAT_VARARGS_1 const T1& v1465#define TINYFORMAT_VARARGS_2 const T1& v1, const T2& v2466#define TINYFORMAT_VARARGS_3 const T1& v1, const T2& v2, const T3& v3467#define TINYFORMAT_VARARGS_4 const T1& v1, const T2& v2, const T3& v3, const T4& v4468#define TINYFORMAT_VARARGS_5 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5469#define TINYFORMAT_VARARGS_6 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6470#define TINYFORMAT_VARARGS_7 const T1& v1, const T2& v2, const T3& v3, const T4& v4, const T5& v5, const T6& v6, const T7& v7471#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& v8472#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& v9473#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& v10474#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& v11475#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& v12476#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& v13477#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& v14478#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& v15479#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& v16481#define TINYFORMAT_PASSARGS_1 v1482#define TINYFORMAT_PASSARGS_2 v1, v2483#define TINYFORMAT_PASSARGS_3 v1, v2, v3484#define TINYFORMAT_PASSARGS_4 v1, v2, v3, v4485#define TINYFORMAT_PASSARGS_5 v1, v2, v3, v4, v5486#define TINYFORMAT_PASSARGS_6 v1, v2, v3, v4, v5, v6487#define TINYFORMAT_PASSARGS_7 v1, v2, v3, v4, v5, v6, v7488#define TINYFORMAT_PASSARGS_8 v1, v2, v3, v4, v5, v6, v7, v8489#define TINYFORMAT_PASSARGS_9 v1, v2, v3, v4, v5, v6, v7, v8, v9490#define TINYFORMAT_PASSARGS_10 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10491#define TINYFORMAT_PASSARGS_11 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11492#define TINYFORMAT_PASSARGS_12 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12493#define TINYFORMAT_PASSARGS_13 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13494#define TINYFORMAT_PASSARGS_14 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14495#define TINYFORMAT_PASSARGS_15 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15496#define TINYFORMAT_PASSARGS_16 v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15, v16498#define TINYFORMAT_PASSARGS_TAIL_1499#define TINYFORMAT_PASSARGS_TAIL_2 , v2500#define TINYFORMAT_PASSARGS_TAIL_3 , v2, v3
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.