Ponca  fd5150bf9079aabdb82a6f05e419b76111c1f788
Point Cloud Analysis library
Loading...
Searching...
No Matches
factory.h
1/*
2 This Source Code Form is subject to the terms of the Mozilla Public
3 License, v. 2.0. If a copy of the MPL was not distributed with this
4 file, You can obtain one at http://mozilla.org/MPL/2.0/.
5*/
6#pragma once
7
8#include "factorylist.h"
9#include "typelist.h"
10
11namespace Ponca
12{
13 namespace internal
14 {
23 template <typename _FactoryEntries>
25 {
26 using FactoryEntries = _FactoryEntries;
27 static constexpr size_t N = std::tuple_size_v<FactoryEntries>;
28
29 constexpr ComputeObjectList()
30 : m_entries([]<std::size_t... Is>(std::index_sequence<Is...>) {
31 return FactoryEntries{std::tuple_element_t<Is, FactoryEntries>{Is}...};
32 }(std::make_index_sequence<std::tuple_size_v<FactoryEntries>>{}))
33 {
34 }
35
41 template <template <typename> class... Preds>
42 static decltype(auto) Filter()
43 {
44 using namespace Factory;
45 return ComputeObjectList<typename filterList<conjunction<Preds...>, FactoryEntries>::type>{};
46 };
47
53 template <StringLiteral... Names>
54 static decltype(auto) Filter()
55 {
56 return Filter<MethodProvider<Names>::template pred...>();
57 }
58
69 template <StringLiteral Name>
70 static decltype(auto) GetMethod()
71 {
72 using MethodList = decltype(Filter<MethodProvider<Name>::template pred>());
73
74 static_assert(std::tuple_size_v<typename MethodList::FactoryEntries> == 1);
75 return std::get<0>(typename MethodList::FactoryEntries{0}).object;
76 }
77
98 template <typename Func>
99 void foreach (Func&& fun)
100 {
101 std::apply([&](auto&&... xs) { (fun(xs), ...); }, m_entries);
102 }
103
104 template <typename Func>
105 void ApplyAt(size_t index, Func&& fun)
106 {
107 // Instead of foreach we could add some runtime helper to get the i-th element.
108 // However, this adds utility and compile time generated function for the same
109 // results and is not deemed worthwile at the time of writting this.
110 this->foreach ([&](auto& x) {
111 if (x.id == index)
112 fun(x);
113 });
114 }
115
116 template <typename Func>
117 void ApplyAt(const char* name, Func&& fun)
118 {
119 // Instead of foreach we could add some runtime helper to get the i-th element.
120 // However, this adds utility and compile time generated function for the same
121 // results and is not deemed worthwile at the time of writting this.
122 this->foreach ([&](auto& x) {
123 if (strcmp(name, x.name) == 0)
124 fun(x);
125 });
126 }
127
131 constexpr static size_t size() { return N; }
132
136 constexpr static decltype(auto) GetNames()
137 {
138 return []<std::size_t... Is>(std::index_sequence<Is...>) {
139 return std::array{std::tuple_element_t<Is, FactoryEntries>::name...};
140 }(std::make_index_sequence<std::tuple_size_v<FactoryEntries>>{});
141 }
142
143 private:
144 FactoryEntries m_entries;
145 };
146
155 template <typename _FactoryEntries>
157 {
158 using FactoryEntries = _FactoryEntries;
160
164 template <template <typename> class... Preds>
165 static decltype(auto) Filter()
166 {
167 return FullComputeObjectList::template Filter<Preds...>();
168 }
169
173 template <StringLiteral... Names>
174 static decltype(auto) Filter()
175 {
176 return FullComputeObjectList::template Filter<Names...>();
177 }
178
182 template <StringLiteral Name>
183 static decltype(auto) GetMethod()
184 {
185 return FullComputeObjectList::template GetMethod<Name>();
186 }
187
191 template <typename Func>
192 static void foreach (Func&& fun)
193 {
194 FullComputeObjectList{}.foreach (std::forward<Func>(fun));
195 }
196
200 constexpr static size_t size() { return FullComputeObjectList::N; }
201
205 constexpr static decltype(auto) GetNames() { return FullComputeObjectList::GetNames(); }
206 };
207 } // namespace internal
208
216 template <typename P, typename NF, DiffType DerType>
217 struct Factory : public internal::FactoryBase<FactoryGenericList<P, NF, DerType>>
218 {
219 };
220
230 template <typename P, typename NF, DiffType DerType>
231 requires Is3D<P>
232 struct Factory<P, NF, DerType> : public internal::FactoryBase<Factory3DList<P, NF, DerType>>
233 {
234 };
235
245 template <typename P, typename NF, DiffType DerType>
246 requires Is3D<P> && (bool(DerType & DiffType::FitSpaceDer))
247 struct Factory<P, NF, DerType> : public internal::FactoryBase<Factory3DSpaceDerivatives<P, NF, DerType>>
248 {
249 };
250} // namespace Ponca
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition concepts.h:11
@ FitSpaceDer
Flag indicating a space differentiation.
Definition enums.h:36
Default factory templated on Point, Filter and Derivative.
Definition factory.h:218
StringLiteral as template parameters.
Definition typeutils.h:18
List of compute object that can be iterated over.
Definition factory.h:25
static constexpr size_t size()
Return the number of object held within the object.
Definition factory.h:131
static decltype(auto) Filter()
Returns a new object that is a filtered version of the current one.
Definition factory.h:42
static constexpr decltype(auto) GetNames()
Return the names of the currently held objects.
Definition factory.h:136
static decltype(auto) GetMethod()
Return an object by it's id.
Definition factory.h:70
void foreach(Func &&fun)
Applies a function to each entry of the list.
Definition factory.h:99
Base class for factory.
Definition factory.h:157
static constexpr decltype(auto) GetNames()
Return the names of the currently held objects.
Definition factory.h:205
static decltype(auto) Filter()
Returns a new object that is a filtered version of the current one.
Definition factory.h:165
static decltype(auto) GetMethod()
Return an object by it's id.
Definition factory.h:183
static constexpr size_t size()
Return the number of object held within the Factory.
Definition factory.h:200