AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
fluent.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2012 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include <cstdint>
23#include <functional>
24#include <memory>
25#include <vector>
26
27namespace anch {
28
42 template<typename T, template<typename> typename C>
43 class Fluent {
44
45 // Inner types +
46 private:
50 struct Step {
52 enum class Kind {
53 FILTER,
54 SKIP,
55 LIMIT
56 };
57
59 Kind kind;
60
62 std::function<bool(const T&)> predicate;
63
65 uint64_t count;
66 };
67
71 enum class Verdict {
72 ACCEPT,
73 REJECT,
74 STOP
75 };
76 // Inner types -
77
78 // Attributes +
79 private:
81 std::shared_ptr<C<T>> _owned;
82
84 C<T>* _values;
85
87 std::vector<Step> _steps;
88
90 Fluent<T,C>* _next;
91 // Attributes -
92
93 // Constructors +
94 public:
98 Fluent() = delete;
99
105 Fluent(C<T>& container);
106
112 Fluent(C<T>&& container);
113
117 Fluent(const Fluent& other) = default;
118
122 Fluent(Fluent&& other) noexcept = default;
123 // Constructors -
124
125 // Destructor +
126 public:
130 ~Fluent() = default;
131 // Destructor -
132
133 // Methods +
134 public:
142 Fluent& filter(std::function<bool(const T&)> predicate);
143
151 Fluent& skip(uint64_t skip);
152
160 Fluent& limit(uint64_t limit);
161
168 Fluent& concat(Fluent<T,C>& other) noexcept;
169
175 bool allMatch(std::function<bool(const T&)> predicate);
176
182 bool anyMatch(std::function<bool(const T&)> predicate);
183
189 bool noneMatch(std::function<bool(const T&)> predicate);
190
197 void forEach(std::function<void(T&)> action);
198
204 template<typename U>
205 Fluent<U,std::vector> map(std::function<U(const T&)> mapper);
206
213 template<typename D>
214 void collect(D& container, std::function<void(D&,const T&)> collector);
215
223 template<typename D>
224 D collect(std::function<void(D&,const T&)> collector);
225
232 template<typename D, typename R>
233 void collect(D& container, R(D::*collector)(const T&));
234
242 template<typename D, typename R>
243 D collect(R(D::*collector)(const T&));
244
245 private:
252 template<typename F>
253 void traverse(F&& consumer);
254
263 Verdict apply(const T& value, std::vector<uint64_t>& counters) const;
264 // Methods -
265
266 };
267
268}
269
270#include "impl/fluent.hpp"
Fluent & limit(uint64_t limit)
bool anyMatch(std::function< bool(const T &)> predicate)
void collect(D &container, std::function< void(D &, const T &)> collector)
D collect(std::function< void(D &, const T &)> collector)
Fluent(C< T > &&container)
Fluent & skip(uint64_t skip)
Fluent & filter(std::function< bool(const T &)> predicate)
Fluent(C< T > &container)
Fluent(Fluent &&other) noexcept=default
void collect(D &container, R(D::*collector)(const T &))
D collect(R(D::*collector)(const T &))
Fluent()=delete
Fluent & concat(Fluent< T, C > &other) noexcept
~Fluent()=default
Fluent< U, std::vector > map(std::function< U(const T &)> mapper)
void forEach(std::function< void(T &)> action)
bool noneMatch(std::function< bool(const T &)> predicate)
Fluent(const Fluent &other)=default
bool allMatch(std::function< bool(const T &)> predicate)
AnCH framework base namespace.
Definition app.hpp:30