From 19d4e605619a6fcf445be338709bd5a6c8a9159d Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 1 Sep 2023 18:22:40 +0300 Subject: [PATCH] Replaced details::make_unique with std::make_unique --- README.md | 2 +- bench/formatter-bench.cpp | 2 +- example/example.cpp | 6 +- include/spdlog/common.h | 18 +---- include/spdlog/details/registry.h | 2 +- include/spdlog/logger-inl.h | 2 +- include/spdlog/pattern_formatter-inl.h | 94 +++++++++++------------ include/spdlog/pattern_formatter.h | 2 +- include/spdlog/sinks/ansicolor_sink-inl.h | 2 +- include/spdlog/sinks/base_sink-inl.h | 4 +- include/spdlog/sinks/dist_sink.h | 2 +- include/spdlog/sinks/mongo_sink.h | 2 +- include/spdlog/sinks/stdout_sinks-inl.h | 2 +- include/spdlog/sinks/wincolor_sink-inl.h | 2 +- tests/test_pattern_formatter.cpp | 2 +- 15 files changed, 64 insertions(+), 80 deletions(-) diff --git a/README.md b/README.md index 6ce32fb6..551f9ebc 100644 --- a/README.md +++ b/README.md @@ -323,7 +323,7 @@ public: std::unique_ptr clone() const override { - return spdlog::details::make_unique(); + return spdlog::std::make_unique(); } }; diff --git a/bench/formatter-bench.cpp b/bench/formatter-bench.cpp index 1454c6bb..c95164ff 100644 --- a/bench/formatter-bench.cpp +++ b/bench/formatter-bench.cpp @@ -10,7 +10,7 @@ void bench_formatter(benchmark::State &state, std::string pattern) { - auto formatter = spdlog::details::make_unique(pattern); + auto formatter = spdlog::std::make_unique(pattern); spdlog::memory_buf_t dest; std::string logger_name = "logger-name"; const char *text = "Hello. This is some message with length of 80 "; diff --git a/example/example.cpp b/example/example.cpp index 8d77ad4e..f81af788 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -338,15 +338,13 @@ public: std::unique_ptr clone() const override { - return spdlog::details::make_unique(); + return std::make_unique(); } }; void custom_flags_example() { - - using spdlog::details::make_unique; // for pre c++14 - auto formatter = make_unique(); + auto formatter = std::make_unique(); formatter->add_flag('*').set_pattern("[%n] [%*] [%^%l%$] %v"); // set the new formatter using spdlog::set_formatter(formatter) or logger->set_formatter(formatter) // spdlog::set_formatter(std::move(formatter)); diff --git a/include/spdlog/common.h b/include/spdlog/common.h index e3c97772..abbed079 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -330,29 +330,15 @@ constexpr std::basic_string_view to_string_view(std::basic_format_string= 201402L // C++14 and beyond -using std::enable_if_t; -using std::make_unique; -#else -template -using enable_if_t = typename std::enable_if::type; - -template -std::unique_ptr make_unique(Args &&...args) -{ - static_assert(!std::is_array::value, "arrays not supported"); - return std::unique_ptr(new T(std::forward(args)...)); -} -#endif // to avoid useless casts (see https://github.com/nlohmann/json/issues/2893#issuecomment-889152324) -template::value, int> = 0> +template::value, int> = 0> constexpr T conditional_static_cast(U value) { return static_cast(value); } -template::value, int> = 0> +template::value, int> = 0> constexpr T conditional_static_cast(U value) { return value; diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index bd2f909c..77624491 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -62,7 +62,7 @@ public: { std::lock_guard lock(flusher_mutex_); auto clbk = [this]() { this->flush_all(); }; - periodic_flusher_ = details::make_unique(clbk, interval); + periodic_flusher_ = std::make_unique(clbk, interval); } void set_error_handler(err_handler handler); diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 684e91bd..b4023fb3 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -96,7 +96,7 @@ SPDLOG_INLINE void logger::set_formatter(std::unique_ptr f) SPDLOG_INLINE void logger::set_pattern(std::string pattern, pattern_time_type time_type) { - auto new_formatter = details::make_unique(std::move(pattern), time_type); + auto new_formatter = std::make_unique(std::move(pattern), time_type); set_formatter(std::move(new_formatter)); } diff --git a/include/spdlog/pattern_formatter-inl.h b/include/spdlog/pattern_formatter-inl.h index 01afbe6f..b28a9e93 100644 --- a/include/spdlog/pattern_formatter-inl.h +++ b/include/spdlog/pattern_formatter-inl.h @@ -1041,7 +1041,7 @@ SPDLOG_INLINE pattern_formatter::pattern_formatter(pattern_time_type time_type, , last_log_secs_(0) { std::memset(&cached_tm_, 0, sizeof(cached_tm_)); - formatters_.push_back(details::make_unique(details::padding_info{})); + formatters_.push_back(std::make_unique(details::padding_info{})); } SPDLOG_INLINE std::unique_ptr pattern_formatter::clone() const @@ -1051,7 +1051,7 @@ SPDLOG_INLINE std::unique_ptr pattern_formatter::clone() const { cloned_custom_formatters[it.first] = it.second->clone(); } - auto cloned = details::make_unique(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters)); + auto cloned = std::make_unique(pattern_, pattern_time_type_, eol_, std::move(cloned_custom_formatters)); cloned->need_localtime(need_localtime_); #if defined(__GNUC__) && __GNUC__ < 5 return std::move(cloned); @@ -1118,198 +1118,198 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i switch (flag) { case ('+'): // default formatter - formatters_.push_back(details::make_unique(padding)); + formatters_.push_back(std::make_unique(padding)); need_localtime_ = true; break; case 'n': // logger name - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case 'l': // level - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case 'L': // short level - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('t'): // thread id - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('v'): // the message text - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('a'): // weekday - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('A'): // short weekday - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('b'): case ('h'): // month - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('B'): // short month - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('c'): // datetime - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('C'): // year 2 digits - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('Y'): // year 4 digits - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('D'): case ('x'): // datetime MM/DD/YY - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('m'): // month 1-12 - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('d'): // day of month 1-31 - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('H'): // hours 24 - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('I'): // hours 12 - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('M'): // minutes - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('S'): // seconds - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('e'): // milliseconds - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('f'): // microseconds - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('F'): // nanoseconds - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('E'): // seconds since epoch - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('p'): // am/pm - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('r'): // 12 hour clock 02:55:02 pm - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('R'): // 24-hour HH:MM time - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('T'): case ('X'): // ISO 8601 time format (HH:MM:SS) - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('z'): // timezone - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); need_localtime_ = true; break; case ('P'): // pid - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('^'): // color range start - formatters_.push_back(details::make_unique(padding)); + formatters_.push_back(std::make_unique(padding)); break; case ('$'): // color range end - formatters_.push_back(details::make_unique(padding)); + formatters_.push_back(std::make_unique(padding)); break; case ('@'): // source location (filename:filenumber) - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('s'): // short source filename - without directory name - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('g'): // full source filename - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('#'): // source line number - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('!'): // source funcname - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('%'): // % char - formatters_.push_back(details::make_unique('%')); + formatters_.push_back(std::make_unique('%')); break; case ('u'): // elapsed time since last log message in nanos - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('i'): // elapsed time since last log message in micros - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('o'): // elapsed time since last log message in millis - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; case ('O'): // elapsed time since last log message in seconds - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); break; default: // Unknown flag appears as is - auto unknown_flag = details::make_unique(); + auto unknown_flag = std::make_unique(); if (!padding.truncate_) { @@ -1323,7 +1323,7 @@ SPDLOG_INLINE void pattern_formatter::handle_flag_(char flag, details::padding_i else { padding.truncate_ = false; - formatters_.push_back(details::make_unique>(padding)); + formatters_.push_back(std::make_unique>(padding)); unknown_flag->add_ch(flag); formatters_.push_back((std::move(unknown_flag))); } @@ -1423,7 +1423,7 @@ SPDLOG_INLINE void pattern_formatter::compile_pattern_(const std::string &patter { if (!user_chars) { - user_chars = details::make_unique(); + user_chars = std::make_unique(); } user_chars->add_ch(*it); } diff --git a/include/spdlog/pattern_formatter.h b/include/spdlog/pattern_formatter.h index 4c87b21e..8811b8f7 100644 --- a/include/spdlog/pattern_formatter.h +++ b/include/spdlog/pattern_formatter.h @@ -94,7 +94,7 @@ public: template pattern_formatter &add_flag(char flag, Args &&...args) { - custom_handlers_[flag] = details::make_unique(std::forward(args)...); + custom_handlers_[flag] = std::make_unique(std::forward(args)...); return *this; } void set_pattern(std::string pattern); diff --git a/include/spdlog/sinks/ansicolor_sink-inl.h b/include/spdlog/sinks/ansicolor_sink-inl.h index c924fc5b..aea90c27 100644 --- a/include/spdlog/sinks/ansicolor_sink-inl.h +++ b/include/spdlog/sinks/ansicolor_sink-inl.h @@ -17,7 +17,7 @@ template SPDLOG_INLINE ansicolor_sink::ansicolor_sink(FILE *target_file, color_mode mode) : target_file_(target_file) , mutex_(ConsoleMutex::mutex()) - , formatter_(details::make_unique()) + , formatter_(std::make_unique()) { set_color_mode(mode); diff --git a/include/spdlog/sinks/base_sink-inl.h b/include/spdlog/sinks/base_sink-inl.h index 421fdf9d..b63cd67d 100644 --- a/include/spdlog/sinks/base_sink-inl.h +++ b/include/spdlog/sinks/base_sink-inl.h @@ -14,7 +14,7 @@ template SPDLOG_INLINE spdlog::sinks::base_sink::base_sink() - : formatter_{details::make_unique()} + : formatter_{std::make_unique()} {} template @@ -53,7 +53,7 @@ void SPDLOG_INLINE spdlog::sinks::base_sink::set_formatter(std::unique_pt template void SPDLOG_INLINE spdlog::sinks::base_sink::set_pattern_(const std::string &pattern) { - set_formatter_(details::make_unique(pattern)); + set_formatter_(std::make_unique(pattern)); } template diff --git a/include/spdlog/sinks/dist_sink.h b/include/spdlog/sinks/dist_sink.h index 7ec3a2ec..8eb1aa95 100644 --- a/include/spdlog/sinks/dist_sink.h +++ b/include/spdlog/sinks/dist_sink.h @@ -76,7 +76,7 @@ protected: void set_pattern_(const std::string &pattern) override { - set_formatter_(details::make_unique(pattern)); + set_formatter_(std::make_unique(pattern)); } void set_formatter_(std::unique_ptr sink_formatter) override diff --git a/include/spdlog/sinks/mongo_sink.h b/include/spdlog/sinks/mongo_sink.h index 6a8927f5..91f39b9b 100644 --- a/include/spdlog/sinks/mongo_sink.h +++ b/include/spdlog/sinks/mongo_sink.h @@ -45,7 +45,7 @@ public: { try { - client_ = spdlog::details::make_unique(mongocxx::uri{uri}); + client_ = spdlog::std::make_unique(mongocxx::uri{uri}); } catch (const std::exception &e) { diff --git a/include/spdlog/sinks/stdout_sinks-inl.h b/include/spdlog/sinks/stdout_sinks-inl.h index c1754370..3d9aa5b0 100644 --- a/include/spdlog/sinks/stdout_sinks-inl.h +++ b/include/spdlog/sinks/stdout_sinks-inl.h @@ -32,7 +32,7 @@ template SPDLOG_INLINE stdout_sink_base::stdout_sink_base(FILE *file) : mutex_(ConsoleMutex::mutex()) , file_(file) - , formatter_(details::make_unique()) + , formatter_(std::make_unique()) { #ifdef _WIN32 // get windows handle from the FILE* object diff --git a/include/spdlog/sinks/wincolor_sink-inl.h b/include/spdlog/sinks/wincolor_sink-inl.h index 8311929e..ee713590 100644 --- a/include/spdlog/sinks/wincolor_sink-inl.h +++ b/include/spdlog/sinks/wincolor_sink-inl.h @@ -19,7 +19,7 @@ template SPDLOG_INLINE wincolor_sink::wincolor_sink(void *out_handle, color_mode mode) : out_handle_(out_handle) , mutex_(ConsoleMutex::mutex()) - , formatter_(details::make_unique()) + , formatter_(std::make_unique()) { set_color_mode_impl(mode); diff --git a/tests/test_pattern_formatter.cpp b/tests/test_pattern_formatter.cpp index bafea884..e6edfca1 100644 --- a/tests/test_pattern_formatter.cpp +++ b/tests/test_pattern_formatter.cpp @@ -354,7 +354,7 @@ public: std::unique_ptr clone() const override { - return spdlog::details::make_unique(some_txt); + return std::make_unique(some_txt); } }; // test clone with custom flag formatters