diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-03-14 00:59:17 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-03-14 00:59:17 +0000 |
commit | e0f8297e3e57512c423c571fb4dde5ced594e803 (patch) | |
tree | a83627887444d1d9a55acef48edf2e3acde86d63 | |
parent | Don't take string_view by reference (diff) | |
download | libadhocutil-e0f8297e3e57512c423c571fb4dde5ced594e803.tar.bz2 libadhocutil-e0f8297e3e57512c423c571fb4dde5ced594e803.tar.xz libadhocutil-e0f8297e3e57512c423c571fb4dde5ced594e803.zip |
Lots of perfect forwarding over const refs
-rw-r--r-- | libadhocutil/compileTimeFormatter.h | 64 | ||||
-rw-r--r-- | libadhocutil/ctf-impl/printf-compat.h | 46 | ||||
-rw-r--r-- | libadhocutil/exception.h | 3 | ||||
-rw-r--r-- | libadhocutil/plugins.h | 5 | ||||
-rw-r--r-- | libadhocutil/resourcePool.cpp | 2 | ||||
-rw-r--r-- | libadhocutil/resourcePool.h | 4 | ||||
-rw-r--r-- | libadhocutil/resourcePool.impl.h | 2 | ||||
-rw-r--r-- | 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 <optional> #include <sstream> // IWYU pragma: export #include <type_traits> +#include <utility> // Mapped for for BOOST_PP_VARIADIC_SIZE, BOOST_PP... in tests // IWYU pragma: no_include <boost/test/unit_test.hpp> @@ -15,14 +16,14 @@ namespace AdHoc { // Template char utils template<typename char_type> constexpr bool - isdigit(const char_type & ch) + isdigit(const char_type ch) { return (ch >= '0' && ch <= '9'); } template<typename char_type> 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<typename... Pn> 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<typename... Pn> static inline void - next(stream & s, const Pn &... pn) + next(stream & s, Pn &&... pn) { - FormatterDetail<S, L>::template Parser<stream, pos + 1, Pn...>::run(s, pn...); + FormatterDetail<S, L>::template Parser<stream, pos + 1, Pn...>::run(s, std::forward<Pn>(pn)...); } }; @@ -114,10 +115,10 @@ namespace AdHoc { StreamWriterT('?') { template<typename P, typename... Pn> 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>(p); + StreamWriter::next(s, std::forward<Pn>(pn)...); } }; @@ -125,10 +126,10 @@ namespace AdHoc { StreamWriterT('%') { template<typename... Pn> 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>(pn)...); } }; @@ -165,10 +166,10 @@ namespace AdHoc { */ template<typename... Pn> static inline auto - get(const Pn &... pn) + get(Pn &&... pn) { std::basic_stringstream<char_type> s; - return write(s, pn...).str(); + return write(s, std::forward<Pn>(pn)...).str(); } /** * Get a string containing the result of formatting. @@ -177,9 +178,9 @@ namespace AdHoc { */ template<typename... Pn> inline auto - operator()(const Pn &... pn) const + operator()(Pn &&... pn) const { - return get(pn...); + return get(std::forward<Pn>(pn)...); } /** @@ -190,9 +191,9 @@ namespace AdHoc { */ template<typename stream, typename... Pn> static inline stream & - write(stream & s, const Pn &... pn) + write(stream & s, Pn &&... pn) { - return Parser<stream, 0U, Pn...>::run(s, pn...); + return Parser<stream, 0U, Pn...>::run(s, std::forward<Pn>(pn)...); } /** * Write the result of formatting to the given stream. @@ -202,15 +203,15 @@ namespace AdHoc { */ template<typename stream, typename... Pn> inline typename std::enable_if<std::is_base_of_v<std::basic_ostream<char_type>, 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>(pn)...); } private: template<typename stream, auto pos, typename... Pn> struct Parser { static inline stream & - run(stream & s, const Pn &... pn) + run(stream & s, Pn &&... pn) { if (pos != L) { constexpr auto ph = strchrnul<S, '%', pos, L>(); @@ -218,20 +219,20 @@ namespace AdHoc { appendStream(s, &S[pos], ph - pos); } if constexpr (ph != L) { - packAndWrite<ph>(s, pn...); + packAndWrite<ph>(s, std::forward<Pn>(pn)...); } } return s; } template<strlen_t ph, strlen_t off = 0U, auto... Pck> static inline void - packAndWrite(stream & s, const Pn &... pn) + packAndWrite(stream & s, Pn &&... pn) { if constexpr (ph + off == L || sizeof...(Pck) == 32) { StreamWriter<S, L, ph, stream, void, Pck...>::write(s, pn...); } else if constexpr (ph + off < L) { - packAndWrite<ph, off + 1, Pck..., S[ph + off]>(s, pn...); + packAndWrite<ph, off + 1, Pck..., S[ph + off]>(s, std::forward<Pn>(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<const support::basic_fixed_string Str, typename... Pn> inline auto - scprintf(const Pn &... pn) + scprintf(Pn &&... pn) { - return FormatterDetail<Str, Str.size()>::get(pn...); + return FormatterDetail<Str, Str.size()>::get(std::forward<Pn>(pn)...); } template<const support::basic_fixed_string Str, typename stream, typename... Pn> inline auto & - scprintf(stream & strm, const Pn &... pn) + scprintf(stream & strm, Pn &&... pn) { - return FormatterDetail<Str, Str.size()>::write(strm, pn...); + return FormatterDetail<Str, Str.size()>::write(strm, std::forward<Pn>(pn)...); } template<const support::basic_fixed_string Str, typename... Pn> inline auto & - cprintf(const Pn &... pn) + cprintf(Pn &&... pn) { - return scprintf<Str>(std::cout, pn...); + return scprintf<Str>(std::cout, std::forward<Pn>(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<typename... Pn> \ 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>(pn)...); \ } \ } @@ -71,39 +71,39 @@ namespace AdHoc { StreamWriterT('p') { template<typename Obj, typename... Pn> 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<long unsigned int>(ptr); s.copyfmt(std::ios(nullptr)); - StreamWriter::next(s, pn...); + StreamWriter::next(s, std::forward<Pn>(pn)...); } template<typename Ptr, typename... Pn> 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>(pn)...); } }; StreamWriterT('m') { template<typename... Pn> 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>(pn)...); } }; StreamWriterT('n') { template<typename... Pn> 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>(pn)...); } }; @@ -137,11 +137,12 @@ namespace AdHoc { BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), NS, n), nn, sn...> { \ template<typename... Pn> \ static inline void \ - write(stream & s, const Pn &... pn) \ + write(stream & s, Pn &&... pn) \ { \ constexpr auto p = decdigits<BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), NS, n)>(); \ s << std::setw(p); \ - StreamWriter<S, L, pos + BOOST_PP_ADD(d, 1), stream, void, '%', nn, sn...>::write(s, pn...); \ + StreamWriter<S, L, pos + BOOST_PP_ADD(d, 1), stream, void, '%', nn, sn...>::write( \ + s, std::forward<Pn>(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<typename... Pn> \ static inline void \ - write(stream & s, const Pn &... pn) \ + write(stream & s, Pn &&... pn) \ { \ constexpr auto p = decdigits<BOOST_PP_REPEAT(BOOST_PP_ADD(d, 1), NS, n)>(); \ s << std::setprecision(p); \ - StreamWriter<S, L, pos + BOOST_PP_ADD(d, 2), stream, void, '%', nn, sn...>::write(s, pn...); \ + StreamWriter<S, L, pos + BOOST_PP_ADD(d, 2), stream, void, '%', nn, sn...>::write( \ + s, std::forward<Pn>(pn)...); \ } \ }; BOOST_PP_REPEAT(6, FMTPRECISION, void) @@ -169,26 +171,26 @@ namespace AdHoc { StreamWriterT('.', '*') { template<typename... Pn> static inline void - write(stream & s, int l, const Pn &... pn) + write(stream & s, int l, Pn &&... pn) { s << std::setw(l); - StreamWriter<S, L, pos + 2, stream, void, '%', sn...>::write(s, pn...); + StreamWriter<S, L, pos + 2, stream, void, '%', sn...>::write(s, std::forward<Pn>(pn)...); } }; StreamWriterT('.', '*', 's') { template<typename... Pn> 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<size_t>(l), p, pn...); + return write(s, static_cast<size_t>(l), p, std::forward<Pn>(pn)...); } template<typename... Pn> 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>(pn)...); } }; @@ -197,10 +199,10 @@ namespace AdHoc { StreamWriterT(__VA_ARGS__) { \ template<typename... Pn> \ static inline void \ - write(stream & s, const Pn &... pn) \ + write(stream & s, Pn &&... pn) \ { \ OP; \ - StreamWriter<S, L, pos + 1, stream, void, '%', sn...>::write(s, pn...); \ + StreamWriter<S, L, pos + 1, stream, void, '%', sn...>::write(s, std::forward<Pn>(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 <exception> // IWYU pragma: export #include <optional> #include <string> +#include <utility> 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<typename... T> explicit Exception(const T &... t) : BaseException(t...) { } + template<typename... T> explicit Exception(T &&... t) : BaseException(std::forward<T>(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 <string> #include <string_view> #include <typeinfo> +#include <utility> namespace std { DLL_PUBLIC @@ -146,9 +147,9 @@ namespace AdHoc { */ template<typename T, typename I, typename... Args> 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<T>(std::make_shared<I>(args...), n, f, l); + add<T>(std::make_shared<I>(std::forward<Args>(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<typename R> 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<typename R> - NoCurrentResourceT<R>::NoCurrentResourceT(const std::thread::id & id) : NoCurrentResource(id, typeid(R).name()) + NoCurrentResourceT<R>::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; } |