From e0f8297e3e57512c423c571fb4dde5ced594e803 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 14 Mar 2022 00:59:17 +0000 Subject: Lots of perfect forwarding over const refs --- libadhocutil/compileTimeFormatter.h | 64 ++++++++++++++++------------------- libadhocutil/ctf-impl/printf-compat.h | 46 +++++++++++++------------ libadhocutil/exception.h | 3 +- libadhocutil/plugins.h | 5 +-- libadhocutil/resourcePool.cpp | 2 +- libadhocutil/resourcePool.h | 4 +-- libadhocutil/resourcePool.impl.h | 2 +- libadhocutil/unittests/testCache.cpp | 4 +-- 8 files changed, 65 insertions(+), 65 deletions(-) diff --git a/libadhocutil/compileTimeFormatter.h b/libadhocutil/compileTimeFormatter.h index 8588a35..e13328c 100644 --- a/libadhocutil/compileTimeFormatter.h +++ b/libadhocutil/compileTimeFormatter.h @@ -8,6 +8,7 @@ #include #include // IWYU pragma: export #include +#include // Mapped for for BOOST_PP_VARIADIC_SIZE, BOOST_PP... in tests // IWYU pragma: no_include @@ -15,14 +16,14 @@ namespace AdHoc { // Template char utils template constexpr bool - isdigit(const char_type & ch) + isdigit(const char_type ch) { return (ch >= '0' && ch <= '9'); } template constexpr bool - ispositivedigit(const char_type & ch) + ispositivedigit(const char_type ch) { return (ch >= '1' && ch <= '9'); } @@ -83,7 +84,7 @@ namespace AdHoc { /// Write parameters to stream. template static void - write(stream &, const Pn &...) + write(stream &, Pn &&...) { static_assert(!L, "invalid format string/arguments"); } @@ -94,9 +95,9 @@ namespace AdHoc { /// Continue processing parameters. template static inline void - next(stream & s, const Pn &... pn) + next(stream & s, Pn &&... pn) { - FormatterDetail::template Parser::run(s, pn...); + FormatterDetail::template Parser::run(s, std::forward(pn)...); } }; @@ -114,10 +115,10 @@ namespace AdHoc { StreamWriterT('?') { template static inline void - write(stream & s, const P & p, const Pn &... pn) + write(stream & s, P && p, Pn &&... pn) { - s << p; - StreamWriter::next(s, pn...); + s << std::forward

(p); + StreamWriter::next(s, std::forward(pn)...); } }; @@ -125,10 +126,10 @@ namespace AdHoc { StreamWriterT('%') { template static inline void - write(stream & s, const Pn &... pn) + write(stream & s, Pn &&... pn) { s << '%'; - StreamWriter::next(s, pn...); + StreamWriter::next(s, std::forward(pn)...); } }; @@ -165,10 +166,10 @@ namespace AdHoc { */ template static inline auto - get(const Pn &... pn) + get(Pn &&... pn) { std::basic_stringstream s; - return write(s, pn...).str(); + return write(s, std::forward(pn)...).str(); } /** * Get a string containing the result of formatting. @@ -177,9 +178,9 @@ namespace AdHoc { */ template inline auto - operator()(const Pn &... pn) const + operator()(Pn &&... pn) const { - return get(pn...); + return get(std::forward(pn)...); } /** @@ -190,9 +191,9 @@ namespace AdHoc { */ template static inline stream & - write(stream & s, const Pn &... pn) + write(stream & s, Pn &&... pn) { - return Parser::run(s, pn...); + return Parser::run(s, std::forward(pn)...); } /** * Write the result of formatting to the given stream. @@ -202,15 +203,15 @@ namespace AdHoc { */ template inline typename std::enable_if, stream>, stream>::type & - operator()(stream & s, const Pn &... pn) const + operator()(stream & s, Pn &&... pn) const { - return write(s, pn...); + return write(s, std::forward(pn)...); } private: template struct Parser { static inline stream & - run(stream & s, const Pn &... pn) + run(stream & s, Pn &&... pn) { if (pos != L) { constexpr auto ph = strchrnul(); @@ -218,20 +219,20 @@ namespace AdHoc { appendStream(s, &S[pos], ph - pos); } if constexpr (ph != L) { - packAndWrite(s, pn...); + packAndWrite(s, std::forward(pn)...); } } return s; } template static inline void - packAndWrite(stream & s, const Pn &... pn) + packAndWrite(stream & s, Pn &&... pn) { if constexpr (ph + off == L || sizeof...(Pck) == 32) { StreamWriter::write(s, pn...); } else if constexpr (ph + off < L) { - packAndWrite(s, pn...); + packAndWrite(s, std::forward(pn)...); } } }; @@ -243,12 +244,7 @@ namespace AdHoc { public: // cppcheck-suppress noExplicitConstructor // NOLINTNEXTLINE(hicpp-avoid-c-arrays,modernize-avoid-c-arrays,hicpp-explicit-conversions) - constexpr basic_fixed_string(const CharT (&str)[N + 1]) - { - for (decltype(N) x = 0; x < N; x++) { - this->at(x) = str[x]; - } - } + constexpr basic_fixed_string(const CharT (&str)[N + 1]) : basic_fixed_string {str, N} { } // NOLINTNEXTLINE(hicpp-avoid-c-arrays,modernize-avoid-c-arrays) constexpr basic_fixed_string(const CharT * str, decltype(N) len) { @@ -275,23 +271,23 @@ namespace AdHoc { template inline auto - scprintf(const Pn &... pn) + scprintf(Pn &&... pn) { - return FormatterDetail::get(pn...); + return FormatterDetail::get(std::forward(pn)...); } template inline auto & - scprintf(stream & strm, const Pn &... pn) + scprintf(stream & strm, Pn &&... pn) { - return FormatterDetail::write(strm, pn...); + return FormatterDetail::write(strm, std::forward(pn)...); } template inline auto & - cprintf(const Pn &... pn) + cprintf(Pn &&... pn) { - return scprintf(std::cout, pn...); + return scprintf(std::cout, std::forward(pn)...); } namespace literals { diff --git a/libadhocutil/ctf-impl/printf-compat.h b/libadhocutil/ctf-impl/printf-compat.h index a24c620..6494cb4 100644 --- a/libadhocutil/ctf-impl/printf-compat.h +++ b/libadhocutil/ctf-impl/printf-compat.h @@ -16,11 +16,11 @@ namespace AdHoc { StreamWriterT(__VA_ARGS__) { \ template \ static inline void \ - write(stream & s, const PARAMTYPE & p, const Pn &... pn) \ + write(stream & s, const PARAMTYPE & p, Pn &&... pn) \ { \ OP; \ s.copyfmt(std::ios(nullptr)); \ - StreamWriter::next(s, pn...); \ + StreamWriter::next(s, std::forward(pn)...); \ } \ } @@ -71,39 +71,39 @@ namespace AdHoc { StreamWriterT('p') { template static inline void - write(stream & s, Obj * const ptr, const Pn &... pn) + write(stream & s, Obj * const ptr, Pn &&... pn) { s << std::showbase << std::hex << reinterpret_cast(ptr); s.copyfmt(std::ios(nullptr)); - StreamWriter::next(s, pn...); + StreamWriter::next(s, std::forward(pn)...); } template static inline void - write(stream & s, const Ptr & ptr, const Pn &... pn) + write(stream & s, const Ptr & ptr, Pn &&... pn) { - write(s, ptr.get(), pn...); + write(s, ptr.get(), std::forward(pn)...); } }; StreamWriterT('m') { template static inline void - write(stream & s, const Pn &... pn) + write(stream & s, Pn &&... pn) { s << strerror(errno); s.copyfmt(std::ios(nullptr)); - StreamWriter::next(s, pn...); + StreamWriter::next(s, std::forward(pn)...); } }; StreamWriterT('n') { template static inline void - write(stream & s, std::streamoff * n, const Pn &... pn) + write(stream & s, std::streamoff * n, Pn &&... pn) { BOOST_ASSERT_MSG(n, "%n conversion requires non-null parameter"); *n = streamLength(s); s.copyfmt(std::ios(nullptr)); - StreamWriter::next(s, pn...); + StreamWriter::next(s, std::forward(pn)...); } }; @@ -137,11 +137,12 @@ namespace AdHoc { BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), NS, n), nn, sn...> { \ template \ static inline void \ - write(stream & s, const Pn &... pn) \ + write(stream & s, Pn &&... pn) \ { \ constexpr auto p = decdigits(); \ s << std::setw(p); \ - StreamWriter::write(s, pn...); \ + StreamWriter::write( \ + s, std::forward(pn)...); \ } \ }; BOOST_PP_REPEAT(6, FMTWIDTH, void) @@ -153,11 +154,12 @@ namespace AdHoc { BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), NS, n), nn, sn...> { \ template \ static inline void \ - write(stream & s, const Pn &... pn) \ + write(stream & s, Pn &&... pn) \ { \ constexpr auto p = decdigits(); \ s << std::setprecision(p); \ - StreamWriter::write(s, pn...); \ + StreamWriter::write( \ + s, std::forward(pn)...); \ } \ }; BOOST_PP_REPEAT(6, FMTPRECISION, void) @@ -169,26 +171,26 @@ namespace AdHoc { StreamWriterT('.', '*') { template static inline void - write(stream & s, int l, const Pn &... pn) + write(stream & s, int l, Pn &&... pn) { s << std::setw(l); - StreamWriter::write(s, pn...); + StreamWriter::write(s, std::forward(pn)...); } }; StreamWriterT('.', '*', 's') { template static inline void - write(stream & s, int l, const std::string_view p, const Pn &... pn) + write(stream & s, int l, const std::string_view p, Pn &&... pn) { - return write(s, static_cast(l), p, pn...); + return write(s, static_cast(l), p, std::forward(pn)...); } template static inline void - write(stream & s, size_t l, const std::string_view p, const Pn &... pn) + write(stream & s, size_t l, const std::string_view p, Pn &&... pn) { s << p.substr(0, l); s.copyfmt(std::ios(nullptr)); - StreamWriter::next(s, pn...); + StreamWriter::next(s, std::forward(pn)...); } }; @@ -197,10 +199,10 @@ namespace AdHoc { StreamWriterT(__VA_ARGS__) { \ template \ static inline void \ - write(stream & s, const Pn &... pn) \ + write(stream & s, Pn &&... pn) \ { \ OP; \ - StreamWriter::write(s, pn...); \ + StreamWriter::write(s, std::forward(pn)...); \ } \ } FLAGCONV(s << std::showbase, '#'); diff --git a/libadhocutil/exception.h b/libadhocutil/exception.h index ebc820d..0941657 100644 --- a/libadhocutil/exception.h +++ b/libadhocutil/exception.h @@ -3,6 +3,7 @@ #include // IWYU pragma: export #include #include +#include namespace AdHoc { /// Helper class for lazy creation of exception message text. @@ -10,7 +11,7 @@ namespace AdHoc { public: /// Wrapper constructor to pass to BaseException //@param t parameters to pass. - template explicit Exception(const T &... t) : BaseException(t...) { } + template explicit Exception(T &&... t) : BaseException(std::forward(t)...) { } /// Override of std::exception::what() to create text as required. inline const char * diff --git a/libadhocutil/plugins.h b/libadhocutil/plugins.h index 7ae5bba..decb9ef 100644 --- a/libadhocutil/plugins.h +++ b/libadhocutil/plugins.h @@ -13,6 +13,7 @@ #include #include #include +#include namespace std { DLL_PUBLIC @@ -146,9 +147,9 @@ namespace AdHoc { */ template void - create(const std::string_view n, const std::string_view f, int l, const Args &... args) + create(const std::string_view n, const std::string_view f, int l, Args &&... args) { - add(std::make_shared(args...), n, f, l); + add(std::make_shared(std::forward(args)...), n, f, l); } /** diff --git a/libadhocutil/resourcePool.cpp b/libadhocutil/resourcePool.cpp index 4145068..57dea4d 100644 --- a/libadhocutil/resourcePool.cpp +++ b/libadhocutil/resourcePool.cpp @@ -34,7 +34,7 @@ namespace AdHoc { return TimeOutOnResourcePoolMsg::get(name); } - NoCurrentResource::NoCurrentResource(const std::thread::id & id, const char * const n) : threadId(id), name(n) { } + NoCurrentResource::NoCurrentResource(const std::thread::id id, const char * const n) : threadId(id), name(n) { } AdHocFormatter(NoCurrentResourceMsg, "Thread %? has no current resource handle of type %?"); std::string diff --git a/libadhocutil/resourcePool.h b/libadhocutil/resourcePool.h index 3b463b9..e48dd66 100644 --- a/libadhocutil/resourcePool.h +++ b/libadhocutil/resourcePool.h @@ -149,7 +149,7 @@ namespace AdHoc { class DLL_PUBLIC NoCurrentResource : public AdHoc::StdException { public: /// Construct for a specific thread and resource type. - NoCurrentResource(const std::thread::id &, const char * const type); + NoCurrentResource(const std::thread::id, const char * const type); std::string message() const noexcept override; @@ -163,7 +163,7 @@ namespace AdHoc { template class DLL_PUBLIC NoCurrentResourceT : public NoCurrentResource { public: /// Construct for a specific thread and resource type R. - explicit NoCurrentResourceT(const std::thread::id &); + explicit NoCurrentResourceT(const std::thread::id); }; } diff --git a/libadhocutil/resourcePool.impl.h b/libadhocutil/resourcePool.impl.h index e6e3e0d..4bba861 100644 --- a/libadhocutil/resourcePool.impl.h +++ b/libadhocutil/resourcePool.impl.h @@ -267,7 +267,7 @@ namespace AdHoc { } template - NoCurrentResourceT::NoCurrentResourceT(const std::thread::id & id) : NoCurrentResource(id, typeid(R).name()) + NoCurrentResourceT::NoCurrentResourceT(const std::thread::id id) : NoCurrentResource(id, typeid(R).name()) { } } diff --git a/libadhocutil/unittests/testCache.cpp b/libadhocutil/unittests/testCache.cpp index 2250347..9343fac 100644 --- a/libadhocutil/unittests/testCache.cpp +++ b/libadhocutil/unittests/testCache.cpp @@ -15,7 +15,7 @@ public: // cppcheck-suppress noExplicitConstructor; NOLINTNEXTLINE(hicpp-explicit-conversions) Obj(int i) : v(i) { } bool - operator==(const int & i) const + operator==(const int i) const { return v == i; } @@ -23,7 +23,7 @@ public: }; bool -operator==(const int & i, const Obj & o) +operator==(const int i, const Obj & o) { return i == o.v; } -- cgit v1.2.3