File indexing completed on 2024-06-16 05:06:54

0001 /*
0002     SPDX-FileCopyrightText: 2018 Ivan Čukić <ivan.cukic(at)kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef VOY_OPERATIONS_FILTER_H
0008 #define VOY_OPERATIONS_FILTER_H
0009 
0010 // STL
0011 #include <functional>
0012 
0013 // Self
0014 #include "../utils.h"
0015 #include "../traits.h"
0016 #include "../dsl/node_tags.h"
0017 
0018 namespace voy {
0019 
0020 using voy::utils::non_copyable;
0021 
0022 using voy::dsl::continuator_base,
0023       voy::dsl::transformation_node_tag;
0024 
0025 template <typename Pred>
0026 class filter {
0027 public:
0028     using node_category = transformation_node_tag;
0029 
0030     explicit filter(Pred predicate)
0031         : m_predicate{std::move(predicate)}
0032     {
0033     }
0034 
0035     template <typename Cont>
0036     class node: public continuator_base<Cont>, non_copyable {
0037         using base = continuator_base<Cont>;
0038 
0039     public:
0040         node(Pred predicate, Cont&& continuation)
0041             : base{std::move(continuation)}
0042             , m_predicate{std::move(predicate)}
0043         {
0044         }
0045 
0046         template <typename T>
0047         void operator() (T&& value) const
0048         {
0049             if (std::invoke(m_predicate, value)) {
0050                 base::emit(voy_fwd(value));
0051             }
0052         }
0053 
0054     private:
0055         Pred m_predicate;
0056     };
0057 
0058     template <typename Cont>
0059     auto with_continuation(Cont&& cont) &&
0060     {
0061         return node<Cont>(std::move(m_predicate), voy_fwd(cont));
0062     }
0063 
0064 
0065 private:
0066     Pred m_predicate;
0067 };
0068 
0069 
0070 template <typename Pred>
0071 auto remove_if(Pred&& pred)
0072 {
0073     return filter{std::not_fn(pred)};
0074 }
0075 
0076 
0077 } // namespace voy
0078 
0079 #endif // include guard
0080