From 9f7db84dd3da40143a4f5c774bd2b846a5292c42 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 7 Feb 2019 18:28:52 -0800 Subject: [PATCH] ircd::util: Add string_buffer() reclosure template. --- include/ircd/util/string.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/include/ircd/util/string.h b/include/ircd/util/string.h index b36729197..47ac4eb45 100644 --- a/include/ircd/util/string.h +++ b/include/ircd/util/string.h @@ -40,6 +40,28 @@ namespace ircd::util // toString()'ish template suite (see defs) template std::string string(const mutable_buffer &buf, const T &s); template std::string string(const T &s); + + template std::string string_buffer(const size_t &, F&&, A&&...); +} + +/// Convenience template for working with various functions throughout IRCd +/// with the pattern `size_t func(mutable_buffer, ...)`. This function closes +/// over your function to supply the leading mutable_buffer and use the return +/// value of your function to satisfy util::string() as usual. The prototype +/// pattern works with the closures declared in this suite, so string_view +/// return types work too. +template +std::string +ircd::util::string_buffer(const size_t &size, + F&& function, + A&&... arguments) +{ + return string(size, [&function, &arguments...] + (const mutable_buffer &buf) + { + return function(buf, std::forward(arguments)...); + }); } /// This is the ubiquitous ircd::string() template serving as the "toString()"