From dfe3c76e063eaf2db1d410a605daf8e3fe4fb372 Mon Sep 17 00:00:00 2001 From: virgiliofornazin Date: Mon, 6 Nov 2017 17:06:09 -0200 Subject: [PATCH] Added support for "namespace encapsulation" (better explained as a custom namespace prefix) for fmt library, using the new header file custom_namespace.h This file adds support for using fmt library defining a custom namespace in your code to avoid conflicting with other fmt library versions or 'encapsulate' the implementation you are using inside another namespace. --- .gitignore | 2 + fmt/CMakeLists.txt | 2 +- fmt/container.h | 6 +- fmt/custom_namespace.h | 101 ++++++++++++++++++++++++++++++++++ fmt/format.cc | 12 ++-- fmt/format.h | 68 ++++++++++++++--------- fmt/ostream.cc | 4 ++ fmt/ostream.h | 6 +- fmt/posix.cc | 4 ++ fmt/posix.h | 6 +- fmt/printf.cc | 4 ++ fmt/printf.h | 6 +- fmt/string.h | 8 ++- fmt/time.h | 6 +- test/assert-test.cc | 2 + test/container-test.cc | 2 + test/custom-formatter-test.cc | 2 + test/format-impl-test.cc | 2 + test/format-test.cc | 12 +++- test/gtest-extra-test.cc | 22 ++++++-- test/gtest-extra.cc | 2 + test/gtest-extra.h | 10 ++-- test/macro-test.cc | 2 + test/ostream-test.cc | 2 + test/posix-mock-test.cc | 2 + test/posix-test.cc | 2 + test/printf-test.cc | 2 + test/string-test.cc | 2 + test/time-test.cc | 2 + test/util-test.cc | 2 + test/util.cc | 2 + test/util.h | 2 +- 32 files changed, 258 insertions(+), 51 deletions(-) create mode 100644 fmt/custom_namespace.h diff --git a/.gitignore b/.gitignore index c57070c5..4a014ed7 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ CMakeCache.txt CMakeFiles Makefile run-msbuild.bat +.vs +.vscode diff --git a/fmt/CMakeLists.txt b/fmt/CMakeLists.txt index 90eaf575..1167144f 100644 --- a/fmt/CMakeLists.txt +++ b/fmt/CMakeLists.txt @@ -1,6 +1,6 @@ # Define the fmt library, its includes and the needed defines. # *.cc are added to FMT_HEADERS for the header-only configuration. -set(FMT_HEADERS container.h format.h format.cc ostream.h ostream.cc printf.h +set(FMT_HEADERS container.h custom_namespace.h format.h format.cc ostream.h ostream.cc printf.h printf.cc string.h time.h) if (HAVE_OPEN) set(FMT_HEADERS ${FMT_HEADERS} posix.h) diff --git a/fmt/container.h b/fmt/container.h index cb6303fb..dcf264f6 100644 --- a/fmt/container.h +++ b/fmt/container.h @@ -1,4 +1,4 @@ -/* +/* Formatting library for C++ - standard container utilities Copyright (c) 2012 - 2016, Victor Zverovich @@ -12,6 +12,8 @@ #include "format.h" +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { namespace internal { @@ -79,4 +81,6 @@ class BasicContainerWriter } // namespace fmt +FMT_CUSTOM_NAMESPACE_END + #endif // FMT_CONTAINER_H_ diff --git a/fmt/custom_namespace.h b/fmt/custom_namespace.h new file mode 100644 index 00000000..353ee368 --- /dev/null +++ b/fmt/custom_namespace.h @@ -0,0 +1,101 @@ +/* +Formatting library for C++ + +Copyright (c) 2012 - 2016, Victor Zverovich +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation +and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#ifndef FMT_CUSTOM_NAMESPACE_H_ +#define FMT_CUSTOM_NAMESPACE_H_ + +/* +This file adds support for using fmt library defining a custom namespace in your code +to avoid conflicting with other fmt library versions or 'encapsulate' the implementation +you are using inside another namespace. + +Those four macros below provide this feature: + +- FMT_CUSTOM_NAMESPACE - 1 if you want a custom namespace prefix, 0 otherwise +- FMT_CUSTOM_NAMESPACE_USING_NAMESPACE - the 'using namespace CUSTOM_NAMESPACE;' used into fmt source code, and +MUST terminate with a semicolon. It also could be used by your application if needed (but it's not recommended); +- FMT_CUSTOM_NAMESPACE_PREFIX - the 'CUSTOM_NAMESPACE' namespace prefix for the encapsulation namespace; +- FMT_CUSTOM_NAMESPACE_BEGIN - the 'namespace CUSTOM_NAMESPACE {' namespace begin for the encapsulation namespace; +- FMT_CUSTOM_NAMESPACE_END - the '} // namespace CUSTOM_NAMESPACE' namespace end for the encapsulation namespace; + +Please note that you could use more than one namespace nested, as the example below: + +#define FMT_CUSTOM_NAMESPACE 1 +#define FMT_CUSTOM_NAMESPACE_USING_NAMESPACE using namespace my_cpp_library::formatting_libraries; +#define FMT_CUSTOM_NAMESPACE_PREFIX my_cpp_library::formatting_libraries +#define FMT_CUSTOM_NAMESPACE_BEGIN namespace my_cpp_library { namespace formatting_libraries { +#define FMT_CUSTOM_NAMESPACE_END } } + +Then, in your code, you use this fmt in following way: + +#include +#include + +template +std::string format_some_string(char const * mask, args && ... argsv) +{ + my_cpp_library::formatting_libraries::fmt::MemoryWriter writer; + + writer.write(mask, std::forward(argsv) ...); + + return writer.str(); +} + +int main(int argc, char ** argv) +{ + std::cout << format_some_string("{} from somewhere", "Hello") << std::endl; + + return 0; +} + +Below, the macro definition you could adjust for your needs, please pay attention to example above to show +how to change the final fmt library namespace. +*/ + +#define FMT_CUSTOM_NAMESPACE 0 +#define FMT_CUSTOM_NAMESPACE_USING_NAMESPACE // using namespace my_cpp_library::formatting_libraries; +#define FMT_CUSTOM_NAMESPACE_PREFIX // my_cpp_library::formatting_libraries +#define FMT_CUSTOM_NAMESPACE_BEGIN // namespace my_cpp_library { namespace formatting_libraries { +#define FMT_CUSTOM_NAMESPACE_END // } } + +/* +If you doesn't define FMT_CUSTOM_NAMESPACE to 1, bad things happen, so we disable custom namespaces defined +above, to allow build tests and unit tests pass without any false negatives... +*/ + +#if (FMT_CUSTOM_NAMESPACE != 1) +# undef FMT_CUSTOM_NAMESPACE_USING_NAMESPACE +# define FMT_CUSTOM_NAMESPACE_USING_NAMESPACE +# undef FMT_CUSTOM_NAMESPACE_PREFIX +# define FMT_CUSTOM_NAMESPACE_PREFIX +# undef FMT_CUSTOM_NAMESPACE_BEGIN +# define FMT_CUSTOM_NAMESPACE_BEGIN +# undef FMT_CUSTOM_NAMESPACE_END +# define FMT_CUSTOM_NAMESPACE_END +#endif + +#endif // FMT_CUSTOM_NAMESPACE_H_ diff --git a/fmt/format.cc b/fmt/format.cc index 2d236bc6..f32ab61f 100644 --- a/fmt/format.cc +++ b/fmt/format.cc @@ -73,14 +73,16 @@ // Dummy implementations of strerror_r and strerror_s called if corresponding // system functions are not available. FMT_MAYBE_UNUSED -static inline fmt::internal::Null<> strerror_r(int, char *, ...) { - return fmt::internal::Null<>(); +static inline FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::Null<> strerror_r(int, char *, ...) { + return FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::Null<>(); } FMT_MAYBE_UNUSED -static inline fmt::internal::Null<> strerror_s(char *, std::size_t, ...) { - return fmt::internal::Null<>(); +static inline FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::Null<> strerror_s(char *, std::size_t, ...) { + return FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::Null<>(); } +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { FMT_FUNC internal::RuntimeError::~RuntimeError() FMT_DTOR_NOEXCEPT {} @@ -490,6 +492,8 @@ template FMT_API int internal::CharTraits::format_float( } // namespace fmt +FMT_CUSTOM_NAMESPACE_END + #ifdef _MSC_VER # pragma warning(pop) #endif diff --git a/fmt/format.h b/fmt/format.h index 7748bfac..30bf3977 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -28,6 +28,8 @@ #ifndef FMT_FORMAT_H_ #define FMT_FORMAT_H_ +#include "custom_namespace.h" + #define FMT_INCLUDE #include #include @@ -355,6 +357,12 @@ typedef __int64 intmax_t; // if the clz and clzll builtins are not available. #if FMT_MSC_VER && !defined(FMT_BUILTIN_CLZLL) && !defined(_MANAGED) # include // _BitScanReverse, _BitScanReverse64 +# define FMT_HAS_MSVC_INTRINSICS +#endif + +FMT_CUSTOM_NAMESPACE_BEGIN + +#ifdef FMT_HAS_MSVC_INTRINSICS namespace fmt { namespace internal { @@ -401,8 +409,8 @@ inline uint32_t clzll(uint64_t x) { return 63 - r; } # define FMT_BUILTIN_CLZLL(n) fmt::internal::clzll(n) -} -} +} // namespace internal +} // namespace fmt #endif namespace fmt { @@ -426,24 +434,26 @@ inline DummyInt _isnan(...) { return DummyInt(); } // warnings. template inline T const_check(T value) { return value; } -} +} // namespace internal } // namespace fmt +FMT_CUSTOM_NAMESPACE_END + namespace std { // Standard permits specialization of std::numeric_limits. This specialization // is used to resolve ambiguity between isinf and std::isinf in glibc: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48891 // and the same for isnan and signbit. template <> -class numeric_limits : +class numeric_limits : public std::numeric_limits { public: // Portable version of isinf. template static bool isinfinity(T x) { - using namespace fmt::internal; + using namespace FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal; // The resolution "priority" is: - // isinf macro > std::isinf > ::isinf > fmt::internal::isinf + // isinf macro > std::isinf > ::isinf > FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::isinf if (const_check(sizeof(isinf(x)) == sizeof(bool) || sizeof(isinf(x)) == sizeof(int))) { return isinf(x) != 0; @@ -454,7 +464,7 @@ class numeric_limits : // Portable version of isnan. template static bool isnotanumber(T x) { - using namespace fmt::internal; + using namespace FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal; if (const_check(sizeof(isnan(x)) == sizeof(bool) || sizeof(isnan(x)) == sizeof(int))) { return isnan(x) != 0; @@ -464,7 +474,7 @@ class numeric_limits : // Portable version of signbit. static bool isnegative(double x) { - using namespace fmt::internal; + using namespace FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal; if (const_check(sizeof(signbit(x)) == sizeof(bool) || sizeof(signbit(x)) == sizeof(int))) { return signbit(x) != 0; @@ -479,6 +489,8 @@ class numeric_limits : }; } // namespace std +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { // Fix the warning about long long on older versions of GCC @@ -2427,35 +2439,35 @@ inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) { # define FMT_MAKE_ARG_TYPE(n) T##n # define FMT_MAKE_ARG(n) const T##n &v##n # define FMT_ASSIGN_char(n) \ - arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter >(v##n) + arr[n] = FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::MakeValue< fmt::BasicFormatter >(v##n) # define FMT_ASSIGN_wchar_t(n) \ - arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter >(v##n) + arr[n] = FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::MakeValue< fmt::BasicFormatter >(v##n) #if FMT_USE_VARIADIC_TEMPLATES // Defines a variadic function returning void. # define FMT_VARIADIC_VOID(func, arg_type) \ template \ void func(arg_type arg0, const Args & ... args) { \ - typedef fmt::internal::ArgArray ArgArray; \ + typedef FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::ArgArray ArgArray; \ typename ArgArray::Type array{ \ - ArgArray::template make >(args)...}; \ - func(arg0, fmt::ArgList(fmt::internal::make_type(args...), array)); \ + ArgArray::template make >(args)...}; \ + func(arg0, fmt::ArgList(FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::make_type(args...), array)); \ } // Defines a variadic constructor. # define FMT_VARIADIC_CTOR(ctor, func, arg0_type, arg1_type) \ template \ ctor(arg0_type arg0, arg1_type arg1, const Args & ... args) { \ - typedef fmt::internal::ArgArray ArgArray; \ + typedef FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::ArgArray ArgArray; \ typename ArgArray::Type array{ \ - ArgArray::template make >(args)...}; \ - func(arg0, arg1, fmt::ArgList(fmt::internal::make_type(args...), array)); \ + ArgArray::template make >(args)...}; \ + func(arg0, arg1, fmt::ArgList(FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::make_type(args...), array)); \ } #else # define FMT_MAKE_REF(n) \ - fmt::internal::MakeValue< fmt::BasicFormatter >(v##n) + FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::MakeValue< FMT_CUSTOM_NAMESPACE_PREFIX::fmt::BasicFormatter >(v##n) # define FMT_MAKE_REF2(n) v##n // Defines a wrapper for a function taking one argument of type arg_type @@ -2463,14 +2475,14 @@ inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) { # define FMT_WRAP1(func, arg_type, n) \ template \ inline void func(arg_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \ - const fmt::internal::ArgArray::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \ - func(arg1, fmt::ArgList( \ - fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \ + const FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::ArgArray::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \ + func(arg1, FMT_CUSTOM_NAMESPACE_PREFIX::fmt::ArgList( \ + FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \ } // Emulates a variadic function returning void on a pre-C++11 compiler. # define FMT_VARIADIC_VOID(func, arg_type) \ - inline void func(arg_type arg) { func(arg, fmt::ArgList()); } \ + inline void func(arg_type arg) { func(arg, FMT_CUSTOM_NAMESPACE_PREFIX::fmt::ArgList()); } \ FMT_WRAP1(func, arg_type, 1) FMT_WRAP1(func, arg_type, 2) \ FMT_WRAP1(func, arg_type, 3) FMT_WRAP1(func, arg_type, 4) \ FMT_WRAP1(func, arg_type, 5) FMT_WRAP1(func, arg_type, 6) \ @@ -2480,9 +2492,9 @@ inline uint64_t make_type(FMT_GEN15(FMT_ARG_TYPE_DEFAULT)) { # define FMT_CTOR(ctor, func, arg0_type, arg1_type, n) \ template \ ctor(arg0_type arg0, arg1_type arg1, FMT_GEN(n, FMT_MAKE_ARG)) { \ - const fmt::internal::ArgArray::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \ - func(arg0, arg1, fmt::ArgList( \ - fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \ + const FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::ArgArray::Type array = {FMT_GEN(n, FMT_MAKE_REF)}; \ + func(arg0, arg1, FMT_CUSTOM_NAMESPACE_PREFIX::fmt::ArgList( \ + FMT_CUSTOM_NAMESPACE_PREFIX::fmt::internal::make_type(FMT_GEN(n, FMT_MAKE_REF2)), array)); \ } // Emulates a variadic constructor on a pre-C++11 compiler. @@ -3605,7 +3617,7 @@ template void arg(StringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; template void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; -} +} // namespace fmt #if FMT_GCC_VERSION // Use the system_header pragma to suppress warnings about variadic macros @@ -3717,9 +3729,9 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; #define FMT_VARIADIC_CONST_W(ReturnType, func, ...) \ FMT_VARIADIC_(const, wchar_t, ReturnType, func, return func, __VA_ARGS__) -#define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id) +#define FMT_CAPTURE_ARG_(id, index) FMT_CUSTOM_NAMESPACE_PREFIX::fmt::arg(#id, id) -#define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L###id, id) +#define FMT_CAPTURE_ARG_W_(id, index) FMT_CUSTOM_NAMESPACE_PREFIX::fmt::arg(L###id, id) /** \rst @@ -4150,6 +4162,8 @@ operator"" _a(const wchar_t *s, std::size_t) { return {s}; } } // namespace fmt #endif // FMT_USE_USER_DEFINED_LITERALS +FMT_CUSTOM_NAMESPACE_END + // Restore warnings. #if FMT_GCC_VERSION >= 406 # pragma GCC diagnostic pop diff --git a/fmt/ostream.cc b/fmt/ostream.cc index 2d443f73..0428bce5 100644 --- a/fmt/ostream.cc +++ b/fmt/ostream.cc @@ -9,6 +9,8 @@ #include "ostream.h" +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { namespace internal { @@ -33,3 +35,5 @@ FMT_FUNC void print(std::ostream &os, CStringRef format_str, ArgList args) { internal::write(os, w); } } // namespace fmt + +FMT_CUSTOM_NAMESPACE_END diff --git a/fmt/ostream.h b/fmt/ostream.h index 6848aac2..cb91a2ef 100644 --- a/fmt/ostream.h +++ b/fmt/ostream.h @@ -1,4 +1,4 @@ -/* +/* Formatting library for C++ - std::ostream support Copyright (c) 2012 - 2016, Victor Zverovich @@ -13,6 +13,8 @@ #include "format.h" #include +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { namespace internal { @@ -101,6 +103,8 @@ FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args); FMT_VARIADIC(void, print, std::ostream &, CStringRef) } // namespace fmt +FMT_CUSTOM_NAMESPACE_END + #ifdef FMT_HEADER_ONLY # include "ostream.cc" #endif diff --git a/fmt/posix.cc b/fmt/posix.cc index 356668c1..f99cb276 100644 --- a/fmt/posix.cc +++ b/fmt/posix.cc @@ -48,6 +48,8 @@ # undef fileno #endif +FMT_CUSTOM_NAMESPACE_BEGIN + namespace { #ifdef _WIN32 // Return type of read and write functions. @@ -239,3 +241,5 @@ long fmt::getpagesize() { return size; #endif } + +FMT_CUSTOM_NAMESPACE_END diff --git a/fmt/posix.h b/fmt/posix.h index 88512de5..141f9b1d 100644 --- a/fmt/posix.h +++ b/fmt/posix.h @@ -1,4 +1,4 @@ -/* +/* A C++ interface to POSIX functions. Copyright (c) 2012 - 2016, Victor Zverovich @@ -64,6 +64,8 @@ #define FMT_RETRY(result, expression) FMT_RETRY_VAL(result, expression, -1) +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { // An error code. @@ -356,6 +358,8 @@ class Locale { #endif // FMT_LOCALE } // namespace fmt +FMT_CUSTOM_NAMESPACE_END + #if !FMT_USE_RVALUE_REFERENCES namespace std { // For compatibility with C++98. diff --git a/fmt/printf.cc b/fmt/printf.cc index 95d7a36a..71635562 100644 --- a/fmt/printf.cc +++ b/fmt/printf.cc @@ -10,6 +10,8 @@ #include "format.h" #include "printf.h" +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { template @@ -30,3 +32,5 @@ template void PrintfFormatter::format(WCStringRef format); #endif // FMT_HEADER_ONLY } // namespace fmt + +FMT_CUSTOM_NAMESPACE_END diff --git a/fmt/printf.h b/fmt/printf.h index 46205a78..280db231 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -1,4 +1,4 @@ -/* +/* Formatting library for C++ Copyright (c) 2012 - 2016, Victor Zverovich @@ -15,6 +15,8 @@ #include "ostream.h" +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { namespace internal { @@ -596,6 +598,8 @@ inline int fprintf(std::ostream &os, CStringRef format_str, ArgList args) { FMT_VARIADIC(int, fprintf, std::ostream &, CStringRef) } // namespace fmt +FMT_CUSTOM_NAMESPACE_END + #ifdef FMT_HEADER_ONLY # include "printf.cc" #endif diff --git a/fmt/string.h b/fmt/string.h index 05996eb5..edaa948a 100644 --- a/fmt/string.h +++ b/fmt/string.h @@ -1,4 +1,4 @@ -/* +/* Formatting library for C++ - string utilities Copyright (c) 2012 - 2016, Victor Zverovich @@ -16,6 +16,8 @@ #include "format.h" +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { namespace internal { @@ -143,6 +145,8 @@ std::wstring to_wstring(const T &value) { w << value; return w.str(); } -} +} // namespace fmt + +FMT_CUSTOM_NAMESPACE_END #endif // FMT_STRING_H_ diff --git a/fmt/time.h b/fmt/time.h index c98b0e01..4fb884bb 100644 --- a/fmt/time.h +++ b/fmt/time.h @@ -1,4 +1,4 @@ -/* +/* Formatting library for C++ - time formatting Copyright (c) 2012 - 2016, Victor Zverovich @@ -19,6 +19,8 @@ # pragma warning(disable: 4996) // "deprecated" functions #endif +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { template void format_arg(BasicFormatter &f, @@ -136,6 +138,8 @@ inline std::tm gmtime(std::time_t time) { } } //namespace fmt +FMT_CUSTOM_NAMESPACE_END + #ifdef _MSC_VER # pragma warning(pop) #endif diff --git a/test/assert-test.cc b/test/assert-test.cc index eef342ee..42ff6a79 100644 --- a/test/assert-test.cc +++ b/test/assert-test.cc @@ -28,6 +28,8 @@ #include "fmt/format.h" #include "gtest/gtest.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + #if GTEST_HAS_DEATH_TEST # define EXPECT_DEBUG_DEATH_IF_SUPPORTED(statement, regex) \ EXPECT_DEBUG_DEATH(statement, regex) diff --git a/test/container-test.cc b/test/container-test.cc index 8cafb3d0..8574b417 100644 --- a/test/container-test.cc +++ b/test/container-test.cc @@ -10,6 +10,8 @@ #include "fmt/container.h" #include "gtest/gtest.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + using fmt::internal::ContainerBuffer; TEST(ContainerBufferTest, Empty) { diff --git a/test/custom-formatter-test.cc b/test/custom-formatter-test.cc index cc9c4485..976733c3 100644 --- a/test/custom-formatter-test.cc +++ b/test/custom-formatter-test.cc @@ -10,6 +10,8 @@ #include "fmt/printf.h" #include "gtest-extra.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + using fmt::BasicPrintfArgFormatter; // A custom argument formatter that doesn't print `-` for floating-point values diff --git a/test/format-impl-test.cc b/test/format-impl-test.cc index 1eb5e916..c0341fd4 100644 --- a/test/format-impl-test.cc +++ b/test/format-impl-test.cc @@ -33,6 +33,8 @@ #include "fmt/format.cc" #include "fmt/printf.cc" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + #include #include diff --git a/test/format-test.cc b/test/format-test.cc index f512ef48..5068f020 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -49,6 +49,10 @@ struct LocaleMock { MOCK_METHOD0(localeconv, lconv *()); } *LocaleMock::instance; +#include "fmt/custom_namespace.h" + +FMT_CUSTOM_NAMESPACE_BEGIN + namespace fmt { namespace std { using namespace ::std; @@ -56,11 +60,15 @@ lconv *localeconv() { return LocaleMock::instance ? LocaleMock::instance->localeconv() : ::std::localeconv(); } -} -} +} // namespace std +} // namespace fmt + +FMT_CUSTOM_NAMESPACE_END #include "fmt/format.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + #include "util.h" #include "mock-allocator.h" #include "gtest-extra.h" diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index 6a8c5676..93f09507 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -38,6 +38,8 @@ #include "util.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + using testing::internal::scoped_ptr; namespace { @@ -262,17 +264,29 @@ TEST(ExpectTest, EXPECT_THROW_MSG) { " Actual: test"); } +#if (FMT_CUSTOM_NAMESPACE == 1) +#define FMT_COMPILER_STRINGFY_(x) #x +#define FMT_COMPILER_STRINGFY(x) FMT_COMPILER_STRINGFY_(x) +#define EXPECT_CUSTOM_NAMESPACE_PREFIX FMT_COMPILER_STRINGFY(FMT_CUSTOM_NAMESPACE_PREFIX) +#else +#define EXPECT_CUSTOM_NAMESPACE_PREFIX "" +#endif + +#define EXPECT_SYSTEM_ERROR_THROW_EXCEPTION_MESSAGE "Expected: throw_exception() throws an exception of " \ + "type " EXPECT_CUSTOM_NAMESPACE_PREFIX "::fmt::SystemError.\n Actual: it throws a different type." + +#define EXPECT_SYSTEM_ERROR_DO_NOTHING_MESSAGE "Expected: do_nothing() throws an exception of type " EXPECT_CUSTOM_NAMESPACE_PREFIX "::fmt::SystemError.\n" \ + " Actual: it throws nothing." + // Tests EXPECT_SYSTEM_ERROR. TEST(ExpectTest, EXPECT_SYSTEM_ERROR) { EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "test"); EXPECT_NONFATAL_FAILURE( EXPECT_SYSTEM_ERROR(throw_exception(), EDOM, "test"), - "Expected: throw_exception() throws an exception of " - "type fmt::SystemError.\n Actual: it throws a different type."); + EXPECT_SYSTEM_ERROR_THROW_EXCEPTION_MESSAGE); EXPECT_NONFATAL_FAILURE( EXPECT_SYSTEM_ERROR(do_nothing(), EDOM, "test"), - "Expected: do_nothing() throws an exception of type fmt::SystemError.\n" - " Actual: it throws nothing."); + EXPECT_SYSTEM_ERROR_DO_NOTHING_MESSAGE); EXPECT_NONFATAL_FAILURE( EXPECT_SYSTEM_ERROR(throw_system_error(), EDOM, "other"), fmt::format( diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index 7640d154..fecfb656 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -29,6 +29,8 @@ #if FMT_USE_FILE_DESCRIPTORS +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + using fmt::File; void OutputRedirect::flush() { diff --git a/test/gtest-extra.h b/test/gtest-extra.h index 5f7fe29d..eae91f1d 100644 --- a/test/gtest-extra.h +++ b/test/gtest-extra.h @@ -81,10 +81,10 @@ FMT_TEST_THROW_(statement, expected_exception, \ expected_message, GTEST_NONFATAL_FAILURE_) -std::string format_system_error(int error_code, fmt::StringRef message); +std::string format_system_error(int error_code, FMT_CUSTOM_NAMESPACE_PREFIX::fmt::StringRef message); #define EXPECT_SYSTEM_ERROR(statement, error_code, message) \ - EXPECT_THROW_MSG(statement, fmt::SystemError, \ + EXPECT_THROW_MSG(statement, FMT_CUSTOM_NAMESPACE_PREFIX::fmt::SystemError, \ format_system_error(error_code, message)) #if FMT_USE_FILE_DESCRIPTORS @@ -94,8 +94,8 @@ std::string format_system_error(int error_code, fmt::StringRef message); class OutputRedirect { private: FILE *file_; - fmt::File original_; // Original file passed to redirector. - fmt::File read_end_; // Read end of the pipe where the output is redirected. + FMT_CUSTOM_NAMESPACE_PREFIX::fmt::File original_; // Original file passed to redirector. + FMT_CUSTOM_NAMESPACE_PREFIX::fmt::File read_end_; // Read end of the pipe where the output is redirected. GTEST_DISALLOW_COPY_AND_ASSIGN_(OutputRedirect); @@ -165,7 +165,7 @@ class SuppressAssert { EXPECT_SYSTEM_ERROR(SUPPRESS_ASSERT(statement), error_code, message) // Attempts to read count characters from a file. -std::string read(fmt::File &f, std::size_t count); +std::string read(FMT_CUSTOM_NAMESPACE_PREFIX::fmt::File &f, std::size_t count); #define EXPECT_READ(file, expected_content) \ EXPECT_EQ(expected_content, read(file, std::strlen(expected_content))) diff --git a/test/macro-test.cc b/test/macro-test.cc index f6763eae..cf09813f 100644 --- a/test/macro-test.cc +++ b/test/macro-test.cc @@ -31,6 +31,8 @@ #define FMT_USE_VARIADIC_TEMPLATES 0 #include "fmt/format.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + #define IDENTITY(x) x TEST(UtilTest, Gen) { diff --git a/test/ostream-test.cc b/test/ostream-test.cc index 486981bc..9e84507f 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -32,6 +32,8 @@ #include "gtest-extra.h" #include "util.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + using fmt::format; using fmt::FormatError; diff --git a/test/posix-mock-test.cc b/test/posix-mock-test.cc index 7a78327e..7a7d1cb7 100644 --- a/test/posix-mock-test.cc +++ b/test/posix-mock-test.cc @@ -31,6 +31,8 @@ #include "posix-mock.h" #include "fmt/posix.cc" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + #include #include #include diff --git a/test/posix-test.cc b/test/posix-test.cc index e6332bf0..27b2d6cb 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -32,6 +32,8 @@ #include "gtest-extra.h" #include "util.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + #ifdef fileno # undef fileno #endif diff --git a/test/printf-test.cc b/test/printf-test.cc index 81a041d7..0342e67c 100644 --- a/test/printf-test.cc +++ b/test/printf-test.cc @@ -34,6 +34,8 @@ #include "gtest-extra.h" #include "util.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + using fmt::format; using fmt::FormatError; diff --git a/test/string-test.cc b/test/string-test.cc index 06f55f65..d775d903 100644 --- a/test/string-test.cc +++ b/test/string-test.cc @@ -10,6 +10,8 @@ #include "fmt/string.h" #include "gtest/gtest.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + using fmt::internal::StringBuffer; TEST(StringBufferTest, Empty) { diff --git a/test/time-test.cc b/test/time-test.cc index 8b6c3612..4236db43 100644 --- a/test/time-test.cc +++ b/test/time-test.cc @@ -13,6 +13,8 @@ #include "gmock/gmock.h" #include "fmt/time.h" +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + TEST(TimeTest, Format) { std::tm tm = std::tm(); tm.tm_year = 116; diff --git a/test/util-test.cc b/test/util-test.cc index 6617b857..93ea59ff 100644 --- a/test/util-test.cc +++ b/test/util-test.cc @@ -51,6 +51,8 @@ #undef max +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + using fmt::StringRef; using fmt::internal::Arg; using fmt::Buffer; diff --git a/test/util.cc b/test/util.cc index 7b1cdb57..eedaf5c8 100644 --- a/test/util.cc +++ b/test/util.cc @@ -28,6 +28,8 @@ #include "util.h" #include +FMT_CUSTOM_NAMESPACE_USING_NAMESPACE + void increment(char *s) { for (int i = static_cast(std::strlen(s)) - 1; i >= 0; --i) { if (s[i] != '9') { diff --git a/test/util.h b/test/util.h index b7faf62a..ea207344 100644 --- a/test/util.h +++ b/test/util.h @@ -55,7 +55,7 @@ std::string get_system_error(int error_code); extern const char *const FILE_CONTENT; // Opens a buffered file for reading. -fmt::BufferedFile open_buffered_file(FILE **fp = 0); +FMT_CUSTOM_NAMESPACE_PREFIX::fmt::BufferedFile open_buffered_file(FILE **fp = 0); inline FILE *safe_fopen(const char *filename, const char *mode) { #if defined(_WIN32) && !defined(__MINGW32__)