diff --git a/doc/api.rst b/doc/api.rst index ba847ae3..5a93b4aa 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -58,6 +58,36 @@ formatting:: The format string syntax is described in the documentation of `strftime `_. +Formatting user-defined types +----------------------------- + +A custom ``format_arg`` function may be implemented and used to format any +user-defined type. That is how date and time formatting described in the +previous section is implemented in :file:`fmt/time.h`. The following example +shows how to implement custom formatting for a user-defined structure. + +:: + + struct MyStruct { double a, b; }; + + void format_arg(fmt::BasicFormatter &f, + const char *&format_str, const MyStruct &s) { + f.writer().write("[MyStruct: a={:.1f}, b={:.2f}]", s.a, s.b); + } + + MyStruct m = { 1, 2 }; + std::string s = fmt::format("m={}", n); + // s == "m=[MyStruct: a=1.0, b=2.00]" + +Note in the example above the ``format_arg`` function ignores the contents of +``format_str`` so the type will always be formatted as specified. See +``format_arg`` in :file:`fmt/time.h` for an advanced example of how to use +the ``format_str`` argument to customize the formatted output. + +This section shows how to define a custom format function for a user-defined +type. The next section describes how to get ``fmt`` to use a conventional stream +output ``operator<<`` when one is defined for a user-defined type. + ``std::ostream`` support ------------------------ diff --git a/fmt/format.h b/fmt/format.h index 4432c770..ae7bd493 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -194,6 +194,17 @@ typedef __int64 intmax_t; # endif #endif +#ifndef FMT_OVERRIDE +# if FMT_USE_OVERRIDE || FMT_HAS_FEATURE(cxx_override) || \ + (FMT_GCC_VERSION >= 408 && FMT_HAS_GXX_CXX11) || \ + FMT_MSC_VER >= 1900 +# define FMT_OVERRIDE override +# else +# define FMT_OVERRIDE +# endif +#endif + + // A macro to disallow the copy constructor and operator= functions // This should be used in the private: declarations for a class #ifndef FMT_USE_DELETED_FUNCTIONS @@ -678,7 +689,7 @@ class MemoryBuffer : private Allocator, public Buffer { } protected: - void grow(std::size_t size); + void grow(std::size_t size) FMT_OVERRIDE; public: explicit MemoryBuffer(const Allocator &alloc = Allocator()) diff --git a/fmt/posix.h b/fmt/posix.h index 7c2b0b5e..d4cd1198 100644 --- a/fmt/posix.h +++ b/fmt/posix.h @@ -10,7 +10,7 @@ #ifndef FMT_POSIX_H_ #define FMT_POSIX_H_ -#ifdef __MINGW32__ +#if defined(__MINGW32__) || defined(__CYGWIN__) // Workaround MinGW bug https://sourceforge.net/p/mingw/bugs/2024/. # undef __STRICT_ANSI__ #endif @@ -302,7 +302,8 @@ class File { // Returns the memory page size. long getpagesize(); -#if (defined(LC_NUMERIC_MASK) || defined(_MSC_VER)) && !defined(__ANDROID__) +#if (defined(LC_NUMERIC_MASK) || defined(_MSC_VER)) && \ + !defined(__ANDROID__) && !defined(__CYGWIN__) # define FMT_LOCALE #endif