aboutsummaryrefslogtreecommitdiffstats
path: root/src/libserver
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2021-03-29 14:43:14 +0100
committerVsevolod Stakhov <vsevolod@highsecure.ru>2021-03-29 14:43:14 +0100
commitdfbd181e9cc0dc13eba0d00b0fa77b133ee364f3 (patch)
treef31eab9e3ca194f1303bcecfe59b25bdb4ac276c /src/libserver
parent7f296c74ccf9cc73ea141c99c1a6ca7840e2b5d3 (diff)
downloadrspamd-dfbd181e9cc0dc13eba0d00b0fa77b133ee364f3.tar.gz
rspamd-dfbd181e9cc0dc13eba0d00b0fa77b133ee364f3.zip
[Project] Css: Tidy up lambdas
Diffstat (limited to 'src/libserver')
-rw-r--r--src/libserver/css/css_parser.cxx21
-rw-r--r--src/libserver/css/css_parser.hxx3
-rw-r--r--src/libserver/css/css_rule.cxx2
-rw-r--r--src/libserver/css/css_rule.hxx2
-rw-r--r--src/libserver/css/css_selector.cxx2
-rw-r--r--src/libserver/css/css_selector.hxx4
6 files changed, 20 insertions, 14 deletions
diff --git a/src/libserver/css/css_parser.cxx b/src/libserver/css/css_parser.cxx
index d828ffa46..104792693 100644
--- a/src/libserver/css/css_parser.cxx
+++ b/src/libserver/css/css_parser.cxx
@@ -650,13 +650,7 @@ get_selectors_parser_functor(rspamd_mempool_t *pool,
{
css_parser parser(pool);
- /*
- * We use shared ptr here to avoid std::function limitation around
- * unique_ptr due to copyable of std::function
- * Hence, to elongate consumed_blocks lifetime we need to copy them
- * inside lambda.
- */
- std::shared_ptr<css_consumed_block> consumed_blocks = parser.consume_css_blocks(st);
+ auto &&consumed_blocks = parser.consume_css_blocks(st);
const auto &rules = consumed_blocks->get_blocks_or_empty();
auto rules_it = rules.begin();
@@ -664,7 +658,18 @@ get_selectors_parser_functor(rspamd_mempool_t *pool,
auto cur = children.begin();
auto last = children.end();
- return [cur, consumed_blocks, last](void) mutable -> const css_consumed_block & {
+ /*
+ * We use move only wrapper to state the fact that the cosumed blocks
+ * are moved into the closure, not copied.
+ * It prevents us from thinking about copies of the blocks and
+ * functors.
+ * Mutable lambda is required to copy iterators inside of the closure,
+ * as, again, it is C++ where lifetime of the objects must be explicitly
+ * transferred. On the other hand, we could move all stuff inside and remove
+ * mutable.
+ */
+ return [cur, consumed_blocks = std::move(consumed_blocks), last](void) mutable
+ -> const css_consumed_block & {
if (cur != last) {
const auto &ret = (*cur);
diff --git a/src/libserver/css/css_parser.hxx b/src/libserver/css/css_parser.hxx
index 845031618..4b5598fa9 100644
--- a/src/libserver/css/css_parser.hxx
+++ b/src/libserver/css/css_parser.hxx
@@ -24,6 +24,7 @@
#include <memory>
#include <string>
+#include "function2/function2.hpp"
#include "css_tokeniser.hxx"
#include "parse_error.hxx"
#include "contrib/expected/expected.hpp"
@@ -181,7 +182,7 @@ private:
extern const css_consumed_block css_parser_eof_block;
-using blocks_gen_functor = std::function<const css_consumed_block &(void)>;
+using blocks_gen_functor = fu2::unique_function<const css_consumed_block &(void)>;
class css_style_sheet;
auto parse_css(rspamd_mempool_t *pool, const std::string_view &st) ->
diff --git a/src/libserver/css/css_rule.cxx b/src/libserver/css/css_rule.cxx
index cf04eb689..c301ebdca 100644
--- a/src/libserver/css/css_rule.cxx
+++ b/src/libserver/css/css_rule.cxx
@@ -169,7 +169,7 @@ allowed_property_value(const css_property &prop, const css_consumed_block &parse
}
auto process_declaration_tokens(rspamd_mempool_t *pool,
- const blocks_gen_functor &next_block_functor)
+ blocks_gen_functor &&next_block_functor)
-> css_declarations_block_ptr
{
css_declarations_block_ptr ret;
diff --git a/src/libserver/css/css_rule.hxx b/src/libserver/css/css_rule.hxx
index c7cbae45d..ec9ccde42 100644
--- a/src/libserver/css/css_rule.hxx
+++ b/src/libserver/css/css_rule.hxx
@@ -105,7 +105,7 @@ private:
using css_declarations_block_ptr = std::shared_ptr<css_declarations_block>;
auto process_declaration_tokens(rspamd_mempool_t *pool,
- const blocks_gen_functor &next_token_functor)
+ blocks_gen_functor &&next_token_functor)
-> css_declarations_block_ptr;
}
diff --git a/src/libserver/css/css_selector.cxx b/src/libserver/css/css_selector.cxx
index b198b8553..de7b9afac 100644
--- a/src/libserver/css/css_selector.cxx
+++ b/src/libserver/css/css_selector.cxx
@@ -23,7 +23,7 @@
namespace rspamd::css {
auto process_selector_tokens(rspamd_mempool_t *pool,
- const blocks_gen_functor &next_token_functor)
+ blocks_gen_functor &&next_token_functor)
-> selectors_vec
{
selectors_vec ret;
diff --git a/src/libserver/css/css_selector.hxx b/src/libserver/css/css_selector.hxx
index 5ed4d54b8..7a211c11c 100644
--- a/src/libserver/css/css_selector.hxx
+++ b/src/libserver/css/css_selector.hxx
@@ -23,9 +23,9 @@
#include <string>
#include <optional>
#include <vector>
-#include <functional>
#include <memory>
+#include "function2/function2.hpp"
#include "parse_error.hxx"
#include "css_parser.hxx"
#include "html_tags.h"
@@ -85,7 +85,7 @@ using selectors_vec = std::vector<std::unique_ptr<css_selector>>;
* Consume selectors token and split them to the list of selectors
*/
auto process_selector_tokens(rspamd_mempool_t *pool,
- const blocks_gen_functor &next_token_functor)
+ blocks_gen_functor &&next_token_functor)
-> selectors_vec;
}