From 838400d29b95a02df8a4ea4a722c33aef135afd3 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 12 May 2018 08:33:51 -0700 Subject: [PATCH] Add inline namespace fmt::v5 --- include/fmt/core.h | 15 ++++++++++++--- include/fmt/format-inl.h | 4 ++-- include/fmt/format.h | 21 +++++++++----------- include/fmt/locale.h | 9 +++++++-- include/fmt/ostream.h | 6 ++---- include/fmt/posix.h | 6 +++--- include/fmt/printf.h | 4 ++-- include/fmt/time.h | 4 ++-- src/format.cc | 4 ++-- src/posix.cc | 41 +++++++++++++++++++++------------------- test/format-test.cc | 12 ++++++------ test/util-test.cc | 8 ++++---- 12 files changed, 73 insertions(+), 61 deletions(-) diff --git a/include/fmt/core.h b/include/fmt/core.h index 1d5c6793..f497e79d 100644 --- a/include/fmt/core.h +++ b/include/fmt/core.h @@ -133,6 +133,15 @@ # define FMT_DTOR_NOEXCEPT FMT_NOEXCEPT #endif +#if FMT_HAS_FEATURE(cxx_inline_namespaces) || FMT_MSC_VER >= 1900 +# define FMT_INLINE_NAMESPACE inline namespace +# define FMT_END_NAMESPACE }} +#else +# define FMT_INLINE_NAMESPACE namespace +# define FMT_END_NAMESPACE } using namespace v5; } +#endif +#define FMT_BEGIN_NAMESPACE namespace fmt { FMT_INLINE_NAMESPACE v5 { + #if !defined(FMT_HEADER_ONLY) && defined(_WIN32) # ifdef FMT_EXPORT # define FMT_API __declspec(dllexport) @@ -172,7 +181,7 @@ # include #endif -namespace fmt { +FMT_BEGIN_NAMESPACE // An implementation of declval for pre-C++11 compilers such as gcc 4. namespace internal { @@ -1190,7 +1199,7 @@ template struct is_contiguous> : std::true_type {}; template -struct is_contiguous> : std::true_type {}; +struct is_contiguous> : std::true_type {}; /** Formats a string and writes the output to ``out``. */ template @@ -1288,6 +1297,6 @@ inline void print(wstring_view format_str, const Args & ... args) { format_arg_store as(args...); vprint(format_str, as); } -} // namespace fmt +FMT_END_NAMESPACE #endif // FMT_CORE_H_ diff --git a/include/fmt/format-inl.h b/include/fmt/format-inl.h index 75b62ddc..f5c26d2f 100644 --- a/include/fmt/format-inl.h +++ b/include/fmt/format-inl.h @@ -69,7 +69,7 @@ inline fmt::internal::null<> strerror_s(char *, std::size_t, ...) { return fmt::internal::null<>(); } -namespace fmt { +FMT_BEGIN_NAMESPACE FMT_FUNC format_error::~format_error() throw() {} FMT_FUNC system_error::~system_error() FMT_DTOR_NOEXCEPT {} @@ -522,7 +522,7 @@ FMT_FUNC void vprint_colored(color c, wstring_view format, wformat_args args) { FMT_FUNC locale locale_provider::locale() { return fmt::locale(); } -} // namespace fmt +FMT_END_NAMESPACE #ifdef _MSC_VER # pragma warning(pop) diff --git a/include/fmt/format.h b/include/fmt/format.h index f0bf27af..a513c4ab 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -161,7 +161,7 @@ #if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL) && !defined(_MANAGED) # include // _BitScanReverse, _BitScanReverse64 -namespace fmt { +FMT_BEGIN_NAMESPACE namespace internal { // Avoid Clang with Microsoft CodeGen's -Wunknown-pragmas warning. # ifndef __clang__ @@ -206,10 +206,10 @@ inline uint32_t clzll(uint64_t x) { } # define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n) } -} +FMT_END_NAMESPACE #endif -namespace fmt { +FMT_BEGIN_NAMESPACE namespace internal { // An equivalent of `*reinterpret_cast(&source)` that doesn't produce @@ -246,7 +246,7 @@ struct dummy_int { int data[2]; operator int() const { return 0; } }; -typedef std::numeric_limits fputil; +typedef std::numeric_limits fputil; // Dummy implementations of system functions such as signbit and ecvt called // if the latter are not available. @@ -322,7 +322,7 @@ typename Allocator::value_type *allocate(Allocator& alloc, std::size_t n) { template inline T const_check(T value) { return value; } } // namespace internal -} // namespace fmt +FMT_END_NAMESPACE namespace std { // Standard permits specialization of std::numeric_limits. This specialization @@ -339,7 +339,7 @@ class numeric_limits : using namespace fmt::internal; // The resolution "priority" is: // isinf macro > std::isinf > ::isinf > fmt::internal::isinf - if (const_check(sizeof(isinf(x)) != sizeof(fmt::internal::dummy_int))) + if (const_check(sizeof(isinf(x)) != sizeof(dummy_int))) return isinf(x) != 0; return !_finite(static_cast(x)); } @@ -368,8 +368,7 @@ class numeric_limits : }; } // namespace std -namespace fmt { - +FMT_BEGIN_NAMESPACE template class basic_writer; @@ -2328,7 +2327,7 @@ class system_error : public std::runtime_error { may look like "Unknown error -1" and is platform-dependent. \endrst */ -FMT_API void format_system_error(fmt::internal::buffer &out, int error_code, +FMT_API void format_system_error(internal::buffer &out, int error_code, fmt::string_view message) FMT_NOEXCEPT; /** @@ -3527,10 +3526,8 @@ inline std::size_t formatted_size(string_view format_str, auto it = format_to(internal::counting_iterator(), format_str, args...); return it.count(); } -} // namespace fmt #if FMT_USE_USER_DEFINED_LITERALS -namespace fmt { namespace internal { # if FMT_UDL_TEMPLATE @@ -3611,8 +3608,8 @@ operator"" _a(const char *s, std::size_t) { return {s}; } inline internal::udl_arg operator"" _a(const wchar_t *s, std::size_t) { return {s}; } } // inline namespace literals -} // namespace fmt #endif // FMT_USE_USER_DEFINED_LITERALS +FMT_END_NAMESPACE #define FMT_STRING(s) [] { \ struct S : fmt::format_string { \ diff --git a/include/fmt/locale.h b/include/fmt/locale.h index acc39a12..a6a4b748 100644 --- a/include/fmt/locale.h +++ b/include/fmt/locale.h @@ -5,10 +5,13 @@ // // For the license information refer to format.h. +#ifndef FMT_LOCALE_H_ +#define FMT_LOCALE_H_ + #include "format.h" #include -namespace fmt { +FMT_BEGIN_NAMESPACE class locale { private: std::locale locale_; @@ -17,4 +20,6 @@ class locale { explicit locale(std::locale loc = std::locale()) : locale_(loc) {} std::locale get() { return locale_; } }; -} +FMT_END_NAMESPACE + +#endif // FMT_LOCALE_ diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 55f534de..fdbe1698 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -11,8 +11,7 @@ #include "format.h" #include -namespace fmt { - +FMT_BEGIN_NAMESPACE namespace internal { template @@ -149,7 +148,6 @@ inline void print(std::wostream &os, wstring_view format_str, const Args & ... args) { vprint(os, format_str, make_format_args(args...)); } - -} // namespace fmt +FMT_END_NAMESPACE #endif // FMT_OSTREAM_H_ diff --git a/include/fmt/posix.h b/include/fmt/posix.h index d01b80fd..67f5b91f 100644 --- a/include/fmt/posix.h +++ b/include/fmt/posix.h @@ -62,7 +62,7 @@ #define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1) -namespace fmt { +FMT_BEGIN_NAMESPACE /** \rst @@ -388,7 +388,7 @@ class Locale { Locale() : locale_(newlocale(LC_NUMERIC_MASK, "C", FMT_NULL)) { if (!locale_) - FMT_THROW(fmt::system_error(errno, "cannot create locale")); + FMT_THROW(system_error(errno, "cannot create locale")); } ~Locale() { freelocale(locale_); } @@ -404,7 +404,7 @@ class Locale { } }; #endif // FMT_LOCALE -} // namespace fmt +FMT_END_NAMESPACE #if !FMT_USE_RVALUE_REFERENCES namespace std { diff --git a/include/fmt/printf.h b/include/fmt/printf.h index 43fe65f2..5a396964 100644 --- a/include/fmt/printf.h +++ b/include/fmt/printf.h @@ -13,7 +13,7 @@ #include "ostream.h" -namespace fmt { +FMT_BEGIN_NAMESPACE namespace internal { // Checks if a value fits in int - used to avoid warnings about comparing @@ -706,6 +706,6 @@ inline int fprintf(std::wostream &os, wstring_view format_str, typename printf_context::type>(args...); return vfprintf(os, format_str, vargs); } -} // namespace fmt +FMT_END_NAMESPACE #endif // FMT_PRINTF_H_ diff --git a/include/fmt/time.h b/include/fmt/time.h index e562e96c..5976ea6e 100644 --- a/include/fmt/time.h +++ b/include/fmt/time.h @@ -11,7 +11,7 @@ #include "format.h" #include -namespace fmt { +FMT_BEGIN_NAMESPACE namespace internal{ inline null<> localtime_r(...) { return null<>(); } @@ -146,6 +146,6 @@ struct formatter { basic_memory_buffer tm_format; }; -} +FMT_END_NAMESPACE #endif // FMT_TIME_H_ diff --git a/src/format.cc b/src/format.cc index 23747bc2..9ebce0ca 100644 --- a/src/format.cc +++ b/src/format.cc @@ -7,7 +7,7 @@ #include "fmt/format-inl.h" -namespace fmt { +FMT_BEGIN_NAMESPACE template struct internal::basic_data; @@ -44,4 +44,4 @@ template FMT_API int internal::char_traits::format_float( template FMT_API int internal::char_traits::format_float( wchar_t *buffer, std::size_t size, const wchar_t *format, unsigned width, int precision, long double value); -} // namespace fmt +FMT_END_NAMESPACE diff --git a/src/posix.cc b/src/posix.cc index 7e0fac43..bcdab324 100644 --- a/src/posix.cc +++ b/src/posix.cc @@ -64,20 +64,21 @@ inline std::size_t convert_rwcount(std::size_t count) { return count; } #endif } -fmt::BufferedFile::~BufferedFile() FMT_NOEXCEPT { +FMT_BEGIN_NAMESPACE + +BufferedFile::~BufferedFile() FMT_NOEXCEPT { if (file_ && FMT_SYSTEM(fclose(file_)) != 0) - fmt::report_system_error(errno, "cannot close file"); + report_system_error(errno, "cannot close file"); } -fmt::BufferedFile::BufferedFile( - fmt::cstring_view filename, fmt::cstring_view mode) { +BufferedFile::BufferedFile(cstring_view filename, cstring_view mode) { FMT_RETRY_VAL(file_, FMT_SYSTEM(fopen(filename.c_str(), mode.c_str())), FMT_NULL); if (!file_) FMT_THROW(system_error(errno, "cannot open file {}", filename.c_str())); } -void fmt::BufferedFile::close() { +void BufferedFile::close() { if (!file_) return; int result = FMT_SYSTEM(fclose(file_)); @@ -89,14 +90,14 @@ void fmt::BufferedFile::close() { // A macro used to prevent expansion of fileno on broken versions of MinGW. #define FMT_ARGS -int fmt::BufferedFile::fileno() const { +int BufferedFile::fileno() const { int fd = FMT_POSIX_CALL(fileno FMT_ARGS(file_)); if (fd == -1) FMT_THROW(system_error(errno, "cannot get file descriptor")); return fd; } -fmt::File::File(fmt::cstring_view path, int oflag) { +File::File(cstring_view path, int oflag) { int mode = S_IRUSR | S_IWUSR; #if defined(_WIN32) && !defined(__MINGW32__) fd_ = -1; @@ -108,14 +109,14 @@ fmt::File::File(fmt::cstring_view path, int oflag) { FMT_THROW(system_error(errno, "cannot open file {}", path.c_str())); } -fmt::File::~File() FMT_NOEXCEPT { +File::~File() FMT_NOEXCEPT { // Don't retry close in case of EINTR! // See http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html if (fd_ != -1 && FMT_POSIX_CALL(close(fd_)) != 0) - fmt::report_system_error(errno, "cannot close file"); + report_system_error(errno, "cannot close file"); } -void fmt::File::close() { +void File::close() { if (fd_ == -1) return; // Don't retry close in case of EINTR! @@ -126,7 +127,7 @@ void fmt::File::close() { FMT_THROW(system_error(errno, "cannot close file")); } -long long fmt::File::size() const { +long long File::size() const { #ifdef _WIN32 // Use GetFileSize instead of GetFileSizeEx for the case when _WIN32_WINNT // is less than 0x0500 as is the case with some default MinGW builds. @@ -152,7 +153,7 @@ long long fmt::File::size() const { #endif } -std::size_t fmt::File::read(void *buffer, std::size_t count) { +std::size_t File::read(void *buffer, std::size_t count) { RWResult result = 0; FMT_RETRY(result, FMT_POSIX_CALL(read(fd_, buffer, convert_rwcount(count)))); if (result < 0) @@ -160,7 +161,7 @@ std::size_t fmt::File::read(void *buffer, std::size_t count) { return internal::to_unsigned(result); } -std::size_t fmt::File::write(const void *buffer, std::size_t count) { +std::size_t File::write(const void *buffer, std::size_t count) { RWResult result = 0; FMT_RETRY(result, FMT_POSIX_CALL(write(fd_, buffer, convert_rwcount(count)))); if (result < 0) @@ -168,7 +169,7 @@ std::size_t fmt::File::write(const void *buffer, std::size_t count) { return internal::to_unsigned(result); } -fmt::File fmt::File::dup(int fd) { +File File::dup(int fd) { // Don't retry as dup doesn't return EINTR. // http://pubs.opengroup.org/onlinepubs/009695399/functions/dup.html int new_fd = FMT_POSIX_CALL(dup(fd)); @@ -177,7 +178,7 @@ fmt::File fmt::File::dup(int fd) { return File(new_fd); } -void fmt::File::dup2(int fd) { +void File::dup2(int fd) { int result = 0; FMT_RETRY(result, FMT_POSIX_CALL(dup2(fd_, fd))); if (result == -1) { @@ -186,14 +187,14 @@ void fmt::File::dup2(int fd) { } } -void fmt::File::dup2(int fd, ErrorCode &ec) FMT_NOEXCEPT { +void File::dup2(int fd, ErrorCode &ec) FMT_NOEXCEPT { int result = 0; FMT_RETRY(result, FMT_POSIX_CALL(dup2(fd_, fd))); if (result == -1) ec = ErrorCode(errno); } -void fmt::File::pipe(File &read_end, File &write_end) { +void File::pipe(File &read_end, File &write_end) { // Close the descriptors first to make sure that assignments don't throw // and there are no leaks. read_end.close(); @@ -216,7 +217,7 @@ void fmt::File::pipe(File &read_end, File &write_end) { write_end = File(fds[1]); } -fmt::BufferedFile fmt::File::fdopen(const char *mode) { +BufferedFile File::fdopen(const char *mode) { // Don't retry as fdopen doesn't return EINTR. FILE *f = FMT_POSIX_CALL(fdopen(fd_, mode)); if (!f) @@ -227,7 +228,7 @@ fmt::BufferedFile fmt::File::fdopen(const char *mode) { return file; } -long fmt::getpagesize() { +long getpagesize() { #ifdef _WIN32 SYSTEM_INFO si; GetSystemInfo(&si); @@ -239,3 +240,5 @@ long fmt::getpagesize() { return size; #endif } +FMT_END_NAMESPACE + diff --git a/test/format-test.cc b/test/format-test.cc index 07bdfd7d..4a6533f5 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -1090,7 +1090,7 @@ TEST(FormatterTest, FormatConvertibleToStringView) { EXPECT_EQ("foo", format("{}", ConvertibleToStringView())); } -namespace fmt { +FMT_BEGIN_NAMESPACE template <> struct formatter { template @@ -1106,7 +1106,7 @@ struct formatter { return ctx.out(); } }; -} +FMT_END_NAMESPACE TEST(FormatterTest, FormatCustom) { Date date(2012, 12, 9); @@ -1116,14 +1116,14 @@ TEST(FormatterTest, FormatCustom) { class Answer {}; -namespace fmt { +FMT_BEGIN_NAMESPACE template <> struct formatter : formatter { auto format(Answer, fmt::format_context &ctx) -> decltype(ctx.out()) { return formatter::format(42, ctx); } }; -} +FMT_END_NAMESPACE TEST(FormatterTest, CustomFormat) { EXPECT_EQ("42", format("{0}", Answer())); @@ -1455,7 +1455,7 @@ struct variant { explicit variant(const char *) : type(STRING) {} }; -namespace fmt { +FMT_BEGIN_NAMESPACE template <> struct formatter : dynamic_formatter<> { auto format(variant value, format_context& ctx) -> decltype(ctx.out()) { @@ -1464,7 +1464,7 @@ struct formatter : dynamic_formatter<> { return dynamic_formatter<>::format("foo", ctx); } }; -} +FMT_END_NAMESPACE TEST(FormatTest, DynamicFormatter) { auto num = variant(42); diff --git a/test/util-test.cc b/test/util-test.cc index 772ccb5c..544d933d 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -54,7 +54,7 @@ basic_format_arg make_arg(const T &value) { } } // namespace -namespace fmt { +FMT_BEGIN_NAMESPACE template struct formatter { template @@ -70,7 +70,7 @@ struct formatter { return std::copy_n(test, std::strlen(test), ctx.out()); } }; -} +FMT_END_NAMESPACE void CheckForwarding( MockAllocator &alloc, AllocatorRef< MockAllocator > &ref) { @@ -459,7 +459,7 @@ TEST(UtilTest, MakeValueWithCustomFormatter) { EXPECT_TRUE(ctx.called); } -namespace fmt { +FMT_BEGIN_NAMESPACE namespace internal { template @@ -467,7 +467,7 @@ bool operator==(custom_value lhs, custom_value rhs) { return lhs.value == rhs.value; } } -} +FMT_END_NAMESPACE // Use a unique result type to make sure that there are no undesirable // conversions.