diff --git a/doc/api.rst b/doc/api.rst index 4d09ecb6..62706c61 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -38,7 +38,7 @@ arguments in the resulting string. .. _format: -.. doxygenfunction:: format(string_view, const Args&...) +.. doxygenfunction:: format(const String&, const Args&...) .. doxygenfunction:: vformat(string_view, format_args) .. _print: diff --git a/include/fmt/core.h b/include/fmt/core.h index cb2274e3..14c81ece 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -9,14 +9,13 @@ #define FMT_CORE_H_ #include -#include #include #include #include #include // The fmt library version in the form major * 10000 + minor * 100 + patch. -#define FMT_VERSION 50100 +#define FMT_VERSION 50200 #ifdef __has_feature # define FMT_HAS_FEATURE(x) __has_feature(x) @@ -181,13 +180,6 @@ # define FMT_ASSERT(condition, message) assert((condition) && message) #endif -#define FMT_DELETED = delete - -// A macro to disallow the copy construction and assignment. -#define FMT_DISALLOW_COPY_AND_ASSIGN(Type) \ - Type(const Type &) FMT_DELETED; \ - void operator=(const Type &) FMT_DELETED - // libc++ supports string_view in pre-c++17. #if (FMT_HAS_INCLUDE() && \ (__cplusplus > 201402L || defined(_LIBCPP_VERSION))) || \ @@ -343,7 +335,8 @@ namespace internal { template class basic_buffer { private: - FMT_DISALLOW_COPY_AND_ASSIGN(basic_buffer); + basic_buffer(const basic_buffer &) = delete; + void operator=(const basic_buffer &) = delete; T *ptr_; std::size_t size_; @@ -815,7 +808,8 @@ namespace internal { template class arg_map { private: - FMT_DISALLOW_COPY_AND_ASSIGN(arg_map); + arg_map(const arg_map &) = delete; + void operator=(const arg_map &) = delete; typedef typename Context::char_type char_type; @@ -930,7 +924,8 @@ class basic_format_context : private: internal::arg_map map_; - FMT_DISALLOW_COPY_AND_ASSIGN(basic_format_context); + basic_format_context(const basic_format_context &) = delete; + void operator=(const basic_format_context &) = delete; typedef internal::context_base base; typedef typename base::format_arg format_arg; @@ -1233,7 +1228,7 @@ inline internal::named_arg arg(wstring_view name, const T &arg) { // This function template is deleted intentionally to disable nested named // arguments as in ``format("{}", arg("a", arg("b", 42)))``. template -void arg(S, internal::named_arg) FMT_DELETED; +void arg(S, internal::named_arg) = delete; #ifndef FMT_EXTENDED_COLORS // color and (v)print_colored are deprecated. @@ -1252,6 +1247,61 @@ inline void print_colored(color c, wstring_view format_str, } #endif +// A base class for compile-time strings. It is defined in the fmt namespace to +// make formatting functions visible via ADL, e.g. format(fmt("{}"), 42). +struct compile_string {}; + +namespace internal { +// If S is a format string type, format_string_traints::char_type gives its +// character type. +template +struct format_string_traits { + private: + // Use constructability as a way to detect if format_string_traits is + // specialized because other methods are broken on MSVC2013. + format_string_traits(); +}; + +template +struct format_string_traits_base { typedef Char char_type; }; + +template +struct format_string_traits : format_string_traits_base {}; + +template +struct format_string_traits : format_string_traits_base {}; + +template +struct format_string_traits : format_string_traits_base {}; + +template +struct format_string_traits : format_string_traits_base {}; + +template +struct format_string_traits> : + format_string_traits_base {}; + +template +struct format_string_traits> : + format_string_traits_base {}; + +template +struct is_format_string : + std::integral_constant< + bool, std::is_constructible>::value> {}; + +template +struct is_compile_string : + std::integral_constant::value> {}; + +template +typename std::enable_if::value>::type + check_format_string(S) {} +template +typename std::enable_if::value>::type + check_format_string(S); +} // namespace internal + format_context::iterator vformat_to( internal::buffer &buf, string_view format_str, format_args args); wformat_context::iterator vformat_to( @@ -1292,44 +1342,6 @@ typename std::enable_if< std::string vformat(string_view format_str, format_args args); std::wstring vformat(wstring_view format_str, wformat_args args); -namespace internal { -// If S is a format string type, format_string_traints::char_type gives its -// character type. -template -struct format_string_traits { - private: - // Use construtbility as a way to detect if format_string_traits is - // specialized because other methods are broken on MSVC2013. - format_string_traits(); -}; - -template -struct format_string_traits_base { typedef Char char_type; }; - -template -struct format_string_traits: format_string_traits_base {}; - -template -struct format_string_traits: format_string_traits_base {}; - -template -struct format_string_traits>: - format_string_traits_base {}; - -template -struct format_string_traits>: - format_string_traits_base {}; - -template -struct is_format_string: - std::integral_constant< - bool, std::is_constructible>::value> {}; - -template -typename std::enable_if::value>::type - check_format_string(S) {} -} // namespace internal - /** \rst Formats arguments and returns the result as a string. @@ -1343,15 +1355,15 @@ typename std::enable_if::value>::type template inline std::basic_string< typename internal::format_string_traits::char_type> - format(String format_str, const Args & ... args) { + format(const String &format_str, const Args & ... args) { typedef typename internal::format_string_traits::char_type char_type; internal::check_format_string(format_str); // This should be just - // return vformat(format_str, make_format_args(args...)); + // return vformat(format_str, make_format_args(args...)); // but gcc has trouble optimizing the latter, so break it down. typedef typename buffer_context::type context_type; format_arg_store as{args...}; - return vformat(format_str, as); + return vformat(basic_string_view(format_str), as); } FMT_API void vprint(std::FILE *f, string_view format_str, format_args args); diff --git a/include/fmt/format.h b/include/fmt/format.h index e522ca4f..6013c537 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1352,10 +1352,13 @@ FMT_CONSTEXPR unsigned basic_parse_context::next_arg_id() { return 0; } -struct compile_string {}; - namespace internal { +template +struct format_string_traits< + S, typename std::enable_if::value>::type>: + format_string_traits_base {}; + template FMT_CONSTEXPR void handle_int_type_spec(Char spec, Handler &&handler) { switch (spec) { @@ -1536,8 +1539,6 @@ class arg_formatter_base { writer_type writer_; format_specs &specs_; - FMT_DISALLOW_COPY_AND_ASSIGN(arg_formatter_base); - struct char_writer { char_type value; template @@ -1645,10 +1646,6 @@ class arg_formatter_base { } }; -template -struct is_compile_string: - std::integral_constant::value> {}; - template FMT_CONSTEXPR bool is_name_start(Char c) { return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || '_' == c; @@ -2464,13 +2461,9 @@ class basic_writer { typedef basic_format_specs format_specs; private: - // Output iterator. - iterator out_; - + iterator out_; // Output iterator. std::unique_ptr locale_; - FMT_DISALLOW_COPY_AND_ASSIGN(basic_writer); - iterator out() const { return out_; } // Attempts to reserve space for n extra characters in the output range. @@ -3677,14 +3670,6 @@ inline std::wstring vformat(wstring_view format_str, wformat_args args) { return to_string(buffer); } -template -inline typename std::enable_if< - internal::is_compile_string::value, std::string>::type - format(String format_str, const Args & ... args) { - internal::check_format_string(format_str); - return vformat(format_str.data(), make_format_args(args...)); -} - template inline typename std::enable_if::value>::type print(String format_str, const Args & ... args) { @@ -3983,6 +3968,7 @@ FMT_END_NAMESPACE struct S : fmt::compile_string { \ static FMT_CONSTEXPR pointer data() { return s; } \ static FMT_CONSTEXPR size_t size() { return sizeof(s); } \ + explicit operator fmt::string_view() const { return s; } \ }; \ return S{}; \ }() diff --git a/include/fmt/posix.h b/include/fmt/posix.h index 8682d716..cadb6d2a 100644 --- a/include/fmt/posix.h +++ b/include/fmt/posix.h @@ -184,7 +184,9 @@ public: #else private: - FMT_DISALLOW_COPY_AND_ASSIGN(buffered_file); + buffered_file(const buffered_file &) = delete; + void operator=(const buffered_file &) = delete; + public: buffered_file(buffered_file &&other) FMT_NOEXCEPT : file_(other.file_) { @@ -294,7 +296,8 @@ class file { #else private: - FMT_DISALLOW_COPY_AND_ASSIGN(file); + file(const file &) = delete; + void operator=(const file &) = delete; public: file(file &&other) FMT_NOEXCEPT : fd_(other.fd_) { @@ -381,7 +384,8 @@ class Locale { locale_t locale_; - FMT_DISALLOW_COPY_AND_ASSIGN(Locale); + Locale(const Locale &) = delete; + void operator=(const Locale &) = delete; public: typedef locale_t Type; diff --git a/include/fmt/printf.h b/include/fmt/printf.h index dd2df386..cbf08a49 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -142,8 +142,6 @@ class char_converter: public function { private: basic_format_arg &arg_; - FMT_DISALLOW_COPY_AND_ASSIGN(char_converter); - public: explicit char_converter(basic_format_arg &arg) : arg_(arg) {} @@ -169,8 +167,6 @@ class printf_width_handler: public function { format_specs &spec_; - FMT_DISALLOW_COPY_AND_ASSIGN(printf_width_handler); - public: explicit printf_width_handler(format_specs &spec) : spec_(spec) {}